@@ -341,17 +341,9 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
341
341
}
342
342
}
343
343
344
- existingInputs , _ := d .Get ("input" ).([]any )
345
- inputIDToIndex := make (map [string ]int , len (existingInputs ))
346
- for i , v := range existingInputs {
347
- inputData , _ := v .(map [string ]any )
348
- inputID , _ := inputData ["input_id" ].(string )
349
- inputIDToIndex [inputID ] = i
350
- }
351
-
352
344
newInputs := make ([]any , 0 , len (pkgPolicy .Inputs ))
353
345
for inputID , input := range pkgPolicy .Inputs {
354
- inputMap := map [string ]any {
346
+ inputData := map [string ]any {
355
347
"input_id" : inputID ,
356
348
"enabled" : input .Enabled ,
357
349
}
@@ -361,36 +353,21 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
361
353
if err != nil {
362
354
return diag .FromErr (err )
363
355
}
364
- inputMap ["streams_json" ] = string (data )
356
+ inputData ["streams_json" ] = string (data )
365
357
}
366
358
if input .Vars != nil {
367
359
data , err := json .Marshal (* input .Vars )
368
360
if err != nil {
369
361
return diag .FromErr (err )
370
362
}
371
- inputMap ["vars_json" ] = string (data )
363
+ inputData ["vars_json" ] = string (data )
372
364
}
373
365
374
- newInputs = append (newInputs , inputMap )
366
+ newInputs = append (newInputs , inputData )
375
367
}
376
368
377
- sort .Slice (newInputs , func (i , j int ) bool {
378
- iInput , _ := newInputs [i ].(map [string ]any )
379
- iID , _ := iInput ["input_id" ].(string )
380
- iIdx , ok := inputIDToIndex [iID ]
381
- if ! ok {
382
- return false
383
- }
384
-
385
- jInput , _ := newInputs [j ].(map [string ]any )
386
- jID , _ := jInput ["input_id" ].(string )
387
- jIdx , ok := inputIDToIndex [jID ]
388
- if ! ok {
389
- return true
390
- }
391
-
392
- return iIdx < jIdx
393
- })
369
+ existingInputs , _ := d .Get ("input" ).([]any )
370
+ sortInputs (newInputs , existingInputs )
394
371
395
372
if err := d .Set ("input" , newInputs ); err != nil {
396
373
return diag .FromErr (err )
@@ -414,3 +391,34 @@ func resourceIntegrationPolicyDelete(ctx context.Context, d *schema.ResourceData
414
391
415
392
return diags
416
393
}
394
+
395
+ // sortInputs will sort the 'incoming' list of input definitions based on
396
+ // the order of inputs defined in the 'existing' list. Inputs not present in
397
+ // 'existing' will be placed at the end of the list. Inputs are identified by
398
+ // their ID ('input_id'). The 'incoming' slice will be sorted in-place.
399
+ func sortInputs (incoming []any , existing []any ) {
400
+ idToIndex := make (map [string ]int , len (existing ))
401
+ for i , v := range existing {
402
+ inputData , _ := v .(map [string ]any )
403
+ inputID , _ := inputData ["input_id" ].(string )
404
+ idToIndex [inputID ] = i
405
+ }
406
+
407
+ sort .Slice (incoming , func (i , j int ) bool {
408
+ iInput , _ := incoming [i ].(map [string ]any )
409
+ iID , _ := iInput ["input_id" ].(string )
410
+ iIdx , ok := idToIndex [iID ]
411
+ if ! ok {
412
+ return false
413
+ }
414
+
415
+ jInput , _ := incoming [j ].(map [string ]any )
416
+ jID , _ := jInput ["input_id" ].(string )
417
+ jIdx , ok := idToIndex [jID ]
418
+ if ! ok {
419
+ return true
420
+ }
421
+
422
+ return iIdx < jIdx
423
+ })
424
+ }
0 commit comments