Skip to content

Commit 0716c8b

Browse files
fix resource update
1 parent e63d5a7 commit 0716c8b

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

codefresh/cfclient/permission.go

+21
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,24 @@ func (client *Client) DeletePermission(id string) error {
142142

143143
return nil
144144
}
145+
146+
func (client *Client) UpdatePermissionTags(permission *Permission) error {
147+
148+
fullPath := fmt.Sprintf("/abac/tags/rule/%s", permission.ID)
149+
150+
body, _ := EncodeToJSON(permission.Tags)
151+
152+
opts := RequestOptions{
153+
Path: fullPath,
154+
Method: "POST",
155+
Body: body,
156+
}
157+
158+
_, err := client.RequestAPI(&opts)
159+
160+
if err != nil {
161+
return err
162+
}
163+
164+
return nil
165+
}

codefresh/resource_permission.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ The tags for which to apply the permission. Supports two custom tags:
9999
},
100100
CustomizeDiff: customdiff.All(
101101
resourcePermissionCustomDiff,
102-
customdiff.ForceNewIfChange("related_resource", func(ctx context.Context, oldValue, newValue, meta interface{}) bool {
103-
return true
104-
}),
105102
),
106103
}
107104
}
@@ -163,18 +160,30 @@ func resourcePermissionRead(d *schema.ResourceData, meta interface{}) error {
163160

164161
func resourcePermissionUpdate(d *schema.ResourceData, meta interface{}) error {
165162
client := meta.(*cfclient.Client)
166-
167163
permission := *mapResourceToPermission(d)
168-
resp, err := client.CreatePermission(&permission)
169-
if err != nil {
170-
return err
171-
}
172164

173-
deleteErr := resourcePermissionDelete(d, meta)
174-
if deleteErr != nil {
175-
log.Printf("[WARN] failed to delete permission %v: %v", permission, deleteErr)
165+
// 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)
166+
if d.HasChanges("team", "action", "related_resource", "resource") {
167+
deleteErr := resourcePermissionDelete(d, meta)
168+
169+
if deleteErr != nil {
170+
log.Printf("[WARN] failed to delete permission %v: %v", permission, deleteErr)
171+
}
172+
173+
resp, err := client.CreatePermission(&permission)
174+
175+
if err != nil {
176+
return err
177+
}
178+
179+
d.SetId(resp.ID)
180+
// Only tags can be updated
181+
} else if d.HasChange("tags") {
182+
err := client.UpdatePermissionTags(&permission)
183+
if err != nil {
184+
return err
185+
}
176186
}
177-
d.SetId(resp.ID)
178187

179188
return resourcePermissionRead(d, meta)
180189
}

codefresh/resource_permission_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
2727
resource.TestCheckResourceAttr(resourceName, "action", "create"),
2828
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
2929
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
30-
resource.TestCheckResourceAttr(resourceName, "related_resource",""),
30+
resource.TestCheckResourceAttr(resourceName, "related_resource", ""),
3131
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
3232
),
3333
},

0 commit comments

Comments
 (0)