@@ -256,12 +256,12 @@ func (t *Topic) Update(ctx context.Context, request resource.UpdateRequest, resp
256
256
}
257
257
defer t .dataplaneConn .Close ()
258
258
if ! plan .Configuration .Equal (state .Configuration ) {
259
- cfgToSet , err := utils . MapToUpdateTopicConfiguration ( plan . Configuration )
259
+ cfgToSet , err := parseUpdateConfiguration ( state , plan )
260
260
if err != nil {
261
261
response .Diagnostics .AddError ("failed to parse the configuration map" , err .Error ())
262
262
return
263
263
}
264
- cfgs , err := t .TopicClient .UpdateTopicConfigurations (ctx , & dataplanev1alpha1.UpdateTopicConfigurationsRequest {
264
+ cfgs , err := t .TopicClient .SetTopicConfigurations (ctx , & dataplanev1alpha1.SetTopicConfigurationsRequest {
265
265
TopicName : plan .Name .ValueString (),
266
266
Configurations : cfgToSet ,
267
267
})
@@ -361,3 +361,41 @@ func filterDynamicConfig(configs []*dataplanev1alpha1.Topic_Configuration) []*da
361
361
func isAlreadyExistsError (err error ) bool {
362
362
return strings .Contains (err .Error (), "TOPIC_ALREADY_EXISTS" ) || strings .Contains (err .Error (), "The topic has already been created" )
363
363
}
364
+
365
+ // parseUpdateConfiguration parses both the state and the plan configuration,
366
+ // and generates a new SetConfiguration map with:
367
+ // - Configurations that are both in the state, and the plan.
368
+ // - Configurations that are in the plan and not in the state.
369
+ // - Updated configurations that are in the plan.
370
+ func parseUpdateConfiguration (state , plan models.Topic ) ([]* dataplanev1alpha1.SetTopicConfigurationsRequest_SetConfiguration , error ) {
371
+ var output []* dataplanev1alpha1.SetTopicConfigurationsRequest_SetConfiguration
372
+ // Loop through state configuration.
373
+ for k , v := range state .Configuration .Elements () {
374
+ if v .IsNull () || v .IsUnknown () {
375
+ return nil , fmt .Errorf ("topic configuration %q must have a value" , k )
376
+ }
377
+ // If configuration also exists in plan.
378
+ if pv , ok := plan .Configuration .Elements ()[k ]; ok {
379
+ // Add whatever is in the plan to the output.
380
+ value := strings .Trim (pv .String (), `"` )
381
+ output = append (output , & dataplanev1alpha1.SetTopicConfigurationsRequest_SetConfiguration {
382
+ Name : k ,
383
+ Value : & value ,
384
+ })
385
+ }
386
+ // If it's in the state, but not in the plan, then the
387
+ // config was removed. We do not add that.
388
+ }
389
+ // Now we check for what's in the plan and not in the state.
390
+ for k , v := range plan .Configuration .Elements () {
391
+ // Add to output if it's not in the state.
392
+ if _ , ok := state .Configuration .Elements ()[k ]; ! ok {
393
+ value := strings .Trim (v .String (), `"` )
394
+ output = append (output , & dataplanev1alpha1.SetTopicConfigurationsRequest_SetConfiguration {
395
+ Name : k ,
396
+ Value : & value ,
397
+ })
398
+ }
399
+ }
400
+ return output , nil
401
+ }
0 commit comments