@@ -4,10 +4,9 @@ import (
4
4
"context"
5
5
"errors"
6
6
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
- "log"
8
-
9
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
8
"github.com/scalr/go-scalr"
9
+ "log"
11
10
)
12
11
13
12
func resourceScalrPolicyGroup () * schema.Resource {
@@ -108,9 +107,9 @@ func resourceScalrPolicyGroup() *schema.Resource {
108
107
},
109
108
},
110
109
"environments" : {
111
- Description : "A list of the environments the policy group is linked to." ,
110
+ Description : "A list of the environments the policy group is linked to. Use `[ \" * \" ]` to enforce in all environments. " ,
112
111
Type : schema .TypeList ,
113
- Computed : true ,
112
+ Optional : true ,
114
113
Elem : & schema.Schema {Type : schema .TypeString },
115
114
},
116
115
},
@@ -141,6 +140,24 @@ func resourceScalrPolicyGroupCreate(ctx context.Context, d *schema.ResourceData,
141
140
VCSRepo : vcsOpt ,
142
141
Account : & scalr.Account {ID : accountID },
143
142
VcsProvider : & scalr.VcsProvider {ID : vcsProviderID },
143
+ IsEnforced : scalr .Bool (false ),
144
+ }
145
+
146
+ environments := make ([]* scalr.Environment , 0 )
147
+ if environmentsI , ok := d .GetOk ("environments" ); ok {
148
+ environmentsIDs := environmentsI .([]interface {})
149
+ if (len (environmentsIDs ) == 1 ) && environmentsIDs [0 ].(string ) == "*" {
150
+ opts .IsEnforced = scalr .Bool (true )
151
+ } else if len (environmentsIDs ) > 0 {
152
+ for _ , env := range environmentsIDs {
153
+ if env .(string ) == "*" {
154
+ return diag .Errorf (
155
+ "impossible to enforce the policy group in all and on a limited list of environments. Please remove either wildcard or environment identifiers" ,
156
+ )
157
+ }
158
+ environments = append (environments , & scalr.Environment {ID : env .(string )})
159
+ }
160
+ }
144
161
}
145
162
146
163
// Optional attributes
@@ -154,6 +171,25 @@ func resourceScalrPolicyGroupCreate(ctx context.Context, d *schema.ResourceData,
154
171
}
155
172
156
173
d .SetId (pg .ID )
174
+
175
+ if len (environments ) > 0 && ! * opts .IsEnforced {
176
+ pgEnvs := make ([]* scalr.PolicyGroupEnvironment , 0 )
177
+ for _ , env := range environments {
178
+ pgEnvs = append (pgEnvs , & scalr.PolicyGroupEnvironment {ID : env .ID })
179
+ }
180
+ pgEnvsOpts := scalr.PolicyGroupEnvironmentsCreateOptions {
181
+ PolicyGroupID : pg .ID ,
182
+ PolicyGroupEnvironments : pgEnvs ,
183
+ }
184
+ err = scalrClient .PolicyGroupEnvironments .Create (ctx , pgEnvsOpts )
185
+ if err != nil {
186
+ defer func (ctx context.Context , pgID string ) {
187
+ _ = scalrClient .PolicyGroups .Delete (ctx , pgID )
188
+ }(ctx , pg .ID )
189
+ return diag .Errorf ("error linking environments to policy group '%s': %v" , name , err )
190
+ }
191
+ }
192
+
157
193
return resourceScalrPolicyGroupRead (ctx , d , meta )
158
194
}
159
195
@@ -197,13 +233,16 @@ func resourceScalrPolicyGroupRead(ctx context.Context, d *schema.ResourceData, m
197
233
}
198
234
_ = d .Set ("policies" , policies )
199
235
200
- var envs []string
201
- if len (pg .Environments ) != 0 {
202
- for _ , env := range pg .Environments {
203
- envs = append (envs , env .ID )
236
+ if pg .IsEnforced {
237
+ allEnvironments := []string {"*" }
238
+ _ = d .Set ("environments" , allEnvironments )
239
+ } else {
240
+ environmentIDs := make ([]string , 0 )
241
+ for _ , environment := range pg .Environments {
242
+ environmentIDs = append (environmentIDs , environment .ID )
204
243
}
244
+ _ = d .Set ("environments" , environmentIDs )
205
245
}
206
- _ = d .Set ("environments" , envs )
207
246
208
247
return nil
209
248
}
@@ -214,7 +253,8 @@ func resourceScalrPolicyGroupUpdate(ctx context.Context, d *schema.ResourceData,
214
253
id := d .Id ()
215
254
216
255
if d .HasChange ("name" ) || d .HasChange ("opa_version" ) ||
217
- d .HasChange ("vcs_provider_id" ) || d .HasChange ("vcs_repo" ) {
256
+ d .HasChange ("vcs_provider_id" ) || d .HasChange ("vcs_repo" ) ||
257
+ d .HasChange ("environments" ) {
218
258
219
259
name := d .Get ("name" ).(string )
220
260
vcsProviderID := d .Get ("vcs_provider_id" ).(string )
@@ -234,16 +274,50 @@ func resourceScalrPolicyGroupUpdate(ctx context.Context, d *schema.ResourceData,
234
274
Name : scalr .String (name ),
235
275
VCSRepo : vcsOpt ,
236
276
VcsProvider : & scalr.VcsProvider {ID : vcsProviderID },
277
+ IsEnforced : scalr .Bool (false ),
237
278
}
238
279
if opaVersion , ok := d .GetOk ("opa_version" ); ok {
239
280
opts .OpaVersion = scalr .String (opaVersion .(string ))
240
281
}
241
282
283
+ environments := make ([]* scalr.Environment , 0 )
284
+ if environmentsI , ok := d .GetOk ("environments" ); ok {
285
+ environmentsIDs := environmentsI .([]interface {})
286
+ if (len (environmentsIDs ) == 1 ) && environmentsIDs [0 ].(string ) == "*" {
287
+ opts .IsEnforced = scalr .Bool (true )
288
+ } else if len (environmentsIDs ) > 0 {
289
+ for _ , env := range environmentsIDs {
290
+ if env .(string ) == "*" {
291
+ return diag .Errorf (
292
+ "impossible to enforce the policy group in all and on a limited list of environments. Please remove either wildcard or environment identifiers" ,
293
+ )
294
+ }
295
+ environments = append (environments , & scalr.Environment {ID : env .(string )})
296
+ }
297
+ }
298
+ }
299
+
242
300
log .Printf ("[DEBUG] Update policy group %s" , id )
243
301
_ , err := scalrClient .PolicyGroups .Update (ctx , id , opts )
244
302
if err != nil {
245
303
return diag .Errorf ("error updating policy group %s: %v" , id , err )
246
304
}
305
+
306
+ if ! * opts .IsEnforced {
307
+ pgEnvs := make ([]* scalr.PolicyGroupEnvironment , 0 )
308
+ for _ , env := range environments {
309
+ pgEnvs = append (pgEnvs , & scalr.PolicyGroupEnvironment {ID : env .ID })
310
+ }
311
+ pgEnvsOpts := scalr.PolicyGroupEnvironmentsUpdateOptions {
312
+ PolicyGroupID : id ,
313
+ PolicyGroupEnvironments : pgEnvs ,
314
+ }
315
+
316
+ err = scalrClient .PolicyGroupEnvironments .Update (ctx , pgEnvsOpts )
317
+ if err != nil {
318
+ return diag .Errorf ("error updating environments for policy group %s: %v" , id , err )
319
+ }
320
+ }
247
321
}
248
322
249
323
return resourceScalrPolicyGroupRead (ctx , d , meta )
0 commit comments