Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OPA support for Policy Set API's #575

Merged
merged 7 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions policy_set.go
Expand Up @@ -10,6 +10,15 @@ import (
// Compile-time proof of interface implementation.
var _ PolicySets = (*policySets)(nil)

// PolicyKind is an indicator of the underlying technology that the policy or policy set supports.
// There are two Policykinds documented in the enum.
mrinalirao marked this conversation as resolved.
Show resolved Hide resolved
type PolicyKind string

const (
OPA PolicyKind = "opa"
Sentinel PolicyKind = "sentinel"
)

// PolicySets describes all the policy set related methods that the Terraform
// Enterprise API supports.
//
Expand Down Expand Up @@ -64,6 +73,7 @@ type PolicySet struct {
ID string `jsonapi:"primary,policy-sets"`
Name string `jsonapi:"attr,name"`
Description string `jsonapi:"attr,description"`
Kind string `jsonapi:"attr,kind"`
Global bool `jsonapi:"attr,global"`
PoliciesPath string `jsonapi:"attr,policies-path"`
PolicyCount int `jsonapi:"attr,policy-count"`
Expand Down Expand Up @@ -136,6 +146,9 @@ type PolicySetCreateOptions struct {
// Optional: Whether or not the policy set is global.
Global *bool `jsonapi:"attr,global,omitempty"`

// Optional: The underlying technology that the policy set supports
Kind PolicyKind `jsonapi:"attr,kind,omitempty"`
mrinalirao marked this conversation as resolved.
Show resolved Hide resolved

// Optional: The sub-path within the attached VCS repository to ingress. All
// files and directories outside of this sub-path will be ignored.
// This option may only be specified when a VCS repo is present.
Expand Down
22 changes: 22 additions & 0 deletions policy_set_integration_test.go
Expand Up @@ -112,20 +112,37 @@ func TestPolicySetsCreate(t *testing.T) {
t.Run("with valid attributes", func(t *testing.T) {
options := PolicySetCreateOptions{
Name: String("policy-set"),
Kind: OPA,
}

ps, err := client.PolicySets.Create(ctx, orgTest.Name, options)
require.NoError(t, err)

assert.Equal(t, ps.Name, *options.Name)
assert.Equal(t, ps.Description, "")
assert.Equal(t, ps.Kind, "opa")
assert.False(t, ps.Global)
})

t.Run("with kind missing", func(t *testing.T) {
options := PolicySetCreateOptions{
Name: String("policy-set2"),
}

ps, err := client.PolicySets.Create(ctx, orgTest.Name, options)
require.NoError(t, err)

assert.Equal(t, ps.Name, *options.Name)
assert.Equal(t, ps.Description, "")
assert.Equal(t, ps.Kind, "sentinel")
assert.False(t, ps.Global)
})

t.Run("with all attributes provided", func(t *testing.T) {
options := PolicySetCreateOptions{
Name: String("global"),
Description: String("Policies in this set will be checked in ALL workspaces!"),
Kind: Sentinel,
Global: Bool(true),
}

Expand All @@ -134,6 +151,7 @@ func TestPolicySetsCreate(t *testing.T) {

assert.Equal(t, ps.Name, *options.Name)
assert.Equal(t, ps.Description, *options.Description)
assert.Equal(t, ps.Kind, "sentinel")
assert.True(t, ps.Global)
})

Expand All @@ -146,6 +164,7 @@ func TestPolicySetsCreate(t *testing.T) {
options := PolicySetCreateOptions{
Name: String("populated-policy-set"),
Policies: []*Policy{pTest},
Kind: Sentinel,
Workspaces: []*Workspace{wTest},
}

Expand All @@ -156,6 +175,7 @@ func TestPolicySetsCreate(t *testing.T) {
assert.Equal(t, ps.PolicyCount, 1)
assert.Equal(t, ps.Policies[0].ID, pTest.ID)
assert.Equal(t, ps.WorkspaceCount, 1)
assert.Equal(t, ps.Kind, "sentinel")
assert.Equal(t, ps.Workspaces[0].ID, wTest.ID)
})

Expand All @@ -170,6 +190,7 @@ func TestPolicySetsCreate(t *testing.T) {

options := PolicySetCreateOptions{
Name: String("vcs-policy-set"),
Kind: Sentinel,
PoliciesPath: String("/policy-sets/foo"),
VCSRepo: &VCSRepoOptions{
Branch: String("policies"),
Expand All @@ -190,6 +211,7 @@ func TestPolicySetsCreate(t *testing.T) {
assert.False(t, ps.Global)
assert.Equal(t, ps.PoliciesPath, "/policy-sets/foo")
assert.Equal(t, ps.VCSRepo.Branch, "policies")
assert.Equal(t, ps.Kind, "sentinel")
assert.Equal(t, ps.VCSRepo.DisplayIdentifier, githubIdentifier)
assert.Equal(t, ps.VCSRepo.Identifier, githubIdentifier)
assert.Equal(t, ps.VCSRepo.IngressSubmodules, true)
Expand Down