Skip to content

Commit

Permalink
Merge pull request #1423 from hashicorp/TF-18663-terraform-provider-t…
Browse files Browse the repository at this point in the history
…fe-gh-issue-1422-workspace-url-not-updating-after-changing-workspace-name-and-running-terraform-apply-again

Plan to recompute html_url when name changes
  • Loading branch information
brandonc authored Jul 29, 2024
2 parents d82e459 + 80e9d19 commit a2e3a40
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

BUG FIXES:
* `r/tfe_workspace` html_url is now planned to be recomputed when `name` changes. Previously, changed values would show up on the next plan, by @brandonc [1422](https://github.com/hashicorp/terraform-provider-tfe/issues/1422)

## v0.57.1

* `r/tfe_stack` initial support for this BETA feature was released in v0.57.0 but the documentation link was broken and it was not mentioned in the release notes. NOTE: This resource is subject to change and has limited support in HCP Terraform.
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func resourceTFEWorkspace() *schema.Resource {
return err
}

if d.HasChange("name") {
if err := d.SetNewComputed("html_url"); err != nil {
return err
}
}

return nil
},

Expand Down
83 changes: 83 additions & 0 deletions internal/provider/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,35 @@ func TestAccTFEWorkspace_customProject(t *testing.T) {
})
}

func TestAccTFEWorkspace_HTMLURL(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

// When name is changed, the html_url should be updated as well
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_HTMLURL(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("tfe_workspace.foobar", "name", "workspace-test"),
resource.TestCheckResourceAttrPair("tfe_workspace.foobar", "html_url", "tfe_project.foobar", "description"),
testAccCheckTFEWorkspaceHTMLURLHasSuffix("tfe_workspace.foobar", "workspace-test"),
),
},
{
Config: testAccTFEWorkspace_HTMLURLRenamed(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("tfe_workspace.foobar", "name", "workspace-test-renamed"),
resource.TestCheckResourceAttrPair("tfe_workspace.foobar", "html_url", "tfe_project.foobar", "description"),
testAccCheckTFEWorkspaceHTMLURLHasSuffix("tfe_workspace.foobar", "workspace-test-renamed"),
),
},
},
})
}

func TestTagValidation(t *testing.T) {
testCases := []struct {
tag string
Expand Down Expand Up @@ -2249,6 +2278,22 @@ func TestTFEWorkspace_delete_withoutCanForceDeletePermission(t *testing.T) {
}
}

func testAccCheckTFEWorkspaceHTMLURLHasSuffix(resourceName, suffix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}

url := rs.Primary.Attributes["html_url"]
if !strings.HasSuffix(url, suffix) {
return fmt.Errorf("expected %q to have suffix %q", url, suffix)
}

return nil
}
}

func testAccCheckTFEWorkspaceExists(
n string, workspace *tfe.Workspace, p *schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -2849,6 +2894,44 @@ resource "tfe_workspace" "foobar" {
}`, rInt, aart)
}

func testAccTFEWorkspace_HTMLURL(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "foobar" {
name = "testproject"
organization = tfe_organization.foobar.id
description = tfe_workspace.foobar.html_url
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
}`, rInt)
}

func testAccTFEWorkspace_HTMLURLRenamed(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "foobar" {
name = "testproject"
organization = tfe_organization.foobar.id
description = tfe_workspace.foobar.html_url
}
resource "tfe_workspace" "foobar" {
name = "workspace-test-renamed"
organization = tfe_organization.foobar.id
}`, rInt)
}

func testAccTFEWorkspace_defaultOrg() string {
return `
resource "tfe_workspace" "foobar" {
Expand Down

0 comments on commit a2e3a40

Please sign in to comment.