Skip to content

Commit 3fe2bc4

Browse files
fix - permission resource update failures and ignored related_resource attribute (#150)
1 parent 00f3822 commit 3fe2bc4

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

codefresh/cfclient/permission.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Permission struct {
99
ID string `json:"id,omitempty"`
1010
Team string `json:"role,omitempty"`
1111
Resource string `json:"resource,omitempty"`
12-
RelatedResource string `json:"related_resource,omitempty"`
12+
RelatedResource string `json:"relatedResource,omitempty"`
1313
Action string `json:"action,omitempty"`
1414
Account string `json:"account,omitempty"`
1515
Tags []string `json:"attributes,omitempty"`
@@ -20,7 +20,7 @@ type NewPermission struct {
2020
ID string `json:"_id,omitempty"`
2121
Team string `json:"team,omitempty"`
2222
Resource string `json:"resource,omitempty"`
23-
RelatedResource string `json:"related_resource,omitempty"`
23+
RelatedResource string `json:"relatedResource,omitempty"`
2424
Action string `json:"action,omitempty"`
2525
Account string `json:"account,omitempty"`
2626
Tags []string `json:"tags,omitempty"`
@@ -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

+36-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
99
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
funk "github.com/thoas/go-funk"
@@ -96,7 +97,9 @@ The tags for which to apply the permission. Supports two custom tags:
9697
},
9798
},
9899
},
99-
CustomizeDiff: resourcePermissionCustomDiff,
100+
CustomizeDiff: customdiff.All(
101+
resourcePermissionCustomDiff,
102+
),
100103
}
101104
}
102105

@@ -157,18 +160,30 @@ func resourcePermissionRead(d *schema.ResourceData, meta interface{}) error {
157160

158161
func resourcePermissionUpdate(d *schema.ResourceData, meta interface{}) error {
159162
client := meta.(*cfclient.Client)
160-
161163
permission := *mapResourceToPermission(d)
162-
resp, err := client.CreatePermission(&permission)
163-
if err != nil {
164-
return err
165-
}
166164

167-
deleteErr := resourcePermissionDelete(d, meta)
168-
if deleteErr != nil {
169-
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+
}
170186
}
171-
d.SetId(resp.ID)
172187

173188
return resourcePermissionRead(d, meta)
174189
}
@@ -206,6 +221,11 @@ func mapPermissionToResource(permission *cfclient.Permission, d *schema.Resource
206221
return err
207222
}
208223

224+
err = d.Set("related_resource", permission.RelatedResource)
225+
if err != nil {
226+
return err
227+
}
228+
209229
err = d.Set("tags", permission.Tags)
210230
if err != nil {
211231
return err
@@ -224,11 +244,12 @@ func mapResourceToPermission(d *schema.ResourceData) *cfclient.Permission {
224244
tags = []string{"*", "untagged"}
225245
}
226246
permission := &cfclient.Permission{
227-
ID: d.Id(),
228-
Team: d.Get("team").(string),
229-
Action: d.Get("action").(string),
230-
Resource: d.Get("resource").(string),
231-
Tags: tags,
247+
ID: d.Id(),
248+
Team: d.Get("team").(string),
249+
Action: d.Get("action").(string),
250+
Resource: d.Get("resource").(string),
251+
RelatedResource: d.Get("related_resource").(string),
252+
Tags: tags,
232253
}
233254

234255
return permission

codefresh/resource_permission_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ 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", ""),
31+
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
32+
),
33+
},
34+
{
35+
Config: testAccCodefreshPermissionConfig("create", "pipeline", "project", []string{"production", "*"}),
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckCodefreshPermissionExists(resourceName),
38+
resource.TestCheckResourceAttr(resourceName, "action", "create"),
39+
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
40+
resource.TestCheckResourceAttr(resourceName, "related_resource", "project"),
41+
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
3042
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
3143
),
3244
},

0 commit comments

Comments
 (0)