@@ -50,8 +50,8 @@ type EC2Behavior struct {
5050 DescribeCapacityReservationsOutput AtomicPtr [ec2.DescribeCapacityReservationsOutput ]
5151 DescribeImagesOutput AtomicPtr [ec2.DescribeImagesOutput ]
5252 DescribeLaunchTemplatesOutput AtomicPtr [ec2.DescribeLaunchTemplatesOutput ]
53- DescribeSubnetsOutput AtomicPtr [ ec2.DescribeSubnetsOutput ]
54- DescribeSecurityGroupsOutput AtomicPtr [ ec2.DescribeSecurityGroupsOutput ]
53+ DescribeSubnetsBehavior MockedFunction [ec2. DescribeSubnetsInput , ec2.DescribeSubnetsOutput ]
54+ DescribeSecurityGroupsBehavior MockedFunction [ec2. DescribeSecurityGroupsInput , ec2.DescribeSecurityGroupsOutput ]
5555 DescribeInstanceTypesOutput AtomicPtr [ec2.DescribeInstanceTypesOutput ]
5656 DescribeInstanceTypeOfferingsOutput AtomicPtr [ec2.DescribeInstanceTypeOfferingsOutput ]
5757 DescribeAvailabilityZonesOutput AtomicPtr [ec2.DescribeAvailabilityZonesOutput ]
@@ -67,6 +67,7 @@ type EC2Behavior struct {
6767 InsufficientCapacityPools atomic.Slice [CapacityPool ]
6868 NextError AtomicError
6969
70+ Subnets sync.Map
7071 LaunchTemplates sync.Map
7172 launchTemplatesToCapacityReservations sync.Map // map[lt-name]cr-id
7273}
@@ -88,8 +89,8 @@ var DefaultSupportedUsageClasses = []ec2types.UsageClassType{ec2types.UsageClass
8889func (e * EC2API ) Reset () {
8990 e .DescribeImagesOutput .Reset ()
9091 e .DescribeLaunchTemplatesOutput .Reset ()
91- e .DescribeSubnetsOutput .Reset ()
92- e .DescribeSecurityGroupsOutput .Reset ()
92+ e .DescribeSubnetsBehavior .Reset ()
93+ e .DescribeSecurityGroupsBehavior .Reset ()
9394 e .DescribeInstanceTypesOutput .Reset ()
9495 e .DescribeInstanceTypeOfferingsOutput .Reset ()
9596 e .DescribeAvailabilityZonesOutput .Reset ()
@@ -455,107 +456,109 @@ func (e *EC2API) DeleteLaunchTemplate(_ context.Context, input *ec2.DeleteLaunch
455456}
456457
457458func (e * EC2API ) DescribeSubnets (_ context.Context , input * ec2.DescribeSubnetsInput , _ ... func (* ec2.Options )) (* ec2.DescribeSubnetsOutput , error ) {
458- if ! e .NextError .IsNil () {
459- defer e .NextError .Reset ()
460- return nil , e .NextError .Get ()
461- }
462- if ! e .DescribeSubnetsOutput .IsNil () {
463- describeSubnetsOutput := e .DescribeSubnetsOutput .Clone ()
464- describeSubnetsOutput .Subnets = FilterDescribeSubnets (describeSubnetsOutput .Subnets , input .Filters )
465- return describeSubnetsOutput , nil
466- }
467- subnets := []ec2types.Subnet {
468- {
469- SubnetId : aws .String ("subnet-test1" ),
470- AvailabilityZone : aws .String ("test-zone-1a" ),
471- AvailabilityZoneId : aws .String ("tstz1-1a" ),
472- AvailableIpAddressCount : aws .Int32 (100 ),
473- MapPublicIpOnLaunch : aws .Bool (false ),
474- Tags : []ec2types.Tag {
475- {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-1" )},
476- {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
459+ return e .DescribeSubnetsBehavior .Invoke (input , func (input * ec2.DescribeSubnetsInput ) (* ec2.DescribeSubnetsOutput , error ) {
460+ output := & ec2.DescribeSubnetsOutput {}
461+ e .Subnets .Range (func (key , value any ) bool {
462+ subnet := value .(ec2types.Subnet )
463+ if lo .Contains (input .SubnetIds , lo .FromPtr (subnet .SubnetId )) || len (input .Filters ) != 0 && len (FilterDescribeSubnets ([]ec2types.Subnet {subnet }, input .Filters )) != 0 {
464+ output .Subnets = append (output .Subnets , subnet )
465+ }
466+ return true
467+ })
468+ if len (output .Subnets ) != 0 {
469+ return output , nil
470+ }
471+
472+ defaultSubnets := []ec2types.Subnet {
473+ {
474+ SubnetId : aws .String ("subnet-test1" ),
475+ AvailabilityZone : aws .String ("test-zone-1a" ),
476+ AvailabilityZoneId : aws .String ("tstz1-1a" ),
477+ AvailableIpAddressCount : aws .Int32 (100 ),
478+ MapPublicIpOnLaunch : aws .Bool (false ),
479+ Tags : []ec2types.Tag {
480+ {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-1" )},
481+ {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
482+ },
483+ VpcId : aws .String ("vpc-test1" ),
477484 },
478- },
479- {
480- SubnetId : aws .String ("subnet-test2" ),
481- AvailabilityZone : aws .String ("test-zone-1b" ),
482- AvailabilityZoneId : aws .String ("tstz1-1b" ),
483- AvailableIpAddressCount : aws .Int32 (100 ),
484- MapPublicIpOnLaunch : aws .Bool (true ),
485- Tags : []ec2types.Tag {
486- {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-2" )},
487- {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
485+ {
486+ SubnetId : aws .String ("subnet-test2" ),
487+ AvailabilityZone : aws .String ("test-zone-1b" ),
488+ AvailabilityZoneId : aws .String ("tstz1-1b" ),
489+ AvailableIpAddressCount : aws .Int32 (100 ),
490+ MapPublicIpOnLaunch : aws .Bool (true ),
491+ Tags : []ec2types.Tag {
492+ {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-2" )},
493+ {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
494+ },
495+ VpcId : aws .String ("vpc-test1" ),
488496 },
489- },
490- {
491- SubnetId : aws .String ("subnet-test3" ),
492- AvailabilityZone : aws .String ("test-zone-1c" ),
493- AvailabilityZoneId : aws .String ("tstz1-1c" ),
494- AvailableIpAddressCount : aws .Int32 (100 ),
495- Tags : []ec2types.Tag {
496- {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-3" )},
497- {Key : aws .String ("TestTag" )},
498- {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
497+ {
498+ SubnetId : aws .String ("subnet-test3" ),
499+ AvailabilityZone : aws .String ("test-zone-1c" ),
500+ AvailabilityZoneId : aws .String ("tstz1-1c" ),
501+ AvailableIpAddressCount : aws .Int32 (100 ),
502+ Tags : []ec2types.Tag {
503+ {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-3" )},
504+ {Key : aws .String ("TestTag" )},
505+ {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
506+ },
507+ VpcId : aws .String ("vpc-test1" ),
499508 },
500- },
501- {
502- SubnetId : aws .String ("subnet-test4" ),
503- AvailabilityZone : aws .String ("test-zone-1a-local" ),
504- AvailabilityZoneId : aws .String ("tstz1-1alocal" ),
505- AvailableIpAddressCount : aws .Int32 (100 ),
506- MapPublicIpOnLaunch : aws .Bool (true ),
507- Tags : []ec2types.Tag {
508- {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-4" )},
509+ {
510+ SubnetId : aws .String ("subnet-test4" ),
511+ AvailabilityZone : aws .String ("test-zone-1a-local" ),
512+ AvailabilityZoneId : aws .String ("tstz1-1alocal" ),
513+ AvailableIpAddressCount : aws .Int32 (100 ),
514+ MapPublicIpOnLaunch : aws .Bool (true ),
515+ Tags : []ec2types.Tag {
516+ {Key : aws .String ("Name" ), Value : aws .String ("test-subnet-4" )},
517+ },
518+ VpcId : aws .String ("vpc-test1" ),
509519 },
510- },
511- }
512- if len ( input . Filters ) == 0 {
513- return nil , fmt . Errorf ( "InvalidParameterValue: The filter 'null' is invalid" )
514- }
515- return & ec2. DescribeSubnetsOutput { Subnets : FilterDescribeSubnets ( subnets , input . Filters )}, nil
520+ }
521+ if len ( input . Filters ) == 0 {
522+ return nil , fmt . Errorf ( "InvalidParameterValue: The filter 'null' is invalid" )
523+ }
524+ return & ec2. DescribeSubnetsOutput { Subnets : FilterDescribeSubnets ( defaultSubnets , input . Filters )}, nil
525+ })
516526}
517527
518528func (e * EC2API ) DescribeSecurityGroups (_ context.Context , input * ec2.DescribeSecurityGroupsInput , _ ... func (* ec2.Options )) (* ec2.DescribeSecurityGroupsOutput , error ) {
519- if ! e .NextError .IsNil () {
520- defer e .NextError .Reset ()
521- return nil , e .NextError .Get ()
522- }
523- if ! e .DescribeSecurityGroupsOutput .IsNil () {
524- describeSecurityGroupsOutput := e .DescribeSecurityGroupsOutput .Clone ()
525- describeSecurityGroupsOutput .SecurityGroups = FilterDescribeSecurtyGroups (describeSecurityGroupsOutput .SecurityGroups , input .Filters )
526- return e .DescribeSecurityGroupsOutput .Clone (), nil
527- }
528- sgs := []ec2types.SecurityGroup {
529- {
530- GroupId : aws .String ("sg-test1" ),
531- GroupName : aws .String ("securityGroup-test1" ),
532- Tags : []ec2types.Tag {
533- {Key : aws .String ("Name" ), Value : aws .String ("test-security-group-1" )},
534- {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
529+ return e .DescribeSecurityGroupsBehavior .Invoke (input , func (input * ec2.DescribeSecurityGroupsInput ) (* ec2.DescribeSecurityGroupsOutput , error ) {
530+ defaultSecurityGroups := []ec2types.SecurityGroup {
531+ {
532+ GroupId : aws .String ("sg-test1" ),
533+ GroupName : aws .String ("securityGroup-test1" ),
534+ Tags : []ec2types.Tag {
535+ {Key : aws .String ("Name" ), Value : aws .String ("test-security-group-1" )},
536+ {Key : aws .String ("foo" ), Value : aws .String ("bar" )},
537+ },
535538 },
536- },
537- {
538- GroupId : aws .String ("sg -test2" ),
539- GroupName : aws . String ( "securityGroup-test2" ),
540- Tags : []ec2types. Tag {
541- {Key : aws .String ("Name " ), Value : aws .String ("test-security-group-2 " )},
542- { Key : aws . String ( "foo" ), Value : aws . String ( "bar" ) },
539+ {
540+ GroupId : aws . String ( "sg-test2" ),
541+ GroupName : aws .String ("securityGroup -test2" ),
542+ Tags : []ec2types. Tag {
543+ { Key : aws . String ( "Name" ), Value : aws . String ( "test-security-group-2" )},
544+ {Key : aws .String ("foo " ), Value : aws .String ("bar " )},
545+ },
543546 },
544- },
545- {
546- GroupId : aws .String ("sg -test3" ),
547- GroupName : aws . String ( "securityGroup-test3" ),
548- Tags : []ec2types. Tag {
549- {Key : aws .String ("Name" ), Value : aws . String ( "test-security-group-3 " )},
550- {Key : aws .String ("TestTag " )},
551- { Key : aws . String ( "foo" ), Value : aws . String ( "bar" ) },
547+ {
548+ GroupId : aws . String ( "sg-test3" ),
549+ GroupName : aws .String ("securityGroup -test3" ),
550+ Tags : []ec2types. Tag {
551+ { Key : aws . String ( "Name" ), Value : aws . String ( "test-security-group-3" )},
552+ {Key : aws .String ("TestTag " )},
553+ {Key : aws .String ("foo" ), Value : aws . String ( "bar " )},
554+ },
552555 },
553- },
554- }
555- if len ( input . Filters ) == 0 {
556- return nil , fmt . Errorf ( "InvalidParameterValue: The filter 'null' is invalid" )
557- }
558- return & ec2. DescribeSecurityGroupsOutput { SecurityGroups : FilterDescribeSecurtyGroups ( sgs , input . Filters )}, nil
556+ }
557+ if len ( input . Filters ) == 0 {
558+ return nil , fmt . Errorf ( "InvalidParameterValue: The filter 'null' is invalid" )
559+ }
560+ return & ec2. DescribeSecurityGroupsOutput { SecurityGroups : FilterDescribeSecurtyGroups ( defaultSecurityGroups , input . Filters )}, nil
561+ })
559562}
560563
561564func (e * EC2API ) DescribeAvailabilityZones (context.Context , * ec2.DescribeAvailabilityZonesInput , ... func (* ec2.Options )) (* ec2.DescribeAvailabilityZonesOutput , error ) {
0 commit comments