-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAws.go
116 lines (105 loc) · 2.93 KB
/
Aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"log"
)
//var awsCredentials aws.Auth
var awsRegion aws.Config //set with aws.NewConfig().WithRegion("us-west-2") in config.go
type awsResource struct {
ID string
Name string
}
func awsExtractName(tags []*ec2.Tag) (name string) {
for _, tag := range tags {
if *tag.Key == "Name" {
name = *tag.Value
break
}
}
return
}
func awsClient() (client *ec2.EC2) {
client = ec2.New(session.New(), &awsRegion) //todo, error handling here
return
}
func awsSubnets(vpcid string) []awsResource {
client := awsClient()
params := ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{&vpcid},
},
},
}
response, err := client.DescribeSubnets(¶ms)
if err != nil {
log.Fatalf("Issue encountered retrieving subnets, %s",err)
}
subnets := &response.Subnets
var ReturnedSubs []awsResource
for _, subnet := range *subnets {
subnetID := *subnet.SubnetId
subnetName := awsExtractName(subnet.Tags)
subnetDetails := awsResource{ID: subnetID, Name: subnetName}
ReturnedSubs = append(ReturnedSubs, subnetDetails)
}
return ReturnedSubs
}
func awsVpc(vpcid string) awsResource {
client := awsClient()
vpcids := []*string{&vpcid}
response, err := client.DescribeVpcs(&ec2.DescribeVpcsInput{VpcIds: vpcids})
if err != nil {
log.Fatalf("Ecountered issue retrieving VPCs, %s",err) // todo, better error handling
}
vpc := response.Vpcs[0]
vpcName := awsExtractName(vpc.Tags)
vpcDetails := awsResource{ID: *vpc.VpcId, Name: vpcName}
return vpcDetails
}
func awsIgw(vpcid string) (igwDetails awsResource) {
client := awsClient()
params := ec2.DescribeInternetGatewaysInput{
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.vpc-id"),
Values: []*string{&vpcid},
},
},
}
response, err := client.DescribeInternetGateways(¶ms)
if err != nil {
log.Fatalf("Ecountered issue retrieving Internet Gateways, %s", err)
}
igw := response.InternetGateways[0]
igwName := awsExtractName(igw.Tags)
igwDetails = awsResource{ID: *igw.InternetGatewayId, Name: igwName}
return
}
// todo, extract filtering out to another function
func awsRouteTable(vpcid string) (ReturnedRouteTables []awsResource) {
client := awsClient()
params := ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{&vpcid},
},
},
}
response, err := client.DescribeRouteTables(¶ms)
if err != nil {
log.Fatalf("Ecountered issue retrieving Route Table details, %s",err)
}
routeTables := response.RouteTables
for _, routeTable := range routeTables {
routeTableID := routeTable.RouteTableId
routeTableName := awsExtractName(routeTable.Tags)
routeTableDetails := awsResource{ID: *routeTableID, Name: routeTableName}
ReturnedRouteTables = append(ReturnedRouteTables, routeTableDetails)
}
return
}