Skip to content

Commit 80dc700

Browse files
authored
Update enforcement mode for OPA policies (#1521)
Previously the tfe_policy resource could only set policies to advisory. This commit updates the update function to properly switch path for the policy enforcement name based on the kind. This was due to the previous comparison not comparing the same types so it always failed.
1 parent 842fdd0 commit 80dc700

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased
2+
BUG FIXES:
3+
* `r/tfe_policy`: enforcement level can be updated on OPA policies by @glennsarti [#1521](https://github.com/hashicorp/terraform-provider-tfe/pull/1521)
4+
15
## v0.60.0
26

37
BUG FIXES:

internal/provider/resource_tfe_policy.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ func resourceTFEPolicyRead(d *schema.ResourceData, meta interface{}) error {
271271
func resourceTFEPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
272272
config := meta.(ConfiguredClient)
273273

274+
var kind string
275+
if v, ok := d.GetOk("kind"); ok {
276+
kind = v.(string)
277+
}
278+
274279
// nolint:nestif
275280
if d.HasChange("description") || d.HasChange("enforce_mode") || d.HasChange("query") {
276281
// Create a new options struct.
@@ -281,11 +286,8 @@ func resourceTFEPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
281286
}
282287

283288
path := d.Get("name").(string) + ".sentinel"
284-
vKind, ok := d.GetOk("kind")
285-
if ok {
286-
if vKind == tfe.OPA {
287-
path = d.Get("name").(string) + ".rego"
288-
}
289+
if kind == string(tfe.OPA) {
290+
path = d.Get("name").(string) + ".rego"
289291
}
290292
if d.HasChange("enforce_mode") {
291293
//nolint:staticcheck // this is still used by TFE versions older than 202306-1
@@ -301,11 +303,11 @@ func resourceTFEPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
301303
options.Query = tfe.String(query.(string))
302304
}
303305

304-
log.Printf("[DEBUG] Update configuration for %s policy: %s", vKind, d.Id())
306+
log.Printf("[DEBUG] Update configuration for %s policy: %s", kind, d.Id())
305307
_, err := config.Client.Policies.Update(ctx, d.Id(), options)
306308
if err != nil {
307309
return fmt.Errorf(
308-
"Error updating configuration for %s policy %s: %w", vKind, d.Id(), err)
310+
"Error updating configuration for %s policy %s: %w", kind, d.Id(), err)
309311
}
310312
}
311313

internal/provider/resource_tfe_policy_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,27 @@ func TestAccTFEPolicyOPA_update(t *testing.T) {
319319
"tfe_policy.foobar", "enforce_mode", "advisory"),
320320
),
321321
},
322+
// And check that we can back to what we had before
323+
{
324+
Config: testAccTFEPolicyOPA_updateQuery(org.Name),
325+
Check: resource.ComposeTestCheckFunc(
326+
testAccCheckTFEPolicyExists(
327+
"tfe_policy.foobar", policy),
328+
testAccCheckTFEOPAPolicyAttributesUpdatedQuery(policy),
329+
resource.TestCheckResourceAttr(
330+
"tfe_policy.foobar", "name", "policy-test"),
331+
resource.TestCheckResourceAttr(
332+
"tfe_policy.foobar", "description", "A test policy"),
333+
resource.TestCheckResourceAttr(
334+
"tfe_policy.foobar", "kind", "opa"),
335+
resource.TestCheckResourceAttr(
336+
"tfe_policy.foobar", "policy", "package example rule[\"not allowed\"] { false }"),
337+
resource.TestCheckResourceAttr(
338+
"tfe_policy.foobar", "query", "data.example.ruler"),
339+
resource.TestCheckResourceAttr(
340+
"tfe_policy.foobar", "enforce_mode", "mandatory"),
341+
),
342+
},
322343
},
323344
})
324345
}

0 commit comments

Comments
 (0)