Skip to content

Commit

Permalink
Merge pull request #974 from hashicorp/Netra2104/TF-6351-add-project-…
Browse files Browse the repository at this point in the history
…ids-to-policy-sets-data-source

Add project ids to policy sets data source
  • Loading branch information
Netra2104 committed Jul 26, 2023
2 parents 3ca6281 + cc3ea50 commit 6ab2665
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FEATURES:
* **New Resource**: `r/tfe_saml_settings` manages SAML Settings, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)
* `d/tfe_saml_settings`: Add PrivateKey (sensitive), SignatureSigningMethod, and SignatureDigestMethod attributes, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)
* **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)
* `d/tfe_policy_set`: Add `project_ids` attribute, by @Netra2104 [974](https://github.com/hashicorp/terraform-provider-tfe/pull/974/files)

NOTES:
* 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)
Expand Down
14 changes: 14 additions & 0 deletions tfe/data_source_policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func dataSourceTFEPolicySet() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},

"project_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -164,6 +170,14 @@ func dataSourceTFEPolicySetRead(d *schema.ResourceData, meta interface{}) error
}
d.Set("workspace_ids", workspaceIDs)

var projectIDs []interface{}
if !policySet.Global {
for _, project := range policySet.Projects {
projectIDs = append(projectIDs, project.ID)
}
}
d.Set("project_ids", projectIDs)

d.SetId(policySet.ID)

return nil
Expand Down
32 changes: 30 additions & 2 deletions tfe/data_source_policy_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func TestAccTFEPolicySetDataSource_basic(t *testing.T) {
skipUnlessBeta(t)
tfeClient, err := getClientUsingEnv()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -44,6 +45,8 @@ func TestAccTFEPolicySetDataSource_basic(t *testing.T) {
"data.tfe_policy_set.bar", "policy_ids.#", "1"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "project_ids.#", "1"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
),
Expand Down Expand Up @@ -87,6 +90,8 @@ func TestAccTFEPolicySetDataSourceOPA_basic(t *testing.T) {
"data.tfe_policy_set.bar", "overridable", "true"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "workspace_ids.#", "1"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "project_ids.#", "1"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "vcs_repo.#", "0"),
),
Expand Down Expand Up @@ -144,6 +149,8 @@ func TestAccTFEPolicySetDataSource_vcs(t *testing.T) {
"data.tfe_policy_set.bar", "policy_ids.#", "0"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "workspace_ids.#", "0"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "project_ids.#", "0"),
resource.TestCheckResourceAttr(
"data.tfe_policy_set.bar", "vcs_repo.#", "1"),
),
Expand Down Expand Up @@ -180,6 +187,11 @@ resource "tfe_workspace" "foobar" {
organization = local.organization_name
}
resource "tfe_project" "foobar" {
name = "project-foo-%d"
organization = local.organization_name
}
resource "tfe_sentinel_policy" "foo" {
name = "policy-foo"
policy = "main = rule { true }"
Expand All @@ -192,12 +204,18 @@ resource "tfe_policy_set" "foobar" {
organization = local.organization_name
policy_ids = [tfe_sentinel_policy.foo.id]
workspace_ids = [tfe_workspace.foobar.id]
}
resource "tfe_project_policy_set" "foobar" {
policy_set_id = tfe_policy_set.foobar.id
project_id = tfe_project.foobar.id
}
data "tfe_policy_set" "bar" {
name = tfe_policy_set.foobar.name
organization = local.organization_name
}`, organization, rInt, rInt)
}`, organization, rInt, rInt, rInt)
}

func testAccTFEPolicySetDataSourceConfigOPA_basic(organization string, rInt int) string {
Expand All @@ -211,6 +229,11 @@ resource "tfe_workspace" "foobar" {
organization = local.organization_name
}
resource "tfe_project" "foobar" {
name = "project-foo-%d"
organization = local.organization_name
}
resource "tfe_policy_set" "foobar" {
name = "tst-policy-set-%d"
description = "Policy Set"
Expand All @@ -220,11 +243,16 @@ resource "tfe_policy_set" "foobar" {
workspace_ids = [tfe_workspace.foobar.id]
}
resource "tfe_project_policy_set" "foobar" {
policy_set_id = tfe_policy_set.foobar.id
project_id = tfe_project.foobar.id
}
data "tfe_policy_set" "bar" {
name = tfe_policy_set.foobar.name
organization = local.organization_name
kind = "opa"
}`, organization, rInt, rInt)
}`, organization, rInt, rInt, rInt)
}

func testAccTFEPolicySetDataSourceConfig_vcs(organization string, rInt int) string {
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/policy_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following arguments are supported:
* `kind` - The policy-as-code framework for the policy. Valid values are "sentinel" and "opa".
* `overridable` - Whether users can override this policy when it fails during a run. Only valid for OPA policies.
* `workspace_ids` - IDs of the workspaces that use the policy set.
* `project_ids` - IDs of the projects that use the policy set.
* `policy_ids` - IDs of the policies attached to the policy set.
* `policies_path` - The sub-path within the attached VCS repository when using `vcs_repo`.
* `vcs_repo` - Settings for the workspace's VCS repository.
Expand Down

0 comments on commit 6ab2665

Please sign in to comment.