Skip to content

Commit 8ad1bf9

Browse files
committed
add project to policy-sets
1 parent 9c9e882 commit 8ad1bf9

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# UNRELEASED
22

3+
## Enhancements
4+
* 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)
5+
36
# v1.30.0
47

58
## Enhancements

policy_set.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,20 @@ type PolicySetList struct {
7373

7474
// PolicySet represents a Terraform Enterprise policy set.
7575
type PolicySet struct {
76-
ID string `jsonapi:"primary,policy-sets"`
77-
Name string `jsonapi:"attr,name"`
78-
Description string `jsonapi:"attr,description"`
79-
Kind PolicyKind `jsonapi:"attr,kind"`
80-
Overridable *bool `jsonapi:"attr,overridable"`
81-
Global bool `jsonapi:"attr,global"`
82-
PoliciesPath string `jsonapi:"attr,policies-path"`
83-
PolicyCount int `jsonapi:"attr,policy-count"`
84-
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
85-
WorkspaceCount int `jsonapi:"attr,workspace-count"`
86-
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
87-
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
76+
ID string `jsonapi:"primary,policy-sets"`
77+
Name string `jsonapi:"attr,name"`
78+
Description string `jsonapi:"attr,description"`
79+
Kind PolicyKind `jsonapi:"attr,kind"`
80+
Overridable *bool `jsonapi:"attr,overridable"`
81+
Global bool `jsonapi:"attr,global"`
82+
PoliciesPath string `jsonapi:"attr,policies-path"`
83+
// **Note: This field is still in BETA and subject to change.**
84+
PolicyCount int `jsonapi:"attr,policy-count"`
85+
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
86+
WorkspaceCount int `jsonapi:"attr,workspace-count"`
87+
ProjectCount int `jsonapi:"attr,project-count"`
88+
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
89+
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
8890

8991
// Relations
9092
// The organization to which the policy set belongs to.
@@ -99,6 +101,9 @@ type PolicySet struct {
99101
NewestVersion *PolicySetVersion `jsonapi:"relation,newest-version"`
100102
// The most recent successful policy set version.
101103
CurrentVersion *PolicySetVersion `jsonapi:"relation,current-version"`
104+
// **Note: This field is still in BETA and subject to change.**
105+
// The projects to which the policy set applies.
106+
Projects []*Project `jsonapi:"relation,projects"`
102107
}
103108

104109
// PolicySetIncludeOpt represents the available options for include query params.
@@ -110,6 +115,8 @@ const (
110115
PolicySetWorkspaces PolicySetIncludeOpt = "workspaces"
111116
PolicySetCurrentVersion PolicySetIncludeOpt = "current_version"
112117
PolicySetNewestVersion PolicySetIncludeOpt = "newest_version"
118+
// **Note: This field is still in BETA and subject to change.**
119+
PolicySetProjects PolicySetIncludeOpt = "projects"
113120
)
114121

115122
// PolicySetListOptions represents the options for listing policy sets.
@@ -179,6 +186,10 @@ type PolicySetCreateOptions struct {
179186

180187
// Optional: The initial list of workspaces for which the policy set should be enforced.
181188
Workspaces []*Workspace `jsonapi:"relation,workspaces,omitempty"`
189+
190+
// **Note: This field is still in BETA and subject to change.**
191+
// Optional: The initial list of projects for which the policy set should be enforced.
192+
Projects []*Project `jsonapi:"relation,projects,omitempty"`
182193
}
183194

184195
// PolicySetUpdateOptions represents the options for updating a policy set.
@@ -501,7 +512,7 @@ func (o *PolicySetReadOptions) valid() error {
501512
func validatePolicySetIncludeParams(params []PolicySetIncludeOpt) error {
502513
for _, p := range params {
503514
switch p {
504-
case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion:
515+
case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion, PolicySetProjects:
505516
// do nothing
506517
default:
507518
return ErrInvalidIncludeValue

policy_set_integration_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ func TestPolicySetsCreate(t *testing.T) {
154154
assert.Equal(t, ps.Workspaces[0].ID, wTest.ID)
155155
})
156156

157+
t.Run("with policies, workspaces and projects provided", func(t *testing.T) {
158+
skipUnlessBeta(t)
159+
pTest, pTestCleanup := createPolicy(t, client, orgTest)
160+
defer pTestCleanup()
161+
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
162+
defer wTestCleanup()
163+
prjTest, prjTestCleanup := createProject(t, client, orgTest)
164+
defer prjTestCleanup()
165+
166+
options := PolicySetCreateOptions{
167+
Name: String("project-policy-set"),
168+
Policies: []*Policy{pTest},
169+
Workspaces: []*Workspace{wTest},
170+
Projects: []*Project{prjTest},
171+
}
172+
173+
ps, err := client.PolicySets.Create(ctx, orgTest.Name, options)
174+
require.NoError(t, err)
175+
176+
assert.Equal(t, ps.Name, *options.Name)
177+
assert.Equal(t, ps.PolicyCount, 1)
178+
assert.Equal(t, ps.Policies[0].ID, pTest.ID)
179+
assert.Equal(t, ps.WorkspaceCount, 1)
180+
assert.Equal(t, ps.Workspaces[0].ID, wTest.ID)
181+
assert.Equal(t, ps.ProjectCount, 1)
182+
assert.Equal(t, ps.Projects[0].ID, prjTest.ID)
183+
})
184+
157185
t.Run("with vcs policy set", func(t *testing.T) {
158186
githubIdentifier := os.Getenv("GITHUB_POLICY_SET_IDENTIFIER")
159187
if githubIdentifier == "" {

0 commit comments

Comments
 (0)