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

Support for Policy Evaluation and Policy set outcome API's #583

Merged
merged 3 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* 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)
* Add Policy Evaluation and Policy Set Outcome APIs by @mrinalirao [#583](https://github.com/hashicorp/go-tfe/pull/583)

# v1.12.0

Expand Down
4 changes: 4 additions & 0 deletions errors.go
Expand Up @@ -116,6 +116,10 @@ var (

ErrInvalidPolicyCheckID = errors.New("invalid value for policy check ID")

ErrInvalidPolicyEvaluationID = errors.New("invalid value for policy evaluation ID")

ErrInvalidPolicySetOutcomeID = errors.New("invalid value for policy set outcome ID")

ErrInvalidTag = errors.New("invalid tag id")

ErrInvalidPlanExportID = errors.New("invalid value for plan export ID")
Expand Down
1 change: 1 addition & 0 deletions generate_mocks.sh
Expand Up @@ -61,3 +61,4 @@ mockgen -source=variable_set_variable.go -destination=mocks/variable_set_variabl
mockgen -source=workspace.go -destination=mocks/workspace_mocks.go -package=mocks
mockgen -source=workspace_run_task.go -destination=mocks/workspace_run_tasks_mocks.go -package=mocks
mockgen -source=agent.go -destination=mocks/agents.go -package=mocks
mockgen -source=policy_evaluation.go -destination=mocks/policy_evaluation.go -package=mocks
26 changes: 21 additions & 5 deletions helper_test.go
Expand Up @@ -622,11 +622,20 @@ func createPolicyWithOptions(t *testing.T, client *Client, org *Organization, op
}

name := randomString(t)
path := name + ".sentinel"
if opts.Kind == OPA {
path = name + ".rego"
}
options := PolicyCreateOptions{
Name: String(name),
Kind: opts.Kind,
Query: opts.Query,
Enforce: opts.Enforce,
Name: String(name),
Kind: opts.Kind,
Query: opts.Query,
Enforce: []*EnforcementOptions{
{
Path: String(path),
Mode: opts.Enforce[0].Mode,
},
},
}

ctx := context.Background()
Expand Down Expand Up @@ -687,7 +696,14 @@ func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, or
p, pCleanup := createPolicyWithOptions(t, client, org, opts)

ctx := context.Background()
err := client.Policies.Upload(ctx, p.ID, []byte(fmt.Sprintf("main = rule { %t }", pass)))
policy := fmt.Sprintf("main = rule { %t }", pass)
if opts.Kind == OPA {
policy = `package example rule["not allowed"] { false }`
if !pass {
policy = `package example rule["not allowed"] { true }`
}
}
err := client.Policies.Upload(ctx, p.ID, []byte(policy))
if err != nil {
t.Fatal(err)
}
Expand Down
104 changes: 104 additions & 0 deletions mocks/policy_evaluation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.