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 support for POST/DELETE policy_sets/:policy_set_id/relationships/projects #735

Merged
merged 10 commits into from
Jul 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Enhancements
* Added BETA support for including `projects` relationship and `projects-count` attribute to policy_set on create by @hs26gill [#737](https://github.com/hashicorp/go-tfe/pull/737)

* Added BETA method `AddProjects` and `RemoveProjects` for attaching/detaching policy set to projects by Netra2104 [#735](https://github.com/hashicorp/go-tfe/pull/735)

# v1.30.0

## Enhancements
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ var (

ErrWorkspaceMinLimit = errors.New("must provide at least one workspace")

ErrProjectMinLimit = errors.New("must provide at least one project")

ErrRequiredPlan = errors.New("plan is required")

ErrRequiredPolicies = errors.New("policies is required")
Expand Down
7 changes: 4 additions & 3 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ func createPolicySetParameter(t *testing.T, client *Client, ps *PolicySet) (*Pol
var psCleanup func()

if ps == nil {
ps, psCleanup = createPolicySet(t, client, nil, nil, nil, "")
ps, psCleanup = createPolicySet(t, client, nil, nil, nil, nil, "")
}

ctx := context.Background()
Expand All @@ -619,7 +619,7 @@ func createPolicySetParameter(t *testing.T, client *Client, ps *PolicySet) (*Pol
}
}

func createPolicySet(t *testing.T, client *Client, org *Organization, policies []*Policy, workspaces []*Workspace, kind PolicyKind) (*PolicySet, func()) {
func createPolicySet(t *testing.T, client *Client, org *Organization, policies []*Policy, workspaces []*Workspace, projects []*Project, kind PolicyKind) (*PolicySet, func()) {
var orgCleanup func()

if org == nil {
Expand All @@ -631,6 +631,7 @@ func createPolicySet(t *testing.T, client *Client, org *Organization, policies [
Name: String(randomString(t)),
Policies: policies,
Workspaces: workspaces,
Projects: projects,
Kind: kind,
})
if err != nil {
Expand Down Expand Up @@ -684,7 +685,7 @@ func createPolicySetVersion(t *testing.T, client *Client, ps *PolicySet) (*Polic
var psCleanup func()

if ps == nil {
ps, psCleanup = createPolicySet(t, client, nil, nil, nil, "")
ps, psCleanup = createPolicySet(t, client, nil, nil, nil, nil, "")
}

ctx := context.Background()
Expand Down
28 changes: 28 additions & 0 deletions mocks/policy_set_mocks.go

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

10 changes: 5 additions & 5 deletions policy_check_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPolicyChecksList(t *testing.T) {
defer policyCleanup2()
wTest, wsCleanup := createWorkspace(t, client, orgTest)
defer wsCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest1, pTest2}, []*Workspace{wTest}, "")
createPolicySet(t, client, orgTest, []*Policy{pTest1, pTest2}, []*Workspace{wTest}, nil, "")

rTest, runCleanup := createPolicyCheckedRun(t, client, wTest)
defer runCleanup()
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestPolicyChecksRead(t *testing.T) {

pTest, _ := createUploadedPolicy(t, client, true, orgTest)
wTest, _ := createWorkspace(t, client, orgTest)
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, "")
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, "")

rTest, _ := createPolicyCheckedRun(t, client, wTest)
require.Equal(t, 1, len(rTest.PolicyChecks))
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestPolicyChecksOverride(t *testing.T) {

wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, "")
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, "")
rTest, tTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer tTestCleanup()

Expand All @@ -159,7 +159,7 @@ func TestPolicyChecksOverride(t *testing.T) {

wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, "")
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, "")
rTest, rTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer rTestCleanup()

Expand Down Expand Up @@ -190,7 +190,7 @@ func TestPolicyChecksLogs(t *testing.T) {
defer pTestCleanup()
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, "")
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, "")

rTest, rTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer rTestCleanup()
Expand Down
6 changes: 3 additions & 3 deletions policy_evaluation_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPolicyEvaluationList_Beta(t *testing.T) {
defer policyTestCleanup()

policySet := []*Policy{policyTest}
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, OPA)
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, nil, OPA)
defer psTestCleanup1()

rTest, rTestCleanup := createRun(t, client, wkspaceTest)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestPolicySetOutcomeList_Beta(t *testing.T) {
defer policyTestCleanup()

policySet := []*Policy{policyTest}
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, OPA)
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, nil, OPA)
defer psTestCleanup1()

rTest, rTestCleanup := createPlannedRun(t, client, wkspaceTest)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestPolicySetOutcomeRead_Beta(t *testing.T) {
defer policyTestCleanup()

policySet := []*Policy{policyTest}
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, OPA)
_, psTestCleanup1 := createPolicySet(t, client, orgTest, policySet, []*Workspace{wkspaceTest}, nil, OPA)
defer psTestCleanup1()

rTest, rTestCleanup := createPlannedRun(t, client, wkspaceTest)
Expand Down
76 changes: 76 additions & 0 deletions policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ type PolicySets interface {
// Remove workspaces from a policy set.
RemoveWorkspaces(ctx context.Context, policySetID string, options PolicySetRemoveWorkspacesOptions) error

// Add projects to a policy set.
AddProjects(ctx context.Context, policySetID string, options PolicySetAddProjectsOptions) error

// Remove projects from a policy set.
RemoveProjects(ctx context.Context, policySetID string, options PolicySetRemoveProjectsOptions) error

// Delete a policy set by its ID.
Delete(ctx context.Context, policyID string) error
}
Expand Down Expand Up @@ -255,6 +261,20 @@ type PolicySetRemoveWorkspacesOptions struct {
Workspaces []*Workspace
}

// PolicySetAddProjectsOptions represents the options for adding projects
// to a policy set.
type PolicySetAddProjectsOptions struct {
// The projects to add to the policy set.
Projects []*Project
}

// PolicySetRemoveProjectsOptions represents the options for removing
// projects from a policy set.
type PolicySetRemoveProjectsOptions struct {
// The projects to remove from the policy set.
Projects []*Project
}

// List all the policies for a given organization.
func (s *policySets) List(ctx context.Context, organization string, options *PolicySetListOptions) (*PolicySetList, error) {
if !validStringID(&organization) {
Expand Down Expand Up @@ -425,6 +445,42 @@ func (s *policySets) RemoveWorkspaces(ctx context.Context, policySetID string, o
return req.Do(ctx, nil)
}

// AddProjects adds projects to a given policy set.
func (s *policySets) AddProjects(ctx context.Context, policySetID string, options PolicySetAddProjectsOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}

u := fmt.Sprintf("policy-sets/%s/relationships/projects", url.QueryEscape(policySetID))
req, err := s.client.NewRequest("POST", u, options.Projects)
if err != nil {
return err
}

return req.Do(ctx, nil)
}

// RemoveProjects removes projects from a policy set.
func (s *policySets) RemoveProjects(ctx context.Context, policySetID string, options PolicySetRemoveProjectsOptions) error {
if !validStringID(&policySetID) {
return ErrInvalidPolicySetID
}
if err := options.valid(); err != nil {
return err
}

u := fmt.Sprintf("policy-sets/%s/relationships/projects", url.QueryEscape(policySetID))
req, err := s.client.NewRequest("DELETE", u, options.Projects)
if err != nil {
return err
}

return req.Do(ctx, nil)
}

// Delete a policy set by its ID.
func (s *policySets) Delete(ctx context.Context, policySetID string) error {
if !validStringID(&policySetID) {
Expand Down Expand Up @@ -460,6 +516,16 @@ func (o PolicySetRemoveWorkspacesOptions) valid() error {
return nil
}

func (o PolicySetRemoveProjectsOptions) valid() error {
if o.Projects == nil {
return ErrRequiredProject
}
if len(o.Projects) == 0 {
return ErrProjectMinLimit
}
return nil
}

func (o PolicySetUpdateOptions) valid() error {
if o.Name != nil && !validStringID(o.Name) {
return ErrInvalidName
Expand Down Expand Up @@ -497,6 +563,16 @@ func (o PolicySetAddWorkspacesOptions) valid() error {
return nil
}

func (o PolicySetAddProjectsOptions) valid() error {
if o.Projects == nil {
return ErrRequiredProject
}
if len(o.Projects) == 0 {
return ErrProjectMinLimit
}
return nil
}

func (o *PolicySetReadOptions) valid() error {
if o == nil {
return nil // nothing to validate
Expand Down
8 changes: 4 additions & 4 deletions policy_set_integration_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func TestPolicySetsList_Beta(t *testing.T) {
workspace, workspaceCleanup := createWorkspace(t, client, orgTest)
defer workspaceCleanup()

psTest1, psTestCleanup1 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, "")
psTest1, psTestCleanup1 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, nil, "")
defer psTestCleanup1()
psTest2, psTestCleanup2 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, "")
psTest2, psTestCleanup2 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, nil, "")
defer psTestCleanup2()
psTest3, psTestCleanup3 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, OPA)
psTest3, psTestCleanup3 := createPolicySet(t, client, orgTest, nil, []*Workspace{workspace}, nil, OPA)
defer psTestCleanup3()

t.Run("without list options", func(t *testing.T) {
Expand Down Expand Up @@ -344,7 +344,7 @@ func TestPolicySetsUpdate_Beta(t *testing.T) {

upgradeOrganizationSubscription(t, client, orgTest)

psTest, psTestCleanup := createPolicySet(t, client, orgTest, nil, nil, "opa")
psTest, psTestCleanup := createPolicySet(t, client, orgTest, nil, nil, nil, "opa")
defer psTestCleanup()

t.Run("with valid attributes", func(t *testing.T) {
Expand Down
Loading
Loading