Skip to content

Commit

Permalink
fix resource update
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Jul 10, 2024
1 parent e63d5a7 commit 0716c8b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
21 changes: 21 additions & 0 deletions codefresh/cfclient/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,24 @@ func (client *Client) DeletePermission(id string) error {

return nil
}

func (client *Client) UpdatePermissionTags(permission *Permission) error {

fullPath := fmt.Sprintf("/abac/tags/rule/%s", permission.ID)

body, _ := EncodeToJSON(permission.Tags)

opts := RequestOptions{
Path: fullPath,
Method: "POST",
Body: body,
}

_, err := client.RequestAPI(&opts)

if err != nil {
return err
}

return nil
}
33 changes: 21 additions & 12 deletions codefresh/resource_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ The tags for which to apply the permission. Supports two custom tags:
},
CustomizeDiff: customdiff.All(
resourcePermissionCustomDiff,
customdiff.ForceNewIfChange("related_resource", func(ctx context.Context, oldValue, newValue, meta interface{}) bool {
return true
}),
),
}
}
Expand Down Expand Up @@ -163,18 +160,30 @@ func resourcePermissionRead(d *schema.ResourceData, meta interface{}) error {

func resourcePermissionUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)

permission := *mapResourceToPermission(d)
resp, err := client.CreatePermission(&permission)
if err != nil {
return err
}

deleteErr := resourcePermissionDelete(d, meta)
if deleteErr != nil {
log.Printf("[WARN] failed to delete permission %v: %v", permission, deleteErr)
// In case team, action or relatedResource or resource have changed - a new permission needs to be created (but without recreating the terraform resource as destruction of resources is alarming for end users)
if d.HasChanges("team", "action", "related_resource", "resource") {
deleteErr := resourcePermissionDelete(d, meta)

if deleteErr != nil {
log.Printf("[WARN] failed to delete permission %v: %v", permission, deleteErr)
}

resp, err := client.CreatePermission(&permission)

if err != nil {
return err
}

d.SetId(resp.ID)
// Only tags can be updated
} else if d.HasChange("tags") {
err := client.UpdatePermissionTags(&permission)
if err != nil {
return err
}
}
d.SetId(resp.ID)

return resourcePermissionRead(d, meta)
}
Expand Down
2 changes: 1 addition & 1 deletion codefresh/resource_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "action", "create"),
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
resource.TestCheckResourceAttr(resourceName, "related_resource",""),
resource.TestCheckResourceAttr(resourceName, "related_resource", ""),
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
),
},
Expand Down

0 comments on commit 0716c8b

Please sign in to comment.