@@ -284,7 +284,12 @@ func (e *ProjectEnvironmentVariables) toCreateEnvironmentVariablesRequest(ctx co
284
284
// convertResponseToProjectEnvironmentVariables is used to populate terraform state based on an API response.
285
285
// Where possible, values from the API response are used to populate state. If not possible,
286
286
// values from plan are used.
287
- func convertResponseToProjectEnvironmentVariables (ctx context.Context , response []client.EnvironmentVariable , plan ProjectEnvironmentVariables ) (ProjectEnvironmentVariables , diag.Diagnostics ) {
287
+ func convertResponseToProjectEnvironmentVariables (
288
+ ctx context.Context ,
289
+ response []client.EnvironmentVariable ,
290
+ plan ProjectEnvironmentVariables ,
291
+ unchanged []EnvironmentItem ,
292
+ ) (ProjectEnvironmentVariables , diag.Diagnostics ) {
288
293
environment , diags := plan .environment (ctx )
289
294
if diags .HasError () {
290
295
return ProjectEnvironmentVariables {}, diags
@@ -344,20 +349,7 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
344
349
alreadyPresent [e .ID ] = struct {}{}
345
350
346
351
env = append (env , types .ObjectValueMust (
347
- map [string ]attr.Type {
348
- "key" : types .StringType ,
349
- "value" : types .StringType ,
350
- "target" : types.SetType {
351
- ElemType : types .StringType ,
352
- },
353
- "custom_environment_ids" : types.SetType {
354
- ElemType : types .StringType ,
355
- },
356
- "git_branch" : types .StringType ,
357
- "id" : types .StringType ,
358
- "sensitive" : types .BoolType ,
359
- "comment" : types .StringType ,
360
- },
352
+ envVariableElemType .AttrTypes ,
361
353
map [string ]attr.Value {
362
354
"key" : types .StringValue (e .Key ),
363
355
"value" : value ,
@@ -371,6 +363,10 @@ func convertResponseToProjectEnvironmentVariables(ctx context.Context, response
371
363
))
372
364
}
373
365
366
+ for _ , e := range unchanged {
367
+ env = append (env , e .toAttrValue ())
368
+ }
369
+
374
370
return ProjectEnvironmentVariables {
375
371
TeamID : toTeamID (plan .TeamID .ValueString ()),
376
372
ProjectID : plan .ProjectID ,
@@ -410,7 +406,7 @@ func (r *projectEnvironmentVariablesResource) Create(ctx context.Context, req re
410
406
)
411
407
}
412
408
413
- result , diags := convertResponseToProjectEnvironmentVariables (ctx , created , plan )
409
+ result , diags := convertResponseToProjectEnvironmentVariables (ctx , created , plan , nil )
414
410
if diags .HasError () {
415
411
resp .Diagnostics .Append (diags ... )
416
412
return
@@ -475,7 +471,7 @@ func (r *projectEnvironmentVariablesResource) Read(ctx context.Context, req reso
475
471
}
476
472
}
477
473
478
- result , diags := convertResponseToProjectEnvironmentVariables (ctx , toUse , state )
474
+ result , diags := convertResponseToProjectEnvironmentVariables (ctx , toUse , state , nil )
479
475
if diags .HasError () {
480
476
resp .Diagnostics .Append (diags ... )
481
477
return
@@ -530,6 +526,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
530
526
}
531
527
532
528
var toRemove []EnvironmentItem
529
+ var unchanged []EnvironmentItem
533
530
for _ , e := range stateEnvs {
534
531
plannedEnv , ok := plannedEnvsByID [e .ID .ValueString ()]
535
532
if ! ok {
@@ -539,11 +536,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
539
536
if ! plannedEnv .equal (& e ) {
540
537
toRemove = append (toRemove , e )
541
538
toAdd = append (toAdd , plannedEnv )
539
+ continue
542
540
}
541
+ unchanged = append (unchanged , e )
543
542
}
544
543
545
- tflog .Debug (ctx , "Removing environment variables" , map [string ]interface {}{"to_remove" : toRemove })
546
- tflog .Debug (ctx , "Adding environment variables" , map [string ]interface {}{"to_add" : toAdd })
544
+ tflog .Info (ctx , "Removing environment variables" , map [string ]interface {}{"to_remove" : toRemove })
545
+ tflog .Info (ctx , "Adding environment variables" , map [string ]interface {}{"to_add" : toAdd })
547
546
548
547
for _ , v := range toRemove {
549
548
err := r .client .DeleteEnvironmentVariable (ctx , state .ProjectID .ValueString (), state .TeamID .ValueString (), v .ID .ValueString ())
@@ -574,7 +573,7 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
574
573
resp .Diagnostics .Append (diags ... )
575
574
return
576
575
}
577
- tflog .Debug (ctx , "create request" , map [string ]any {
576
+ tflog .Info (ctx , "create request" , map [string ]any {
578
577
"request" : request ,
579
578
})
580
579
response , err = r .client .CreateEnvironmentVariables (ctx , request )
@@ -585,10 +584,13 @@ func (r *projectEnvironmentVariablesResource) Update(ctx context.Context, req re
585
584
)
586
585
return
587
586
}
588
- } else {
589
587
}
590
588
591
- result , diags := convertResponseToProjectEnvironmentVariables (ctx , response , plan )
589
+ tflog .Info (ctx , "project env var response" , map [string ]any {
590
+ "response" : response ,
591
+ })
592
+
593
+ result , diags := convertResponseToProjectEnvironmentVariables (ctx , response , plan , unchanged )
592
594
if diags .HasError () {
593
595
resp .Diagnostics .Append (diags ... )
594
596
return
0 commit comments