diff --git a/CHANGELOG.md b/CHANGELOG.md index 31eed47ab..9b38403e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/internal/provider/resource_tfe_workspace.go b/internal/provider/resource_tfe_workspace.go index 0a2f3ccc2..667acc7e7 100644 --- a/internal/provider/resource_tfe_workspace.go +++ b/internal/provider/resource_tfe_workspace.go @@ -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 }, diff --git a/internal/provider/resource_tfe_workspace_test.go b/internal/provider/resource_tfe_workspace_test.go index e308ac9b8..7d8a23526 100644 --- a/internal/provider/resource_tfe_workspace_test.go +++ b/internal/provider/resource_tfe_workspace_test.go @@ -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 @@ -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 { @@ -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 = "admin@company.com" +} + +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 = "admin@company.com" +} + +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" {