-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04.NeoLoadSubnetAndZones.py
71 lines (57 loc) · 2.59 KB
/
04.NeoLoadSubnetAndZones.py
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
import os, json, yaml
from py2neo import Graph
with open("./AWSNEoConfig.json") as c:
conf = json.load(c)
print(conf[0]["AccountsFilepath"])
print(conf[0]["NeoParametes"][0]["Url"])
c.close
graph = Graph(conf[0]["NeoParametes"][0]["Url"], auth=(conf[0]["NeoParametes"][0]["Username"], conf[0]["NeoParametes"][0]["Password"]))
AccountsFilepath = conf[0]["AccountsFilepath"]
outputfiles = conf[0]["custodianFiles"]
def NeoLoadSubnetes(filepath,region):
# loads contents of Ec2 files
try:
with open(filepath) as f:
data = json.load(f)
for subnet in data:
print(subnet["CidrBlock"])
print(subnet["VpcId"])
print(subnet["AvailabilityZone"])
print(subnet["AvailabilityZoneId"])
print(subnet["SubnetId"])
try:
tags = subnet["Tags"]
for tag in tags:
if tag["Key"] == "Name":
sunbnetName = (tag["Value"])
print(sunbnetName)
NeoInsertSubnetsAndZones(subnet["CidrBlock"],subnet["VpcId"],subnet["AvailabilityZone"],subnet["AvailabilityZoneId"],sunbnetName, subnet["SubnetId"], region)
except:
sunbnetName = "NoName"
print(sunbnetName)
NeoInsertSubnetsAndZones(subnet["CidrBlock"],subnet["VpcId"],subnet["AvailabilityZone"],subnet["AvailabilityZoneId"],sunbnetName, subnet["SubnetId"] ,region)
f.close()
except Exception as inst:
print(inst)
def NeoInsertSubnetsAndZones(CidrBlock, VpcId,SubnetZone,ZoneId,Name , SubnetId, AwsRegion):#
query = 'MERGE (v: AWSVpc { Id : "'+VpcId+'" })\n\
MERGE (z: AWSZone { Name : "'+ZoneId+'", Id: "'+ZoneId+'" })\n\
MERGE (r: AWSRegion { Id : "'+AwsRegion+'"})\n\
MERGE (s: Subnet {Name : "'+Name+'" , Id : "'+SubnetId+'" , CidrBlock : "'+CidrBlock+'" })\n\
MERGE (s)-[:InZone]->(z)\n\
MERGE (s)-[:InVpc]->(v)\n\
MERGE (z)-[:INRegion]->(r)'
print(query)
result = graph.run(query).to_table()
print(result)
def NeoLoadAccounts(filepath):
with open(filepath) as f:
accountList = yaml.safe_load(f)
for acc in accountList["accounts"]:
print(acc['name'])
for folder in os.listdir(outputfiles+acc['name'] ):
Aregion = (folder)
SubenetFilepath = outputfiles+acc['name']+"/"+Aregion+"/subnets/resources.json"
print(SubenetFilepath)
NeoLoadSubnetes(SubenetFilepath,Aregion)
NeoLoadAccounts(AccountsFilepath)