diff --git a/app.py b/app.py new file mode 100644 index 0000000..ae756e3 --- /dev/null +++ b/app.py @@ -0,0 +1,82 @@ +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/type.py b/type.py index 624de3f..27a5aaa 100644 --- a/type.py +++ b/type.py @@ -1,5 +1,34 @@ +from dataclasses import dataclass +from ipaddress import IPv4Network + vimpelcomSWType = { "has_primary_ip": "true", "tenant": "vimpelcom", "role": "Kommutator", -} \ No newline at end of file +} + + +# 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') + vrf: str = 'VK374' + + +@dataclass +class vimpelcomIPaddressSSPD: + prefix: IPv4Network = IPv4Network('12.0.0.0/8') + vrf: str = 'SORM_BB' + +@dataclass +class vimpelcomIPaddressTEST: + prefix: IPv4Network = IPv4Network('192.168.0.0/16') + vrf: str = 'TEST'