Skip to content

Commit b05e86a

Browse files
committed
add project to policy-sets
1 parent 07adc0e commit b05e86a

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* New `RunPlannedAndSaved` `RunStatus` value
1010
* New `PlannedAndSavedAt` field in `RunStatusTimestamps`
1111
* New `RunOperationSavePlan` constant for run list filters
12+
* 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)
1213

1314
# v1.28.0
1415

policy_set.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ type PolicySet struct {
8080
Overridable *bool `jsonapi:"attr,overridable"`
8181
Global bool `jsonapi:"attr,global"`
8282
PoliciesPath string `jsonapi:"attr,policies-path"`
83+
// **Note: This field is still in BETA and subject to change.**
8384
PolicyCount int `jsonapi:"attr,policy-count"`
8485
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
8586
WorkspaceCount int `jsonapi:"attr,workspace-count"`
87+
ProjectCount int `jsonapi:"attr,project-count"`
8688
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
8789
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
8890

@@ -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)