Skip to content

Commit 13fe246

Browse files
authored
Merged automatically by CI pipeline
SCALRCORE-28980 API > Policy Engine > Enforce policy group for all cu…
2 parents c357edb + a953f1e commit 13fe246

File tree

6 files changed

+98
-18
lines changed

6 files changed

+98
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- `scalr_policy_group`: `environments` attribute became optional instead of read-only ([#288](https://github.com/Scalr/terraform-provider-scalr/pull/288))
13+
1014
## [1.6.0] - 2023-10-27
1115

1216
### Added

docs/resources/policy_group.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ resource "scalr_policy_group" "example" {
3838
### Optional
3939

4040
- `account_id` (String) The identifier of the Scalr account, in the format `acc-<RANDOM STRING>`.
41+
- `environments` (List of String) A list of the environments the policy group is linked to. Use `["*"]` to enforce in all environments.
4142
- `opa_version` (String) The version of Open Policy Agent to run policies against. If omitted, the system default version is assigned.
4243

4344
### Read-Only
4445

45-
- `environments` (List of String) A list of the environments the policy group is linked to.
4646
- `error_message` (String) A detailed error if Scalr failed to process the policy group.
4747
- `id` (String) The ID of this resource.
4848
- `policies` (List of Object) A list of the OPA policies the group verifies each run. (see [below for nested schema](#nestedatt--policies))

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/hashicorp/terraform-plugin-docs v0.16.0
77
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
88
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
9-
github.com/scalr/go-scalr v0.0.0-20231004160327-c0bbb43d3b4f
9+
github.com/scalr/go-scalr v0.0.0-20231117090940-913594e4e135
1010
)
1111

1212
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
260260
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
261261
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
262262
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
263-
github.com/scalr/go-scalr v0.0.0-20231004160327-c0bbb43d3b4f h1:2acK1M8YNfTe1rpn9UUhR0UIGMWCmHw4BMxQCCM7lPo=
264-
github.com/scalr/go-scalr v0.0.0-20231004160327-c0bbb43d3b4f/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
263+
github.com/scalr/go-scalr v0.0.0-20231117090940-913594e4e135 h1:EAfMV+rwOLld3pJPwUnrFRyt3jxYx/N0q+fjextco0s=
264+
github.com/scalr/go-scalr v0.0.0-20231117090940-913594e4e135/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
265265
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
266266
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
267267
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=

scalr/data_source_scalr_policy_group.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func dataSourceScalrPolicyGroup() *schema.Resource {
107107
},
108108
},
109109
"environments": {
110-
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, or `[\"*\"]` if enforced in all environments.",
111111
Type: schema.TypeList,
112112
Computed: true,
113113
Elem: &schema.Schema{Type: schema.TypeString},
@@ -183,13 +183,15 @@ func dataSourceScalrPolicyGroupRead(ctx context.Context, d *schema.ResourceData,
183183
}
184184
_ = d.Set("policies", policies)
185185

186-
var envs []string
187-
if len(pg.Environments) != 0 {
186+
if pg.IsEnforced {
187+
_ = d.Set("environments", []string{"*"})
188+
} else {
189+
envs := make([]string, 0)
188190
for _, env := range pg.Environments {
189191
envs = append(envs, env.ID)
190192
}
193+
_ = d.Set("environments", envs)
191194
}
192-
_ = d.Set("environments", envs)
193195

194196
d.SetId(pg.ID)
195197

scalr/resource_scalr_policy_group.go

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"context"
55
"errors"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7-
"log"
8-
97
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
108
"github.com/scalr/go-scalr"
9+
"log"
1110
)
1211

1312
func resourceScalrPolicyGroup() *schema.Resource {
@@ -108,9 +107,9 @@ func resourceScalrPolicyGroup() *schema.Resource {
108107
},
109108
},
110109
"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.",
112111
Type: schema.TypeList,
113-
Computed: true,
112+
Optional: true,
114113
Elem: &schema.Schema{Type: schema.TypeString},
115114
},
116115
},
@@ -141,6 +140,24 @@ func resourceScalrPolicyGroupCreate(ctx context.Context, d *schema.ResourceData,
141140
VCSRepo: vcsOpt,
142141
Account: &scalr.Account{ID: accountID},
143142
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+
}
144161
}
145162

146163
// Optional attributes
@@ -154,6 +171,25 @@ func resourceScalrPolicyGroupCreate(ctx context.Context, d *schema.ResourceData,
154171
}
155172

156173
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+
157193
return resourceScalrPolicyGroupRead(ctx, d, meta)
158194
}
159195

@@ -197,13 +233,16 @@ func resourceScalrPolicyGroupRead(ctx context.Context, d *schema.ResourceData, m
197233
}
198234
_ = d.Set("policies", policies)
199235

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)
204243
}
244+
_ = d.Set("environments", environmentIDs)
205245
}
206-
_ = d.Set("environments", envs)
207246

208247
return nil
209248
}
@@ -214,7 +253,8 @@ func resourceScalrPolicyGroupUpdate(ctx context.Context, d *schema.ResourceData,
214253
id := d.Id()
215254

216255
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") {
218258

219259
name := d.Get("name").(string)
220260
vcsProviderID := d.Get("vcs_provider_id").(string)
@@ -234,16 +274,50 @@ func resourceScalrPolicyGroupUpdate(ctx context.Context, d *schema.ResourceData,
234274
Name: scalr.String(name),
235275
VCSRepo: vcsOpt,
236276
VcsProvider: &scalr.VcsProvider{ID: vcsProviderID},
277+
IsEnforced: scalr.Bool(false),
237278
}
238279
if opaVersion, ok := d.GetOk("opa_version"); ok {
239280
opts.OpaVersion = scalr.String(opaVersion.(string))
240281
}
241282

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+
242300
log.Printf("[DEBUG] Update policy group %s", id)
243301
_, err := scalrClient.PolicyGroups.Update(ctx, id, opts)
244302
if err != nil {
245303
return diag.Errorf("error updating policy group %s: %v", id, err)
246304
}
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+
}
247321
}
248322

249323
return resourceScalrPolicyGroupRead(ctx, d, meta)

0 commit comments

Comments
 (0)