-
Notifications
You must be signed in to change notification settings - Fork 2
/
resources.go
173 lines (142 loc) · 3.85 KB
/
resources.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"net"
"os"
"strings"
"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/dynport/gocloud/aws/cloudformation"
"github.com/dynport/gocloud/aws/ec2"
"github.com/dynport/gocloud/aws/elb"
)
func resources(name string) {
cf := cloudformation.NewFromEnv()
ec2Client := ec2.NewFromEnv()
elbClient := elb.NewFromEnv()
resources, err := cf.ListStackResources(
cloudformation.ListStackResourcesParameters{
StackName: name,
},
)
if err != nil {
fatal(err)
}
stub := make(map[string]map[string]interface{})
for _, resource := range resources.StackResources {
typeSegments := strings.Split(resource.ResourceType, "::")
typeBase := typeSegments[len(typeSegments)-1]
byType, found := stub[typeBase]
if !found {
byType = make(map[string]interface{})
stub[typeBase] = byType
}
cleanName := strings.Replace(resource.LogicalResourceId, typeBase, "", 1)
if cleanName == "" {
cleanName = resource.LogicalResourceId
}
byType[cleanName] = resource.PhysicalResourceId
}
err = grabSecurityGroupNames(stub, ec2Client)
if err != nil {
fatal(err)
}
err = grabSubnetInfo(stub, ec2Client)
if err != nil {
fatal(err)
}
err = grabLoadBalancerDNSNames(stub, elbClient)
if err != nil {
fatal(err)
}
err = candiedyaml.NewEncoder(os.Stdout).Encode(map[string]interface{}{
"Region": ec2Client.Client.Region,
"AccessKeyID": ec2Client.Client.Key,
"SecretAccessKey": ec2Client.Client.Secret,
"Resources": stub,
})
if err != nil {
fatal(err)
}
}
func grabSecurityGroupNames(stub map[string]map[string]interface{}, ec2Client *ec2.Client) error {
names := make(map[string]interface{})
stub["SecurityGroupName"] = names
securityGroupIds := []string{}
nameForId := make(map[string]string)
for name, id := range stub["SecurityGroup"] {
securityGroupIds = append(securityGroupIds, id.(string))
nameForId[id.(string)] = name
}
groups, err := ec2Client.DescribeSecurityGroups(
&ec2.DescribeSecurityGroupsParameters{
GroupIds: securityGroupIds,
},
)
if err != nil {
return err
}
for _, group := range groups {
name := nameForId[group.GroupId]
names[name] = group.GroupName
}
return nil
}
func grabLoadBalancerDNSNames(stub map[string]map[string]interface{}, elbClient *elb.Client) error {
lbs, err := elbClient.DescribeLoadBalancers()
if err != nil {
return err
}
allNames := map[string]string{}
for _, lb := range lbs {
allNames[lb.LoadBalancerName] = lb.DNSName
}
stackNames := map[string]interface{}{}
for name, id := range stub["LoadBalancer"] {
stackNames[name] = allNames[id.(string)]
}
stub["LoadBalancerDNSName"] = stackNames
return nil
}
func grabSubnetInfo(stub map[string]map[string]interface{}, ec2Client *ec2.Client) error {
zones := make(map[string]interface{})
octets := make(map[string]interface{})
cidrs := make(map[string]interface{})
stub["SubnetAvailabilityZone"] = zones
stub["SubnetOctets"] = octets
stub["SubnetCIDR"] = cidrs
subnetIds := []string{}
nameForId := make(map[string]string)
for name, id := range stub["Subnet"] {
subnetIds = append(subnetIds, id.(string))
nameForId[id.(string)] = name
}
subnets, err := ec2Client.DescribeSubnets(
&ec2.DescribeSubnetsParameters{
Filters: []*ec2.Filter{
{
Name: "subnet-id",
Values: subnetIds,
},
},
},
)
if err != nil {
return err
}
for _, subnet := range subnets.Subnets {
name := nameForId[subnet.SubnetId]
_, ipNet, err := net.ParseCIDR(subnet.CidrBlock)
if err != nil {
return err
}
zones[name] = subnet.AvailabilityZone
cidrs[name] = subnet.CidrBlock
octets[name] = []string{
fmt.Sprintf("%d", ipNet.IP[0]),
fmt.Sprintf("%d.%d", ipNet.IP[0], ipNet.IP[1]),
fmt.Sprintf("%d.%d.%d", ipNet.IP[0], ipNet.IP[1], ipNet.IP[2]),
fmt.Sprintf("%d.%d.%d.%d", ipNet.IP[0], ipNet.IP[1], ipNet.IP[2], ipNet.IP[3]),
}
}
return nil
}