Skip to content

Commit 6ab2665

Browse files
authored
Merge pull request #974 from hashicorp/Netra2104/TF-6351-add-project-ids-to-policy-sets-data-source
Add project ids to policy sets data source
2 parents 3ca6281 + cc3ea50 commit 6ab2665

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FEATURES:
44
* **New Resource**: `r/tfe_saml_settings` manages SAML Settings, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)
55
* `d/tfe_saml_settings`: Add PrivateKey (sensitive), SignatureSigningMethod, and SignatureDigestMethod attributes, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)
66
* **New Resource**: `r/tfe_project_policy_set` is a new resource to attach/detach an existing `project` to an existing `policy set`, by @Netra2104 [972](https://github.com/hashicorp/terraform-provider-tfe/pull/972)
7+
* `d/tfe_policy_set`: Add `project_ids` attribute, by @Netra2104 [974](https://github.com/hashicorp/terraform-provider-tfe/pull/974/files)
78

89
NOTES:
910
* The provider is now using go-tfe [v1.30.0](https://github.com/hashicorp/go-tfe/releases/tag/v1.30.0), by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)

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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
func TestAccTFEPolicySetDataSource_basic(t *testing.T) {
17+
skipUnlessBeta(t)
1718
tfeClient, err := getClientUsingEnv()
1819
if err != nil {
1920
t.Fatal(err)
@@ -44,6 +45,8 @@ func TestAccTFEPolicySetDataSource_basic(t *testing.T) {
4445
"data.tfe_policy_set.bar", "policy_ids.#", "1"),
4546
resource.TestCheckResourceAttr(
4647
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
48+
resource.TestCheckResourceAttr(
49+
"data.tfe_policy_set.bar", "project_ids.#", "1"),
4750
resource.TestCheckResourceAttr(
4851
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
4952
),
@@ -87,6 +90,8 @@ func TestAccTFEPolicySetDataSourceOPA_basic(t *testing.T) {
8790
"data.tfe_policy_set.bar", "overridable", "true"),
8891
resource.TestCheckResourceAttr(
8992
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
93+
resource.TestCheckResourceAttr(
94+
"data.tfe_policy_set.bar", "project_ids.#", "1"),
9095
resource.TestCheckResourceAttr(
9196
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
9297
),
@@ -144,6 +149,8 @@ func TestAccTFEPolicySetDataSource_vcs(t *testing.T) {
144149
"data.tfe_policy_set.bar", "policy_ids.#", "0"),
145150
resource.TestCheckResourceAttr(
146151
"data.tfe_policy_set.bar", "workspace_ids.#", "0"),
152+
resource.TestCheckResourceAttr(
153+
"data.tfe_policy_set.bar", "project_ids.#", "0"),
147154
resource.TestCheckResourceAttr(
148155
"data.tfe_policy_set.bar", "vcs_repo.#", "1"),
149156
),
@@ -180,6 +187,11 @@ resource "tfe_workspace" "foobar" {
180187
organization = local.organization_name
181188
}
182189
190+
resource "tfe_project" "foobar" {
191+
name = "project-foo-%d"
192+
organization = local.organization_name
193+
}
194+
183195
resource "tfe_sentinel_policy" "foo" {
184196
name = "policy-foo"
185197
policy = "main = rule { true }"
@@ -192,12 +204,18 @@ resource "tfe_policy_set" "foobar" {
192204
organization = local.organization_name
193205
policy_ids = [tfe_sentinel_policy.foo.id]
194206
workspace_ids = [tfe_workspace.foobar.id]
207+
208+
}
209+
210+
resource "tfe_project_policy_set" "foobar" {
211+
policy_set_id = tfe_policy_set.foobar.id
212+
project_id = tfe_project.foobar.id
195213
}
196214
197215
data "tfe_policy_set" "bar" {
198216
name = tfe_policy_set.foobar.name
199217
organization = local.organization_name
200-
}`, organization, rInt, rInt)
218+
}`, organization, rInt, rInt, rInt)
201219
}
202220

203221
func testAccTFEPolicySetDataSourceConfigOPA_basic(organization string, rInt int) string {
@@ -211,6 +229,11 @@ resource "tfe_workspace" "foobar" {
211229
organization = local.organization_name
212230
}
213231
232+
resource "tfe_project" "foobar" {
233+
name = "project-foo-%d"
234+
organization = local.organization_name
235+
}
236+
214237
resource "tfe_policy_set" "foobar" {
215238
name = "tst-policy-set-%d"
216239
description = "Policy Set"
@@ -220,11 +243,16 @@ resource "tfe_policy_set" "foobar" {
220243
workspace_ids = [tfe_workspace.foobar.id]
221244
}
222245
246+
resource "tfe_project_policy_set" "foobar" {
247+
policy_set_id = tfe_policy_set.foobar.id
248+
project_id = tfe_project.foobar.id
249+
}
250+
223251
data "tfe_policy_set" "bar" {
224252
name = tfe_policy_set.foobar.name
225253
organization = local.organization_name
226254
kind = "opa"
227-
}`, organization, rInt, rInt)
255+
}`, organization, rInt, rInt, rInt)
228256
}
229257

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

website/docs/d/policy_set.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The following arguments are supported:
3737
* `kind` - The policy-as-code framework for the policy. Valid values are "sentinel" and "opa".
3838
* `overridable` - Whether users can override this policy when it fails during a run. Only valid for OPA policies.
3939
* `workspace_ids` - IDs of the workspaces that use the policy set.
40+
* `project_ids` - IDs of the projects that use the policy set.
4041
* `policy_ids` - IDs of the policies attached to the policy set.
4142
* `policies_path` - The sub-path within the attached VCS repository when using `vcs_repo`.
4243
* `vcs_repo` - Settings for the workspace's VCS repository.

0 commit comments

Comments
 (0)