@@ -16,6 +16,7 @@ const (
1616 tagBrevRefID = "brev-ref-id"
1717 tagBrevVPCID = "brev-vpc-id"
1818 tagBrevSubnetType = "brev-subnet-type"
19+ tagBrevCloudSDK = "brev-cloud-sdk"
1920
2021 natGatewayDeleteWaitInterval = 10 * time .Second
2122 natGatewayCreateWaitInterval = 10 * time .Second
@@ -71,6 +72,7 @@ func createVPC(ctx context.Context, awsClient *ec2.Client, name string, cidrBloc
7172 tags := makeTags (map [string ]string {
7273 "Name" : name ,
7374 tagBrevRefID : brevRefID ,
75+ "CreatedBy" : tagBrevCloudSDK ,
7476 })
7577
7678 input := & ec2.CreateVpcInput {
@@ -125,6 +127,7 @@ func createInternetGateway(ctx context.Context, awsClient *ec2.Client, vpc *type
125127 tags := makeTags (map [string ]string {
126128 "Name" : fmt .Sprintf ("%s-public" , * vpc .VpcId ),
127129 tagBrevVPCID : * vpc .VpcId ,
130+ "CreatedBy" : tagBrevCloudSDK ,
128131 })
129132
130133 createInput := & ec2.CreateInternetGatewayInput {
@@ -208,8 +211,10 @@ func createCompleteVPC(ctx context.Context, awsClient *ec2.Client, args v1.Creat
208211
209212func createPublicSubnet (ctx context.Context , awsClient * ec2.Client , vpc * types.Vpc , args v1.CreateSubnetArgs ) (* types.Subnet , error ) {
210213 tags := makeTags (map [string ]string {
214+ "Name" : fmt .Sprintf ("%s-public" , * vpc .VpcId ),
211215 tagBrevVPCID : * vpc .VpcId ,
212216 tagBrevSubnetType : string (args .Type ),
217+ "CreatedBy" : tagBrevCloudSDK ,
213218 })
214219 input := & ec2.CreateSubnetInput {
215220 VpcId : vpc .VpcId ,
@@ -280,6 +285,7 @@ func getOrCreatePublicRouteTable(ctx context.Context, awsClient *ec2.Client, vpc
280285 tags := makeTags (map [string ]string {
281286 "Name" : rtNameTag ,
282287 tagBrevVPCID : * vpc .VpcId ,
288+ "CreatedBy" : tagBrevCloudSDK ,
283289 })
284290 input := & ec2.CreateRouteTableInput {
285291 VpcId : aws .String (* vpc .VpcId ),
@@ -348,7 +354,8 @@ func getOrCreateInternetGateway(ctx context.Context, awsClient *ec2.Client, vpc
348354
349355 // If there is no internet gateway, create one
350356 tags := makeTags (map [string ]string {
351- "Name" : igwNameTag ,
357+ "Name" : igwNameTag ,
358+ "CreatedBy" : tagBrevCloudSDK ,
352359 })
353360 input := & ec2.CreateInternetGatewayInput {
354361 TagSpecifications : []types.TagSpecification {
@@ -368,8 +375,10 @@ func getOrCreateInternetGateway(ctx context.Context, awsClient *ec2.Client, vpc
368375
369376func createPrivateSubnet (ctx context.Context , awsClient * ec2.Client , vpc * types.Vpc , natGatewaySubnet * types.Subnet , args v1.CreateSubnetArgs ) (* types.Subnet , error ) {
370377 subnetTags := makeTags (map [string ]string {
378+ "Name" : fmt .Sprintf ("%s-private" , * vpc .VpcId ),
371379 tagBrevVPCID : * vpc .VpcId ,
372380 tagBrevSubnetType : string (args .Type ),
381+ "CreatedBy" : tagBrevCloudSDK ,
373382 })
374383 createSubnetInput := & ec2.CreateSubnetInput {
375384 VpcId : vpc .VpcId ,
@@ -397,6 +406,7 @@ func createPrivateSubnet(ctx context.Context, awsClient *ec2.Client, vpc *types.
397406 routeTableTags := makeTags (map [string ]string {
398407 "Name" : fmt .Sprintf ("%s-private" , * vpc .VpcId ),
399408 tagBrevVPCID : * vpc .VpcId ,
409+ "CreatedBy" : tagBrevCloudSDK ,
400410 })
401411 createRouteTableInput := & ec2.CreateRouteTableInput {
402412 VpcId : aws .String (* vpc .VpcId ),
@@ -449,6 +459,7 @@ func createNatGateway(ctx context.Context, awsClient *ec2.Client, vpc *types.Vpc
449459 natGatewayTags := makeTags (map [string ]string {
450460 "Name" : fmt .Sprintf ("%s-nat" , * vpc .VpcId ),
451461 tagBrevVPCID : * vpc .VpcId ,
462+ "CreatedBy" : tagBrevCloudSDK ,
452463 })
453464 createNatGatewayInput := & ec2.CreateNatGatewayInput {
454465 SubnetId : subnet .SubnetId ,
@@ -497,14 +508,14 @@ func (c *AWSClient) GetVPC(ctx context.Context, args v1.GetVPCArgs) (*v1.VPC, er
497508 return nil , err
498509 }
499510
500- nameTag := ""
501- refIDTag := ""
511+ brevVPCName := ""
512+ brevRefID := ""
502513 for _ , tag := range awsVPC .Tags {
503514 if * tag .Key == "Name" {
504- nameTag = * tag .Value
515+ brevVPCName = * tag .Value
505516 }
506517 if * tag .Key == tagBrevRefID {
507- refIDTag = * tag .Value
518+ brevRefID = * tag .Value
508519 }
509520 }
510521
@@ -513,16 +524,22 @@ func (c *AWSClient) GetVPC(ctx context.Context, args v1.GetVPCArgs) (*v1.VPC, er
513524 return nil , err
514525 }
515526
527+ subnets , err := getVPCSubnets (ctx , awsClient , awsVPC )
528+ if err != nil {
529+ return nil , err
530+ }
531+
516532 return & v1.VPC {
517- RefID : refIDTag ,
518- Name : nameTag ,
533+ RefID : brevRefID ,
534+ Name : brevVPCName ,
519535 Location : args .Location ,
520536 CloudID : * awsVPC .VpcId ,
521537 CloudCredRefID : c .GetReferenceID (),
522538 Provider : CloudProviderID ,
523539 Cloud : CloudProviderID ,
524540 CidrBlock : * awsVPC .CidrBlock ,
525541 Status : status ,
542+ Subnets : subnets ,
526543 }, nil
527544}
528545
@@ -586,6 +603,42 @@ func getVPCStatus(ctx context.Context, awsClient *ec2.Client, awsVPC *types.Vpc)
586603 return v1 .VPCStatusAvailable , nil
587604}
588605
606+ func getVPCSubnets (ctx context.Context , awsClient * ec2.Client , awsVPC * types.Vpc ) ([]v1.Subnet , error ) {
607+ describeSubnetsOutput , err := awsClient .DescribeSubnets (ctx , & ec2.DescribeSubnetsInput {
608+ Filters : []types.Filter {
609+ {
610+ Name : aws .String ("vpc-id" ),
611+ Values : []string {* awsVPC .VpcId },
612+ },
613+ },
614+ })
615+ if err != nil {
616+ return nil , err
617+ }
618+
619+ subnets := make ([]v1.Subnet , 0 )
620+ for _ , subnet := range describeSubnetsOutput .Subnets {
621+ var subnetType v1.SubnetType
622+
623+ // Get subnet type tag
624+ for _ , tag := range subnet .Tags {
625+ if * tag .Key == tagBrevSubnetType {
626+ subnetType = v1 .SubnetType (* tag .Value )
627+ break
628+ }
629+ }
630+
631+ subnets = append (subnets , v1.Subnet {
632+ CloudID : * subnet .SubnetId ,
633+ VPCID : * awsVPC .VpcId ,
634+ Location : * subnet .AvailabilityZone ,
635+ CidrBlock : * subnet .CidrBlock ,
636+ Type : subnetType ,
637+ })
638+ }
639+ return subnets , nil
640+ }
641+
589642func (c * AWSClient ) DeleteVPC (ctx context.Context , args v1.DeleteVPCArgs ) error {
590643 // Create the AWS client in the specified region
591644 awsClient := ec2 .NewFromConfig (c .awsConfig , func (o * ec2.Options ) {
0 commit comments