@@ -154,7 +154,7 @@ type extensions struct {
154
154
155
155
// Types corresponding to extensions
156
156
var extensionsTypes = map [string ]attr.Type {
157
- "argus" : basetypes.ObjectType {AttrTypes : argusExtensionTypes },
157
+ "argus" : basetypes.ObjectType {AttrTypes : argusTypes },
158
158
"acl" : basetypes.ObjectType {AttrTypes : aclTypes },
159
159
}
160
160
@@ -169,13 +169,13 @@ var aclTypes = map[string]attr.Type{
169
169
"allowed_cidrs" : basetypes.ListType {ElemType : types .StringType },
170
170
}
171
171
172
- type argusExtension struct {
172
+ type argus struct {
173
173
Enabled types.Bool `tfsdk:"enabled"`
174
174
ArgusInstanceId types.String `tfsdk:"argus_instance_id"`
175
175
}
176
176
177
177
// Types corresponding to argusExtension
178
- var argusExtensionTypes = map [string ]attr.Type {
178
+ var argusTypes = map [string ]attr.Type {
179
179
"enabled" : basetypes.BoolType {},
180
180
"argus_instance_id" : basetypes.StringType {},
181
181
}
@@ -490,7 +490,7 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
490
490
},
491
491
"allowed_cidrs" : schema.ListAttribute {
492
492
Description : "Specify a list of CIDRs to whitelist." ,
493
- Required : true ,
493
+ Optional : true ,
494
494
ElementType : types .StringType ,
495
495
},
496
496
},
@@ -818,24 +818,24 @@ func toExtensionsPayload(ctx context.Context, m *Cluster) (*ske.Extension, error
818
818
}
819
819
}
820
820
821
- var skeArgusExtension * ske.Argus
821
+ var skeArgus * ske.Argus
822
822
if ! (ex .Argus .IsNull () || ex .Argus .IsUnknown ()) {
823
- argus := argusExtension {}
823
+ argus := argus {}
824
824
diags = ex .Argus .As (ctx , & argus , basetypes.ObjectAsOptions {})
825
825
if diags .HasError () {
826
826
return nil , fmt .Errorf ("converting extensions.argus object: %v" , diags .Errors ())
827
827
}
828
828
argusEnabled := conversion .BoolValueToPointer (argus .Enabled )
829
829
argusInstanceId := conversion .StringValueToPointer (argus .ArgusInstanceId )
830
- skeArgusExtension = & ske.Argus {
830
+ skeArgus = & ske.Argus {
831
831
Enabled : argusEnabled ,
832
832
ArgusInstanceId : argusInstanceId ,
833
833
}
834
834
}
835
835
836
836
return & ske.Extension {
837
837
Acl : skeAcl ,
838
- Argus : skeArgusExtension ,
838
+ Argus : skeArgus ,
839
839
}, nil
840
840
}
841
841
@@ -1040,11 +1040,28 @@ func mapTaints(t *[]ske.Taint, nodePool map[string]attr.Value) error {
1040
1040
}
1041
1041
1042
1042
func mapHibernations (cl * ske.ClusterResponse , m * Cluster ) error {
1043
- if cl .Hibernation == nil || cl .Hibernation .Schedules == nil {
1043
+ if cl .Hibernation == nil {
1044
+ if ! m .Hibernations .IsNull () {
1045
+ emptyHibernations , diags := basetypes .NewListValue (basetypes.ObjectType {AttrTypes : hibernationTypes }, []attr.Value {})
1046
+ if diags .HasError () {
1047
+ return fmt .Errorf ("hibernations is an empty list, converting to terraform empty list: %w" , core .DiagsToError (diags ))
1048
+ }
1049
+ m .Hibernations = emptyHibernations
1050
+ return nil
1051
+ }
1044
1052
m .Hibernations = basetypes .NewListNull (basetypes.ObjectType {AttrTypes : hibernationTypes })
1045
1053
return nil
1046
1054
}
1047
1055
1056
+ if cl .Hibernation .Schedules == nil {
1057
+ emptyHibernations , diags := basetypes .NewListValue (basetypes.ObjectType {AttrTypes : hibernationTypes }, []attr.Value {})
1058
+ if diags .HasError () {
1059
+ return fmt .Errorf ("hibernations is an empty list, converting to terraform empty list: %w" , core .DiagsToError (diags ))
1060
+ }
1061
+ m .Hibernations = emptyHibernations
1062
+ return nil
1063
+ }
1064
+
1048
1065
hibernations := []attr.Value {}
1049
1066
for i , hibernationResp := range * cl .Hibernation .Schedules {
1050
1067
hibernation := map [string ]attr.Value {
@@ -1149,14 +1166,73 @@ func getMaintenanceTimes(ctx context.Context, cl *ske.ClusterResponse, m *Cluste
1149
1166
return startTime , endTime , nil
1150
1167
}
1151
1168
1169
+ func checkDisabledExtensions (ctx context.Context , ex extensions ) (aclDisabled , argusDisabled bool , err error ) {
1170
+ var diags diag.Diagnostics
1171
+ acl := acl {}
1172
+ if ex .ACL .IsNull () {
1173
+ acl .Enabled = types .BoolValue (false )
1174
+ } else {
1175
+ diags = ex .ACL .As (ctx , & acl , basetypes.ObjectAsOptions {})
1176
+ if diags .HasError () {
1177
+ return false , false , fmt .Errorf ("converting extensions.acl object: %v" , diags .Errors ())
1178
+ }
1179
+ }
1180
+
1181
+ argus := argus {}
1182
+ if ex .Argus .IsNull () {
1183
+ argus .Enabled = types .BoolValue (false )
1184
+ } else {
1185
+ diags = ex .Argus .As (ctx , & argus , basetypes.ObjectAsOptions {})
1186
+ if diags .HasError () {
1187
+ return false , false , fmt .Errorf ("converting extensions.argus object: %v" , diags .Errors ())
1188
+ }
1189
+ }
1190
+
1191
+ return ! acl .Enabled .ValueBool (), ! argus .Enabled .ValueBool (), nil
1192
+ }
1193
+
1152
1194
func mapExtensions (ctx context.Context , cl * ske.ClusterResponse , m * Cluster ) error {
1153
- if cl .Extensions == nil || ( cl . Extensions . Argus == nil && cl . Extensions . Acl == nil ) {
1195
+ if cl .Extensions == nil {
1154
1196
m .Extensions = types .ObjectNull (extensionsTypes )
1155
1197
return nil
1156
1198
}
1157
1199
1158
1200
var diags diag.Diagnostics
1159
- acl := types .ObjectNull (aclTypes )
1201
+ ex := extensions {}
1202
+ if ! m .Extensions .IsNull () {
1203
+ diags := m .Extensions .As (ctx , & ex , basetypes.ObjectAsOptions {})
1204
+ if diags .HasError () {
1205
+ return fmt .Errorf ("converting extensions object: %v" , diags .Errors ())
1206
+ }
1207
+ }
1208
+
1209
+ // If the user provides the extensions block with the enabled flags as false
1210
+ // the SKE API will return an empty extensions block, which throws an inconsistent
1211
+ // result after apply error. To prevent this error, if both flags are false,
1212
+ // we set the fields provided by the user in the terraform model
1213
+
1214
+ // If the extensions field is not provided, the SKE API returns an empty object.
1215
+ // If we parse that object into the terraform model, it will produce an inconsistent result after apply
1216
+ // error
1217
+
1218
+ aclDisabled , argusDisabled , err := checkDisabledExtensions (ctx , ex )
1219
+ if err != nil {
1220
+ return fmt .Errorf ("checking if extensions are disabled: %w" , err )
1221
+ }
1222
+ disabledExtensions := false
1223
+ if aclDisabled && argusDisabled {
1224
+ disabledExtensions = true
1225
+ }
1226
+
1227
+ emptyExtensions := & ske.Extension {}
1228
+ if * cl .Extensions == * emptyExtensions && (disabledExtensions || m .Extensions .IsNull ()) {
1229
+ if m .Extensions .Attributes () == nil {
1230
+ m .Extensions = types .ObjectNull (extensionsTypes )
1231
+ }
1232
+ return nil
1233
+ }
1234
+
1235
+ aclExtension := types .ObjectNull (aclTypes )
1160
1236
if cl .Extensions .Acl != nil {
1161
1237
enabled := types .BoolNull ()
1162
1238
if cl .Extensions .Acl .Enabled != nil {
@@ -1173,13 +1249,15 @@ func mapExtensions(ctx context.Context, cl *ske.ClusterResponse, m *Cluster) err
1173
1249
"allowed_cidrs" : cidrsList ,
1174
1250
}
1175
1251
1176
- acl , diags = types .ObjectValue (aclTypes , aclValues )
1252
+ aclExtension , diags = types .ObjectValue (aclTypes , aclValues )
1177
1253
if diags .HasError () {
1178
1254
return fmt .Errorf ("creating acl: %w" , core .DiagsToError (diags ))
1179
1255
}
1256
+ } else if aclDisabled && ! ex .ACL .IsNull () {
1257
+ aclExtension = ex .ACL
1180
1258
}
1181
1259
1182
- argusExtension := types .ObjectNull (argusExtensionTypes )
1260
+ argusExtension := types .ObjectNull (argusTypes )
1183
1261
if cl .Extensions .Argus != nil {
1184
1262
enabled := types .BoolNull ()
1185
1263
if cl .Extensions .Argus .Enabled != nil {
@@ -1196,14 +1274,16 @@ func mapExtensions(ctx context.Context, cl *ske.ClusterResponse, m *Cluster) err
1196
1274
"argus_instance_id" : argusInstanceId ,
1197
1275
}
1198
1276
1199
- argusExtension , diags = types .ObjectValue (argusExtensionTypes , argusExtensionValues )
1277
+ argusExtension , diags = types .ObjectValue (argusTypes , argusExtensionValues )
1200
1278
if diags .HasError () {
1201
1279
return fmt .Errorf ("creating argus extension: %w" , core .DiagsToError (diags ))
1202
1280
}
1281
+ } else if argusDisabled && ! ex .Argus .IsNull () {
1282
+ argusExtension = ex .Argus
1203
1283
}
1204
1284
1205
1285
extensionsValues := map [string ]attr.Value {
1206
- "acl" : acl ,
1286
+ "acl" : aclExtension ,
1207
1287
"argus" : argusExtension ,
1208
1288
}
1209
1289
0 commit comments