diff --git a/CHANGELOG.md b/CHANGELOG.md index 249057164..ac628b5b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Enhancements -* Add OPA support to the Policy Set API's by @mrinalirao [#575](https://github.com/hashicorp/go-tfe/pull/575) +* Add OPA support to the Policy Set APIs by @mrinalirao [#575](https://github.com/hashicorp/go-tfe/pull/575) +* Add OPA support to the Policy APIs by @mrinalirao [#579](https://github.com/hashicorp/go-tfe/pull/579) # v1.12.0 diff --git a/errors.go b/errors.go index a53769241..31371e7da 100644 --- a/errors.go +++ b/errors.go @@ -199,7 +199,7 @@ var ( ErrRequiredName = errors.New("name is required") - ErrRequiredQuery = errors.New("invalid attribute\n\nQuery can't be blank") + ErrRequiredQuery = errors.New("query cannot be empty") ErrRequiredEnabled = errors.New("enabled is required") diff --git a/helper_test.go b/helper_test.go index f9f804daa..b424a5e2e 100644 --- a/helper_test.go +++ b/helper_test.go @@ -614,7 +614,7 @@ func createPolicy(t *testing.T, client *Client, org *Organization) (*Policy, fun } } -func createPolicyWithOptions(t *testing.T, client *Client, org *Organization, opts *PolicyCreateOptions) (*Policy, func()) { +func createPolicyWithOptions(t *testing.T, client *Client, org *Organization, opts PolicyCreateOptions) (*Policy, func()) { var orgCleanup func() if org == nil { @@ -677,7 +677,7 @@ func createUploadedPolicy(t *testing.T, client *Client, pass bool, org *Organiza } } -func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, org *Organization, opts *PolicyCreateOptions) (*Policy, func()) { +func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, org *Organization, opts PolicyCreateOptions) (*Policy, func()) { var orgCleanup func() if org == nil { diff --git a/policy.go b/policy.go index 43552bb58..0baa9530b 100644 --- a/policy.go +++ b/policy.go @@ -111,7 +111,7 @@ type PolicyCreateOptions struct { Name *string `jsonapi:"attr,name"` // **Note: This field is still in BETA and subject to change.** - // Optional: The underlying technology that the policy supports. + // Optional: The underlying technology that the policy supports. Defaults to Sentinel if not specified for PolicyCreate. Kind PolicyKind `jsonapi:"attr,kind,omitempty"` // **Note: This field is still in BETA and subject to change.** @@ -289,6 +289,9 @@ func (o PolicyCreateOptions) valid() error { if !validStringID(o.Name) { return ErrInvalidName } + if o.Kind == OPA && !validString(o.Query) { + return ErrRequiredQuery + } if o.Enforce == nil { return ErrRequiredEnforce } diff --git a/policy_integration_beta_test.go b/policy_integration_beta_test.go index f7100a696..93948186e 100644 --- a/policy_integration_beta_test.go +++ b/policy_integration_beta_test.go @@ -193,7 +193,7 @@ func TestPoliciesCreate_Beta(t *testing.T) { assert.Equal(t, err, ErrRequiredEnforcementPath) }) - t.Run("when options is missing enforcement path", func(t *testing.T) { + t.Run("when options is missing enforcement mode", func(t *testing.T) { name := randomString(t) options := PolicyCreateOptions{ Name: String(name), @@ -235,7 +235,7 @@ func TestPoliciesList_Beta(t *testing.T) { defer pTestCleanup1() pTest2, pTestCleanup2 := createPolicy(t, client, orgTest) defer pTestCleanup2() - opaOptions := &PolicyCreateOptions{ + opaOptions := PolicyCreateOptions{ Kind: OPA, Query: String("terraform.policy1.deny"), Enforce: []*EnforcementOptions{ @@ -322,7 +322,7 @@ func TestPoliciesUpdate_Beta(t *testing.T) { defer orgTestCleanup() t.Run("with a new query", func(t *testing.T) { - options := &PolicyCreateOptions{ + options := PolicyCreateOptions{ Description: String("A sample policy"), Kind: OPA, Query: String("terraform.main"), @@ -346,4 +346,19 @@ func TestPoliciesUpdate_Beta(t *testing.T) { assert.NotEqual(t, *pBefore.Query, *pAfter.Query) assert.Equal(t, "terraform.policy1.deny", *pAfter.Query) }) + + t.Run("update query when kind is not OPA", func(t *testing.T) { + pBefore, pBeforeCleanup := createUploadedPolicy(t, client, true, orgTest) + defer pBeforeCleanup() + + pAfter, err := client.Policies.Update(ctx, pBefore.ID, PolicyUpdateOptions{ + Query: String("terraform.policy1.deny"), + }) + require.NoError(t, err) + + assert.Equal(t, pBefore.Name, pAfter.Name) + assert.Equal(t, pBefore.Enforce, pAfter.Enforce) + assert.Equal(t, Sentinel, pAfter.Kind) + assert.Nil(t, pAfter.Query) + }) }