@@ -113,6 +113,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
113
113
resp .Diagnostics .AddError ("Failed to get DB client" , err .Error ())
114
114
return
115
115
}
116
+ state .Region = types .StringValue (string (region ))
116
117
117
118
o := materialize.MaterializeObject {ObjectType : "CLUSTER" , Name : state .Name .ValueString ()}
118
119
b := materialize .NewClusterBuilder (metaDb , o )
@@ -131,7 +132,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
131
132
if strings .HasSuffix (size , "cc" ) || strings .HasSuffix (size , "C" ) {
132
133
// DISK option not supported for cluster sizes ending in cc or C.
133
134
log .Printf ("[WARN] disk option not supported for cluster size %s, disk is always enabled" , size )
134
- b .Disk (true )
135
+ state .Disk = types . BoolValue (true )
135
136
} else if ! state .Disk .IsNull () {
136
137
b .Disk (state .Disk .ValueBool ())
137
138
}
@@ -164,17 +165,16 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
164
165
}
165
166
166
167
// Ownership.
167
- // TODO: Fix failing error
168
- // if !state.OwnershipRole.IsNull() {
169
- // ownership := materialize.NewOwnershipBuilder(metaDb, o)
170
-
171
- // if err := ownership.Alter(state.OwnershipRole.ValueString()); err != nil {
172
- // log.Printf("[DEBUG] resource failed ownership, dropping object: %s", o.Name)
173
- // b.Drop()
174
- // resp.Diagnostics.AddError("Failed to set ownership", err.Error())
175
- // return
176
- // }
177
- // }
168
+ if ! state .OwnershipRole .IsNull () && state .OwnershipRole .ValueString () != "" {
169
+ ownership := materialize .NewOwnershipBuilder (metaDb , o )
170
+
171
+ if err := ownership .Alter (state .OwnershipRole .ValueString ()); err != nil {
172
+ log .Printf ("[DEBUG] resource failed ownership, dropping object: %s" , o .Name )
173
+ b .Drop ()
174
+ resp .Diagnostics .AddError ("Failed to set ownership" , err .Error ())
175
+ return
176
+ }
177
+ }
178
178
179
179
// Object comment.
180
180
if ! state .Comment .IsNull () {
@@ -217,64 +217,23 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
217
217
218
218
func (r * clusterResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
219
219
var state ClusterStateModelV0
220
+
220
221
// Retrieve the current state
221
222
diags := req .State .Get (ctx , & state )
222
223
resp .Diagnostics .Append (diags ... )
223
224
if resp .Diagnostics .HasError () {
224
225
return
225
226
}
226
227
227
- // Extract the ID and region from the state
228
- clusterID := state .ID .ValueString ()
229
-
230
- metaDb , region , err := utils .NewGetDBClientFromMeta (r .client , state .Region .ValueString ())
231
- if err != nil {
232
- resp .Diagnostics .AddError ("Failed to get DB client" , err .Error ())
233
- return
234
- }
235
-
236
- s , err := materialize .ScanCluster (metaDb , utils .ExtractId (clusterID ))
237
- if err != nil {
238
- if err == sql .ErrNoRows {
239
- // If no rows are returned, set the resource ID to an empty string to mark it as removed
240
- state .ID = types.String {}
241
- resp .State .Set (ctx , state )
242
- return
243
- } else {
244
- resp .Diagnostics .AddError ("Failed to read the cluster" , err .Error ())
245
- return
246
- }
247
- }
248
-
249
- // Update the state with the fetched values
250
- state .ID = types .StringValue (utils .TransformIdWithRegion (string (region ), clusterID ))
251
- state .Name = types .StringValue (s .ClusterName .String )
252
- state .OwnershipRole = types .StringValue (s .OwnerName .String )
253
- state .ReplicationFactor = types .Int64Value (s .ReplicationFactor .Int64 )
254
- state .Size = types .StringValue (s .Size .String )
255
- state .Disk = types .BoolValue (s .Disk .Bool )
228
+ // Use the lower-case read function to get the updated state
229
+ updatedState , _ := r .read (ctx , & state , false )
256
230
257
- // Convert the availability zones to the appropriate type
258
- azs := make ([]types.String , len (s .AvailabilityZones ))
259
- for i , az := range s .AvailabilityZones {
260
- azs [i ] = types .StringValue (az )
261
- }
262
- azValues := make ([]attr.Value , len (s .AvailabilityZones ))
263
- for i , az := range s .AvailabilityZones {
264
- azValues [i ] = types .StringValue (az )
265
- }
266
-
267
- azList , diags := types .ListValue (types .StringType , azValues )
231
+ // Set the updated state in the response
232
+ diags = resp .State .Set (ctx , updatedState )
268
233
resp .Diagnostics .Append (diags ... )
269
234
if resp .Diagnostics .HasError () {
270
235
return
271
236
}
272
-
273
- state .AvailabilityZones = azList
274
- state .Comment = types .StringValue (s .Comment .String )
275
-
276
- // Set the updated state in the response
277
- resp .State .Set (ctx , state )
278
237
}
279
238
280
239
func (r * clusterResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
@@ -292,17 +251,18 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
292
251
return
293
252
}
294
253
295
- metaDb , _ , err := utils .NewGetDBClientFromMeta (r .client , state .Region .ValueString ())
254
+ metaDb , region , err := utils .NewGetDBClientFromMeta (r .client , state .Region .ValueString ())
296
255
if err != nil {
297
256
resp .Diagnostics .AddError ("Failed to get DB client" , err .Error ())
298
257
return
299
258
}
259
+ state .Region = types .StringValue (string (region ))
300
260
301
261
o := materialize.MaterializeObject {ObjectType : "CLUSTER" , Name : state .Name .ValueString ()}
302
262
b := materialize .NewClusterBuilder (metaDb , o )
303
263
304
264
// Update cluster attributes if they have changed
305
- if state .OwnershipRole .ValueString () != plan .OwnershipRole .ValueString () {
265
+ if state .OwnershipRole .ValueString () != plan .OwnershipRole .ValueString () && plan . OwnershipRole . ValueString () != "" {
306
266
ownershipBuilder := materialize .NewOwnershipBuilder (metaDb , o )
307
267
if err := ownershipBuilder .Alter (plan .OwnershipRole .ValueString ()); err != nil {
308
268
resp .Diagnostics .AddError ("Failed to update ownership role" , err .Error ())
@@ -319,9 +279,15 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
319
279
320
280
// Handle changes in the 'disk' attribute
321
281
if state .Disk .ValueBool () != plan .Disk .ValueBool () {
322
- if err := b .SetDisk (plan .Disk .ValueBool ()); err != nil {
323
- resp .Diagnostics .AddError ("Failed to update disk setting" , err .Error ())
324
- return
282
+ if strings .HasSuffix (state .Size .ValueString (), "cc" ) || strings .HasSuffix (state .Size .ValueString (), "C" ) {
283
+ // DISK option not supported for cluster sizes ending in cc or C.
284
+ log .Printf ("[WARN] disk option not supported for cluster size %s, disk is always enabled" , state .Size .ValueString ())
285
+ state .Disk = types .BoolValue (true )
286
+ } else {
287
+ if err := b .SetDisk (plan .Disk .ValueBool ()); err != nil {
288
+ resp .Diagnostics .AddError ("Failed to update disk setting" , err .Error ())
289
+ return
290
+ }
325
291
}
326
292
}
327
293
@@ -334,7 +300,7 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
334
300
}
335
301
336
302
// Handle changes in the 'availability_zones' attribute
337
- if ! state .AvailabilityZones .Equal (plan .AvailabilityZones ) {
303
+ if ! state .AvailabilityZones .Equal (plan .AvailabilityZones ) && len ( plan . AvailabilityZones . Elements ()) > 0 {
338
304
azs := make ([]string , len (plan .AvailabilityZones .Elements ()))
339
305
for i , elem := range plan .AvailabilityZones .Elements () {
340
306
azs [i ] = elem .(types.String ).ValueString ()
@@ -415,7 +381,7 @@ func (r *clusterResource) Delete(ctx context.Context, req resource.DeleteRequest
415
381
func (r * clusterResource ) read (ctx context.Context , data * ClusterStateModelV0 , dryRun bool ) (* ClusterStateModelV0 , diag.Diagnostics ) {
416
382
diags := diag.Diagnostics {}
417
383
418
- metaDb , _ , err := utils .NewGetDBClientFromMeta (r .client , data .Region .ValueString ())
384
+ metaDb , region , err := utils .NewGetDBClientFromMeta (r .client , data .Region .ValueString ())
419
385
if err != nil {
420
386
diags = append (diags , diag.Diagnostic {
421
387
Severity : diag .Error ,
@@ -457,10 +423,26 @@ func (r *clusterResource) read(ctx context.Context, data *ClusterStateModelV0, d
457
423
data .Disk = types .BoolValue (clusterDetails .Disk .Bool )
458
424
data .OwnershipRole = types .StringValue (getNullString (clusterDetails .OwnerName ))
459
425
460
- // TODO: Fix failing error for the following fields when they are not set
461
- // data.Size = types.StringValue(getNullString(clusterDetails.Size))
462
- // data.Comment = types.StringValue(getNullString(clusterDetails.Comment))
463
- // data.Region = types.StringValue(string(region))
426
+ // Handle the Size attribute
427
+ if clusterDetails .Size .Valid && clusterDetails .Size .String != "" {
428
+ data .Size = types .StringValue (clusterDetails .Size .String )
429
+ } else {
430
+ data .Size = types .StringNull ()
431
+ }
432
+
433
+ // Handle the Comment attribute
434
+ if clusterDetails .Comment .Valid && clusterDetails .Comment .String != "" {
435
+ data .Comment = types .StringValue (clusterDetails .Comment .String )
436
+ } else {
437
+ data .Comment = types .StringNull ()
438
+ }
439
+
440
+ regionStr := string (region )
441
+ if regionStr != "" {
442
+ data .Region = types .StringValue (regionStr )
443
+ } else {
444
+ data .Region = types .StringNull ()
445
+ }
464
446
465
447
// Handle the AvailabilityZones which is a slice of strings.
466
448
azValues := make ([]attr.Value , len (clusterDetails .AvailabilityZones ))
0 commit comments