-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.py
39 lines (32 loc) · 1.09 KB
/
insert.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
import os
import yaml
from mongoengine import connect
from models import Template, Region
__DATABASE = 'templates'
__PASSWORD = os.environ.get("MONGO_DEVELOPER_PASSWORD")
__client = connect(
__DATABASE,
host=f"mongodb+srv://developer:{__PASSWORD}@devcluster.25vmv.mongodb.net/{__DATABASE}?retryWrites=true&w=majority",
alias="default"
)
def insert(model: str) -> None:
with open("sample.yml", "r") as file:
data = yaml.load(file.read(), yaml.Loader)
for row in data[model]:
if model == 'templates':
template = Template(
name=row['name'],
region=row['region'],
mpls_count=row['mpls_count'],
isp_count=row['isp_count'],
dia_count=row['dia_count'],
high_availability=row['high_availability']
)
template.save()
elif model == 'regions':
region = Region(
name=row['name']
)
region.save()
insert('regions')
insert('templates')