From ef305bdeca90227fd0a5f7f4a7f7c0bb448a80f8 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 29 Oct 2023 23:31:33 +0200 Subject: [PATCH] fix project update --- bitbucket/resource_project.go | 41 ++++++++++++++++++++++++++++-- bitbucket/resource_project_test.go | 27 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/bitbucket/resource_project.go b/bitbucket/resource_project.go index 80068b29..69ea14c5 100644 --- a/bitbucket/resource_project.go +++ b/bitbucket/resource_project.go @@ -86,6 +86,10 @@ func resourceProject() *schema.Resource { } } +// type SmallProject struct { +// Links *bitbucket.ProjectLinks `json:"links,omitempty"` +// } + func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project { project := &bitbucket.Project{ Name: d.Get("name").(string), @@ -94,7 +98,7 @@ func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project { Key: d.Get("key").(string), } - if v, ok := d.GetOk("link"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil { + if v, ok := d.GetOk("link"); d.IsNewResource() && ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil { project.Links = expandProjectLinks(v.([]interface{})) } @@ -103,6 +107,7 @@ func newProjectFromResource(d *schema.ResourceData) *bitbucket.Project { func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { c := m.(Clients).genClient + // client := m.(Clients).httpClient projectApi := c.ApiClient.ProjectsApi project := newProjectFromResource(d) @@ -112,11 +117,41 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf projectKey = d.Get("key").(string) } - _, _, err := projectApi.WorkspacesWorkspaceProjectsProjectKeyPut(c.AuthContext, *project, projectKey, d.Get("owner").(string)) + log.Printf("[DEBUG] Project Update Body: %#v", project) + project.Links = nil + + prj, _, err := projectApi.WorkspacesWorkspaceProjectsProjectKeyPut(c.AuthContext, *project, projectKey, d.Get("owner").(string)) if err := handleClientError(err); err != nil { return diag.FromErr(err) } + log.Printf("[DEBUG] Project Update Res: %#v", prj) + + // if d.HasChange("link") { + // if v, ok := d.GetOk("link"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil { + + // smallProject := SmallProject{ + // Links: expandProjectLinks(v.([]interface{})), + // } + + // payload, err := json.Marshal(smallProject) + // if err != nil { + // return diag.FromErr(err) + // } + + // log.Printf("[DEBUG] Project Update Links Body: %s", string(payload)) + + // _, err = client.Put(fmt.Sprintf("2.0/workspaces/%s/projects/%s", + // d.Get("owner").(string), d.Get("key").(string), + // ), bytes.NewBuffer(payload)) + + // if err != nil { + // return diag.FromErr(err) + // } + + // } + // } + return resourceProjectRead(ctx, d, m) } @@ -133,6 +168,8 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interf owner := d.Get("owner").(string) + log.Printf("[DEBUG] Project Create Body: %#v", project) + projRes, _, err := projectApi.WorkspacesWorkspaceProjectsPost(c.AuthContext, *project, owner) if err := handleClientError(err); err != nil { return diag.FromErr(err) diff --git a/bitbucket/resource_project_test.go b/bitbucket/resource_project_test.go index 955b9729..a50c82e4 100644 --- a/bitbucket/resource_project_test.go +++ b/bitbucket/resource_project_test.go @@ -42,6 +42,22 @@ func TestAccBitbucketProject_basic(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccBitbucketProjectDescConfig(testTeam, rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckBitbucketProjectExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "has_publicly_visible_repos", "false"), + resource.TestCheckResourceAttr(resourceName, "key", "AAAAAA"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "owner", testTeam), + resource.TestCheckResourceAttr(resourceName, "description", rName), + resource.TestCheckResourceAttr(resourceName, "is_private", "true"), + resource.TestCheckResourceAttrSet(resourceName, "uuid"), + resource.TestCheckResourceAttr(resourceName, "link.#", "1"), + resource.TestCheckResourceAttr(resourceName, "link.0.avatar.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "link.0.avatar.0.href"), + ), + }, }, }) } @@ -84,6 +100,17 @@ resource "bitbucket_project" "test" { `, team, rName) } +func testAccBitbucketProjectDescConfig(team, rName, desc string) string { + return fmt.Sprintf(` +resource "bitbucket_project" "test" { + owner = %[1]q + name = %[2]q + key = "AAAAAA" + description = %[3]q +} +`, team, rName, desc) +} + func testAccBitbucketProjectAvatarConfig(team, rName string) string { return fmt.Sprintf(` resource "bitbucket_project" "test" {