7
7
8
8
"github.com/hashicorp/terraform-plugin-framework/diag"
9
9
"github.com/hashicorp/terraform-plugin-framework/types"
10
+ "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
10
11
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/schemafunc"
11
12
)
12
13
@@ -37,6 +38,7 @@ func useStateForUnknowns(ctx context.Context, diags *diag.Diagnostics, state, pl
37
38
attributeChanges := schemafunc .NewAttributeChanges (ctx , state , plan )
38
39
keepUnknown := []string {"connection_strings" , "state_name" } // Volatile attributes, should not be copied from state
39
40
keepUnknown = append (keepUnknown , attributeChanges .KeepUnknown (attributeRootChangeMapping )... )
41
+ keepUnknown = append (keepUnknown , determineKeepUnknownsAutoScaling (ctx , diags , state , plan )... )
40
42
schemafunc .CopyUnknowns (ctx , state , plan , keepUnknown )
41
43
if slices .Contains (keepUnknown , "replication_specs" ) {
42
44
useStateForUnknownsReplicationSpecs (ctx , diags , state , plan , & attributeChanges )
@@ -51,6 +53,7 @@ func useStateForUnknownsReplicationSpecs(ctx context.Context, diags *diag.Diagno
51
53
}
52
54
planWithUnknowns := []TFReplicationSpecsModel {}
53
55
keepUnknownsUnchangedSpec := determineKeepUnknownsUnchangedReplicationSpecs (ctx , diags , state , plan , attrChanges )
56
+ keepUnknownsUnchangedSpec = append (keepUnknownsUnchangedSpec , determineKeepUnknownsAutoScaling (ctx , diags , state , plan )... )
54
57
if diags .HasError () {
55
58
return
56
59
}
@@ -95,6 +98,48 @@ func determineKeepUnknownsUnchangedReplicationSpecs(ctx context.Context, diags *
95
98
return keepUnknowns
96
99
}
97
100
101
+ func determineKeepUnknownsAutoScaling (ctx context.Context , diags * diag.Diagnostics , state , plan * TFModel ) []string {
102
+ var keepUnknown []string
103
+ computedUsed , diskUsed := autoScalingUsed (ctx , diags , state , plan )
104
+ if computedUsed {
105
+ keepUnknown = append (keepUnknown , "instance_size" )
106
+ keepUnknown = append (keepUnknown , attributeReplicationSpecChangeMapping ["instance_size" ]... )
107
+ }
108
+ if diskUsed {
109
+ keepUnknown = append (keepUnknown , "disk_size_gb" )
110
+ keepUnknown = append (keepUnknown , attributeReplicationSpecChangeMapping ["disk_size_gb" ]... )
111
+ }
112
+ return keepUnknown
113
+ }
114
+
115
+ // autoScalingUsed checks is auto-scaling was enabled (state) or will be enabled (plan).
116
+ func autoScalingUsed (ctx context.Context , diags * diag.Diagnostics , state , plan * TFModel ) (computedUsed , diskUsed bool ) {
117
+ for _ , model := range []* TFModel {state , plan } {
118
+ repSpecsTF := TFModelList [TFReplicationSpecsModel ](ctx , diags , model .ReplicationSpecs )
119
+ for i := range repSpecsTF {
120
+ regiongConfigsTF := TFModelList [TFRegionConfigsModel ](ctx , diags , repSpecsTF [i ].RegionConfigs )
121
+ for j := range regiongConfigsTF {
122
+ for _ , autoScalingTF := range []types.Object {regiongConfigsTF [j ].AutoScaling , regiongConfigsTF [j ].AnalyticsAutoScaling } {
123
+ if autoScalingTF .IsNull () || autoScalingTF .IsUnknown () {
124
+ continue
125
+ }
126
+ autoscaling := TFModelObject [TFAutoScalingModel ](ctx , diags , autoScalingTF )
127
+ if autoscaling == nil {
128
+ continue
129
+ }
130
+ if autoscaling .ComputeEnabled .ValueBool () {
131
+ computedUsed = true
132
+ }
133
+ if autoscaling .DiskGBEnabled .ValueBool () {
134
+ diskUsed = true
135
+ }
136
+ }
137
+ }
138
+ }
139
+ }
140
+ return
141
+ }
142
+
98
143
func TFModelList [T any ](ctx context.Context , diags * diag.Diagnostics , input types.List ) []T {
99
144
elements := make ([]T , len (input .Elements ()))
100
145
if localDiags := input .ElementsAs (ctx , & elements , false ); len (localDiags ) > 0 {
@@ -103,3 +148,12 @@ func TFModelList[T any](ctx context.Context, diags *diag.Diagnostics, input type
103
148
}
104
149
return elements
105
150
}
151
+
152
+ func TFModelObject [T any ](ctx context.Context , diags * diag.Diagnostics , input types.Object ) * T {
153
+ item := new (T )
154
+ if localDiags := input .As (ctx , item , basetypes.ObjectAsOptions {}); len (localDiags ) > 0 {
155
+ diags .Append (localDiags ... )
156
+ return nil
157
+ }
158
+ return item
159
+ }
0 commit comments