From 04b414f5e35833b0c0afaf96cfe89fcedaba8f60 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 | 1 + policy_set.go | 12 +++++++++++- policy_set_integration_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 559fc0fac..a62599987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * New `RunPlannedAndSaved` `RunStatus` value * New `PlannedAndSavedAt` field in `RunStatusTimestamps` * New `RunOperationSavePlan` constant for run list filters +* 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.28.0 diff --git a/policy_set.go b/policy_set.go index d805829a2..4bfc60a4c 100644 --- a/policy_set.go +++ b/policy_set.go @@ -83,6 +83,7 @@ type PolicySet struct { 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"` @@ -99,6 +100,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 +114,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 +185,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 +511,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..eaac6be1f 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 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("populated-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 == "" {