Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ignore_additional_tag_names to tfe_workspace resource #1254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## UNRELEASED

FEATURES:
* `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)

## v0.52.0

FEATURES:
Expand Down
10 changes: 9 additions & 1 deletion internal/provider/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func resourceTFEWorkspace() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},

"ignore_additional_tag_names": {
Type: schema.TypeBool,
Optional: true,
},

"terraform_version": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -529,8 +534,11 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("agent_pool_id", agentPoolID)

var tagNames []interface{}
managedTags := d.Get("tag_names").(*schema.Set)
brandonc marked this conversation as resolved.
Show resolved Hide resolved
for _, tagName := range workspace.TagNames {
tagNames = append(tagNames, tagName)
if managedTags.Contains(tagName) || !d.Get("ignore_additional_tag_names").(bool) {
tagNames = append(tagNames, tagName)
}
}
d.Set("tag_names", tagNames)

Expand Down
57 changes: 57 additions & 0 deletions internal/provider/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,10 @@ func TestAccTFEWorkspace_patternsAndPrefixesConflicting(t *testing.T) {
func TestAccTFEWorkspace_changeTags(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
tfeClient, err := getClientUsingEnv()
if err != nil {
t.Fatal(err)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -1339,6 +1343,44 @@ func TestAccTFEWorkspace_changeTags(t *testing.T) {
Config: testAccTFEWorkspace_basicBadTag(rInt),
ExpectError: regexp.MustCompile(`"-Hello" is not a valid tag name.`),
},
{
Config: testAccTFEWorkspace_ignoreAdditional(rInt),
},
{
PreConfig: func() {
newTags := tfe.WorkspaceAddTagsOptions{Tags: []*tfe.Tag{{Name: "unmanaged"}}}
err := tfeClient.Workspaces.AddTags(context.Background(), workspace.ID, newTags)
if err != nil {
t.Fatal(err)
}
},
Config: testAccTFEWorkspace_ignoreAdditional(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace, testAccProvider),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "tag_names.#", "2"),
resource.TestCheckTypeSetElemAttr(
"tfe_workspace.foobar", "tag_names.*", "foo"),
resource.TestCheckTypeSetElemAttr(
"tfe_workspace.foobar", "tag_names.*", "bar"),
func(state *terraform.State) error {
r, err := tfeClient.Workspaces.ListTags(context.Background(), workspace.ID, &tfe.WorkspaceTagListOptions{})
if err != nil {
return err
}
if len(r.Items) != 3 {
return fmt.Errorf("expected 3 tags, got %d", len(r.Items))
}
for _, tag := range r.Items {
if tag.Name == "unmanaged" {
return nil
}
}
return fmt.Errorf("unmanaged tag not found on workspace")
},
),
},
},
})
}
Expand Down Expand Up @@ -2781,6 +2823,21 @@ resource "tfe_workspace" "foobar" {
}`, rInt)
}

func testAccTFEWorkspace_ignoreAdditional(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
auto_apply = true
tag_names = ["foo", "bar"]
ignore_additional_tag_names = true
}`, rInt)
}

func testAccTFEWorkspace_basicBadTag(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ The following arguments are supported:
workspace will display their output as text logs.
* `ssh_key_id` - (Optional) The ID of an SSH key to assign to the workspace.
* `tag_names` - (Optional) A list of tag names for this workspace. Note that tags must only contain lowercase letters, numbers, colons, or hyphens.
* `ignore_additional_tag_names` - (Optional) Explicitly ignores `tag_names`
_not_ defined by config so they will not be overwritten by the configured
tags. This creates exceptional behavior in terraform with respect
to `tag_names` and is not recommended. This value must be applied before it
will be used.
* `terraform_version` - (Optional) The version of Terraform to use for this
workspace. This can be either an exact version or a
[version constraint](https://developer.hashicorp.com/terraform/language/expressions/version-constraints)
Expand Down
Loading