Skip to content

Commit 2ea0533

Browse files
committed
add project to policy-sets
1 parent 41c83f3 commit 2ea0533

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* New `RunPlannedAndSaved` `RunStatus` value
1616
* New `PlannedAndSavedAt` field in `RunStatusTimestamps`
1717
* New `RunOperationSavePlan` constant for run list filters
18+
* 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)
1819

1920
# v1.28.0
2021

policy_set.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type PolicySet struct {
8383
PolicyCount int `jsonapi:"attr,policy-count"`
8484
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
8585
WorkspaceCount int `jsonapi:"attr,workspace-count"`
86+
ProjectCount int `jsonapi:"attr,project-count"`
8687
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
8788
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
8889

@@ -99,6 +100,9 @@ type PolicySet struct {
99100
NewestVersion *PolicySetVersion `jsonapi:"relation,newest-version"`
100101
// The most recent successful policy set version.
101102
CurrentVersion *PolicySetVersion `jsonapi:"relation,current-version"`
103+
// **Note: This field is still in BETA and subject to change.**
104+
// The projects to which the policy set applies.
105+
Projects []*Project `jsonapi:"relation,projects"`
102106
}
103107

104108
// PolicySetIncludeOpt represents the available options for include query params.
@@ -110,6 +114,8 @@ const (
110114
PolicySetWorkspaces PolicySetIncludeOpt = "workspaces"
111115
PolicySetCurrentVersion PolicySetIncludeOpt = "current_version"
112116
PolicySetNewestVersion PolicySetIncludeOpt = "newest_version"
117+
// **Note: This field is still in BETA and subject to change.**
118+
PolicySetProjects PolicySetIncludeOpt = "projects"
113119
)
114120

115121
// PolicySetListOptions represents the options for listing policy sets.
@@ -179,6 +185,10 @@ type PolicySetCreateOptions struct {
179185

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

184194
// PolicySetUpdateOptions represents the options for updating a policy set.
@@ -501,7 +511,7 @@ func (o *PolicySetReadOptions) valid() error {
501511
func validatePolicySetIncludeParams(params []PolicySetIncludeOpt) error {
502512
for _, p := range params {
503513
switch p {
504-
case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion:
514+
case PolicySetPolicies, PolicySetWorkspaces, PolicySetCurrentVersion, PolicySetNewestVersion, PolicySetProjects:
505515
// do nothing
506516
default:
507517
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 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("populated-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)