Skip to content

Commit f0aeca0

Browse files
authored
fix: Fixes tags in mongodbatlas_flex_cluster (#2829)
* fix tags * add tests * fix
1 parent 66ff7e7 commit f0aeca0

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

internal/common/conversion/tags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ func NewTFTags(tags []admin.ResourceTag) types.Map {
3131
}
3232
return types.MapValueMust(types.StringType, typesTags)
3333
}
34+
35+
func UseNilForEmpty(planTag, newTag types.Map) bool {
36+
return planTag.IsNull() && len(newTag.Elements()) == 0
37+
}

internal/service/flexcluster/resource.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-framework/path"
1010
"github.com/hashicorp/terraform-plugin-framework/resource"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
1112
"go.mongodb.org/atlas-sdk/v20241113001/admin"
1213

1314
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
@@ -77,6 +78,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou
7778
resp.Diagnostics.Append(diags...)
7879
return
7980
}
81+
82+
if conversion.UseNilForEmpty(tfModel.Tags, newFlexClusterModel.Tags) {
83+
newFlexClusterModel.Tags = types.MapNull(types.StringType)
84+
}
85+
8086
resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClusterModel)...)
8187
}
8288

@@ -103,6 +109,11 @@ func (r *rs) Read(ctx context.Context, req resource.ReadRequest, resp *resource.
103109
resp.Diagnostics.Append(diags...)
104110
return
105111
}
112+
113+
if conversion.UseNilForEmpty(flexClusterState.Tags, newFlexClusterModel.Tags) {
114+
newFlexClusterModel.Tags = types.MapNull(types.StringType)
115+
}
116+
106117
resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClusterModel)...)
107118
}
108119

@@ -145,6 +156,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou
145156
resp.Diagnostics.Append(diags...)
146157
return
147158
}
159+
160+
if conversion.UseNilForEmpty(plan.Tags, newFlexClusterModel.Tags) {
161+
newFlexClusterModel.Tags = types.MapNull(types.StringType)
162+
}
163+
148164
resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClusterModel)...)
149165
}
150166

internal/service/flexcluster/resource_test.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ func basicTestCase(t *testing.T) *resource.TestCase {
4444
CheckDestroy: checkDestroy,
4545
Steps: []resource.TestStep{
4646
{
47-
Config: configBasic(projectID, clusterName, provider, region, true),
48-
Check: checksFlexCluster(projectID, clusterName, true),
47+
Config: configBasic(projectID, clusterName, provider, region, true, false),
48+
Check: checksFlexCluster(projectID, clusterName, true, false),
4949
},
5050
{
51-
Config: configBasic(projectID, clusterName, provider, region, false),
52-
Check: checksFlexCluster(projectID, clusterName, false),
51+
Config: configBasic(projectID, clusterName, provider, region, false, true),
52+
Check: checksFlexCluster(projectID, clusterName, false, true),
5353
},
5454
{
55-
Config: configBasic(projectID, clusterName, provider, region, true),
55+
Config: configBasic(projectID, clusterName, provider, region, true, true),
5656
ResourceName: resourceName,
5757
ImportStateIdFunc: importStateIDFunc(resourceName),
5858
ImportState: true,
@@ -80,30 +80,37 @@ func failedUpdateTestCase(t *testing.T) *resource.TestCase {
8080
CheckDestroy: checkDestroy,
8181
Steps: []resource.TestStep{
8282
{
83-
Config: configBasic(projectID, clusterName, provider, region, false),
84-
Check: checksFlexCluster(projectID, clusterName, false),
83+
Config: configBasic(projectID, clusterName, provider, region, false, false),
84+
Check: checksFlexCluster(projectID, clusterName, false, false),
8585
},
8686
{
87-
Config: configBasic(projectID, clusterNameUpdated, provider, region, false),
87+
Config: configBasic(projectID, clusterNameUpdated, provider, region, false, false),
8888
ExpectError: regexp.MustCompile("name cannot be updated"),
8989
},
9090
{
91-
Config: configBasic(projectIDUpdated, clusterName, provider, region, false),
91+
Config: configBasic(projectIDUpdated, clusterName, provider, region, false, false),
9292
ExpectError: regexp.MustCompile("project_id cannot be updated"),
9393
},
9494
{
95-
Config: configBasic(projectID, clusterName, providerUpdated, region, false),
95+
Config: configBasic(projectID, clusterName, providerUpdated, region, false, false),
9696
ExpectError: regexp.MustCompile("provider_settings.backing_provider_name cannot be updated"),
9797
},
9898
{
99-
Config: configBasic(projectID, clusterName, provider, regionUpdated, false),
99+
Config: configBasic(projectID, clusterName, provider, regionUpdated, false, false),
100100
ExpectError: regexp.MustCompile("provider_settings.region_name cannot be updated"),
101101
},
102102
},
103103
}
104104
}
105105

106-
func configBasic(projectID, clusterName, provider, region string, terminationProtectionEnabled bool) string {
106+
func configBasic(projectID, clusterName, provider, region string, terminationProtectionEnabled, tags bool) string {
107+
tagsConfig := ""
108+
if tags {
109+
tagsConfig = `
110+
tags = {
111+
testKey = "testValue"
112+
}`
113+
}
107114
return fmt.Sprintf(`
108115
resource "mongodbatlas_flex_cluster" "test" {
109116
project_id = %[1]q
@@ -113,26 +120,26 @@ func configBasic(projectID, clusterName, provider, region string, terminationPro
113120
region_name = %[4]q
114121
}
115122
termination_protection_enabled = %[5]t
116-
tags = {
117-
testKey = "testValue"
118-
}
123+
%[6]s
119124
}
120125
data "mongodbatlas_flex_cluster" "test" {
121126
project_id = mongodbatlas_flex_cluster.test.project_id
122127
name = mongodbatlas_flex_cluster.test.name
123128
}
124129
data "mongodbatlas_flex_clusters" "test" {
125130
project_id = mongodbatlas_flex_cluster.test.project_id
126-
}`, projectID, clusterName, provider, region, terminationProtectionEnabled)
131+
}`, projectID, clusterName, provider, region, terminationProtectionEnabled, tagsConfig)
127132
}
128133

129-
func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool) resource.TestCheckFunc {
134+
func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled, tagsCheck bool) resource.TestCheckFunc {
130135
checks := []resource.TestCheckFunc{checkExists()}
131136
attrMap := map[string]string{
132137
"project_id": projectID,
133138
"name": clusterName,
134139
"termination_protection_enabled": fmt.Sprintf("%v", terminationProtectionEnabled),
135-
"tags.testKey": "testValue",
140+
}
141+
if tagsCheck {
142+
attrMap["tags.testKey"] = "testValue"
136143
}
137144
pluralMap := map[string]string{
138145
"project_id": projectID,

0 commit comments

Comments
 (0)