Skip to content

Commit 933f36f

Browse files
authored
Merge pull request #1357 from hashicorp/TF-14822-fix-constant-diff-on-tfe_registry_module
prevent constant diff even after a successful apply of resource_tfe_registry_module resource
2 parents 0cc8bab + 4ef15dd commit 933f36f

File tree

3 files changed

+363
-14
lines changed

3 files changed

+363
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ ENHANCEMENTS:
66
* `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)
77
* `r/tfe_registry_module`: Add `initial_version` support for Branch Based Modules by @aaabdelgany [#1363](https://github.com/hashicorp/terraform-provider-tfe/pull/1363)
88

9+
BUG FIXES:
10+
* `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)
11+
912
## v0.55.0
1013

1114
FEATURES:

internal/provider/resource_tfe_registry_module.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func resourceTFERegistryModule() *schema.Resource {
3232
StateContext: resourceTFERegistryModuleImporter,
3333
},
3434

35+
CustomizeDiff: func(c context.Context, d *schema.ResourceDiff, meta interface{}) error {
36+
return validateVcsRepo(d)
37+
},
3538
Schema: map[string]*schema.Schema{
3639
"organization": {
3740
Type: schema.TypeString,
@@ -95,6 +98,7 @@ func resourceTFERegistryModule() *schema.Resource {
9598
"tags": {
9699
Type: schema.TypeBool,
97100
Optional: true,
101+
Computed: true,
98102
},
99103
},
100104
},
@@ -125,6 +129,7 @@ func resourceTFERegistryModule() *schema.Resource {
125129
"test_config": {
126130
Type: schema.TypeList,
127131
Optional: true,
132+
Computed: true,
128133
Elem: &schema.Resource{
129134
Schema: map[string]*schema.Schema{
130135
"tests_enabled": {
@@ -173,8 +178,8 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
173178
branch, branchOk := vcsRepo["branch"].(string)
174179
initialVersion, initialVersionOk := d.GetOk("initial_version")
175180

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

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

315-
if tagsOk && tags && branchOk && branch != "" {
316-
return fmt.Errorf("tags must be set to false when a branch is provided")
317-
}
318-
319320
if tagsOk {
320321
options.VCSRepo.Tags = tfe.Bool(tags)
321322
}
@@ -348,7 +349,7 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
348349
})
349350

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

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

415416
testConfig = append(testConfig, testConfigValues)
416-
417-
d.Set("test_config", testConfig)
418417
}
419418

419+
d.Set("test_config", testConfig)
420+
420421
return nil
421422
}
422423

@@ -481,3 +482,30 @@ func resourceTFERegistryModuleImporter(ctx context.Context, d *schema.ResourceDa
481482
d.Id(),
482483
)
483484
}
485+
486+
func validateVcsRepo(d *schema.ResourceDiff) error {
487+
vcsRepo, ok := d.GetRawConfig().AsValueMap()["vcs_repo"]
488+
if !ok || vcsRepo.LengthInt() == 0 {
489+
return nil
490+
}
491+
492+
branchValue := vcsRepo.AsValueSlice()[0].GetAttr("branch")
493+
tagsValue := vcsRepo.AsValueSlice()[0].GetAttr("tags")
494+
495+
if !tagsValue.IsNull() && tagsValue.False() && branchValue.IsNull() {
496+
return fmt.Errorf("branch must be provided when tags is set to false")
497+
}
498+
499+
if !tagsValue.IsNull() && !branchValue.IsNull() {
500+
tags := tagsValue.True()
501+
branch := branchValue.AsString()
502+
// tags must be set to true or branch provided but not both
503+
if tags && branch != "" {
504+
return fmt.Errorf("tags must be set to false when a branch is provided")
505+
} else if !tags && branch == "" {
506+
return fmt.Errorf("tags must be set to true when no branch is provided")
507+
}
508+
}
509+
510+
return nil
511+
}

0 commit comments

Comments
 (0)