7
7
8
8
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
9
9
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
10
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
10
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12
13
funk "github.com/thoas/go-funk"
@@ -96,7 +97,9 @@ The tags for which to apply the permission. Supports two custom tags:
96
97
},
97
98
},
98
99
},
99
- CustomizeDiff : resourcePermissionCustomDiff ,
100
+ CustomizeDiff : customdiff .All (
101
+ resourcePermissionCustomDiff ,
102
+ ),
100
103
}
101
104
}
102
105
@@ -157,18 +160,30 @@ func resourcePermissionRead(d *schema.ResourceData, meta interface{}) error {
157
160
158
161
func resourcePermissionUpdate (d * schema.ResourceData , meta interface {}) error {
159
162
client := meta .(* cfclient.Client )
160
-
161
163
permission := * mapResourceToPermission (d )
162
- resp , err := client .CreatePermission (& permission )
163
- if err != nil {
164
- return err
165
- }
166
164
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
+ }
170
186
}
171
- d .SetId (resp .ID )
172
187
173
188
return resourcePermissionRead (d , meta )
174
189
}
@@ -206,6 +221,11 @@ func mapPermissionToResource(permission *cfclient.Permission, d *schema.Resource
206
221
return err
207
222
}
208
223
224
+ err = d .Set ("related_resource" , permission .RelatedResource )
225
+ if err != nil {
226
+ return err
227
+ }
228
+
209
229
err = d .Set ("tags" , permission .Tags )
210
230
if err != nil {
211
231
return err
@@ -224,11 +244,12 @@ func mapResourceToPermission(d *schema.ResourceData) *cfclient.Permission {
224
244
tags = []string {"*" , "untagged" }
225
245
}
226
246
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 ,
232
253
}
233
254
234
255
return permission
0 commit comments