Skip to content

Commit

Permalink
Merge pull request #182 from DrFaust92/project-update
Browse files Browse the repository at this point in the history
Fix Project update
  • Loading branch information
DrFaust92 authored Oct 29, 2023
2 parents 41d08d0 + ef305bd commit 6b04a68
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
41 changes: 39 additions & 2 deletions bitbucket/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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{}))
}

Expand All @@ -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)

Expand All @@ -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)
}

Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions bitbucket/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
})
}
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 6b04a68

Please sign in to comment.