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
158161func 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
0 commit comments