|
| 1 | +package awshandler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/Cloud-Code-AI/cloudstate/services/utils" |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 12 | +) |
| 13 | + |
| 14 | +type vpcInfo struct { |
| 15 | + VpcEndpoints []types.VpcEndpoint `json:"vpc_endpoints"` |
| 16 | + Vpcs []types.Vpc `json:"vpcs"` |
| 17 | + Subnets []types.Subnet `json:"subnets"` |
| 18 | + RouteTables []types.RouteTable `json:"route_table"` |
| 19 | +} |
| 20 | + |
| 21 | +// Gets all the EC2 instance for a given regions and |
| 22 | +// stores the results in output/{region}/ec2/instances.json file |
| 23 | +func ListVpcFn(sdkConfig aws.Config, parentpath string) { |
| 24 | + const maxItems = 50 |
| 25 | + |
| 26 | + // Create EC2 service client |
| 27 | + client := ec2.NewFromConfig(sdkConfig) |
| 28 | + vpcData := vpcInfo{ |
| 29 | + VpcEndpoints: getVpcEndpoints(client), |
| 30 | + Vpcs: getVpcs(client), |
| 31 | + Subnets: getSubnets(client), |
| 32 | + RouteTables: getRouteTable(client), |
| 33 | + } |
| 34 | + |
| 35 | + const ( |
| 36 | + path = "/vpc/data.json" |
| 37 | + ) |
| 38 | + |
| 39 | + stats := addVPCStats(vpcData) |
| 40 | + output := BasicTemplate{ |
| 41 | + Data: vpcData, |
| 42 | + Stats: stats, |
| 43 | + } |
| 44 | + |
| 45 | + filepath := parentpath + sdkConfig.Region + path |
| 46 | + |
| 47 | + err := utils.WriteJSONToFile(filepath, output) |
| 48 | + if err != nil { |
| 49 | + fmt.Println("Error writing lambda function lists") |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +func addVPCStats(i vpcInfo) interface{} { |
| 55 | + s := make(map[string]float64) |
| 56 | + s["vpcs"] = float64(len(i.Vpcs)) |
| 57 | + s["vpc_endpoints"] = float64(len(i.VpcEndpoints)) |
| 58 | + s["subnets"] = float64(len(i.Subnets)) |
| 59 | + s["route_tables"] = float64(len(i.RouteTables)) |
| 60 | + return s |
| 61 | +} |
| 62 | + |
| 63 | +func getVpcEndpoints(client *ec2.Client) []types.VpcEndpoint { |
| 64 | + var vpc_endpoints []types.VpcEndpoint |
| 65 | + result, err := client.DescribeVpcEndpoints(context.TODO(), &ec2.DescribeVpcEndpointsInput{}) |
| 66 | + if err != nil { |
| 67 | + log.Fatalf("Unable to retrieve instances, %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + if err != nil { |
| 71 | + log.Printf("Couldn't list VPC endpoints. Here's why: %v\n", err) |
| 72 | + } else { |
| 73 | + vpc_endpoints = result.VpcEndpoints |
| 74 | + for result.NextToken != nil { |
| 75 | + result, err = client.DescribeVpcEndpoints(context.TODO(), &ec2.DescribeVpcEndpointsInput{ |
| 76 | + MaxResults: aws.Int32(1000), |
| 77 | + NextToken: result.NextToken, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + log.Printf("Couldn't list VPC endpoints. Here's why: %v\n", err) |
| 81 | + break |
| 82 | + } |
| 83 | + vpc_endpoints = append(vpc_endpoints, result.VpcEndpoints...) |
| 84 | + } |
| 85 | + } |
| 86 | + return vpc_endpoints |
| 87 | +} |
| 88 | + |
| 89 | +func getVpcs(client *ec2.Client) []types.Vpc { |
| 90 | + var vpcs []types.Vpc |
| 91 | + result, err := client.DescribeVpcs(context.TODO(), &ec2.DescribeVpcsInput{}) |
| 92 | + if err != nil { |
| 93 | + log.Fatalf("Unable to list VPCs, %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + log.Printf("Couldn't list VPCs. Here's why: %v\n", err) |
| 98 | + } else { |
| 99 | + vpcs = result.Vpcs |
| 100 | + for result.NextToken != nil { |
| 101 | + result, err = client.DescribeVpcs(context.TODO(), &ec2.DescribeVpcsInput{ |
| 102 | + MaxResults: aws.Int32(1000), |
| 103 | + NextToken: result.NextToken, |
| 104 | + }) |
| 105 | + if err != nil { |
| 106 | + log.Printf("Couldn't list VPCs. Here's why: %v\n", err) |
| 107 | + break |
| 108 | + } |
| 109 | + vpcs = append(vpcs, result.Vpcs...) |
| 110 | + } |
| 111 | + } |
| 112 | + return vpcs |
| 113 | +} |
| 114 | + |
| 115 | +func getSubnets(client *ec2.Client) []types.Subnet { |
| 116 | + var subnets []types.Subnet |
| 117 | + result, err := client.DescribeSubnets(context.TODO(), &ec2.DescribeSubnetsInput{}) |
| 118 | + if err != nil { |
| 119 | + log.Fatalf("Unable to retrieve Subnets, %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + log.Printf("Couldn't list VPC Subnets. Here's why: %v\n", err) |
| 124 | + } else { |
| 125 | + subnets = result.Subnets |
| 126 | + for result.NextToken != nil { |
| 127 | + result, err = client.DescribeSubnets(context.TODO(), &ec2.DescribeSubnetsInput{ |
| 128 | + MaxResults: aws.Int32(1000), |
| 129 | + NextToken: result.NextToken, |
| 130 | + }) |
| 131 | + if err != nil { |
| 132 | + log.Printf("Couldn't list VPC Subnets. Here's why: %v\n", err) |
| 133 | + break |
| 134 | + } |
| 135 | + subnets = append(subnets, result.Subnets...) |
| 136 | + } |
| 137 | + } |
| 138 | + return subnets |
| 139 | +} |
| 140 | + |
| 141 | +func getRouteTable(client *ec2.Client) []types.RouteTable { |
| 142 | + var route_tables []types.RouteTable |
| 143 | + result, err := client.DescribeRouteTables(context.TODO(), &ec2.DescribeRouteTablesInput{}) |
| 144 | + if err != nil { |
| 145 | + log.Fatalf("Unable to retrieve Subnets, %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + if err != nil { |
| 149 | + log.Printf("Couldn't list VPC Subnets. Here's why: %v\n", err) |
| 150 | + } else { |
| 151 | + route_tables = result.RouteTables |
| 152 | + for result.NextToken != nil { |
| 153 | + result, err = client.DescribeRouteTables(context.TODO(), &ec2.DescribeRouteTablesInput{ |
| 154 | + MaxResults: aws.Int32(1000), |
| 155 | + NextToken: result.NextToken, |
| 156 | + }) |
| 157 | + if err != nil { |
| 158 | + log.Printf("Couldn't list VPC Subnets. Here's why: %v\n", err) |
| 159 | + break |
| 160 | + } |
| 161 | + route_tables = append(route_tables, result.RouteTables...) |
| 162 | + } |
| 163 | + } |
| 164 | + return route_tables |
| 165 | +} |
0 commit comments