def setUpTestData(cls): site = Site.objects.create(name='Site 1', slug='site-1') manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1') devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') devices = ( Device(name='Device 1', site=site, device_type=devicetype, device_role=devicerole), Device(name='Device 2', site=site, device_type=devicetype, device_role=devicerole), ) Device.objects.bulk_create(devices) services = ( Service(device=devices[0], name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=1), Service(device=devices[0], name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=2), Service(device=devices[0], name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=3), ) Service.objects.bulk_create(services) cls.create_data = [ { 'device': devices[1].pk, 'name': 'Service 4', 'protocol': ServiceProtocolChoices.PROTOCOL_TCP, 'port': 4, }, { 'device': devices[1].pk, 'name': 'Service 5', 'protocol': ServiceProtocolChoices.PROTOCOL_TCP, 'port': 5, }, { 'device': devices[1].pk, 'name': 'Service 6', 'protocol': ServiceProtocolChoices.PROTOCOL_TCP, 'port': 6, }, ]
def setUpTestData(cls): site = Site.objects.create(name='Site 1', slug='site-1') manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1') devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') device = Device.objects.create(name='Device 1', site=site, device_type=devicetype, device_role=devicerole) Service.objects.bulk_create([ Service(device=device, name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=101), Service(device=device, name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=102), Service(device=device, name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=103), ]) tags = cls.create_tags('Alpha', 'Bravo', 'Charlie') cls.form_data = { 'device': device.pk, 'virtual_machine': None, 'name': 'Service X', 'protocol': ServiceProtocolChoices.PROTOCOL_TCP, 'port': 999, 'ipaddresses': [], 'description': 'A new service', 'tags': [t.pk for t in tags], } cls.csv_data = ( "device,name,protocol,port,description", "Device 1,Service 1,TCP,1,First service", "Device 1,Service 2,TCP,2,Second service", "Device 1,Service 3,UDP,3,Third service", ) cls.bulk_edit_data = { 'protocol': ServiceProtocolChoices.PROTOCOL_UDP, 'port': 888, 'description': 'New description', }
def setUpTestData(cls): site = Site.objects.create(name='Site 1', slug='site-1') manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1') device_role = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1') devices = ( Device(device_type=device_type, name='Device 1', site=site, device_role=device_role), Device(device_type=device_type, name='Device 2', site=site, device_role=device_role), Device(device_type=device_type, name='Device 3', site=site, device_role=device_role), ) Device.objects.bulk_create(devices) clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') cluster = Cluster.objects.create(type=clustertype, name='Cluster 1') virtual_machines = ( VirtualMachine(name='Virtual Machine 1', cluster=cluster), VirtualMachine(name='Virtual Machine 2', cluster=cluster), VirtualMachine(name='Virtual Machine 3', cluster=cluster), ) VirtualMachine.objects.bulk_create(virtual_machines) services = ( Service(device=devices[0], name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=1001), Service(device=devices[1], name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=1002), Service(device=devices[2], name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_UDP, port=1003), Service(virtual_machine=virtual_machines[0], name='Service 4', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=2001), Service(virtual_machine=virtual_machines[1], name='Service 5', protocol=ServiceProtocolChoices.PROTOCOL_TCP, port=2002), Service(virtual_machine=virtual_machines[2], name='Service 6', protocol=ServiceProtocolChoices.PROTOCOL_UDP, port=2003), ) Service.objects.bulk_create(services)
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 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)