Skip to content

Commit

Permalink
Merge pull request #1163 from hashicorp/TF-11213-terraform-provider-t…
Browse files Browse the repository at this point in the history
…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
brandonc authored Dec 6, 2023
2 parents afb668a + 55ea3a8 commit 36b76db
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 17 deletions.
14 changes: 0 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ linters:
- gosec #https://github.com/securego/gosec
- predeclared #https://github.com/nishanths/predeclared
- unconvert #https://github.com/mdempsky/unconvert
- wrapcheck #https://github.com/tomarrell/wrapcheck

issues:
exclude-rules:
Expand Down Expand Up @@ -101,19 +100,6 @@ issues:
-|resource_tfe_run_trigger|resource_tfe_team_access|resource_tfe_organization_membership|resource_tfe_policy_set|resource_tfe_team_member|
-|resource_tfe_workspace|resource_tfe_team_organization_member|resource_tfe_variable_set|resource_tfe_team_project_access)\.go
text: "SA1019"
- linters:
- wrapcheck
path: (config_unix|data_source_slug|data_source_workspace|provider|resource_tfe_variable|resource_tfe_workspace|provider_test)\.go
text: "error returned from external package is unwrapped"
- linters:
- wrapcheck
path: (logging|plugin_provider|resource_tfe_oauth_client|resource_tfe_organization|resource_tfe_terraform_version|workspace_helpers|resource_tfe_agent_pool_test|
-|resource_tfe_agent_token_test|resource_tfe_notification_configuration_test|resource_tfe_oauth_client_test|resource_tfe_organization_membership_test|
-|resource_tfe_organization_test|resource_tfe_organization_token_test|resource_tfe_policy_set_parameter_test|resource_tfe_policy_set_test|resource_tfe_registry_module_test|
-|resource_tfe_run_trigger_test|resource_tfe_sentinel_policy_test|resource_tfe_ssh_key_test|resource_tfe_team_access_test|resource_tfe_team_member_test|
-|resource_tfe_team_member_test|resource_tfe_team_organization_member_test|resource_tfe_team_test|resource_tfe_team_token_test|resource_tfe_terraform_version_test|
-|resource_tfe_variable_set_test|resource_tfe_workspace_test|resource_tfe_team_members_test|resource_tfe_team_members_test|resource_tfe_variable_test)\.go
text: "error returned from interface method should be wrapped"
linters-settings:
# errcheck:
# # https://github.com/kisielk/errcheck#excluding-functions
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BREAKING CHANGES:

BUG FIXES:
* `r/tfe_policy`: Fix the provider ignoring updates to the `query` field, by @skeggse [1108](https://github.com/hashicorp/terraform-provider-tfe/pull/1108)
* Fix the undetected change when modifying the `organization` default in the provider configuration by @brandonc [1152](https://github.com/hashicorp/terraform-provider-tfe/issue/1152)

FEATURES:
* `d/tfe_registry_module`: Add `vcs_repo.tags` and `vcs_repo.branch` attributes to allow configuration of `publishing_mechanism`. Add `test_config` to support running tests on `branch`-based registry modules, by @hashimoon [1096](https://github.com/hashicorp/terraform-provider-tfe/pull/1096)
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/provider_custom_diffs.go
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
}
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_admin_organization_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func resourceTFEAdminOrganizationSettings() *schema.Resource {
Update: resourceTFEAdminOrganizationSettingsUpdate,
Delete: resourceTFEAdminOrganizationSettingsDelete,

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func resourceTFEAgentPool() *schema.Resource {
StateContext: resourceTFEAgentPoolImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_no_code_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func resourceTFENoCodeModule() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func resourceTFEOAuthClient() *schema.Resource {
Read: resourceTFEOAuthClientRead,
Delete: resourceTFEOAuthClientDelete,

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"log"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
)

func resourceTFEOrganizationDefaultExecutionMode() *schema.Resource {
Expand All @@ -19,6 +20,8 @@ func resourceTFEOrganizationDefaultExecutionMode() *schema.Resource {
StateContext: resourceTFEOrganizationDefaultExecutionModeImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_organization_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func resourceTFEOrganizationMembership() *schema.Resource {
StateContext: resourceTFEOrganizationMembershipImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/resource_tfe_organization_module_sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceTFEOrganizationModuleSharing() *schema.Resource {
Read: resourceTFEOrganizationModuleSharingRead,
Update: resourceTFEOrganizationModuleSharingUpdate,
Delete: resourceTFEOrganizationModuleSharingDelete,

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_organization_run_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func resourceTFEOrganizationRunTask() *schema.Resource {
StateContext: resourceTFEOrganizationRunTaskImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_organization_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func resourceTFEOrganizationToken() *schema.Resource {
StateContext: resourceTFEOrganizationTokenImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func resourceTFEPolicy() *schema.Resource {
StateContext: resourceTFEPolicyImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Description: "The name of the policy",
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func resourceTFEPolicySet() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func resourceTFEProject() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_registry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func resourceTFERegistryModule() *schema.Resource {
StateContext: resourceTFERegistryModuleImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_tfe_registry_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_sentinel_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func resourceTFESentinelPolicy() *schema.Resource {
StateContext: resourceTFESentinelPolicyImporter,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func resourceTFESSHKey() *schema.Resource {
Update: resourceTFESSHKeyUpdate,
Delete: resourceTFESSHKeyDelete,

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_tfe_variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func resourceTFEVariableSet() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func resourceTFEWorkspace() *schema.Resource {
return err
}

if err := customizeDiffIfProviderDefaultOrganizationChanged(c, d, meta); err != nil {
return err
}

return nil
},

Expand Down
59 changes: 59 additions & 0 deletions internal/provider/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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!"
}`
}

0 comments on commit 36b76db

Please sign in to comment.