-
Notifications
You must be signed in to change notification settings - Fork 10
/
subnets.go
105 lines (93 loc) · 3.43 KB
/
subnets.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
package main
import (
"encoding/json"
"log"
"github.com/aws/aws-sdk-go/service/ec2"
)
type subnetList struct {
*ec2.DescribeSubnetsOutput
}
type subnetNodes []subnetNode
type subnetNode struct {
UID string `json:"uid,omitempty"`
Type []string `json:"dgraph.type,omitempty"`
Name string `json:"name,omitempty"` // This field is only for Ratel Viz
Region string `json:"Region,omitempty"`
OwnerID string `json:"OwnerId,omitempty"`
OwnerName string `json:"OwnerName,omitempty"`
Service string `json:"Service,omitempty"`
SubnetID string `json:"SubnetId,omitempty"`
CidrBlock string `json:"CidrBlock,omitempty"`
MapPublicIPOnLaunch bool `json:"MapPublicIpOnLaunch,omitempty"`
DefaultForAz bool `json:"MapPuDefaultForAzblicIpOnLaunch,omitempty"`
State string `json:"State,omitempty"`
AssignIPv6AddressOnCreation bool `json:"AssignIpv6AddressOnCreation,omitempty"`
AvailableIPAddressCount int64 `json:"AvailableIpAddressCount,omitempty"`
AvailabilityZone availabilityZoneNodes `json:"_AvailabilityZone,omitempty"`
Vpc vpcNode `json:"_Vpc,omitempty"`
}
func (c *connector) listSubnets() subnetList {
defer c.waitGroup.Done()
log.Println("List Subnets")
response, err := ec2.New(c.awsSession).DescribeSubnets(&ec2.DescribeSubnetsInput{})
if err != nil {
log.Fatal(err)
}
return subnetList{response}
}
func (list subnetList) addNodes(c *connector) {
defer c.waitGroup.Done()
if len(list.Subnets) == 0 {
return
}
log.Println("Add Subnet Nodes")
a := make(subnetNodes, 0, len(list.Subnets))
for _, i := range list.Subnets {
var b subnetNode
b.Service = "ec2"
b.Region = c.awsRegion
b.OwnerID = c.awsAccountID
b.OwnerName = c.awsAccountName
b.Type = []string{"Subnet"}
b.Name = *i.SubnetId
for _, tag := range i.Tags {
if *tag.Key == "Name" {
b.Name = *tag.Value
}
}
b.SubnetID = *i.SubnetId
b.MapPublicIPOnLaunch = *i.MapPublicIpOnLaunch
b.AvailableIPAddressCount = *i.AvailableIpAddressCount
b.DefaultForAz = *i.DefaultForAz
b.State = *i.State
b.CidrBlock = *i.CidrBlock
b.AssignIPv6AddressOnCreation = *i.AssignIpv6AddressOnCreation
a = append(a, b)
}
c.dgraphAddNodes(a)
c.stats.NumberOfNodes += len(a)
m := make(map[string]subnetNodes)
n := make(map[string]string)
json.Unmarshal(c.dgraphQuery("Subnet"), &m)
for _, i := range m["list"] {
n[i.SubnetID] = i.UID
}
c.ressources["Subnets"] = n
}
func (list subnetList) addEdges(c *connector) {
defer c.waitGroup.Done()
if len(list.Subnets) == 0 {
return
}
log.Println("Add Subnet Edges")
a := subnetNodes{}
for _, i := range list.Subnets {
b := subnetNode{
UID: c.ressources["Subnets"][*i.SubnetId],
Vpc: vpcNode{UID: c.ressources["Vpcs"][*i.VpcId]},
AvailabilityZone: availabilityZoneNodes{availabilityZoneNode{UID: c.ressources["AvailabilityZones"][*i.AvailabilityZone]}},
}
a = append(a, b)
}
c.dgraphAddNodes(a)
}