From 2740507fc1dc2be10089303d8a8b8419dd4ac3ff Mon Sep 17 00:00:00 2001 From: Netra Mali Date: Fri, 1 Sep 2023 11:31:31 -0400 Subject: [PATCH 1/3] added the data source --- internal/provider/data_source_policy_set.go | 14 ++++++++++++++ .../provider/data_source_policy_set_test.go | 17 ++++++++++++++++- website/docs/d/policy_set.html.markdown | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/provider/data_source_policy_set.go b/internal/provider/data_source_policy_set.go index 9692b9c25..40790878d 100644 --- a/internal/provider/data_source_policy_set.go +++ b/internal/provider/data_source_policy_set.go @@ -98,6 +98,12 @@ func dataSourceTFEPolicySet() *schema.Resource { Computed: true, }, + "excluded_workspace_ids": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "project_ids": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, @@ -170,6 +176,14 @@ func dataSourceTFEPolicySetRead(d *schema.ResourceData, meta interface{}) error } d.Set("workspace_ids", workspaceIDs) + var excludedWorkspaceIDs []interface{} + if !policySet.Global { + for _, excludedWorkspace := range policySet.WorkspaceExclusions { + excludedWorkspaceIDs = append(excludedWorkspaceIDs, excludedWorkspace.ID) + } + } + d.Set("excluded_workspace_ids", excludedWorkspaceIDs) + var projectIDs []interface{} if !policySet.Global { for _, project := range policySet.Projects { diff --git a/internal/provider/data_source_policy_set_test.go b/internal/provider/data_source_policy_set_test.go index 0defac31a..b83bfcbd2 100644 --- a/internal/provider/data_source_policy_set_test.go +++ b/internal/provider/data_source_policy_set_test.go @@ -45,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", "excluded_workspace_ids.#", "1"), resource.TestCheckResourceAttr( "data.tfe_policy_set.bar", "project_ids.#", "1"), resource.TestCheckResourceAttr( @@ -90,6 +92,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", "excluded_workspace_ids.#", "1"), resource.TestCheckResourceAttr( "data.tfe_policy_set.bar", "project_ids.#", "1"), resource.TestCheckResourceAttr( @@ -149,6 +153,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", "excluded_workspace_ids.#", "0"), resource.TestCheckResourceAttr( "data.tfe_policy_set.bar", "project_ids.#", "0"), resource.TestCheckResourceAttr( @@ -204,7 +210,6 @@ 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" { @@ -212,6 +217,11 @@ resource "tfe_project_policy_set" "foobar" { project_id = tfe_project.foobar.id } +resource "tfe_workspace_policy_set_exclusion" { + policy_set_id = tfe_policy_set.foobar.id + workspace_id = tfe_workspace.foobar.id +} + data "tfe_policy_set" "bar" { name = tfe_policy_set.foobar.name organization = local.organization_name @@ -248,6 +258,11 @@ resource "tfe_project_policy_set" "foobar" { project_id = tfe_project.foobar.id } +resource "tfe_workspace_policy_set_exclusion" { + policy_set_id = tfe_policy_set.foobar.id + workspace_id = tfe_workspace.foobar.id +} + data "tfe_policy_set" "bar" { name = tfe_policy_set.foobar.name organization = local.organization_name diff --git a/website/docs/d/policy_set.html.markdown b/website/docs/d/policy_set.html.markdown index 3aab68f54..fe614630f 100644 --- a/website/docs/d/policy_set.html.markdown +++ b/website/docs/d/policy_set.html.markdown @@ -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. +* `excluded_workspace_ids` - IDs of the workspaces that do not 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`. From a5d440e7be023b152262932981aa77979bd5ad71 Mon Sep 17 00:00:00 2001 From: Netra Mali Date: Fri, 1 Sep 2023 15:43:20 -0400 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 031b67f75..5cf3990eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ BUG FIXES: * `r/tfe_team_project_access`: Fixes a panic that occurs when the client is configured against an older TFE release, by @sebasslash [1011](https://github.com/hashicorp/terraform-provider-tfe/pull/1011) * The provider no longer makes two service discovery requests per provider config, by @brandonc [1034](https://github.com/hashicorp/terraform-provider-tfe/pull/1034) +* `d/tfe_policy_set`: Add `excluded_workspace_ids` attribute, by @Netra2104 [1035](https://github.com/hashicorp/terraform-provider-tfe/pull/1035) FEATURES: * `d/tfe_organization_membership`: Add `organization_membership_id` attribute, by @laurenolivia [997](https://github.com/hashicorp/terraform-provider-tfe/pull/997) From 8695677019202ae7d985091bb784aaf015238dcd Mon Sep 17 00:00:00 2001 From: Netra Mali Date: Tue, 5 Sep 2023 16:08:18 -0400 Subject: [PATCH 3/3] fixed tests --- internal/provider/data_source_policy_set.go | 6 ++---- internal/provider/data_source_policy_set_test.go | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/provider/data_source_policy_set.go b/internal/provider/data_source_policy_set.go index 40790878d..66f8bd518 100644 --- a/internal/provider/data_source_policy_set.go +++ b/internal/provider/data_source_policy_set.go @@ -177,10 +177,8 @@ func dataSourceTFEPolicySetRead(d *schema.ResourceData, meta interface{}) error d.Set("workspace_ids", workspaceIDs) var excludedWorkspaceIDs []interface{} - if !policySet.Global { - for _, excludedWorkspace := range policySet.WorkspaceExclusions { - excludedWorkspaceIDs = append(excludedWorkspaceIDs, excludedWorkspace.ID) - } + for _, excludedWorkspace := range policySet.WorkspaceExclusions { + excludedWorkspaceIDs = append(excludedWorkspaceIDs, excludedWorkspace.ID) } d.Set("excluded_workspace_ids", excludedWorkspaceIDs) diff --git a/internal/provider/data_source_policy_set_test.go b/internal/provider/data_source_policy_set_test.go index b83bfcbd2..2985204e7 100644 --- a/internal/provider/data_source_policy_set_test.go +++ b/internal/provider/data_source_policy_set_test.go @@ -217,7 +217,7 @@ resource "tfe_project_policy_set" "foobar" { project_id = tfe_project.foobar.id } -resource "tfe_workspace_policy_set_exclusion" { +resource "tfe_workspace_policy_set_exclusion" "foobar" { policy_set_id = tfe_policy_set.foobar.id workspace_id = tfe_workspace.foobar.id } @@ -258,7 +258,7 @@ resource "tfe_project_policy_set" "foobar" { project_id = tfe_project.foobar.id } -resource "tfe_workspace_policy_set_exclusion" { +resource "tfe_workspace_policy_set_exclusion" "foobar" { policy_set_id = tfe_policy_set.foobar.id workspace_id = tfe_workspace.foobar.id }