From 8ad1bf982ec8b178603a754016405f65a04354b3 Mon Sep 17 00:00:00 2001 From: Harkanwal Gill Date: Tue, 11 Jul 2023 16:23:10 -0400 Subject: [PATCH] add project to policy-sets --- CHANGELOG.md | 3 +++ policy_set.go | 37 ++++++++++++++++++++++------------ policy_set_integration_test.go | 28 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa27c6a0..e2cd25cf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # UNRELEASED +## 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) + # v1.30.0 ## Enhancements diff --git a/policy_set.go b/policy_set.go index d805829a2..777cb16ce 100644 --- a/policy_set.go +++ b/policy_set.go @@ -73,18 +73,20 @@ type PolicySetList struct { // PolicySet represents a Terraform Enterprise policy set. type PolicySet struct { - ID string `jsonapi:"primary,policy-sets"` - Name string `jsonapi:"attr,name"` - Description string `jsonapi:"attr,description"` - Kind PolicyKind `jsonapi:"attr,kind"` - Overridable *bool `jsonapi:"attr,overridable"` - Global bool `jsonapi:"attr,global"` - PoliciesPath string `jsonapi:"attr,policies-path"` - PolicyCount int `jsonapi:"attr,policy-count"` - VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"` - WorkspaceCount int `jsonapi:"attr,workspace-count"` - CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"` - UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"` + ID string `jsonapi:"primary,policy-sets"` + Name string `jsonapi:"attr,name"` + Description string `jsonapi:"attr,description"` + Kind PolicyKind `jsonapi:"attr,kind"` + Overridable *bool `jsonapi:"attr,overridable"` + Global bool `jsonapi:"attr,global"` + PoliciesPath string `jsonapi:"attr,policies-path"` + // **Note: This field is still in BETA and subject to change.** + PolicyCount int `jsonapi:"attr,policy-count"` + VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"` + WorkspaceCount int `jsonapi:"attr,workspace-count"` + ProjectCount int `jsonapi:"attr,project-count"` + CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"` + UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"` // Relations // The organization to which the policy set belongs to. @@ -99,6 +101,9 @@ type PolicySet struct { NewestVersion *PolicySetVersion `jsonapi:"relation,newest-version"` // The most recent successful policy set version. CurrentVersion *PolicySetVersion `jsonapi:"relation,current-version"` + // **Note: This field is still in BETA and subject to change.** + // The projects to which the policy set applies. + Projects []*Project `jsonapi:"relation,projects"` } // PolicySetIncludeOpt represents the available options for include query params. @@ -110,6 +115,8 @@ const ( PolicySetWorkspaces PolicySetIncludeOpt = "workspaces" PolicySetCurrentVersion PolicySetIncludeOpt = "current_version" PolicySetNewestVersion PolicySetIncludeOpt = "newest_version" + // **Note: This field is still in BETA and subject to change.** + PolicySetProjects PolicySetIncludeOpt = "projects" ) // PolicySetListOptions represents the options for listing policy sets. @@ -179,6 +186,10 @@ type PolicySetCreateOptions struct { // Optional: The initial list of workspaces for which the policy set should be enforced. Workspaces []*Workspace `jsonapi:"relation,workspaces,omitempty"` + + // **Note: This field is still in BETA and subject to change.** + // Optional: The initial list of projects for which the policy set should be enforced. + Projects []*Project `jsonapi:"relation,projects,omitempty"` } // PolicySetUpdateOptions represents the options for updating a policy set. @@ -501,7 +512,7 @@ func (o *PolicySetReadOptions) valid() error { func validatePolicySetIncludeParams(params []PolicySetIncludeOpt) error { for _, p := range params { switch p { - case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion: + case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion, PolicySetProjects: // do nothing default: return ErrInvalidIncludeValue diff --git a/policy_set_integration_test.go b/policy_set_integration_test.go index 77487e084..196bc5ee2 100644 --- a/policy_set_integration_test.go +++ b/policy_set_integration_test.go @@ -154,6 +154,34 @@ func TestPolicySetsCreate(t *testing.T) { assert.Equal(t, ps.Workspaces[0].ID, wTest.ID) }) + t.Run("with policies, workspaces and projects provided", func(t *testing.T) { + skipUnlessBeta(t) + pTest, pTestCleanup := createPolicy(t, client, orgTest) + defer pTestCleanup() + wTest, wTestCleanup := createWorkspace(t, client, orgTest) + defer wTestCleanup() + prjTest, prjTestCleanup := createProject(t, client, orgTest) + defer prjTestCleanup() + + options := PolicySetCreateOptions{ + Name: String("project-policy-set"), + Policies: []*Policy{pTest}, + Workspaces: []*Workspace{wTest}, + Projects: []*Project{prjTest}, + } + + ps, err := client.PolicySets.Create(ctx, orgTest.Name, options) + require.NoError(t, err) + + assert.Equal(t, ps.Name, *options.Name) + 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.Workspaces[0].ID, wTest.ID) + assert.Equal(t, ps.ProjectCount, 1) + assert.Equal(t, ps.Projects[0].ID, prjTest.ID) + }) + t.Run("with vcs policy set", func(t *testing.T) { githubIdentifier := os.Getenv("GITHUB_POLICY_SET_IDENTIFIER") if githubIdentifier == "" {