Skip to content

Commit

Permalink
Merge pull request #1357 from hashicorp/TF-14822-fix-constant-diff-on…
Browse files Browse the repository at this point in the history
…-tfe_registry_module

prevent constant diff even after a successful apply of resource_tfe_registry_module resource
  • Loading branch information
Uk1288 committed May 29, 2024
2 parents 0cc8bab + 4ef15dd commit 933f36f
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ ENHANCEMENTS:
* `d/tfe_workspace`: Add an `auto_destroy_at` attribute for reading a scheduled auto-destroy, by @notchairmk [1354](https://github.com/hashicorp/terraform-provider-tfe/pull/1354)
* `r/tfe_registry_module`: Add `initial_version` support for Branch Based Modules by @aaabdelgany [#1363](https://github.com/hashicorp/terraform-provider-tfe/pull/1363)

BUG FIXES:
* `r/tfe_registry_module`: Prevents constant diff after a successful apply when `tags` and `tests_enabled` is not set by @Uk1288 [#1357](https://github.com/hashicorp/terraform-provider-tfe/pull/1357)

## v0.55.0

FEATURES:
Expand Down
46 changes: 37 additions & 9 deletions internal/provider/resource_tfe_registry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func resourceTFERegistryModule() *schema.Resource {
StateContext: resourceTFERegistryModuleImporter,
},

CustomizeDiff: func(c context.Context, d *schema.ResourceDiff, meta interface{}) error {
return validateVcsRepo(d)
},
Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Expand Down Expand Up @@ -95,6 +98,7 @@ func resourceTFERegistryModule() *schema.Resource {
"tags": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
},
Expand Down Expand Up @@ -125,6 +129,7 @@ func resourceTFERegistryModule() *schema.Resource {
"test_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tests_enabled": {
Expand Down Expand Up @@ -173,8 +178,8 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
branch, branchOk := vcsRepo["branch"].(string)
initialVersion, initialVersionOk := d.GetOk("initial_version")

if tagsOk && tags && branchOk && branch != "" {
return nil, fmt.Errorf("tags must be set to false when a branch is provided")
if tagsOk {
options.VCSRepo.Tags = tfe.Bool(tags)
}

if branchOk && branch != "" {
Expand Down Expand Up @@ -312,10 +317,6 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
tags, tagsOk := vcsRepo["tags"].(bool)
branch, branchOk := vcsRepo["branch"].(string)

if tagsOk && tags && branchOk && branch != "" {
return fmt.Errorf("tags must be set to false when a branch is provided")
}

if tagsOk {
options.VCSRepo.Tags = tfe.Bool(tags)
}
Expand Down Expand Up @@ -348,7 +349,7 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
})

if err != nil {
return fmt.Errorf("Error while waiting for module %s/%s to be updated: %w", registryModule.Organization.Name, registryModule.Name, err)
return fmt.Errorf("Error while waiting for module %s/%s to be updated: %w", rmID.Organization, rmID.Name, err)
}

d.SetId(registryModule.ID)
Expand Down Expand Up @@ -413,10 +414,10 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err
}

testConfig = append(testConfig, testConfigValues)

d.Set("test_config", testConfig)
}

d.Set("test_config", testConfig)

return nil
}

Expand Down Expand Up @@ -481,3 +482,30 @@ func resourceTFERegistryModuleImporter(ctx context.Context, d *schema.ResourceDa
d.Id(),
)
}

func validateVcsRepo(d *schema.ResourceDiff) error {
vcsRepo, ok := d.GetRawConfig().AsValueMap()["vcs_repo"]
if !ok || vcsRepo.LengthInt() == 0 {
return nil
}

branchValue := vcsRepo.AsValueSlice()[0].GetAttr("branch")
tagsValue := vcsRepo.AsValueSlice()[0].GetAttr("tags")

if !tagsValue.IsNull() && tagsValue.False() && branchValue.IsNull() {
return fmt.Errorf("branch must be provided when tags is set to false")
}

if !tagsValue.IsNull() && !branchValue.IsNull() {
tags := tagsValue.True()
branch := branchValue.AsString()
// tags must be set to true or branch provided but not both
if tags && branch != "" {
return fmt.Errorf("tags must be set to false when a branch is provided")
} else if !tags && branch == "" {
return fmt.Errorf("tags must be set to true when no branch is provided")
}
}

return nil
}
Loading

0 comments on commit 933f36f

Please sign in to comment.