Fixed further requirements.

This commit is contained in:
Alvaro Arriola 2019-10-10 17:35:06 +02:00
parent 0b5214d247
commit 821d6c8672
11 changed files with 61 additions and 25 deletions

View File

@ -1,8 +1,8 @@
#- device: server01
# enabled: true
# form_factor: 0
# form_factor: Virtual
# name: to-server02
#- device: server02
# enabled: true
# form_factor: 0
# form_factor: Virtual
# name: to-server01

View File

@ -1,13 +1,13 @@
#- address: 10.1.1.1/24
# device: server01
# interface: Loopback0
# status: 1
# status: Active
# vrf: vrf1
#- address: 10.1.1.2/24
# device: server02
# interface: Vlan5
# status: 1
# status: Active
#- address: 10.1.1.10/24
# description: reserved IP
# status: 2
# status: Reserved
# tenant: tenant1

View File

@ -1,13 +1,13 @@
#- description: prefix1
# prefix: 10.1.1.0/24
# site: AMS 1
# status: 1
# status: Active
# tenant: tenant1
# vlan: vlan1
#- description: prefix2
# prefix: 10.1.2.0/24
# site: AMS 2
# status: 1
# status: Active
# tenant: tenant2
# vlan: vlan2
# is_pool: true
@ -16,6 +16,6 @@
#- description: ipv6 prefix1
# prefix: fd00:ccdd:a000:1::/64
# site: AMS 2
# status: 1
# status: Active
# tenant: tenant2
# vlan: vlan2

View File

@ -4,7 +4,7 @@
# memory: 4096
# name: virtual machine 1
# platform: Platform 2
# status: 1
# status: Active
# tenant: tenant1
# vcpus: 8
#- cluster: cluster1
@ -13,6 +13,6 @@
# memory: 2048
# name: virtual machine 2
# platform: Platform 2
# status: 1
# status: Active
# tenant: tenant1
# vcpus: 8

View File

@ -1,11 +1,11 @@
#- name: vlan1
# site: AMS 1
# status: 1
# status: Active
# vid: 5
# role: Main Management
# description: VLAN 5 for MGMT
#- group: VLAN group 2
# name: vlan2
# site: AMS 1
# status: 1
# status: Active
# vid: 1300

View File

@ -1,5 +1,6 @@
from dcim.models import Site
from ipam.models import VLAN, VLANGroup, Role
from ipam.constants import VLAN_STATUS_CHOICES
from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
@ -34,6 +35,11 @@ with file.open('r') as stream:
params[assoc] = model.objects.get(**query)
if 'status' in params:
for vlan_status in VLAN_STATUS_CHOICES:
if params['status'] in vlan_status:
params['status'] = vlan_status[0]
vlan, created = VLAN.objects.get_or_create(**params)
if created:

View File

@ -1,5 +1,6 @@
from dcim.models import Site
from ipam.models import Prefix, VLAN, Role, VRF
from ipam.constants import PREFIX_STATUS_CHOICES
from tenancy.models import Tenant, TenantGroup
from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
@ -37,6 +38,11 @@ with file.open('r') as stream:
params[assoc] = model.objects.get(**query)
if 'status' in params:
for prefix_status in PREFIX_STATUS_CHOICES:
if params['status'] in prefix_status:
params['status'] = prefix_status[0]
prefix, created = Prefix.objects.get_or_create(**params)
if created:

View File

@ -1,5 +1,6 @@
from dcim.models import Site, Platform, DeviceRole
from virtualization.models import Cluster, VirtualMachine
from virtualization.constants import VM_STATUS_CHOICES
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
@ -42,6 +43,11 @@ with file.open('r') as stream:
params[assoc] = model.objects.get(**query)
if 'status' in params:
for vm_status in VM_STATUS_CHOICES:
if params['status'] in vm_status:
params['status'] = vm_status[0]
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
if created:

View File

@ -14,7 +14,7 @@ with file.open('r') as stream:
yaml = YAML(typ='safe')
interfaces = yaml.load(stream)
optional_assocs = {
required_assocs = {
'virtual_machine': (VirtualMachine, 'name')
}
@ -22,12 +22,11 @@ with file.open('r') as stream:
for params in interfaces:
custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }
for assoc, details in required_assocs.items():
model, field = details
query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query)
params[assoc] = model.objects.get(**query)
interface, created = Interface.objects.get_or_create(**params)

View File

@ -1,4 +1,5 @@
from dcim.models import Interface, Device
from dcim.constants import IFACE_TYPE_CHOICES
from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML
@ -13,7 +14,7 @@ with file.open('r') as stream:
yaml = YAML(typ='safe')
interfaces = yaml.load(stream)
optional_assocs = {
required_assocs = {
'device': (Device, 'name')
}
@ -21,12 +22,21 @@ with file.open('r') as stream:
for params in interfaces:
custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }
for assoc, details in required_assocs.items():
model, field = details
query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query)
params[assoc] = model.objects.get(**query)
if 'form_factor' in params:
for outer_list in IFACE_TYPE_CHOICES:
for ffactor_choices in outer_list[1]:
if params['form_factor'] in ffactor_choices:
params['form_factor'] = ffactor_choices[0]
break
else:
continue
break
interface, created = Interface.objects.get_or_create(**params)
@ -43,4 +53,3 @@ with file.open('r') as stream:
interface.custom_field_values.add(custom_field_value)
print("🧷 Created interface", interface.name, interface.device.name)

View File

@ -1,4 +1,5 @@
from ipam.models import IPAddress, VRF
from ipam.constants import IPADDRESS_STATUS_CHOICES
from dcim.models import Device, Interface
from virtualization.models import VirtualMachine
from tenancy.models import Tenant
@ -30,6 +31,10 @@ with file.open('r') as stream:
custom_fields = params.pop('custom_fields', None)
params['address'] = IPNetwork(params['address'])
if vm and device:
print("IP Address can only specify one of the following: virtual_machine or device.")
sys.exit()
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
@ -44,6 +49,11 @@ with file.open('r') as stream:
query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query)
if 'status' in params:
for ip_status in IPADDRESS_STATUS_CHOICES:
if params['status'] in ip_status:
params['status'] = ip_status[0]
ip_address, created = IPAddress.objects.get_or_create(**params)
if created: