@@ -113,6 +113,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
113113 resp .Diagnostics .AddError ("Failed to get DB client" , err .Error ())
114114 return
115115 }
116+ state .Region = types .StringValue (string (region ))
116117
117118 o := materialize.MaterializeObject {ObjectType : "CLUSTER" , Name : state .Name .ValueString ()}
118119 b := materialize .NewClusterBuilder (metaDb , o )
@@ -131,7 +132,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
131132 if strings .HasSuffix (size , "cc" ) || strings .HasSuffix (size , "C" ) {
132133 // DISK option not supported for cluster sizes ending in cc or C.
133134 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 )
135136 } else if ! state .Disk .IsNull () {
136137 b .Disk (state .Disk .ValueBool ())
137138 }
@@ -164,17 +165,16 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
164165 }
165166
166167 // 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+ }
178178
179179 // Object comment.
180180 if ! state .Comment .IsNull () {
@@ -217,64 +217,23 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
217217
218218func (r * clusterResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
219219 var state ClusterStateModelV0
220+
220221 // Retrieve the current state
221222 diags := req .State .Get (ctx , & state )
222223 resp .Diagnostics .Append (diags ... )
223224 if resp .Diagnostics .HasError () {
224225 return
225226 }
226227
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 )
256230
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 )
268233 resp .Diagnostics .Append (diags ... )
269234 if resp .Diagnostics .HasError () {
270235 return
271236 }
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 )
278237}
279238
280239func (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
292251 return
293252 }
294253
295- metaDb , _ , err := utils .NewGetDBClientFromMeta (r .client , state .Region .ValueString ())
254+ metaDb , region , err := utils .NewGetDBClientFromMeta (r .client , state .Region .ValueString ())
296255 if err != nil {
297256 resp .Diagnostics .AddError ("Failed to get DB client" , err .Error ())
298257 return
299258 }
259+ state .Region = types .StringValue (string (region ))
300260
301261 o := materialize.MaterializeObject {ObjectType : "CLUSTER" , Name : state .Name .ValueString ()}
302262 b := materialize .NewClusterBuilder (metaDb , o )
303263
304264 // 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 () != "" {
306266 ownershipBuilder := materialize .NewOwnershipBuilder (metaDb , o )
307267 if err := ownershipBuilder .Alter (plan .OwnershipRole .ValueString ()); err != nil {
308268 resp .Diagnostics .AddError ("Failed to update ownership role" , err .Error ())
@@ -319,9 +279,15 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
319279
320280 // Handle changes in the 'disk' attribute
321281 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+ }
325291 }
326292 }
327293
@@ -334,7 +300,7 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
334300 }
335301
336302 // 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 {
338304 azs := make ([]string , len (plan .AvailabilityZones .Elements ()))
339305 for i , elem := range plan .AvailabilityZones .Elements () {
340306 azs [i ] = elem .(types.String ).ValueString ()
@@ -415,7 +381,7 @@ func (r *clusterResource) Delete(ctx context.Context, req resource.DeleteRequest
415381func (r * clusterResource ) read (ctx context.Context , data * ClusterStateModelV0 , dryRun bool ) (* ClusterStateModelV0 , diag.Diagnostics ) {
416382 diags := diag.Diagnostics {}
417383
418- metaDb , _ , err := utils .NewGetDBClientFromMeta (r .client , data .Region .ValueString ())
384+ metaDb , region , err := utils .NewGetDBClientFromMeta (r .client , data .Region .ValueString ())
419385 if err != nil {
420386 diags = append (diags , diag.Diagnostic {
421387 Severity : diag .Error ,
@@ -457,10 +423,26 @@ func (r *clusterResource) read(ctx context.Context, data *ClusterStateModelV0, d
457423 data .Disk = types .BoolValue (clusterDetails .Disk .Bool )
458424 data .OwnershipRole = types .StringValue (getNullString (clusterDetails .OwnerName ))
459425
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+ }
464446
465447 // Handle the AvailabilityZones which is a slice of strings.
466448 azValues := make ([]attr.Value , len (clusterDetails .AvailabilityZones ))
0 commit comments