Skip to content

Commit 2b1f823

Browse files
authored
Merge pull request #1254 from hashicorp/TF-13152-hashicorp-tfe-allow-unmanaged-workspace-tag-names
Adds `ignore_additional_tag_names` to tfe_workspace resource
2 parents 4916ed8 + 5e8cbdb commit 2b1f823

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## UNRELEASED
2+
3+
FEATURES:
4+
* `r/tfe_workspace`: Add `ignore_additional_tag_names` which explicitly ignores `tag_names` _not_ defined by config so they will not be overwritten by the configured tags, by @brandonc and @mbillow [1254](https://github.com/hashicorp/terraform-provider-tfe/pull/1254)
5+
16
## v0.52.0
27

38
FEATURES:

internal/provider/resource_tfe_workspace.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ func resourceTFEWorkspace() *schema.Resource {
209209
Elem: &schema.Schema{Type: schema.TypeString},
210210
},
211211

212+
"ignore_additional_tag_names": {
213+
Type: schema.TypeBool,
214+
Optional: true,
215+
},
216+
212217
"terraform_version": {
213218
Type: schema.TypeString,
214219
Optional: true,
@@ -529,8 +534,11 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
529534
d.Set("agent_pool_id", agentPoolID)
530535

531536
var tagNames []interface{}
537+
managedTags := d.Get("tag_names").(*schema.Set)
532538
for _, tagName := range workspace.TagNames {
533-
tagNames = append(tagNames, tagName)
539+
if managedTags.Contains(tagName) || !d.Get("ignore_additional_tag_names").(bool) {
540+
tagNames = append(tagNames, tagName)
541+
}
534542
}
535543
d.Set("tag_names", tagNames)
536544

internal/provider/resource_tfe_workspace_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,10 @@ func TestAccTFEWorkspace_patternsAndPrefixesConflicting(t *testing.T) {
12261226
func TestAccTFEWorkspace_changeTags(t *testing.T) {
12271227
workspace := &tfe.Workspace{}
12281228
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
1229+
tfeClient, err := getClientUsingEnv()
1230+
if err != nil {
1231+
t.Fatal(err)
1232+
}
12291233

12301234
resource.Test(t, resource.TestCase{
12311235
PreCheck: func() { testAccPreCheck(t) },
@@ -1339,6 +1343,44 @@ func TestAccTFEWorkspace_changeTags(t *testing.T) {
13391343
Config: testAccTFEWorkspace_basicBadTag(rInt),
13401344
ExpectError: regexp.MustCompile(`"-Hello" is not a valid tag name.`),
13411345
},
1346+
{
1347+
Config: testAccTFEWorkspace_ignoreAdditional(rInt),
1348+
},
1349+
{
1350+
PreConfig: func() {
1351+
newTags := tfe.WorkspaceAddTagsOptions{Tags: []*tfe.Tag{{Name: "unmanaged"}}}
1352+
err := tfeClient.Workspaces.AddTags(context.Background(), workspace.ID, newTags)
1353+
if err != nil {
1354+
t.Fatal(err)
1355+
}
1356+
},
1357+
Config: testAccTFEWorkspace_ignoreAdditional(rInt),
1358+
Check: resource.ComposeTestCheckFunc(
1359+
testAccCheckTFEWorkspaceExists(
1360+
"tfe_workspace.foobar", workspace, testAccProvider),
1361+
resource.TestCheckResourceAttr(
1362+
"tfe_workspace.foobar", "tag_names.#", "2"),
1363+
resource.TestCheckTypeSetElemAttr(
1364+
"tfe_workspace.foobar", "tag_names.*", "foo"),
1365+
resource.TestCheckTypeSetElemAttr(
1366+
"tfe_workspace.foobar", "tag_names.*", "bar"),
1367+
func(state *terraform.State) error {
1368+
r, err := tfeClient.Workspaces.ListTags(context.Background(), workspace.ID, &tfe.WorkspaceTagListOptions{})
1369+
if err != nil {
1370+
return err
1371+
}
1372+
if len(r.Items) != 3 {
1373+
return fmt.Errorf("expected 3 tags, got %d", len(r.Items))
1374+
}
1375+
for _, tag := range r.Items {
1376+
if tag.Name == "unmanaged" {
1377+
return nil
1378+
}
1379+
}
1380+
return fmt.Errorf("unmanaged tag not found on workspace")
1381+
},
1382+
),
1383+
},
13421384
},
13431385
})
13441386
}
@@ -2781,6 +2823,21 @@ resource "tfe_workspace" "foobar" {
27812823
}`, rInt)
27822824
}
27832825

2826+
func testAccTFEWorkspace_ignoreAdditional(rInt int) string {
2827+
return fmt.Sprintf(`
2828+
resource "tfe_organization" "foobar" {
2829+
name = "tst-terraform-%d"
2830+
2831+
}
2832+
resource "tfe_workspace" "foobar" {
2833+
name = "workspace-test"
2834+
organization = tfe_organization.foobar.id
2835+
auto_apply = true
2836+
tag_names = ["foo", "bar"]
2837+
ignore_additional_tag_names = true
2838+
}`, rInt)
2839+
}
2840+
27842841
func testAccTFEWorkspace_basicBadTag(rInt int) string {
27852842
return fmt.Sprintf(`
27862843
resource "tfe_organization" "foobar" {

website/docs/r/workspace.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ The following arguments are supported:
113113
workspace will display their output as text logs.
114114
* `ssh_key_id` - (Optional) The ID of an SSH key to assign to the workspace.
115115
* `tag_names` - (Optional) A list of tag names for this workspace. Note that tags must only contain lowercase letters, numbers, colons, or hyphens.
116+
* `ignore_additional_tag_names` - (Optional) Explicitly ignores `tag_names`
117+
_not_ defined by config so they will not be overwritten by the configured
118+
tags. This creates exceptional behavior in terraform with respect
119+
to `tag_names` and is not recommended. This value must be applied before it
120+
will be used.
116121
* `terraform_version` - (Optional) The version of Terraform to use for this
117122
workspace. This can be either an exact version or a
118123
[version constraint](https://developer.hashicorp.com/terraform/language/expressions/version-constraints)

0 commit comments

Comments
 (0)