Skip to content

Commit 5941f3f

Browse files
committed
init
1 parent 3ca6281 commit 5941f3f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

tfe/data_source_policy_set.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ func dataSourceTFEPolicySet() *schema.Resource {
9797
Elem: &schema.Schema{Type: schema.TypeString},
9898
Computed: true,
9999
},
100+
101+
"project_ids": {
102+
Type: schema.TypeSet,
103+
Elem: &schema.Schema{Type: schema.TypeString},
104+
Computed: true,
105+
},
100106
},
101107
}
102108
}
@@ -164,6 +170,14 @@ func dataSourceTFEPolicySetRead(d *schema.ResourceData, meta interface{}) error
164170
}
165171
d.Set("workspace_ids", workspaceIDs)
166172

173+
var projectIDs []interface{}
174+
if !policySet.Global {
175+
for _, project := range policySet.Projects {
176+
projectIDs = append(projectIDs, project.ID)
177+
}
178+
}
179+
d.Set("project_ids", projectIDs)
180+
167181
d.SetId(policySet.ID)
168182

169183
return nil

tfe/data_source_policy_set_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func TestAccTFEPolicySetDataSource_basic(t *testing.T) {
4444
"data.tfe_policy_set.bar", "policy_ids.#", "1"),
4545
resource.TestCheckResourceAttr(
4646
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
47+
resource.TestCheckResourceAttr(
48+
"data.tfe_policy_set.bar", "project_ids.#", "1"),
4749
resource.TestCheckResourceAttr(
4850
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
4951
),
@@ -87,6 +89,8 @@ func TestAccTFEPolicySetDataSourceOPA_basic(t *testing.T) {
8789
"data.tfe_policy_set.bar", "overridable", "true"),
8890
resource.TestCheckResourceAttr(
8991
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
92+
resource.TestCheckResourceAttr(
93+
"data.tfe_policy_set.bar", "project_ids.#", "1"),
9094
resource.TestCheckResourceAttr(
9195
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
9296
),
@@ -144,6 +148,8 @@ func TestAccTFEPolicySetDataSource_vcs(t *testing.T) {
144148
"data.tfe_policy_set.bar", "policy_ids.#", "0"),
145149
resource.TestCheckResourceAttr(
146150
"data.tfe_policy_set.bar", "workspace_ids.#", "0"),
151+
resource.TestCheckResourceAttr(
152+
"data.tfe_policy_set.bar", "project_ids.#", "0"),
147153
resource.TestCheckResourceAttr(
148154
"data.tfe_policy_set.bar", "vcs_repo.#", "1"),
149155
),
@@ -180,6 +186,11 @@ resource "tfe_workspace" "foobar" {
180186
organization = local.organization_name
181187
}
182188
189+
resource "tfe_project" "foobar" {
190+
name = "project-foo-%d"
191+
organization = local.organization_name
192+
}
193+
183194
resource "tfe_sentinel_policy" "foo" {
184195
name = "policy-foo"
185196
policy = "main = rule { true }"
@@ -192,12 +203,13 @@ resource "tfe_policy_set" "foobar" {
192203
organization = local.organization_name
193204
policy_ids = [tfe_sentinel_policy.foo.id]
194205
workspace_ids = [tfe_workspace.foobar.id]
206+
project_ids = [tfe_project.foobar.id]
195207
}
196208
197209
data "tfe_policy_set" "bar" {
198210
name = tfe_policy_set.foobar.name
199211
organization = local.organization_name
200-
}`, organization, rInt, rInt)
212+
}`, organization, rInt, rInt, rInt)
201213
}
202214

203215
func testAccTFEPolicySetDataSourceConfigOPA_basic(organization string, rInt int) string {
@@ -211,20 +223,26 @@ resource "tfe_workspace" "foobar" {
211223
organization = local.organization_name
212224
}
213225
226+
resource "tfe_project" "foobar" {
227+
name = "project-foo-%d"
228+
organization = local.organization_name
229+
}
230+
214231
resource "tfe_policy_set" "foobar" {
215232
name = "tst-policy-set-%d"
216233
description = "Policy Set"
217234
organization = local.organization_name
218235
kind = "opa"
219236
overridable = true
220237
workspace_ids = [tfe_workspace.foobar.id]
238+
project_ids = [tfe_project.foobar.id]
221239
}
222240
223241
data "tfe_policy_set" "bar" {
224242
name = tfe_policy_set.foobar.name
225243
organization = local.organization_name
226244
kind = "opa"
227-
}`, organization, rInt, rInt)
245+
}`, organization, rInt, rInt, rInt)
228246
}
229247

230248
func testAccTFEPolicySetDataSourceConfig_vcs(organization string, rInt int) string {

tfe/resource_tfe_policy_set.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ func resourceTFEPolicySet() *schema.Resource {
136136
Elem: &schema.Schema{Type: schema.TypeString},
137137
ConflictsWith: []string{"global"},
138138
},
139+
140+
"project_ids": {
141+
Type: schema.TypeSet,
142+
Optional: true,
143+
Computed: true,
144+
Elem: &schema.Schema{Type: schema.TypeString},
145+
ConflictsWith: []string{"global"},
146+
},
139147
},
140148
}
141149
}
@@ -197,6 +205,10 @@ func resourceTFEPolicySetCreate(d *schema.ResourceData, meta interface{}) error
197205
options.Workspaces = append(options.Workspaces, &tfe.Workspace{ID: workspaceID.(string)})
198206
}
199207

208+
for _, projectID := range d.Get("project_ids").(*schema.Set).List() {
209+
options.Projects = append(options.Projects, &tfe.Project{ID: projectID.(string)})
210+
}
211+
200212
log.Printf("[DEBUG] Create policy set %s for organization: %s", name, organization)
201213
policySet, err := config.Client.PolicySets.Create(ctx, organization, options)
202214
if err != nil {

0 commit comments

Comments
 (0)