Skip to content

Commit

Permalink
add exclude_tags to data_source_workspace_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Uk1288 committed Jul 8, 2022
1 parent 921da93 commit bb32e3a
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 2 deletions.
26 changes: 24 additions & 2 deletions tfe/data_source_workspace_ids.go
Expand Up @@ -26,6 +26,12 @@ func dataSourceTFEWorkspaceIDs() *schema.Resource {
Optional: true,
},

"exclude_tags": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},

"organization": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -70,11 +76,27 @@ func dataSourceTFEWorkspaceIDsRead(d *schema.ResourceData, meta interface{}) err

options := &tfe.WorkspaceListOptions{}

excludeTagLookupMap := make(map[string]bool)
var excludeTagBuf strings.Builder
for _, excludedTag := range d.Get("exclude_tags").([]interface{}) {
if exTag, ok := excludedTag.(string); ok && len(strings.TrimSpace(exTag)) != 0 {
excludeTagLookupMap[exTag] = true

if excludeTagBuf.Len() > 0 {
excludeTagBuf.WriteByte(',')
}
excludeTagBuf.WriteString(exTag)
}
}

if excludeTagBuf.Len() > 0 {
options.ExcludeTags = excludeTagBuf.String()
}

// Create a search string with all the tag names we are looking for.
var tagSearchParts []string
for _, tagName := range d.Get("tag_names").([]interface{}) {
name := tagName.(string)
if len(strings.TrimSpace(name)) != 0 {
if name, ok := tagName.(string); ok && len(strings.TrimSpace(name)) != 0 {
id += name // add to the state id
tagSearchParts = append(tagSearchParts, name)
}
Expand Down
139 changes: 139 additions & 0 deletions tfe/data_source_workspace_ids_test.go
Expand Up @@ -263,6 +263,72 @@ func TestAccTFEWorkspaceIDsDataSource_namesEmpty(t *testing.T) {
})
}

func TestAccTFEWorkspaceIDsDataSource_excludeTags(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspaceIDsDataSourceConfig_excludeTags(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "organization", orgName),

// full_names attribute
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "full_names.%", "1"),
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good",
fmt.Sprintf("full_names.workspace-bar-%d", rInt),
fmt.Sprintf("tst-terraform-%d/workspace-bar-%d", rInt, rInt),
),

// ids attribute
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "ids.%", "1"),
resource.TestCheckResourceAttrSet(
"data.tfe_workspace_ids.good", fmt.Sprintf("ids.workspace-bar-%d", rInt)),

// id attribute
resource.TestCheckResourceAttrSet("data.tfe_workspace_ids.good", "id"),
),
},
},
})
}

func TestAccTFEWorkspaceIDsDataSource_sameTagInTagNamesAndExcludeTags(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspaceIDsDataSourceConfig_sameTagInTagNamesAndExcludeTags(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "organization", orgName),

// full_names attribute should be empty
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "full_names.%", "0"),

// ids attribute should be empty
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "ids.%", "0"),
),
},
},
})
}

func testAccTFEWorkspaceIDsDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down Expand Up @@ -427,3 +493,76 @@ data "tfe_workspace_ids" "good" {
organization = tfe_workspace.foo.organization
}`, rInt, rInt, rInt, rInt)
}

func testAccTFEWorkspaceIDsDataSourceConfig_excludeTags(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foo" {
name = "workspace-foo-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "happy"]
}
resource "tfe_workspace" "bar" {
name = "workspace-bar-%d"
organization = tfe_organization.foobar.id
tag_names = ["good"]
}
resource "tfe_workspace" "dummy" {
name = "workspace-dummy-%d"
organization = tfe_organization.foobar.id
}
data "tfe_workspace_ids" "good" {
tag_names = ["good"]
exclude_tags = ["happy"]
organization = tfe_workspace.foo.organization
depends_on = [
tfe_workspace.foo,
tfe_workspace.bar,
tfe_workspace.dummy
]
}`, rInt, rInt, rInt, rInt)
}

func testAccTFEWorkspaceIDsDataSourceConfig_sameTagInTagNamesAndExcludeTags(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foo" {
name = "workspace-foo-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "happy"]
}
resource "tfe_workspace" "bar" {
name = "workspace-bar-%d"
organization = tfe_organization.foobar.id
tag_names = ["happy", "play"]
}
resource "tfe_workspace" "dummy" {
name = "workspace-dummy-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "play", "happy"]
}
data "tfe_workspace_ids" "good" {
tag_names = ["good", "happy"]
exclude_tags = ["happy"]
organization = tfe_workspace.foo.organization
depends_on = [
tfe_workspace.foo,
tfe_workspace.bar,
tfe_workspace.dummy
]
}`, rInt, rInt, rInt, rInt)
}

0 comments on commit bb32e3a

Please sign in to comment.