From 821d6c8672acc3757f95ff944efb79419db1d85f Mon Sep 17 00:00:00 2001 From: Alvaro Arriola Date: Thu, 10 Oct 2019 17:35:06 +0200 Subject: [PATCH] Fixed further requirements. --- initializers/dcim_interfaces.yml | 4 ++-- initializers/ip_addresses.yml | 6 ++--- initializers/prefixes.yml | 6 ++--- initializers/virtual_machines.yml | 4 ++-- initializers/vlans.yml | 4 ++-- startup_scripts/210_vlans.py | 6 +++++ startup_scripts/220_prefixes.py | 6 +++++ startup_scripts/230_virtual_machines.py | 6 +++++ .../240_virtualization_interfaces.py | 11 ++++----- startup_scripts/250_dcim_interfaces.py | 23 +++++++++++++------ startup_scripts/260_ip_addresses.py | 10 ++++++++ 11 files changed, 61 insertions(+), 25 deletions(-) diff --git a/initializers/dcim_interfaces.yml b/initializers/dcim_interfaces.yml index e02bf07..8e6281e 100644 --- a/initializers/dcim_interfaces.yml +++ b/initializers/dcim_interfaces.yml @@ -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 diff --git a/initializers/ip_addresses.yml b/initializers/ip_addresses.yml index 7427a4a..abcc918 100644 --- a/initializers/ip_addresses.yml +++ b/initializers/ip_addresses.yml @@ -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 diff --git a/initializers/prefixes.yml b/initializers/prefixes.yml index 8644a9e..ef6330a 100644 --- a/initializers/prefixes.yml +++ b/initializers/prefixes.yml @@ -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 diff --git a/initializers/virtual_machines.yml b/initializers/virtual_machines.yml index 8fd57ed..ca61a80 100644 --- a/initializers/virtual_machines.yml +++ b/initializers/virtual_machines.yml @@ -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 diff --git a/initializers/vlans.yml b/initializers/vlans.yml index 6e7353d..c78f6ca 100644 --- a/initializers/vlans.yml +++ b/initializers/vlans.yml @@ -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 diff --git a/startup_scripts/210_vlans.py b/startup_scripts/210_vlans.py index ab6bd2b..c667354 100644 --- a/startup_scripts/210_vlans.py +++ b/startup_scripts/210_vlans.py @@ -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: diff --git a/startup_scripts/220_prefixes.py b/startup_scripts/220_prefixes.py index d13578a..69d78f2 100644 --- a/startup_scripts/220_prefixes.py +++ b/startup_scripts/220_prefixes.py @@ -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: diff --git a/startup_scripts/230_virtual_machines.py b/startup_scripts/230_virtual_machines.py index 449df8a..af30ee2 100644 --- a/startup_scripts/230_virtual_machines.py +++ b/startup_scripts/230_virtual_machines.py @@ -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: diff --git a/startup_scripts/240_virtualization_interfaces.py b/startup_scripts/240_virtualization_interfaces.py index e1c2b8d..8d2ca96 100644 --- a/startup_scripts/240_virtualization_interfaces.py +++ b/startup_scripts/240_virtualization_interfaces.py @@ -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) diff --git a/startup_scripts/250_dcim_interfaces.py b/startup_scripts/250_dcim_interfaces.py index eca5415..b0baf39 100644 --- a/startup_scripts/250_dcim_interfaces.py +++ b/startup_scripts/250_dcim_interfaces.py @@ -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) - diff --git a/startup_scripts/260_ip_addresses.py b/startup_scripts/260_ip_addresses.py index 13c755d..589c5d3 100644 --- a/startup_scripts/260_ip_addresses.py +++ b/startup_scripts/260_ip_addresses.py @@ -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: