-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstances.go
134 lines (121 loc) · 4.43 KB
/
instances.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
package main
import (
"encoding/json"
"log"
"github.com/aws/aws-sdk-go/service/ec2"
)
type instanceList struct{ *ec2.DescribeInstancesOutput }
type instanceNodes []instanceNode
type instanceNode struct {
UID string `json:"uid,omitempty"`
Type []string `json:"dgraph.type,omitempty"`
Name string `json:"name,omitempty"` // This field is only for Ratel Viz
OwnerID string `json:"OwnerId,omitempty"`
OwnerName string `json:"OwnerName,omitempty"`
Region string `json:"Region,omitempty"`
Service string `json:"Service,omitempty"`
InstanceID string `json:"InstanceId,omitempty"`
InstanceType string `json:"InstanceType,omitempty"`
State string `json:"State,omitempty"`
EbsOptimized bool `json:"EbsOptimized,omitempty"`
Hypervisor string `json:"Hypervisor,omitempty"`
VirtualizationType string `json:"VirtualizationType,omitempty"`
PrivateIPAddress string `json:"PrivateIpAddress,omitempty"`
PublicIPAddress string `json:"PublicIpAddress,omitempty"`
KeyName keyPairNode `json:"_KeyName,omitempty"`
AvailabilityZone availabilityZoneNodes `json:"_AvailabilityZone,omitempty"`
Vpc vpcNode `json:"_Vpc,omitempty"`
InstanceProfile instanceProfileNode `json:"_InstanceProfile,omitempty"`
Image imageNode `json:"_Image,omitempty"`
Subnet subnetNodes `json:"_Subnet,omitempty"`
SecurityGroup securityGroupNodes `json:"_SecurityGroup,omitempty"`
}
func (c *connector) listInstances() instanceList {
defer c.waitGroup.Done()
log.Println("List Instances")
response, err := ec2.New(c.awsSession).DescribeInstances(&ec2.DescribeInstancesInput{})
if err != nil {
log.Fatal(err)
}
return instanceList{response}
}
func (list instanceList) addNodes(c *connector) {
defer c.waitGroup.Done()
if len(list.Reservations) == 0 {
return
}
log.Println("Add Instance Nodes")
a := make(instanceNodes, 0, len(list.Reservations))
for _, r := range list.Reservations {
for _, i := range r.Instances {
var b instanceNode
b.Service = "ec2"
b.Type = []string{"Instance"}
b.OwnerID = c.awsAccountID
b.OwnerName = c.awsAccountName
b.Region = c.awsRegion
b.Name = *i.InstanceId
for _, tag := range i.Tags {
if *tag.Key == "Name" {
b.Name = *tag.Value
}
}
b.InstanceID = *i.InstanceId
b.InstanceType = *i.InstanceType
b.State = *i.State.Name
b.VirtualizationType = *i.VirtualizationType
b.Hypervisor = *i.Hypervisor
b.EbsOptimized = *i.EbsOptimized
if i.PrivateIpAddress != nil {
b.PrivateIPAddress = *i.PrivateIpAddress
}
if i.PublicIpAddress != nil {
b.PublicIPAddress = *i.PublicIpAddress
}
a = append(a, b)
}
}
c.dgraphAddNodes(a)
c.stats.NumberOfNodes += len(a)
m := make(map[string]instanceNodes)
n := make(map[string]string)
json.Unmarshal(c.dgraphQuery("Instance"), &m)
for _, i := range m["list"] {
n[i.InstanceID] = i.UID
}
c.ressources["Instances"] = n
}
func (list instanceList) addEdges(c *connector) {
defer c.waitGroup.Done()
if len(list.Reservations) == 0 {
return
}
log.Println("Add Instance Edges")
a := instanceNodes{}
for _, r := range list.Reservations {
for _, i := range r.Instances {
b := instanceNode{
UID: c.ressources["Instances"][*i.InstanceId],
Image: imageNode{UID: c.ressources["Images"][*i.ImageId]},
AvailabilityZone: availabilityZoneNodes{availabilityZoneNode{UID: c.ressources["AvailabilityZones"][*i.Placement.AvailabilityZone]}},
}
if i.KeyName != nil {
b.KeyName = keyPairNode{UID: c.ressources["KeyPairs"][*i.KeyName]}
}
if i.SubnetId != nil {
b.Subnet = subnetNodes{subnetNode{UID: c.ressources["Subnets"][*i.SubnetId]}}
}
if i.IamInstanceProfile != nil {
b.InstanceProfile = instanceProfileNode{UID: c.ressources["InstanceProfiles"][*i.IamInstanceProfile.Id]}
}
if i.VpcId != nil {
b.Vpc = vpcNode{UID: c.ressources["Vpcs"][*i.VpcId]}
}
for _, j := range i.SecurityGroups {
b.SecurityGroup = append(b.SecurityGroup, securityGroupNode{UID: c.ressources["SecurityGroups"][*j.GroupId]})
}
a = append(a, b)
}
}
c.dgraphAddNodes(a)
}