Skip to content

Commit 2721673

Browse files
authored
Merge pull request #67 from DrFaust92/repo-var-bb-client
Repo Var - refactor to use BB client (+ better acctests)
2 parents 10472b6 + 56fe7fa commit 2721673

File tree

5 files changed

+95
-89
lines changed

5 files changed

+95
-89
lines changed

bitbucket/resource_project.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
132132

133133
owner := d.Get("owner").(string)
134134

135-
log.Printf("haha %#v", project)
136-
137135
projRes, _, err := projectApi.WorkspacesWorkspaceProjectsPost(c.AuthContext, *project, owner)
138136
if err != nil {
139137
return fmt.Errorf("error creating project (%s): %w", projectKey, err)
Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
package bitbucket
22

33
import (
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-
2313
func 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

6656
func 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

10079
func 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

141112
func 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

165136
func 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
}

bitbucket/resource_repository_variable_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitbucket
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"testing"
78

@@ -46,9 +47,28 @@ func TestAccBitbucketRepositoryVariable_basic(t *testing.T) {
4647
}
4748

4849
func testAccCheckBitbucketRepositoryVariableDestroy(s *terraform.State) error {
49-
_, ok := s.RootModule().Resources["bitbucket_repository_variable.test"]
50-
if !ok {
51-
return fmt.Errorf("Not found %s", "bitbucket_repository_variable.test")
50+
client := testAccProvider.Meta().(Clients).genClient
51+
pipeApi := client.ApiClient.PipelinesApi
52+
53+
for _, rs := range s.RootModule().Resources {
54+
if rs.Type != "bitbucket_repository_variable" {
55+
continue
56+
}
57+
58+
workspace, repoSlug, err := repoVarId(rs.Primary.Attributes["repository"])
59+
if err != nil {
60+
return err
61+
}
62+
63+
_, res, err := pipeApi.GetRepositoryPipelineVariable(client.AuthContext, workspace, repoSlug, rs.Primary.Attributes["uuid"])
64+
65+
if err == nil {
66+
return fmt.Errorf("The resource was found should have errored")
67+
}
68+
69+
if res.StatusCode != http.StatusNotFound {
70+
return fmt.Errorf("Repository Variable still exists")
71+
}
5272
}
5373
return nil
5474
}
@@ -76,7 +96,6 @@ resource "bitbucket_repository_variable" "test" {
7696
key = "test"
7797
value = %[3]q
7898
repository = bitbucket_repository.test.id
79-
secured = false
8099
}
81100
`, team, rName, val)
82101
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/terraform-providers/terraform-provider-bitbucket
22

33
require (
4-
github.com/DrFaust92/bitbucket-go-client v0.0.6
4+
github.com/DrFaust92/bitbucket-go-client v0.0.8
55
github.com/antihax/optional v1.0.0
66
github.com/hashicorp/terraform-plugin-sdk/v2 v2.15.0
77
github.com/satori/go.uuid v1.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
3333
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3434
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3535
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
36-
github.com/DrFaust92/bitbucket-go-client v0.0.6 h1:vpXZC11cFNSByLi7sRZa06T4Qxso/HhVJvqgj2YdLZg=
37-
github.com/DrFaust92/bitbucket-go-client v0.0.6/go.mod h1:3UJtT6PmlsB7MCc1MbegmGlT2waRlIMIHtaQLTcd794=
36+
github.com/DrFaust92/bitbucket-go-client v0.0.8 h1:sU4J6d+uXPpRAjGb00upd6ZaGEh4TGQuWhfilANDQoM=
37+
github.com/DrFaust92/bitbucket-go-client v0.0.8/go.mod h1:3UJtT6PmlsB7MCc1MbegmGlT2waRlIMIHtaQLTcd794=
3838
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
3939
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
4040
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=

0 commit comments

Comments
 (0)