diff --git a/app.py b/app.py index ae756e3..e69de29 100644 --- a/app.py +++ b/app.py @@ -1,82 +0,0 @@ -import json -import logging -import os.path - -import pynetbox -from oxi.manager import OxidizedAPI - -from settings import settings -from type import vimpelcomIPaddress374, vimpelcomIPaddressSSPD, vimpelcomIPaddressTEST - -logging.basicConfig() -log = logging.getLogger() - -netbox = pynetbox.api( - settings.nb_url, - token=settings.nb_token) -netbox.http_session.verify = False - -filters = { - "has_primary_ip": "true", - "tenant": "vimpelcom", - "role": "Kommutator", -} - -oxi = OxidizedAPI(username=settings.oxi_username, password=settings.oxi_password, verify=False) -devices = netbox.dcim.devices.filter(**filters) -device_list = [item.name for item in devices] -devices = netbox.dcim.devices.filter(**filters) -if not os.path.exists('devices.json'): - with open('devices.json', 'w') as file: - json.dump(device_list, file, ensure_ascii=False, indent=3) -for device in devices: - if device.name not in device_list: - continue - ex_interface = netbox.dcim.interfaces.filter(device=device.name) - log.debug("device: %r", device.name) - try: - oxidized_device = oxi.node(device.name.replace("(1)", "")) - except ValueError: - log.warning("%r no exist in Oxidized", device.name) - continue - log.debug("Vlan-interfaces: %r", oxidized_device.config.vlaninterfaces()) - if oxidized_device.config.vlaninterfaces(): - for vlan_interface in oxidized_device.config.vlaninterfaces(): - log.debug("IPaddress: %r", vlan_interface.ip_address) - checker = netbox.dcim.interfaces.get( - name=vlan_interface.interface, - device=device.name - ) - if checker: - continue - child_interface = netbox.dcim.interfaces.create( - name=vlan_interface.interface, - type='virtual', - device=device.id - ) - print(device) - print("description: ", vlan_interface.description) - if vlan_interface.description: - child_interface.description = vlan_interface.description - child_interface.save() - print("child_interface: ", child_interface) - print("ip address:", vlan_interface.ip_address) - if vlan_interface.ip_address: - netbox_ip = netbox.ipam.ip_addresses.create( - address=str(vlan_interface.ip_address), - tenant=device.tenant.id, - ) - if vlan_interface.ip_address in vimpelcomIPaddress374.prefix: - netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddress374.vrf).id - elif vlan_interface.ip_address in vimpelcomIPaddressSSPD.prefix: - netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressSSPD.vrf).id - else: - netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressTEST.vrf).id - netbox_ip.assigned_object = child_interface - netbox_ip.assigned_object_id = child_interface.id - netbox_ip.assigned_object_type = 'dcim.interface' - netbox_ip.save() - device_list.remove(device.name) - with open('devices.json', 'w') as file: - json.dump(device_list, file, ensure_ascii=False, indent=3) - # child_interface.save() diff --git a/crud.py b/crud.py new file mode 100644 index 0000000..497582d --- /dev/null +++ b/crud.py @@ -0,0 +1,77 @@ +import json +import logging +import os +from typing import TYPE_CHECKING +from init import netbox, oxi +from type import vimpelcomIPaddress374, vimpelcomIPaddressSSPD, vimpelcomIPaddressTEST + +if TYPE_CHECKING: + from pynetbox.core.response import RecordSet + +logging.basicConfig() +log = logging.getLogger() + + +def get_devices_filtered(filters: dict): + devices = netbox.dcim.devices.filter(**filters) + device_list = [item.name for item in devices] + devices = netbox.dcim.devices.filter(**filters) + if not os.path.exists(f'devices_{filters.get('tenant')}.json'): + with open(f'devices_{filters.get('tenant')}.json', 'w') as file: + json.dump(device_list, file, ensure_ascii=False, indent=3) + return devices + + +def create_interfaces(devices: 'RecordSet', device_list: list): + for device in devices: + if device.name not in device_list: + continue + ex_interface = netbox.dcim.interfaces.filter(device=device.name) + log.debug("device: %r", device.name) + try: + oxidized_device = oxi.node(device.name.replace("(1)", "")) + except ValueError: + log.warning("%r no exist in Oxidized", device.name) + continue + log.debug("Vlan-interfaces: %r", oxidized_device.config.vlaninterfaces()) + if oxidized_device.config.vlaninterfaces(): + for vlan_interface in oxidized_device.config.vlaninterfaces(): + log.debug("IPaddress: %r", vlan_interface.ip_address) + checker = netbox.dcim.interfaces.get( + name=vlan_interface.interface, + device=device.name + ) + if checker: + continue + child_interface = netbox.dcim.interfaces.create( + name=vlan_interface.interface, + type='virtual', + device=device.id + ) + print(device) + print("description: ", vlan_interface.description) + if vlan_interface.description: + child_interface.description = vlan_interface.description + child_interface.save() + print("child_interface: ", child_interface) + print("ip address:", vlan_interface.ip_address) + if vlan_interface.ip_address: + netbox_ip = netbox.ipam.ip_addresses.create( + address=str(vlan_interface.ip_address), + tenant=device.tenant.id, + ) + if vlan_interface.ip_address in vimpelcomIPaddress374.prefix: + netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddress374.vrf).id + elif vlan_interface.ip_address in vimpelcomIPaddressSSPD.prefix: + netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressSSPD.vrf).id + else: + netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressTEST.vrf).id + netbox_ip.assigned_object = child_interface + netbox_ip.assigned_object_id = child_interface.id + netbox_ip.assigned_object_type = 'dcim.interface' + netbox_ip.save() + device_list.remove(device.name) + with open('devices.json', 'w') as file: + json.dump(device_list, file, ensure_ascii=False, indent=3) + + diff --git a/init.py b/init.py new file mode 100644 index 0000000..ceae22f --- /dev/null +++ b/init.py @@ -0,0 +1,14 @@ +import pynetbox + +from oxi.manager import OxidizedAPI +from settings import settings + +netbox = pynetbox.api( + settings.nb_url, + token=settings.nb_token) +netbox.http_session.verify = False + +oxi = OxidizedAPI( + username=settings.oxi_username, + password=settings.oxi_password, + verify=False) diff --git a/pynet.py b/pynet.py deleted file mode 100644 index d736f3f..0000000 --- a/pynet.py +++ /dev/null @@ -1,21 +0,0 @@ -import pynetbox - -from oxi.manager import OxidizedAPI -from settings import settings - - -netbox = pynetbox.api( - settings.nb_url, - token=settings.nb_token) -netbox.http_session.verify = False - -filters = { - "has_primary_ip": "true", - "tenant": "vimpelcom", - "role": "Kommutator", -} - - -# devices = netbox.dcim.devices.filter(**filters) -# for device in devices: -# print(f"{device.name} (IP: {device.primary_ip})") \ No newline at end of file diff --git a/type.py b/type.py index 27a5aaa..1fdfe45 100644 --- a/type.py +++ b/type.py @@ -7,16 +7,6 @@ vimpelcomSWType = { "role": "Kommutator", } - -# vimpelcomIPaddress374 = { -# 'prefix': IPv4Network('15.0.0.0/8'), -# 'vrf': 'VK374' -# } -# -# vimpelcomIPaddressSSPD = { -# 'prefix': IPv4Network('12.0.0.0/8'), -# 'vrf': 'SORM_BB' -# } @dataclass class vimpelcomIPaddress374: prefix: IPv4Network = IPv4Network('15.0.0.0/8')