11package bitbucket
22
33import (
4- "bytes"
5- "encoding/json"
64 "fmt"
7- "io/ioutil"
85 "log"
96 "net/http"
10- "net/url "
7+ "strings "
118
9+ "github.com/DrFaust92/bitbucket-go-client"
1210 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1311)
1412
15- // RepositoryVariable structure for handling key info
16- type RepositoryVariable struct {
17- Key string `json:"key"`
18- Value string `json:"value"`
19- UUID string `json:"uuid,omitempty"`
20- Secured bool `json:"secured"`
21- }
22-
2313func resourceRepositoryVariable () * schema.Resource {
2414 return & schema.Resource {
2515 Create : resourceRepositoryVariableCreate ,
@@ -54,8 +44,8 @@ func resourceRepositoryVariable() *schema.Resource {
5444 }
5545}
5646
57- func newRepositoryVariableFromResource (d * schema.ResourceData ) * RepositoryVariable {
58- dk := & RepositoryVariable {
47+ func newRepositoryVariableFromResource (d * schema.ResourceData ) bitbucket. PipelineVariable {
48+ dk := bitbucket. PipelineVariable {
5949 Key : d .Get ("key" ).(string ),
6050 Value : d .Get ("value" ).(string ),
6151 Secured : d .Get ("secured" ).(bool ),
@@ -64,109 +54,108 @@ func newRepositoryVariableFromResource(d *schema.ResourceData) *RepositoryVariab
6454}
6555
6656func resourceRepositoryVariableCreate (d * schema.ResourceData , m interface {}) error {
67-
68- client := m .( Clients ). httpClient
57+ c := m .( Clients ). genClient
58+ pipeApi := c . ApiClient . PipelinesApi
6959 rvcr := newRepositoryVariableFromResource (d )
70- bytedata , err := json .Marshal (rvcr )
7160
61+ repo := d .Get ("repository" ).(string )
62+ workspace , repoSlug , err := repoVarId (repo )
7263 if err != nil {
7364 return err
7465 }
75- req , err := client .Post (fmt .Sprintf ("2.0/repositories/%s/pipelines_config/variables/" ,
76- d .Get ("repository" ).(string ),
77- ), bytes .NewBuffer (bytedata ))
7866
79- if err != nil {
80- return err
81- }
82-
83- var rv RepositoryVariable
67+ rvRes , _ , err := pipeApi .CreateRepositoryPipelineVariable (c .AuthContext , rvcr , workspace , repoSlug )
8468
85- body , readerr := ioutil .ReadAll (req .Body )
86- if readerr != nil {
87- return readerr
69+ if err != nil {
70+ return fmt .Errorf ("error creating Repository Variable (%s): %w" , repo , err )
8871 }
8972
90- decodeerr := json .Unmarshal (body , & rv )
91- if decodeerr != nil {
92- return decodeerr
93- }
94- d .Set ("uuid" , rv .UUID )
95- d .SetId (rv .Key )
73+ d .Set ("uuid" , rvRes .Uuid )
74+ d .SetId (rvRes .Key )
9675
9776 return resourceRepositoryVariableRead (d , m )
9877}
9978
10079func resourceRepositoryVariableRead (d * schema.ResourceData , m interface {}) error {
80+ c := m .(Clients ).genClient
81+ pipeApi := c .ApiClient .PipelinesApi
10182
102- client := m .(Clients ).httpClient
103- rvReq , _ := client .Get (fmt .Sprintf ("2.0/repositories/%s/pipelines_config/variables/%s" ,
104- d .Get ("repository" ).(string ),
105- d .Get ("uuid" ).(string ),
106- ))
107-
108- log .Printf ("ID: %s" , url .PathEscape (d .Id ()))
109-
110- if rvReq .StatusCode == 200 {
111- var rv RepositoryVariable
112- body , readerr := ioutil .ReadAll (rvReq .Body )
113- if readerr != nil {
114- return readerr
115- }
116-
117- decodeerr := json .Unmarshal (body , & rv )
118- if decodeerr != nil {
119- return decodeerr
120- }
121-
122- d .Set ("uuid" , rv .UUID )
123- d .Set ("key" , rv .Key )
124- d .Set ("secured" , rv .Secured )
125-
126- if ! rv .Secured {
127- d .Set ("value" , rv .Value )
128- } else {
129- d .Set ("value" , d .Get ("value" ).(string ))
130- }
83+ repo := d .Get ("repository" ).(string )
84+ workspace , repoSlug , err := repoVarId (repo )
85+ if err != nil {
86+ return err
13187 }
13288
133- if rvReq .StatusCode == http .StatusNotFound {
89+ rvRes , res , err := pipeApi .GetRepositoryPipelineVariable (c .AuthContext , workspace , repoSlug , d .Get ("uuid" ).(string ))
90+ if err != nil {
91+ return fmt .Errorf ("error reading Repository Variable (%s): %w" , d .Id (), err )
92+ }
93+ if res .StatusCode == http .StatusNotFound {
94+ log .Printf ("[WARN] Repository Variable (%s) not found, removing from state" , d .Id ())
13495 d .SetId ("" )
13596 return nil
13697 }
13798
99+ d .Set ("uuid" , rvRes .Uuid )
100+ d .Set ("key" , rvRes .Key )
101+ d .Set ("secured" , rvRes .Secured )
102+
103+ if ! rvRes .Secured {
104+ d .Set ("value" , rvRes .Value )
105+ } else {
106+ d .Set ("value" , d .Get ("value" ).(string ))
107+ }
108+
138109 return nil
139110}
140111
141112func resourceRepositoryVariableUpdate (d * schema.ResourceData , m interface {}) error {
142- client := m .(Clients ).httpClient
143- rvcr := newRepositoryVariableFromResource (d )
144- bytedata , err := json .Marshal (rvcr )
113+ c := m .(Clients ).genClient
114+ pipeApi := c .ApiClient .PipelinesApi
145115
116+ repo := d .Get ("repository" ).(string )
117+ workspace , repoSlug , err := repoVarId (repo )
146118 if err != nil {
147119 return err
148120 }
149- req , err := client .Put (fmt .Sprintf ("2.0/repositories/%s/pipelines_config/variables/%s" ,
150- d .Get ("repository" ).(string ),
151- d .Get ("uuid" ).(string ),
152- ), bytes .NewBuffer (bytedata ))
153121
122+ rvcr := newRepositoryVariableFromResource (d )
123+
124+ _ , _ , err = pipeApi .UpdateRepositoryPipelineVariable (c .AuthContext , rvcr , workspace , repoSlug , d .Get ("uuid" ).(string ))
154125 if err != nil {
155- return err
126+ return fmt . Errorf ( "error updating Repository Variable (%s): %w" , d . Id (), err )
156127 }
157128
158- if req . StatusCode != 200 {
159- return nil
129+ if err != nil {
130+ return err
160131 }
161132
162133 return resourceRepositoryVariableRead (d , m )
163134}
164135
165136func resourceRepositoryVariableDelete (d * schema.ResourceData , m interface {}) error {
166- client := m .(Clients ).httpClient
167- _ , err := client .Delete (fmt .Sprintf (fmt .Sprintf ("2.0/repositories/%s/pipelines_config/variables/%s" ,
168- d .Get ("repository" ).(string ),
169- d .Get ("uuid" ).(string ),
170- )))
171- return err
137+ c := m .(Clients ).genClient
138+ pipeApi := c .ApiClient .PipelinesApi
139+
140+ repo := d .Get ("repository" ).(string )
141+ workspace , repoSlug , err := repoVarId (repo )
142+ if err != nil {
143+ return err
144+ }
145+
146+ _ , err = pipeApi .DeleteRepositoryPipelineVariable (c .AuthContext , workspace , repoSlug , d .Get ("uuid" ).(string ))
147+ if err != nil {
148+ return fmt .Errorf ("error deleting Repository Variable (%s): %w" , d .Id (), err )
149+ }
150+
151+ return nil
152+ }
153+
154+ func repoVarId (repo string ) (string , string , error ) {
155+ idparts := strings .Split (repo , "/" )
156+ if len (idparts ) == 2 {
157+ return idparts [0 ], idparts [1 ], nil
158+ } else {
159+ return "" , "" , fmt .Errorf ("incorrect ID format, should match `owner/key`" )
160+ }
172161}
0 commit comments