def setUp(self): user = create_test_user(permissions=['dcim.view_inventoryitem']) self.client = Client() self.client.force_login(user) site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() InventoryItem.objects.bulk_create([ InventoryItem(device=device, name='Inventory Item 1'), InventoryItem(device=device, name='Inventory Item 2'), InventoryItem(device=device, name='Inventory Item 3'), ])
def run(self, data, commit): # Create the new site site = Site(name=data['site_name'], slug=slugify(data['site_name']), status=SiteStatusChoices.STATUS_PLANNED) site.save() self.log_success(f"Created new site: {site}") # Create access switches switch_role = DeviceRole.objects.get(name='Access Switch') for i in range(1, data['switch_count'] + 1): switch = Device(device_type=data['switch_model'], name=f'{site.slug}-switch{i}', site=site, status=DeviceStatusChoices.STATUS_PLANNED, device_role=switch_role) switch.save() self.log_success(f"Created new switch: {switch}") # Generate a CSV table of new devices output = ['name,make,model'] for switch in Device.objects.filter(site=site): attrs = [ switch.name, switch.device_type.manufacturer.name, switch.device_type.model ] output.append(','.join(attrs)) return '\n'.join(output)
def setUp(self): user = create_test_user(permissions=['secrets.view_secret']) self.client = Client() self.client.force_login(user) site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(manufacturer=manufacturer, model='Device Type 1') devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() secretrole = SecretRole(name='Secret Role 1', slug='secret-role-1') secretrole.save() Secret.objects.bulk_create([ Secret(device=device, role=secretrole, name='Secret 1', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 2', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 3', ciphertext=b'1234567890'), ])
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() InventoryItem.objects.bulk_create([ InventoryItem(device=device, name='Inventory Item 1'), InventoryItem(device=device, name='Inventory Item 2'), InventoryItem(device=device, name='Inventory Item 3'), ])
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(manufacturer=manufacturer, model='Device Type 1') devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() secretrole = SecretRole(name='Secret Role 1', slug='secret-role-1') secretrole.save() Secret.objects.bulk_create([ Secret(device=device, role=secretrole, name='Secret 1', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 2', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 3', ciphertext=b'1234567890'), ])
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device1 = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device1.save() device2 = Device(name='Device 2', site=site, device_type=devicetype, device_role=devicerole) device2.save() iface1 = Interface(device=device1, name='Interface 1', form_factor=IFACE_FF_1GE_FIXED) iface1.save() iface2 = Interface(device=device1, name='Interface 2', form_factor=IFACE_FF_1GE_FIXED) iface2.save() iface3 = Interface(device=device1, name='Interface 3', form_factor=IFACE_FF_1GE_FIXED) iface3.save() iface4 = Interface(device=device2, name='Interface 1', form_factor=IFACE_FF_1GE_FIXED) iface4.save() iface5 = Interface(device=device2, name='Interface 2', form_factor=IFACE_FF_1GE_FIXED) iface5.save() iface6 = Interface(device=device2, name='Interface 3', form_factor=IFACE_FF_1GE_FIXED) iface6.save() Cable(termination_a=iface1, termination_b=iface4, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface2, termination_b=iface5, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface3, termination_b=iface6, type=CABLE_TYPE_CAT6).save()
def setUp(self): user = create_test_user(permissions=[ 'secrets.view_secret', 'secrets.add_secret', ]) # Set up a master key userkey = UserKey(user=user, public_key=PUBLIC_KEY) userkey.save() master_key = userkey.get_master_key(PRIVATE_KEY) self.session_key = SessionKey(userkey=userkey) self.session_key.save(master_key) self.client = Client() self.client.force_login(user) site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(manufacturer=manufacturer, model='Device Type 1') devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() secretrole = SecretRole(name='Secret Role 1', slug='secret-role-1') secretrole.save() Secret.objects.bulk_create([ Secret(device=device, role=secretrole, name='Secret 1', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 2', ciphertext=b'1234567890'), Secret(device=device, role=secretrole, name='Secret 3', ciphertext=b'1234567890'), ])
def create_patch_panel(self, site, rack, name, ports): pp_name = "pp-%s-%s.1" % (site.slug, name) try: pp = Device.objects.get(name=pp_name) self.log_info("Patch panel %s already present, carrying on." % pp) return pp except Device.DoesNotExist: pass pp_type = DeviceType.objects.get(manufacturer__name='Telegärtner', model='Patchpanel') pp = Device(device_type=pp_type, device_role=DeviceRole.objects.get(name='Patchpanel'), site=site, status=DeviceStatusChoices.STATUS_PLANNED, name=pp_name, rack=rack, position=rack.u_height, face=DeviceFaceChoices.FACE_FRONT) pp.save() self.log_success("Created patch panel {}".format(pp)) # Create front and rear ports for n in range(1, int(ports) + 1): rear_port = RearPort(device=pp, name=str(n), type=PortTypeChoices.TYPE_8P8C, positions=1) rear_port.save() front_port = FrontPort( device=pp, name=str(n), type=PortTypeChoices.TYPE_8P8C, rear_port=rear_port, rear_port_position=1, ) front_port.save() return pp
def setUp(self): user = create_test_user(permissions=['ipam.view_service']) self.client = Client() self.client.force_login(user) site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(manufacturer=manufacturer, model='Device Type 1') devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() Service.objects.bulk_create([ Service(device=device, name='Service 1', protocol=IP_PROTOCOL_TCP, port=101), Service(device=device, name='Service 2', protocol=IP_PROTOCOL_TCP, port=102), Service(device=device, name='Service 3', protocol=IP_PROTOCOL_TCP, port=103), ])
def create_and_connect_surges(self, site, rack, pp, pole_setup): # surge_config will be of format <pole no>:<num surges>[ <pole no>:<num surges> [...]] # So first split by spaces to get a single pole config and then iterate of surge at this pole. # The RearPort of the 1st surge protector of the 1st pole will be connected to PP port 1 then # continuing upwards. surge_type = DeviceType.objects.get(manufacturer__name='Ubnt', model='Surge Protector') pp_port = 1 for pole_config in pole_setup.split(): pole_no, num_surges = pole_config.split(':') for n in range(1, int(num_surges) + 1): # Create surge surge_name = "sp-%s-mast%s-%s" % (site.slug.lower(), pole_no, n) surge = Device( device_type=surge_type, device_role=DeviceRole.objects.get(name='Surge Protector'), name=surge_name, status=DeviceStatusChoices.STATUS_PLANNED, site=site) surge.save() # Link RearPort of SP to next free panel port cable = Cable(termination_a=RearPort.objects.get( device=pp, name=str(pp_port)), termination_b=RearPort.objects.get(device=surge, name=str(1)), status=CableStatusChoices.STATUS_PLANNED) cable.save() self.log_success( "Created surge protector %s and linked it to patch panel port %s." % (surge, pp_port)) pp_port += 1
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device1 = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device1.save() device2 = Device(name='Device 2', site=site, device_type=devicetype, device_role=devicerole) device2.save() iface1 = Interface(device=device1, name='Interface 1', form_factor=IFACE_FF_1GE_FIXED) iface1.save() iface2 = Interface(device=device1, name='Interface 2', form_factor=IFACE_FF_1GE_FIXED) iface2.save() iface3 = Interface(device=device1, name='Interface 3', form_factor=IFACE_FF_1GE_FIXED) iface3.save() iface4 = Interface(device=device2, name='Interface 1', form_factor=IFACE_FF_1GE_FIXED) iface4.save() iface5 = Interface(device=device2, name='Interface 2', form_factor=IFACE_FF_1GE_FIXED) iface5.save() iface6 = Interface(device=device2, name='Interface 3', form_factor=IFACE_FF_1GE_FIXED) iface6.save() Cable(termination_a=iface1, termination_b=iface4, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface2, termination_b=iface5, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface3, termination_b=iface6, type=CABLE_TYPE_CAT6).save()
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() InventoryItem.objects.bulk_create([ InventoryItem(device=device, name='Inventory Item 1'), InventoryItem(device=device, name='Inventory Item 2'), InventoryItem(device=device, name='Inventory Item 3'), ])
def setUp(self): self.client = Client() site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(manufacturer=manufacturer, model='Device Type 1') devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device.save() Service.objects.bulk_create([ Service(device=device, name='Service 1', protocol=IP_PROTOCOL_TCP, port=101), Service(device=device, name='Service 2', protocol=IP_PROTOCOL_TCP, port=102), Service(device=device, name='Service 3', protocol=IP_PROTOCOL_TCP, port=103), ])
def run(self, data, commit): # Create the device device = Device(name=data['business_name'], device_role_id=self.DEVICE_ROLE_ID, device_type_id=data["hardware_choice"], platform_id=self.PLATFORM_ID, site_id=self.SITE_ID) device.save() interfaces = Interface.objects.filter(device_id=device.id) enabled_interfaces = [] mgmt_intf = interfaces.get(name="b107") enabled_interfaces.append(mgmt_intf) uplk_intf = interfaces.get(name="ether10") enabled_interfaces.append(uplk_intf) uplk_intf.mode = "tagged" uplk_intf.tagged_vlans.set([self.INET_VLAN, self.MGMT_VLAN]) uplk_intf.description = "Uplink" uplk_intf.save() inet_intf = interfaces.get(name="ether1") enabled_interfaces.append(inet_intf) inet_intf.description = "Internet" inet_intf.mode = "access" inet_intf.untagged_vlan = self.INET_VLAN inet_intf.save() mgmt_intf.save() for intf in interfaces: intf.enabled = False intf.save() for intf in enabled_interfaces: intf.enabled = True intf.mtu = 1500 intf.save() available_ip = Prefix.objects.get( vlan=self.MGMT_VLAN).get_first_available_ip() ip = IPAddress( address=available_ip, assigned_object_type=ContentType.objects.get_for_model(Interface), assigned_object_id=mgmt_intf.id) ip.save() device.primary_ip4_id = ip.id device.primary_ip_id = ip.id device.comments = data['comments'] device.save() # ############ if (not data["skip_zabbix"] and commit): # Post to Zabbix API to create host in mikrotik group and ICMP # template try: hostid = self.ZAPI.host.create( host=data["business_name"], interfaces=dict(type=2, main=1, useip=1, ip=available_ip.replace("/24", ""), port=161, dns="", details=dict( version="1", bulk="0", community=self.SNMP_COMMUNITY)), groups=dict(groupid=15), templates=dict(templateid=10186)) self.log_info("zabbix configured successfully") except Exception as e: self.log_info("failed to configure zabbix {0}".format(e)) if (not data["skip_uplink_port"] and commit): try: agg_switches = Device.objects.filter( site=data["uplink_site"], device_role_id=self.AGG_ROLE_ID, status="active") selected_interface = "" for agg_switch in agg_switches: interfaces = Interface.objects.filter( device_id=agg_switch.id) for interface in interfaces: if (interface.connection_status is not True and interface.enabled is True and interface.description == '' and interface.type == '1000base-x-sfp'): selected_interface = interface break if selected_interface != "": selected_interface.enabled = True selected_interface.description = device.name selected_interface.mode = "tagged" selected_interface.tagged_vlans.set( [self.INET_VLAN, self.MGMT_VLAN]) selected_interface.save() cable = Cable( termination_a=uplk_intf, termination_b=selected_interface, ) cable.save() self.log_info( "uplink switch chosen. Port {0} on {1}".format( selected_interface.name, agg_switch.name)) break if selected_interface == "": self.log_failure("No available aggregate port found. \ No aggregate port assigned.") except BaseException: self.log("failed to document uplink switch") self.log_success("Created {0}".format(device.name))
def setup_swtich(self, site, rack, pp, panel_ports, vlan, site_no, asset_tag): sw_name = "sw-%s-01.in.ffho.net" % site.slug try: sw = Device.objects.get(name=sw_name) self.log_info("Switch %s already present, carrying on." % sw_name) return sw except Device.DoesNotExist: pass sw_type = DeviceType.objects.get(manufacturer__name='Netonix', model='WS-12-250-AC') sw = Device(device_type=sw_type, device_role=DeviceRole.objects.get(name='Switch'), platform=Platform.objects.get(name='Netonix'), name=sw_name, asset_tag=asset_tag, status=DeviceStatusChoices.STATUS_PLANNED, site=site, rack=rack, position=rack.u_height - 2, face=DeviceFaceChoices.FACE_FRONT) sw.save() self.log_success("Created switch %s" % sw) # Link switch ports for panel ports for n in range(1, int(panel_ports) + 1): cable = Cable(termination_a=Interface.objects.get(device=sw, name=str(n)), termination_b=FrontPort.objects.get(device=pp, name=str(n)), status=CableStatusChoices.STATUS_PLANNED) cable.save() # Disable interfaces which aren't connected unused_ifaces = [13, 14] if panel_ports < 10: unused_ifaces.extend(list(range(int(panel_ports + 1), 10))) unused_ifaces = [str(x) for x in sorted(unused_ifaces)] for n in unused_ifaces: iface = Interface.objects.get(device=sw, name=n) iface.enabled = False iface.save() self.log_success("Disabled switch unsued ports %s" % ",".join(unused_ifaces)) # Set up Mgmt port sw_mgmt_port = Interface.objects.get(device=sw, name="10") sw_mgmt_port.mode = InterfaceModeChoices.MODE_ACCESS sw_mgmt_port.untagged_vlan = vlan sw_mgmt_port.description = "Mgmt" sw_mgmt_port.save() self.log_success("Set mgmt interface 10 to untagged VLAN %s" % vlan) # Set po1 tagged-all and bundle ports 11 + 12 into it sw_po1 = Interface.objects.get(device=sw, name='po1') sw_po1.mode = InterfaceModeChoices.MODE_TAGGED_ALL sw_po1.save() for n in [11, 12]: sw_port = Interface.objects.get(device=sw, name=str(n)) sw_port.lag = sw_po1 sw_port.save() self.log_success("Linked first %s ports of %s to %s" % (panel_ports, sw, pp)) # Set up Mgmt vlan interface + IP sw_mgmt_iface = Interface( device=sw, name="vlan%d" % vlan.vid, type=InterfaceTypeChoices.TYPE_VIRTUAL, ) sw_mgmt_iface.save() sw_mgmt_ip = IPAddress(address="172.30.%d.10/24" % site_no, interface=sw_mgmt_iface) sw_mgmt_ip.save() sw.primary_ip4 = sw_mgmt_ip sw.save() self.log_success("Configured %s on interface %s of %s" % (sw_mgmt_ip, sw_mgmt_iface, sw)) return sw
def run(self, data, commit): # Load the Snipe IT API token from file with open("/home/netbox/snipeit_token.json", "r") as f: token_data = json.load(f) token = token_data['token'] # HTTP headers for each request to Snipe IT headers = { "authorization": f"Bearer {token}", "accept": "application/json" } # Get the location from Snipe IT that corresponds to the NetBox site r = requests.get(f"{BASE_PATH}/locations", params={"name": data['site'].name}, headers=headers) snipe_data = r.json() if len(snipe_data['rows']) != 1: self.log_failure("Site not found in Snipe IT") return location = snipe_data['rows'][0] # Get Snipe IT assets (devices) assigned to the location r = requests.get(f"{BASE_PATH}/hardware", params={"location_id": location['id']}, headers=headers) snipe_devices = r.json()['rows'] # Create NetBox devices for snipe_device in snipe_devices: # Check if the device already exists by asset tag if not Device.objects.filter( asset_tag=snipe_device['asset_tag']).exists(): try: # Verify device type exists that matches the Snipe IT manufacturer and model device_type = DeviceType.objects.get( manufacturer__name=snipe_device['manufacturer'] ['name'], model=snipe_device['model']['name']) except ObjectDoesNotExist: self.log_failure( f"Matching device type for {snipe_device['model']['name']} not found" ) return try: # Verify device tyrolepe exists that matches the Snipe IT category device_role = DeviceRole.objects.get( name=snipe_device['category']['name']) except ObjectDoesNotExist: self.log_failure( f"Matching device role for {snipe_device['category']['name']} not found" ) return # Create the actual NetBox device in the site with status 'Inventory' name = slugify( f"{data['site'].slug}-{snipe_device['category']['name']}-{snipe_device['asset_tag']}" ) device = Device(name=name.lower(), site=data['site'], asset_tag=snipe_device['asset_tag'], serial=snipe_device['serial'], device_type=device_type, device_role=device_role, status=DeviceStatusChoices.STATUS_INVENTORY) device.save() self.log_success(f"Created device {device.name}")
def setup_bbr(self, site, rack, vlan, site_no, node_id, asset_tag, sw): bbr_name = "bbr-%s.in.ffho.net" % site.slug try: bbr = Device.objects.get(name=bbr_name) self.log_info("Backbone router %s already present, carrying on." % bbr_name) return bbr except Device.DoesNotExist: pass bbr_type = DeviceType.objects.get(manufacturer__name='PCEngines', model='APU2c4-19"') bbr = Device( device_type=bbr_type, device_role=DeviceRole.objects.get(name='Backbone router'), platform=Platform.objects.get(name='Linux'), name=bbr_name, asset_tag=asset_tag, status=DeviceStatusChoices.STATUS_PLANNED, site=site, rack=rack, position=rack.u_height - 4, face=DeviceFaceChoices.FACE_FRONT, ) bbr.save() self.log_success("Created backbone router %s" % bbr) # Set bond0 mode to tagged-all, bundle enp<n>s0 into it and connect enp<n>s0 to switchport 10 + n bbr_bond0 = Interface.objects.get(device=bbr, name="bond0") bbr_bond0.mode = InterfaceModeChoices.MODE_TAGGED_ALL bbr_bond0.save() # Link enp1s0 and enp2s0 to switch port 10 and 11 respectivly for n in [1, 2]: bbr_port = Interface.objects.get(device=bbr, name="enp%ds0" % n) sw_port = Interface.objects.get(device=sw, name=str(10 + n)) cable = Cable(termination_a=sw_port, termination_b=bbr_port, status=CableStatusChoices.STATUS_PLANNED) cable.save() bbr_port.lag = bbr_bond0 bbr_port.save() self.log_success("Linked %s to %s" % (bbr, sw)) # Disable enp3s0 enp3s0 = Interface.objects.get(device=bbr, name="enp3s0") enp3s0.enabled = False enp3s0.save() # Set up Mgmt vlan interface + IP bbr_mgmt_iface = Interface( device=bbr, name="vlan%d" % vlan.vid, type=InterfaceTypeChoices.TYPE_VIRTUAL, ) bbr_mgmt_iface.save() bbr_mgmt_ip = IPAddress(address="172.30.%d.1/24" % site_no, interface=bbr_mgmt_iface) bbr_mgmt_ip.save() self.log_success("Configured %s on interface %s of %s" % (bbr_mgmt_ip, bbr_mgmt_iface, bbr)) # Set up loopback IPs bbr_lo_iface = Interface.objects.get(device=bbr, name="lo") ipv4 = IPAddress(address="10.132.255.%s/32" % node_id, interface=bbr_lo_iface) ipv4.save() ipv6 = IPAddress(address="2a03:2260:2342:ffff::%s/128" % node_id, interface=bbr_lo_iface) ipv6.save() bbr.primary_ip4 = ipv4 bbr.primary_ip6 = ipv6 bbr.save() self.log_success("Configured %s + %s on lo interface of %s" % (ipv4, ipv6, bbr))
def setUp(self): user = create_test_user(permissions=['dcim.view_cable']) self.client = Client() self.client.force_login(user) site = Site(name='Site 1', slug='site-1') site.save() manufacturer = Manufacturer(name='Manufacturer 1', slug='manufacturer-1') manufacturer.save() devicetype = DeviceType(model='Device Type 1', manufacturer=manufacturer) devicetype.save() devicerole = DeviceRole(name='Device Role 1', slug='device-role-1') devicerole.save() device1 = Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) device1.save() device2 = Device(name='Device 2', site=site, device_type=devicetype, device_role=devicerole) device2.save() iface1 = Interface(device=device1, name='Interface 1', type=IFACE_TYPE_1GE_FIXED) iface1.save() iface2 = Interface(device=device1, name='Interface 2', type=IFACE_TYPE_1GE_FIXED) iface2.save() iface3 = Interface(device=device1, name='Interface 3', type=IFACE_TYPE_1GE_FIXED) iface3.save() iface4 = Interface(device=device2, name='Interface 1', type=IFACE_TYPE_1GE_FIXED) iface4.save() iface5 = Interface(device=device2, name='Interface 2', type=IFACE_TYPE_1GE_FIXED) iface5.save() iface6 = Interface(device=device2, name='Interface 3', type=IFACE_TYPE_1GE_FIXED) iface6.save() Cable(termination_a=iface1, termination_b=iface4, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface2, termination_b=iface5, type=CABLE_TYPE_CAT6).save() Cable(termination_a=iface3, termination_b=iface6, type=CABLE_TYPE_CAT6).save()
def run(self, data, commit): services_list = [ { 'id_s': 's2', 'port': 22, 'name': 'SSHv2', 'protocol': 'tcp' }, { 'id_s': 's1', 'port': 22, 'name': 'SSHv1', 'protocol': 'tcp' }, { 'id_s': 't', 'port': 23, 'name': 'Telnet', 'protocol': 'tcp' }, { 'id_s': 'y', 'port': 443, 'name': 'YANG', 'protocol': 'tcp' }, { 'id_s': 'r', 'port': 443, 'name': 'REST', 'protocol': 'tcp' }, ] dev_role = DeviceRole.objects.get(slug='access-switch') device_new = Device( name=data['dev_name'], device_type=data['dev_model'], site=data['site'], rack=data['rack'], position=data['position'], device_role=dev_role, serial=data['dev_serial'], ) device_new.save() device_new.custom_field_data['fMonitoring'] = data['monitoring'] device_new.custom_field_data['fBackup'] = data['backup'] device_new.save() output = [] for iServ in data['services']: output.append(iServ) print(output) res = [row for row in services_list if row['id_s'] == iServ] s1 = Service( device=device_new, name=res[0]['name'], ports=[res[0]['port']], protocol=res[0]['protocol'], ) s1.save() dev_mgmt_int = Interface( device=device_new, name=data['mgmt_int_name'], type='virtual', ) dev_mgmt_int.save() ipa_type = ContentType.objects.get(app_label='dcim', model='interface') ipa = IPAddress( address=data['mgmt_int_ip'], assigned_object_id=dev_mgmt_int.id, assigned_object_type=ipa_type, ) ipa.save() device_new.primary_ip4 = ipa device_new.save() self.log_success(f"Created new Juniper device: {device_new}") return ''.join(output)