-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03.NeoLoadVPC.py
More file actions
64 lines (51 loc) · 2.06 KB
/
Copy path03.NeoLoadVPC.py
File metadata and controls
64 lines (51 loc) · 2.06 KB
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
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 NeoLoadVPC(filepath,region):
# loads contents of Ec2 files
try:
with open(filepath) as f:
data = json.load(f)
for vpc in data:
print(vpc["CidrBlock"])
print(vpc["VpcId"])
print(vpc["OwnerId"])
try:
tags = vpc["Tags"]
for tag in tags:
if tag["Key"] == "Name":
vpcname = (tag["Value"])
NeoInsertVPC(vpc["CidrBlock"],vpc["VpcId"],vpc["OwnerId"],vpcname, region)
except:
vpcname = "NoName"
NeoInsertVPC(vpc["CidrBlock"],vpc["VpcId"],vpc["OwnerId"],vpcname, region)
f.close()
except Exception as inst:
print(inst)
def NeoInsertVPC(CidrBlock, VpcId,OwnerId,Name , AwsRegion):#
query = 'MERGE (v: AWSVpc { Name: "'+Name+'" , Id : "'+VpcId+'" , CidrBlock : "'+CidrBlock+'" })\n\
MERGE (r: AWSRegion { Id : "'+AwsRegion+'"})\n\
MERGE (l: LinkedAccount {Id : "'+OwnerId+'"})\n\
MERGE (l)-[:owner]->(v)\n\
MERGE (r)-[:geo]->(v)'
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)
vpcfile = outputfiles+acc['name']+"/"+Aregion+"/vpcs/resources.json"
print(vpcfile)
NeoLoadVPC(vpcfile,Aregion)
NeoLoadAccounts(AccountsFilepath)