Skip to content

Commit

Permalink
Merge pull request #737 from hashicorp/hs26gill/TF-6272-create-policy…
Browse files Browse the repository at this point in the history
…-set-with-project-params

Update policy_set to support project relationship on CREATE
  • Loading branch information
hs26gill committed Jul 18, 2023
2 parents 9c9e882 + 8ad1bf9 commit 29b2afa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 24 additions & 13 deletions policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions policy_set_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down

0 comments on commit 29b2afa

Please sign in to comment.