-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1163 from hashicorp/TF-11213-terraform-provider-t…
…fe-gh-issue-1152-organization-change-in-the-provider-configuration-is-not-detected-in-the-resouces Fix 'no changes detected' when provider default organization changes
- Loading branch information
Showing
22 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func customizeDiffIfProviderDefaultOrganizationChanged(c context.Context, diff *schema.ResourceDiff, meta interface{}) error { | ||
config := meta.(ConfiguredClient) | ||
|
||
configOrg := diff.GetRawConfig().GetAttr("organization") | ||
plannedOrg := diff.Get("organization").(string) | ||
|
||
if configOrg.IsNull() && config.Organization != plannedOrg { | ||
// There is no organization configured on the resource, yet it is different from | ||
// the state organization. We must conclude that the provider default organization changed. | ||
if err := diff.SetNew("organization", config.Organization); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -974,7 +974,6 @@ func testAccTFERegistryModule_vcs(rInt int) string { | |
resource "tfe_organization" "foobar" { | ||
name = "tst-terraform-%d" | ||
email = "[email protected]" | ||
} | ||
resource "tfe_oauth_client" "foobar" { | ||
|
@@ -986,6 +985,7 @@ resource "tfe_oauth_client" "foobar" { | |
} | ||
resource "tfe_registry_module" "foobar" { | ||
organization = tfe_organization.foobar.name | ||
vcs_repo { | ||
display_identifier = "%s" | ||
identifier = "%s" | ||
|
@@ -1055,7 +1055,7 @@ resource "tfe_registry_module" "foobar" { | |
identifier = "%s" | ||
oauth_token_id = tfe_oauth_client.foobar.oauth_token_id | ||
branch = "main" | ||
tags = false | ||
tags = false | ||
} | ||
test_config { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"time" | ||
|
||
tfe "github.com/hashicorp/go-tfe" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
@@ -109,6 +110,57 @@ func TestAccTFEWorkspace_defaultOrg(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccTFEWorkspaceProviderDefaultOrgChanged(t *testing.T) { | ||
// Tests the situation when the provider default organization changes but the | ||
// config does not change. | ||
workspace := &tfe.Workspace{} | ||
defaultOrgName, rInt := setupDefaultOrganization(t) | ||
providers := providerWithDefaultOrganization(defaultOrgName) | ||
|
||
client, err := getClientUsingEnv() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
anotherOrg, cleanup := createOrganization(t, client, tfe.OrganizationCreateOptions{ | ||
Name: tfe.String(fmt.Sprintf("another-organization-%d", rInt)), | ||
Email: tfe.String(fmt.Sprintf("%[email protected]", randomString(t))), | ||
}) | ||
t.Cleanup(cleanup) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: providers, | ||
CheckDestroy: testAccCheckTFEWorkspaceDestroyProvider(providers["tfe"]), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTFEWorkspace_defaultOrg(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckTFEWorkspaceExists( | ||
"tfe_workspace.foobar", workspace, providers["tfe"]), | ||
resource.TestCheckResourceAttr("tfe_workspace.foobar", "organization", defaultOrgName), | ||
), | ||
}, | ||
{ | ||
PreConfig: func() { | ||
// Modify the provider to return a different default organization | ||
providers["tfe"].ConfigureContextFunc = func(ctx context.Context, rd *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
client, err := getClientUsingEnv() | ||
return ConfiguredClient{ | ||
Client: client, | ||
Organization: anotherOrg.Name, | ||
}, diag.FromErr(err) | ||
} | ||
}, | ||
Config: testAccTFEWorkspace_defaultOrg(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("tfe_workspace.foobar", "organization", anotherOrg.Name), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccTFEWorkspace_basicReadProjectId(t *testing.T) { | ||
workspace := &tfe.Workspace{} | ||
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int() | ||
|
@@ -3701,3 +3753,10 @@ resource "tfe_workspace" "foobar" { | |
source_name = "Example Source" | ||
}`, rInt) | ||
} | ||
|
||
func testAccTFEWorkspace_mismatchOrganization() string { | ||
return `resource "tfe_workspace" "foobar" { | ||
name = "workspace-test" | ||
description = "My favorite workspace!" | ||
}` | ||
} |