Skip to content

Commit 6b04a68

Browse files
authored
Merge pull request #182 from DrFaust92/project-update
Fix Project update
2 parents 41d08d0 + ef305bd commit 6b04a68

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

Diff for: bitbucket/resource_project.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func resourceProject() *schema.Resource {
8686
}
8787
}
8888

89+
// type SmallProject struct {
90+
// Links *bitbucket.ProjectLinks `json:"links,omitempty"`
91+
// }
92+
8993
func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project {
9094
project := &bitbucket.Project{
9195
Name: d.Get("name").(string),
@@ -94,7 +98,7 @@ func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project {
9498
Key: d.Get("key").(string),
9599
}
96100

97-
if v, ok := d.GetOk("link"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
101+
if v, ok := d.GetOk("link"); d.IsNewResource() && ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
98102
project.Links = expandProjectLinks(v.([]interface{}))
99103
}
100104

@@ -103,6 +107,7 @@ func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project {
103107

104108
func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
105109
c := m.(Clients).genClient
110+
// client := m.(Clients).httpClient
106111
projectApi := c.ApiClient.ProjectsApi
107112
project := newProjectFromResource(d)
108113

@@ -112,11 +117,41 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf
112117
projectKey = d.Get("key").(string)
113118
}
114119

115-
_, _, err := projectApi.WorkspacesWorkspaceProjectsProjectKeyPut(c.AuthContext, *project, projectKey, d.Get("owner").(string))
120+
log.Printf("[DEBUG] Project Update Body: %#v", project)
121+
project.Links = nil
122+
123+
prj, _, err := projectApi.WorkspacesWorkspaceProjectsProjectKeyPut(c.AuthContext, *project, projectKey, d.Get("owner").(string))
116124
if err := handleClientError(err); err != nil {
117125
return diag.FromErr(err)
118126
}
119127

128+
log.Printf("[DEBUG] Project Update Res: %#v", prj)
129+
130+
// if d.HasChange("link") {
131+
// if v, ok := d.GetOk("link"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
132+
133+
// smallProject := SmallProject{
134+
// Links: expandProjectLinks(v.([]interface{})),
135+
// }
136+
137+
// payload, err := json.Marshal(smallProject)
138+
// if err != nil {
139+
// return diag.FromErr(err)
140+
// }
141+
142+
// log.Printf("[DEBUG] Project Update Links Body: %s", string(payload))
143+
144+
// _, err = client.Put(fmt.Sprintf("2.0/workspaces/%s/projects/%s",
145+
// d.Get("owner").(string), d.Get("key").(string),
146+
// ), bytes.NewBuffer(payload))
147+
148+
// if err != nil {
149+
// return diag.FromErr(err)
150+
// }
151+
152+
// }
153+
// }
154+
120155
return resourceProjectRead(ctx, d, m)
121156
}
122157

@@ -133,6 +168,8 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interf
133168

134169
owner := d.Get("owner").(string)
135170

171+
log.Printf("[DEBUG] Project Create Body: %#v", project)
172+
136173
projRes, _, err := projectApi.WorkspacesWorkspaceProjectsPost(c.AuthContext, *project, owner)
137174
if err := handleClientError(err); err != nil {
138175
return diag.FromErr(err)

Diff for: bitbucket/resource_project_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ func TestAccBitbucketProject_basic(t *testing.T) {
4242
ImportState: true,
4343
ImportStateVerify: true,
4444
},
45+
{
46+
Config: testAccBitbucketProjectDescConfig(testTeam, rName, rName),
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckBitbucketProjectExists(resourceName),
49+
resource.TestCheckResourceAttr(resourceName, "has_publicly_visible_repos", "false"),
50+
resource.TestCheckResourceAttr(resourceName, "key", "AAAAAA"),
51+
resource.TestCheckResourceAttr(resourceName, "name", rName),
52+
resource.TestCheckResourceAttr(resourceName, "owner", testTeam),
53+
resource.TestCheckResourceAttr(resourceName, "description", rName),
54+
resource.TestCheckResourceAttr(resourceName, "is_private", "true"),
55+
resource.TestCheckResourceAttrSet(resourceName, "uuid"),
56+
resource.TestCheckResourceAttr(resourceName, "link.#", "1"),
57+
resource.TestCheckResourceAttr(resourceName, "link.0.avatar.#", "1"),
58+
resource.TestCheckResourceAttrSet(resourceName, "link.0.avatar.0.href"),
59+
),
60+
},
4561
},
4662
})
4763
}
@@ -84,6 +100,17 @@ resource "bitbucket_project" "test" {
84100
`, team, rName)
85101
}
86102

103+
func testAccBitbucketProjectDescConfig(team, rName, desc string) string {
104+
return fmt.Sprintf(`
105+
resource "bitbucket_project" "test" {
106+
owner = %[1]q
107+
name = %[2]q
108+
key = "AAAAAA"
109+
description = %[3]q
110+
}
111+
`, team, rName, desc)
112+
}
113+
87114
func testAccBitbucketProjectAvatarConfig(team, rName string) string {
88115
return fmt.Sprintf(`
89116
resource "bitbucket_project" "test" {

0 commit comments

Comments
 (0)