@@ -4,20 +4,24 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
+ "reflect"
7
8
"time"
8
9
9
10
"github.com/aws/aws-sdk-go/aws"
10
11
"github.com/aws/aws-sdk-go/service/appstream"
11
12
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
13
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15
17
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
16
18
"github.com/hashicorp/terraform-provider-aws/internal/conns"
19
+ "github.com/hashicorp/terraform-provider-aws/internal/create"
17
20
"github.com/hashicorp/terraform-provider-aws/internal/flex"
18
21
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
19
22
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
20
23
"github.com/hashicorp/terraform-provider-aws/internal/verify"
24
+ "github.com/hashicorp/terraform-provider-aws/names"
21
25
)
22
26
23
27
func ResourceFleet () * schema.Resource {
@@ -26,9 +30,16 @@ func ResourceFleet() *schema.Resource {
26
30
ReadWithoutTimeout : resourceFleetRead ,
27
31
UpdateWithoutTimeout : resourceFleetUpdate ,
28
32
DeleteWithoutTimeout : resourceFleetDelete ,
33
+
29
34
Importer : & schema.ResourceImporter {
30
35
StateContext : schema .ImportStatePassthroughContext ,
31
36
},
37
+
38
+ CustomizeDiff : customdiff .Sequence (
39
+ resourceFleetCustDiff ,
40
+ verify .SetTagsDiff ,
41
+ ),
42
+
32
43
Schema : map [string ]* schema.Schema {
33
44
"arn" : {
34
45
Type : schema .TypeString ,
@@ -91,10 +102,12 @@ func ResourceFleet() *schema.Resource {
91
102
"directory_name" : {
92
103
Type : schema .TypeString ,
93
104
Optional : true ,
105
+ Computed : true ,
94
106
},
95
107
"organizational_unit_distinguished_name" : {
96
108
Type : schema .TypeString ,
97
109
Optional : true ,
110
+ Computed : true ,
98
111
},
99
112
},
100
113
},
@@ -327,17 +340,25 @@ func resourceFleetRead(ctx context.Context, d *schema.ResourceData, meta interfa
327
340
328
341
d .Set ("arn" , fleet .Arn )
329
342
330
- if err = d .Set ("compute_capacity" , flattenComputeCapacity (fleet .ComputeCapacityStatus )); err != nil {
331
- return diag .FromErr (fmt .Errorf ("error setting `%s` for AppStream Fleet (%s): %w" , "compute_capacity" , d .Id (), err ))
343
+ if fleet .ComputeCapacityStatus != nil {
344
+ if err = d .Set ("compute_capacity" , []interface {}{flattenComputeCapacity (fleet .ComputeCapacityStatus )}); err != nil {
345
+ return create .DiagSettingError (names .AppStream , "Fleet" , d .Id (), "compute_capacity" , err )
346
+ }
347
+ } else {
348
+ d .Set ("compute_capacity" , nil )
332
349
}
333
350
334
351
d .Set ("created_time" , aws .TimeValue (fleet .CreatedTime ).Format (time .RFC3339 ))
335
352
d .Set ("description" , fleet .Description )
336
353
d .Set ("display_name" , fleet .DisplayName )
337
354
d .Set ("disconnect_timeout_in_seconds" , fleet .DisconnectTimeoutInSeconds )
338
355
339
- if err = d .Set ("domain_join_info" , flattenDomainInfo (fleet .DomainJoinInfo )); err != nil {
340
- return diag .FromErr (fmt .Errorf ("error setting `%s` for AppStream Fleet (%s): %w" , "domain_join_info" , d .Id (), err ))
356
+ if fleet .DomainJoinInfo != nil {
357
+ if err = d .Set ("domain_join_info" , []interface {}{flattenDomainInfo (fleet .DomainJoinInfo )}); err != nil {
358
+ return create .DiagSettingError (names .AppStream , "Fleet" , d .Id (), "domain_join_info" , err )
359
+ }
360
+ } else {
361
+ d .Set ("domain_join_info" , nil )
341
362
}
342
363
343
364
d .Set ("idle_disconnect_timeout_in_seconds" , fleet .IdleDisconnectTimeoutInSeconds )
@@ -352,8 +373,12 @@ func resourceFleetRead(ctx context.Context, d *schema.ResourceData, meta interfa
352
373
d .Set ("state" , fleet .State )
353
374
d .Set ("stream_view" , fleet .StreamView )
354
375
355
- if err = d .Set ("vpc_config" , flattenVPCConfig (fleet .VpcConfig )); err != nil {
356
- return diag .FromErr (fmt .Errorf ("error setting `%s` for AppStream Fleet (%s): %w" , "vpc_config" , d .Id (), err ))
376
+ if fleet .VpcConfig != nil {
377
+ if err = d .Set ("vpc_config" , []interface {}{flattenVPCConfig (fleet .VpcConfig )}); err != nil {
378
+ return create .DiagSettingError (names .AppStream , "Fleet" , d .Id (), "vpc_config" , err )
379
+ }
380
+ } else {
381
+ d .Set ("vpc_config" , nil )
357
382
}
358
383
359
384
tg , err := conn .ListTagsForResource (& appstream.ListTagsForResourceInput {
@@ -525,6 +550,17 @@ func resourceFleetDelete(ctx context.Context, d *schema.ResourceData, meta inter
525
550
return nil
526
551
}
527
552
553
+ func resourceFleetCustDiff (_ context.Context , diff * schema.ResourceDiff , meta interface {}) error {
554
+ if diff .HasChange ("domain_join_info" ) {
555
+ o , n := diff .GetChange ("domain_join_info" )
556
+
557
+ if reflect .DeepEqual (expandDomainJoinInfo (o .([]interface {})), expandDomainJoinInfo (n .([]interface {}))) {
558
+ diff .Clear ("domain_join_info" )
559
+ }
560
+ }
561
+ return nil
562
+ }
563
+
528
564
func expandComputeCapacity (tfList []interface {}) * appstream.ComputeCapacity {
529
565
if len (tfList ) == 0 {
530
566
return nil
@@ -537,21 +573,41 @@ func expandComputeCapacity(tfList []interface{}) *appstream.ComputeCapacity {
537
573
apiObject .DesiredInstances = aws .Int64 (int64 (v .(int )))
538
574
}
539
575
576
+ if reflect .DeepEqual (& appstream.ComputeCapacity {}, apiObject ) {
577
+ return nil
578
+ }
579
+
540
580
return apiObject
541
581
}
542
582
543
- func flattenComputeCapacity (apiObject * appstream.ComputeCapacityStatus ) [ ]interface {} {
583
+ func flattenComputeCapacity (apiObject * appstream.ComputeCapacityStatus ) map [ string ]interface {} {
544
584
if apiObject == nil {
545
585
return nil
546
586
}
547
587
548
- tfList := map [string ]interface {}{}
549
- tfList ["desired_instances" ] = aws .Int64Value (apiObject .Desired )
550
- tfList ["available" ] = aws .Int64Value (apiObject .Available )
551
- tfList ["in_use" ] = aws .Int64Value (apiObject .InUse )
552
- tfList ["running" ] = aws .Int64Value (apiObject .Running )
588
+ tfMap := map [string ]interface {}{}
589
+
590
+ if v := apiObject .Desired ; v != nil {
591
+ tfMap ["desired_instances" ] = aws .Int64Value (v )
592
+ }
593
+
594
+ if v := apiObject .Available ; v != nil {
595
+ tfMap ["available" ] = aws .Int64Value (v )
596
+ }
597
+
598
+ if v := apiObject .InUse ; v != nil {
599
+ tfMap ["in_use" ] = aws .Int64Value (v )
600
+ }
601
+
602
+ if v := apiObject .Running ; v != nil {
603
+ tfMap ["running" ] = aws .Int64Value (v )
604
+ }
553
605
554
- return []interface {}{tfList }
606
+ if reflect .DeepEqual (map [string ]interface {}{}, tfMap ) {
607
+ return nil
608
+ }
609
+
610
+ return tfMap
555
611
}
556
612
557
613
func expandDomainJoinInfo (tfList []interface {}) * appstream.DomainJoinInfo {
@@ -575,19 +631,33 @@ func expandDomainJoinInfo(tfList []interface{}) *appstream.DomainJoinInfo {
575
631
apiObject .OrganizationalUnitDistinguishedName = aws .String (v .(string ))
576
632
}
577
633
634
+ if reflect .DeepEqual (& appstream.DomainJoinInfo {}, apiObject ) {
635
+ return nil
636
+ }
637
+
578
638
return apiObject
579
639
}
580
640
581
- func flattenDomainInfo (apiObject * appstream.DomainJoinInfo ) [ ]interface {} {
641
+ func flattenDomainInfo (apiObject * appstream.DomainJoinInfo ) map [ string ]interface {} {
582
642
if apiObject == nil {
583
643
return nil
584
644
}
585
645
586
- tfList := map [string ]interface {}{}
587
- tfList ["directory_name" ] = aws .StringValue (apiObject .DirectoryName )
588
- tfList ["organizational_unit_distinguished_name" ] = aws .StringValue (apiObject .OrganizationalUnitDistinguishedName )
646
+ tfMap := map [string ]interface {}{}
647
+
648
+ if v := apiObject .DirectoryName ; v != nil && aws .StringValue (v ) != "" {
649
+ tfMap ["directory_name" ] = aws .StringValue (v )
650
+ }
651
+
652
+ if v := apiObject .OrganizationalUnitDistinguishedName ; v != nil && aws .StringValue (v ) != "" {
653
+ tfMap ["organizational_unit_distinguished_name" ] = aws .StringValue (v )
654
+ }
655
+
656
+ if reflect .DeepEqual (map [string ]interface {}{}, tfMap ) {
657
+ return nil
658
+ }
589
659
590
- return [] interface {}{ tfList }
660
+ return tfMap
591
661
}
592
662
593
663
func expandVPCConfig (tfList []interface {}) * appstream.VpcConfig {
@@ -601,21 +671,36 @@ func expandVPCConfig(tfList []interface{}) *appstream.VpcConfig {
601
671
if v , ok := tfMap ["security_group_ids" ]; ok {
602
672
apiObject .SecurityGroupIds = flex .ExpandStringList (v .([]interface {}))
603
673
}
674
+
604
675
if v , ok := tfMap ["subnet_ids" ]; ok {
605
676
apiObject .SubnetIds = flex .ExpandStringList (v .([]interface {}))
606
677
}
607
678
679
+ if reflect .DeepEqual (& appstream.VpcConfig {}, apiObject ) {
680
+ return nil
681
+ }
682
+
608
683
return apiObject
609
684
}
610
685
611
- func flattenVPCConfig (apiObject * appstream.VpcConfig ) [ ]interface {} {
686
+ func flattenVPCConfig (apiObject * appstream.VpcConfig ) map [ string ]interface {} {
612
687
if apiObject == nil {
613
688
return nil
614
689
}
615
690
616
- tfList := map [string ]interface {}{}
617
- tfList ["security_group_ids" ] = aws .StringValueSlice (apiObject .SecurityGroupIds )
618
- tfList ["subnet_ids" ] = aws .StringValueSlice (apiObject .SubnetIds )
691
+ tfMap := map [string ]interface {}{}
692
+
693
+ if v := apiObject .SecurityGroupIds ; v != nil {
694
+ tfMap ["security_group_ids" ] = aws .StringValueSlice (v )
695
+ }
696
+
697
+ if v := apiObject .SubnetIds ; v != nil {
698
+ tfMap ["subnet_ids" ] = aws .StringValueSlice (v )
699
+ }
700
+
701
+ if reflect .DeepEqual (map [string ]interface {}{}, tfMap ) {
702
+ return nil
703
+ }
619
704
620
- return [] interface {}{ tfList }
705
+ return tfMap
621
706
}
0 commit comments