11package bitbucket
22
33import (
4- "bytes"
5- "encoding/json"
64 "fmt"
7- "io/ioutil"
85 "log"
96 "net/http"
10- "net/url"
117 "strings"
128 "time"
139
10+ "github.com/DrFaust92/bitbucket-go-client"
1411 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1512)
1613
17- // DeploymentVariable structure for handling key info
18- type DeploymentVariable struct {
19- Key string `json:"key"`
20- Value string `json:"value"`
21- UUID string `json:"uuid,omitempty"`
22- Secured bool `json:"secured"`
23- }
24-
25- // PaginatedReviewers is a paginated list that the bitbucket api returns
26- type PaginatedDeploymentVariables struct {
27- Values []DeploymentVariable `json:"values,omitempty"`
28- Page int `json:"page,omitempty"`
29- Size int `json:"size,omitempty"`
30- Next string `json:"next,omitempty"`
31- }
32-
3314func resourceDeploymentVariable () * schema.Resource {
3415 return & schema.Resource {
3516 Create : resourceDeploymentVariableCreate ,
@@ -64,8 +45,8 @@ func resourceDeploymentVariable() *schema.Resource {
6445 }
6546}
6647
67- func newDeploymentVariableFromResource (d * schema.ResourceData ) * DeploymentVariable {
68- dk := & DeploymentVariable {
48+ func newDeploymentVariableFromResource (d * schema.ResourceData ) * bitbucket. DeploymentVariable {
49+ dk := & bitbucket. DeploymentVariable {
6950 Key : d .Get ("key" ).(string ),
7051 Value : d .Get ("value" ).(string ),
7152 Secured : d .Get ("secured" ).(bool ),
@@ -79,129 +60,127 @@ func parseDeploymentId(str string) (repository string, deployment string) {
7960}
8061
8162func resourceDeploymentVariableCreate (d * schema.ResourceData , m interface {}) error {
82-
83- client := m .( Clients ). httpClient
63+ c := m .( Clients ). genClient
64+ pipeApi := c . ApiClient . PipelinesApi
8465 rvcr := newDeploymentVariableFromResource (d )
85- bytedata , err := json .Marshal (rvcr )
8666
87- if err != nil {
88- return err
89- }
9067 repository , deployment := parseDeploymentId (d .Get ("deployment" ).(string ))
91- req , err := client .Post (fmt .Sprintf ("2.0/repositories/%s/deployments_config/environments/%s/variables" ,
92- repository ,
93- deployment ,
94- ), bytes .NewBuffer (bytedata ))
95-
68+ workspace , repoSlug , err := deployVarId (repository )
9669 if err != nil {
9770 return err
9871 }
9972
100- var rv DeploymentVariable
73+ rvRes , _ , err := pipeApi . CreateDeploymentVariable ( c . AuthContext , * rvcr , workspace , repoSlug , deployment )
10174
102- body , readerr := ioutil .ReadAll (req .Body )
103- if readerr != nil {
104- return readerr
75+ if err != nil {
76+ return fmt .Errorf ("error creating Deployment Variable (%s): %w" , d .Get ("deployment" ).(string ), err )
10577 }
10678
107- decodeerr := json .Unmarshal (body , & rv )
108- if decodeerr != nil {
109- return decodeerr
110- }
111- d .Set ("uuid" , rv .UUID )
112- d .SetId (rv .UUID )
79+ d .Set ("uuid" , rvRes .Uuid )
80+ d .SetId (rvRes .Uuid )
11381
11482 time .Sleep (5000 * time .Millisecond ) // sleep for a while, to allow BitBucket cache to catch up
11583 return resourceDeploymentVariableRead (d , m )
11684}
11785
11886func resourceDeploymentVariableRead (d * schema.ResourceData , m interface {}) error {
87+ c := m .(Clients ).genClient
88+ pipeApi := c .ApiClient .PipelinesApi
11989
12090 repository , deployment := parseDeploymentId (d .Get ("deployment" ).(string ))
121- client := m .(Clients ).httpClient
122- rvReq , _ := client .Get (fmt .Sprintf ("2.0/repositories/%s/deployments_config/environments/%s/variables?pagelen=100" ,
123- repository ,
124- deployment ,
125- ))
126-
127- log .Printf ("ID: %s" , url .PathEscape (d .Id ()))
128-
129- if rvReq .StatusCode == 200 {
130- var prv PaginatedDeploymentVariables
131- body , readerr := ioutil .ReadAll (rvReq .Body )
132- if readerr != nil {
133- return readerr
134- }
91+ workspace , repoSlug , err := deployVarId (repository )
92+ if err != nil {
93+ return err
94+ }
13595
136- decodeerr := json . Unmarshal ( body , & prv )
137- if decodeerr != nil {
138- return decodeerr
139- }
96+ rvRes , res , err := pipeApi . GetDeploymentVariables ( c . AuthContext , workspace , repoSlug , deployment )
97+ if err != nil {
98+ return fmt . Errorf ( "error reading Deployment Variable (%s): %w" , d . Id (), err )
99+ }
140100
141- if prv .Size < 1 {
142- d .SetId ("" )
143- return nil
144- }
101+ if res .StatusCode == http .StatusNotFound {
102+ log .Printf ("[WARN] Deployment Variable (%s) not found, removing from state" , d .Id ())
103+ d .SetId ("" )
104+ return nil
105+ }
145106
146- for _ , rv := range prv . Values {
147- if rv . UUID == d .Id () {
148- d .SetId (rv . UUID )
149- d . Set ( "key" , rv . Key )
150- d . Set ( "secured" , rv . Secured )
107+ if rvRes . Size < 1 {
108+ log . Printf ( "[WARN] Deployment Variable (%s) not found, removing from state" , d .Id ())
109+ d .SetId ("" )
110+ return nil
111+ }
151112
152- if ! rv .Secured {
153- d .Set ("value" , rv .Value )
154- } else {
155- d .Set ("value" , d .Get ("value" ).(string ))
156- }
113+ var deployVar * bitbucket.DeploymentVariable
157114
158- return nil
159- }
115+ for _ , rv := range rvRes .Values {
116+ if rv .Uuid == d .Id () {
117+ deployVar = & rv
118+ break
160119 }
161- d .SetId ("" )
162120 }
163121
164- if rvReq .StatusCode == http .StatusNotFound {
122+ if deployVar == nil {
123+ log .Printf ("[WARN] Deployment Variable (%s) not found, removing from state" , d .Id ())
165124 d .SetId ("" )
166125 return nil
167126 }
168127
128+ d .Set ("key" , deployVar .Key )
129+ d .Set ("uuid" , deployVar .Uuid )
130+ d .Set ("secured" , deployVar .Secured )
131+
132+ if ! deployVar .Secured {
133+ d .Set ("value" , deployVar .Value )
134+ } else {
135+ d .Set ("value" , d .Get ("value" ).(string ))
136+ }
137+
169138 return nil
170139}
171140
172141func resourceDeploymentVariableUpdate (d * schema.ResourceData , m interface {}) error {
173- client := m .(Clients ).httpClient
142+ c := m .(Clients ).genClient
143+ pipeApi := c .ApiClient .PipelinesApi
174144 rvcr := newDeploymentVariableFromResource (d )
175- bytedata , err := json .Marshal (rvcr )
176145
177- if err != nil {
178- return err
179- }
180146 repository , deployment := parseDeploymentId (d .Get ("deployment" ).(string ))
181- req , err := client .Put (fmt .Sprintf ("2.0/repositories/%s/deployments_config/environments/%s/variables/%s" ,
182- repository ,
183- deployment ,
184- d .Get ("uuid" ).(string ),
185- ), bytes .NewBuffer (bytedata ))
186-
147+ workspace , repoSlug , err := deployVarId (repository )
187148 if err != nil {
188149 return err
189150 }
190151
191- if req .StatusCode != 200 {
192- return nil
152+ _ , _ , err = pipeApi .UpdateDeploymentVariable (c .AuthContext , * rvcr , workspace , repoSlug , deployment , d .Get ("uuid" ).(string ))
153+
154+ if err != nil {
155+ return fmt .Errorf ("error updating Deployment Variable (%s): %w" , d .Get ("deployment" ).(string ), err )
193156 }
194157
195158 return resourceDeploymentVariableRead (d , m )
196159}
197160
198161func resourceDeploymentVariableDelete (d * schema.ResourceData , m interface {}) error {
162+ c := m .(Clients ).genClient
163+ pipeApi := c .ApiClient .PipelinesApi
164+
199165 repository , deployment := parseDeploymentId (d .Get ("deployment" ).(string ))
200- client := m .(Clients ).httpClient
201- _ , err := client .Delete (fmt .Sprintf ("2.0/repositories/%s/deployments_config/environments/%s/variables/%s" ,
202- repository ,
203- deployment ,
204- d .Get ("uuid" ).(string ),
205- ))
206- return err
166+ workspace , repoSlug , err := deployVarId (repository )
167+ if err != nil {
168+ return err
169+ }
170+
171+ _ , err = pipeApi .DeleteDeploymentVariable (c .AuthContext , workspace , repoSlug , deployment , d .Get ("uuid" ).(string ))
172+ if err != nil {
173+ return fmt .Errorf ("error deleting Deployment Variable (%s): %w" , d .Id (), err )
174+ }
175+
176+ return nil
177+ }
178+
179+ func deployVarId (repo string ) (string , string , error ) {
180+ idparts := strings .Split (repo , "/" )
181+ if len (idparts ) == 2 {
182+ return idparts [0 ], idparts [1 ], nil
183+ } else {
184+ return "" , "" , fmt .Errorf ("incorrect ID format, should match `owner/key`" )
185+ }
207186}
0 commit comments