Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Project update #182

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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