Exemple #1
0
 def create(cls,
            ethernets=None,
            sn=None,
            model=None,
            model_name=None,
            model_type=None,
            device=None,
            allow_stub=False,
            priority=0,
            **kwargs):
     if 'parent' in kwargs and kwargs['parent'] is None:
         del kwargs['parent']
     if not model and (not model_name or not model_type):
         raise ValueError(
             'Either provide model or model_type and model_name.')
     dev = device
     ethernets = [
         Eth(*e) for e in (ethernets or []) if is_mac_valid(Eth(*e))
     ]
     if ethernets:
         macs = set(
             [MACAddressField.normalize(eth.mac) for eth in ethernets])
         devs = Device.admin_objects.filter(
             ethernet__mac__in=macs).distinct()
         if len(devs) > 1:
             raise ValueError('Multiple devices match MACs: %r' % macs)
         elif len(devs) == 1:
             if dev and devs[0].id != dev.id:
                 raise ValueError('Conflict of devices %r and %r!' %
                                  (dev, devs[0]))
             else:
                 dev = devs[0]
     if sn:
         sn = sn.strip()
     if sn in SERIAL_BLACKLIST:
         sn = None
     if not any((sn, ethernets, allow_stub)):
         raise ValueError(
             "Neither `sn` nor `ethernets` given.  Use `allow_stub` "
             "to override.")
     if sn:
         try:
             sndev = Device.admin_objects.get(sn=sn)
         except Device.DoesNotExist:
             pass
         else:
             if dev is None:
                 dev = sndev
             elif sndev.id != dev.id:
                 # both devices are properly placed in the tree
                 if any((
                         sndev.parent and dev.parent,
                         # the device found using ethernets (or explicitly
                         # given as `device`) has different sn than `sn`
                         dev.sn and dev.sn != sn,
                         # the device found using `sn` already has other
                         # ethernets
                         sndev.ethernet_set.exists())):
                     raise ValueError('Conflict of devices %r and %r!' %
                                      (dev, sndev))
                 sndev.delete()
     if model is None:
         model, model_created = DeviceModel.concurrent_get_or_create(
             name=model_name,
             defaults={
                 'type': model_type.id,
             },
         )
     if dev is None:
         dev, created = Device.concurrent_get_or_create(
             sn=sn,
             defaults={
                 'model': model,
             },
         )
     elif dev.deleted:
         # Ignore the priority and undelete even if it was manually deleted
         priorities = dev.get_save_priorities()
         priorities['deleted'] = 0
         dev.update_save_priorities(priorities)
         dev.deleted = False
     if model and model.type != DeviceType.unknown.id:
         dev.model = model
     if not dev.sn and sn:
         dev.sn = sn
     for k, v in kwargs.iteritems():
         if k in ('name', 'last_seen'):
             continue
         setattr(dev, k, v)
     try:
         user = kwargs.get('user')
     except KeyError:
         user = None
     dev.save(user=user, update_last_seen=True, priority=priority)
     for eth in ethernets:
         ethernet, eth_created = Ethernet.concurrent_get_or_create(
             mac=eth.mac,
             defaults={
                 'device': dev,
             },
         )
         ethernet.device = dev
         ethernet.label = eth.label or 'Autocreated'
         if eth.speed:
             ethernet.speed = eth.speed
         ethernet.save(priority=priority)
     return dev
Exemple #2
0
 def setUp(self):
     self.client = login_as_su()
     venture = Venture(
         name=DEVICE['venture'], symbol=DEVICE['ventureSymbol']
     )
     venture.save()
     self.venture = venture
     venture_role = VentureRole(
         name=DEVICE['venture_role'], venture=self.venture
     )
     venture_role.save()
     self.venture_role = venture_role
     self.device = Device.create(
         sn=DEVICE['sn'],
         barcode=DEVICE['barcode'],
         remarks=DEVICE['remarks'],
         model_name=DEVICE['model_name'],
         model_type=DeviceType.unknown,
         venture=self.venture,
         venture_role=self.venture_role,
         rack=DEVICE['rack'],
         position=DEVICE['position'],
         dc=DATACENTER,
     )
     self.device.name = DEVICE['name']
     self.device.save()
     self.ip = IPAddress(address=DEVICE['ip'], device=self.device)
     self.ip.save()
     self.db_ip = IPAddress.objects.get(address=DEVICE['ip'])
     self.network_terminator = NetworkTerminator(name='simple_terminator')
     self.network_terminator.save()
     self.network_datacenter = DataCenter(name=DATACENTER)
     self.network_datacenter.save()
     self.network = Network(
         name=NETWORK['name'],
         address=NETWORK['address'],
         data_center=self.network_datacenter,
     )
     self.diskshare_device = Device.create(
         sn=DISKSHARE['sn'],
         barcode=DISKSHARE['barcode'],
         model_name='xxx',
         model_type=DeviceType.storage,
     )
     self.diskshare_device.name = DISKSHARE['device']
     self.diskshare_device.save()
     self.cm_generic = ComponentModel(name='GenericModel')
     self.cm_diskshare = ComponentModel(name='DiskShareModel')
     self.cm_processor = ComponentModel(name='ProcessorModel')
     self.cm_memory = ComponentModel(name='MemoryModel')
     self.cm_storage = ComponentModel(name='ComponentModel')
     self.cm_fibre = ComponentModel(name='FibreChannalMidel')
     self.cm_ethernet = ComponentModel(name='EthernetMidel')
     self.cm_software = ComponentModel(name='SoftwareModel')
     self.cm_splunkusage = ComponentModel(name='SplunkusageModel')
     self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel')
     self.generic_component = GenericComponent(
         device=self.device,
         model=self.cm_generic,
         label=COMPONENT['GenericComponent'],
         sn=GENERIC['sn'],
     )
     self.generic_component.save()
     self.diskshare = DiskShare(
         device=self.device,
         model=self.cm_diskshare,
         share_id=self.device.id,
         size=80,
         wwn=DISKSHARE['wwn'],
     )
     self.diskshare.save()
     self.disksharemount = DiskShareMount.concurrent_get_or_create(
         share=self.diskshare,
         device=self.device,
         defaults={
             'volume': COMPONENT['DiskShareMount'],
         },
     )
     self.processor = Processor(
         device=self.device,
         model=self.cm_processor,
         label=COMPONENT['Processor'],
     )
     self.processor.save()
     self.memory = Memory(
         device=self.device,
         model=self.cm_memory,
         label=COMPONENT['Memory'],
     )
     self.memory.save()
     self.storage = Storage(
         device=self.device,
         model=self.cm_storage,
         label=COMPONENT['Storage'],
     )
     self.storage.save()
     self.fibrechannel = FibreChannel(
         device=self.device,
         model=self.cm_fibre,
         label=COMPONENT['Fibre'],
         physical_id='01234',
     )
     self.fibrechannel.save()
     self.ethernet = Ethernet(
         model=self.cm_ethernet,
         device=self.device,
         mac=DEVICE['mac'],
     )
     self.ethernet.save()
     self.software = Software(
         device=self.device,
         model=self.cm_software,
         label=COMPONENT['Software'],
     )
     self.software.save()
     self.splunkusage = SplunkUsage(
         device=self.device,
         model=self.cm_splunkusage,
     )
     self.splunkusage.save()
     self.operatingsystem = OperatingSystem(
         device=self.device,
         model=self.cm_operatingsystem,
         label=COMPONENT['OS'],
     )
     self.operatingsystem.save()
     # device with strange name...
     self.strange_device = Device.create(
         sn='abcabc123123',
         model_name=DEVICE['model_name'],
         model_type=DeviceType.unknown,
         venture=self.venture,
         venture_role=self.venture_role,
         rack=DEVICE['rack'],
         position=DEVICE['position'],
         dc=DATACENTER,
     )
     self.strange_device.name = 'śćź'
     self.strange_device.save()
Exemple #3
0
    def create(cls, ethernets=None, sn=None, model=None,  model_name=None,
            model_type=None, device=None, allow_stub=False, priority=0, **kwargs):
        if 'parent' in kwargs and kwargs['parent'] is None:
            del kwargs['parent']
        if not model and (not model_name or not model_type):
            raise ValueError('Either provide model or model_type and model_name.')
        dev = device
        ethernets = [Eth(*e) for e in (ethernets or []) if is_mac_valid(Eth(*e))]
        if ethernets:
            macs = set([MACAddressField.normalize(eth.mac) for eth in ethernets])
            devs = Device.objects.filter(ethernet__mac__in=macs).distinct()
            if len(devs) > 1:
                raise ValueError('Multiple devices match MACs: %r' % macs)
            elif len(devs) == 1:
                if dev and devs[0].id != dev.id:
                    raise ValueError('Conflict of devices %r and %r!' % (
                        dev, devs[0]))
                else:
                    dev = devs[0]
        if sn:
            sn = sn.strip()
        if sn in SERIAL_BLACKLIST:
            sn = None
        if not any((sn, ethernets, allow_stub)):
            raise ValueError("Neither `sn` nor `ethernets` given. "
                "Use `allow_stub` to override.")
        if sn:
            try:
                sndev = Device.objects.get(sn=sn)
            except Device.DoesNotExist:
                pass
            else:
                if dev is None:
                    dev = sndev
                elif sndev.id != dev.id:
                    if any((# both devices are properly placed in the tree
                            sndev.parent and dev.parent,
                            # the device found using ethernets (or explicitly
                            # given as `device`) has different sn than `sn` 
                            dev.sn and dev.sn != sn,
                            # the device found using `sn` already has other
                            # ethernets
                            sndev.ethernet_set.exists())):
                        raise ValueError('Conflict of devices %r and %r!' %
                                (dev, sndev))
                    sndev.delete()
        if model is None:
            model, model_created = DeviceModel.concurrent_get_or_create(
                name=model_name, type=model_type.id)
        if dev is None:
            dev, created = Device.concurrent_get_or_create(sn=sn, model=model)
        if model and model.type != DeviceType.unknown.id:
            dev.model = model

        if not dev.sn and sn:
            dev.sn = sn
        for k, v in kwargs.iteritems():
            if k in ('name', 'last_seen'):
                continue
            setattr(dev, k, v)
        dev.save(update_last_seen=True, priority=priority)

        for eth in ethernets:
            ethernet, eth_created = Ethernet.concurrent_get_or_create(
                    device=dev, mac=eth.mac)
            if eth_created:
                ethernet.label = eth.label or 'Autocreated'
                if eth.speed:
                    ethernet.speed = eth.speed
            ethernet.save(priority=priority)
        return dev
Exemple #4
0
 def setUp(self):
     self.client = login_as_su()
     venture = Venture(name=DEVICE["venture"], symbol=DEVICE["ventureSymbol"])
     venture.save()
     self.venture = venture
     venture_role = VentureRole(name=DEVICE["venture_role"], venture=self.venture)
     venture_role.save()
     self.venture_role = venture_role
     self.device = Device.create(
         sn=DEVICE["sn"],
         barcode=DEVICE["barcode"],
         remarks=DEVICE["remarks"],
         model_name=DEVICE["model_name"],
         model_type=DeviceType.unknown,
         venture=self.venture,
         venture_role=self.venture_role,
         rack=DEVICE["rack"],
         position=DEVICE["position"],
         dc=DATACENTER,
     )
     self.device.name = DEVICE["name"]
     self.device.save()
     self.ip = IPAddress(address=DEVICE["ip"], device=self.device)
     self.ip.save()
     self.db_ip = IPAddress.objects.get(address=DEVICE["ip"])
     self.network_terminator = NetworkTerminator(name="simple_terminator")
     self.network_terminator.save()
     self.network_datacenter = DataCenter(name=DATACENTER)
     self.network_datacenter.save()
     self.network = Network(name=NETWORK["name"], address=NETWORK["address"], data_center=self.network_datacenter)
     self.diskshare_device = Device.create(
         sn=DISKSHARE["sn"], barcode=DISKSHARE["barcode"], model_name="xxx", model_type=DeviceType.storage
     )
     self.diskshare_device.name = DISKSHARE["device"]
     self.diskshare_device.save()
     self.cm_generic = ComponentModel(name="GenericModel")
     self.cm_diskshare = ComponentModel(name="DiskShareModel")
     self.cm_processor = ComponentModel(name="ProcessorModel")
     self.cm_memory = ComponentModel(name="MemoryModel")
     self.cm_storage = ComponentModel(name="ComponentModel")
     self.cm_fibre = ComponentModel(name="FibreChannalMidel")
     self.cm_ethernet = ComponentModel(name="EthernetMidel")
     self.cm_software = ComponentModel(name="SoftwareModel")
     self.cm_splunkusage = ComponentModel(name="SplunkusageModel")
     self.cm_operatingsystem = ComponentModel(name="OperatingSystemModel")
     self.generic_component = GenericComponent(
         device=self.device, model=self.cm_generic, label=COMPONENT["GenericComponent"], sn=GENERIC["sn"]
     )
     self.generic_component.save()
     self.diskshare = DiskShare(
         device=self.device, model=self.cm_diskshare, share_id=self.device.id, size=80, wwn=DISKSHARE["wwn"]
     )
     self.diskshare.save()
     self.disksharemount = DiskShareMount.concurrent_get_or_create(
         share=self.diskshare, device=self.device, volume=COMPONENT["DiskShareMount"]
     )
     self.processor = Processor(device=self.device, model=self.cm_processor, label=COMPONENT["Processor"])
     self.processor.save()
     self.memory = Memory(device=self.device, model=self.cm_memory, label=COMPONENT["Memory"])
     self.memory.save()
     self.storage = Storage(device=self.device, model=self.cm_storage, label=COMPONENT["Storage"])
     self.storage.save()
     self.fibrechannel = FibreChannel(
         device=self.device, model=self.cm_fibre, label=COMPONENT["Fibre"], physical_id="01234"
     )
     self.fibrechannel.save()
     self.ethernet = Ethernet(model=self.cm_ethernet, device=self.device, mac=DEVICE["mac"])
     self.ethernet.save()
     self.software = Software(device=self.device, model=self.cm_software, label=COMPONENT["Software"])
     self.software.save()
     self.splunkusage = SplunkUsage(device=self.device, model=self.cm_splunkusage)
     self.splunkusage.save()
     self.operatingsystem = OperatingSystem(device=self.device, model=self.cm_operatingsystem, label=COMPONENT["OS"])
     self.operatingsystem.save()
Exemple #5
0
class SearchTest(TestCase):

    """
    TODO:
    1. when search return more than 1 result
    2. verification objects in html
    """

    def setUp(self):
        self.client = login_as_su()
        venture = Venture(
            name=DEVICE['venture'], symbol=DEVICE['ventureSymbol']
        )
        venture.save()
        self.venture = venture
        venture_role = VentureRole(
            name=DEVICE['venture_role'], venture=self.venture
        )
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE['sn'],
            barcode=DEVICE['barcode'],
            remarks=DEVICE['remarks'],
            model_name=DEVICE['model_name'],
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE['rack'],
            position=DEVICE['position'],
            dc=DATACENTER,
        )
        self.device.name = DEVICE['name']
        self.device.save()
        self.ip = IPAddress(address=DEVICE['ip'], device=self.device)
        self.ip.save()
        self.db_ip = IPAddress.objects.get(address=DEVICE['ip'])
        self.network_terminator = NetworkTerminator(name='simple_terminator')
        self.network_terminator.save()
        self.network_datacenter = DataCenter(name=DATACENTER)
        self.network_datacenter.save()
        self.network = Network(
            name=NETWORK['name'],
            address=NETWORK['address'],
            data_center=self.network_datacenter,
        )
        self.diskshare_device = Device.create(
            sn=DISKSHARE['sn'],
            barcode=DISKSHARE['barcode'],
            model_name='xxx',
            model_type=DeviceType.storage,
        )
        self.diskshare_device.name = DISKSHARE['device']
        self.diskshare_device.save()
        self.cm_generic = ComponentModel(name='GenericModel')
        self.cm_diskshare = ComponentModel(name='DiskShareModel')
        self.cm_processor = ComponentModel(name='ProcessorModel')
        self.cm_memory = ComponentModel(name='MemoryModel')
        self.cm_storage = ComponentModel(name='ComponentModel')
        self.cm_fibre = ComponentModel(name='FibreChannalMidel')
        self.cm_ethernet = ComponentModel(name='EthernetMidel')
        self.cm_software = ComponentModel(name='SoftwareModel')
        self.cm_splunkusage = ComponentModel(name='SplunkusageModel')
        self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel')
        self.generic_component = GenericComponent(
            device=self.device,
            model=self.cm_generic,
            label=COMPONENT['GenericComponent'],
            sn=GENERIC['sn'],
        )
        self.generic_component.save()
        self.diskshare = DiskShare(
            device=self.device,
            model=self.cm_diskshare,
            share_id=self.device.id,
            size=80,
            wwn=DISKSHARE['wwn'],
        )
        self.diskshare.save()
        self.disksharemount = DiskShareMount.concurrent_get_or_create(
            share=self.diskshare,
            device=self.device,
            defaults={
                'volume': COMPONENT['DiskShareMount'],
            },
        )
        self.processor = Processor(
            device=self.device,
            model=self.cm_processor,
            label=COMPONENT['Processor'],
        )
        self.processor.save()
        self.memory = Memory(
            device=self.device,
            model=self.cm_memory,
            label=COMPONENT['Memory'],
        )
        self.memory.save()
        self.storage = Storage(
            device=self.device,
            model=self.cm_storage,
            label=COMPONENT['Storage'],
        )
        self.storage.save()
        self.fibrechannel = FibreChannel(
            device=self.device,
            model=self.cm_fibre,
            label=COMPONENT['Fibre'],
            physical_id='01234',
        )
        self.fibrechannel.save()
        self.ethernet = Ethernet(
            model=self.cm_ethernet,
            device=self.device,
            mac=DEVICE['mac'],
        )
        self.ethernet.save()
        self.software = Software(
            device=self.device,
            model=self.cm_software,
            label=COMPONENT['Software'],
        )
        self.software.save()
        self.splunkusage = SplunkUsage(
            device=self.device,
            model=self.cm_splunkusage,
        )
        self.splunkusage.save()
        self.operatingsystem = OperatingSystem(
            device=self.device,
            model=self.cm_operatingsystem,
            label=COMPONENT['OS'],
        )
        self.operatingsystem.save()
        # device with strange name...
        self.strange_device = Device.create(
            sn='abcabc123123',
            model_name=DEVICE['model_name'],
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE['rack'],
            position=DEVICE['position'],
            dc=DATACENTER,
        )
        self.strange_device.name = 'śćź'
        self.strange_device.save()

    def test_access_to_device(self):
        # User has perm to device list and device details
        device_list = self.client.get('/ui/search/info/')
        self.assertEqual(device_list.status_code, 200)
        url = '/ui/search/info/%s' % self.device.id
        device_details = self.client.get(url, follow=True)
        self.assertEqual(device_details.status_code, 200)

    def test_name_field_old(self):
        # Search objects in response.context
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(context.name, self.device.name)

    def test_address_or_network_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        # test ip
        self.assertEqual(context.name, self.device.name)
        ip = context.ipaddress_set.filter(address=DEVICE['ip']).count()
        self.assertTrue(ip > 0)
        # test network
        """
        FIXME - i can`t tests network, i need more info !
        """

    def test_remarks_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(context.remarks, DEVICE['remarks'])

    def test_venture_or_role_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.venture.name, DEVICE['venture'])
        self.assertEqual(context.venture.name, DEVICE['venture'])
        self.assertEqual(context.venture_role_id, self.venture_role.id)
        self.assertEqual(context.venture.symbol, self.venture.symbol)

    def test_model_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.model.type, DeviceType.unknown)
        self.assertEqual(context.model.type, DeviceType.unknown)
        self.assertEqual(context.model.name, DEVICE['model_name'])

    def test_component_or_software_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        processor = context.processor_set.filter(device=self.device.id).count()
        self.assertTrue(processor > 0)

    def test_serial_number_mac_or_wwn_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.sn, context.sn)
        mac = context.ethernet_set.filter(mac=DEVICE['mac']).count()
        self.assertTrue(mac > 0)
        diskshare = context.diskshare_set.filter(device=self.device.id).count()
        self.assertTrue(diskshare > 0)
        dsm = context.disksharemount_set.filter(device=self.device.id).count()
        self.assertTrue(dsm > 0)

    def test_barcode_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.barcode, DEVICE['barcode'])
        self.assertEqual(context.barcode, DEVICE['barcode'])

    def test_datacenter_rack_or_position_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.position, DEVICE['position'])
        self.assertEqual(context.position, DEVICE['position'])
        self.assertEqual(self.device.rack, DEVICE['rack'])
        self.assertEqual(context.rack, DEVICE['rack'])
        self.assertEqual(self.device.dc, DATACENTER)
        self.assertEqual(context.dc, DATACENTER)

    def test_history_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        db_query = HistoryChange.objects.get(
            device=self.device, new_value=DEVICE['name'],
        )
        object_query = context.historychange_set.get(
            device=self.device, new_value=DEVICE['name'],
        )
        self.assertEqual(db_query.old_value, '')
        self.assertEqual(db_query.new_value, DEVICE['name'])
        self.assertEqual(object_query.old_value, '')
        self.assertEqual(object_query.new_value, DEVICE['name'])

    def test_device_type_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.model.type, DeviceType.unknown.id)
        self.assertEqual(context.model.type, DeviceType.unknown)

    def test_device_name_with_strange_characters(self):
        url = '/ui/search/info/?name=śćź'
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(context.name, u'śćź')
Exemple #6
0
class TestSearch(TestCase):
    """
    TODO:
    1. when search return more than 1 result
    2. verification objects in html
    """

    def setUp(self):
        self.client = login_as_su()
        venture = Venture(name=DEVICE["venture"], symbol=DEVICE["ventureSymbol"])
        venture.save()
        self.venture = venture
        venture_role = VentureRole(name=DEVICE["venture_role"], venture=self.venture)
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE["sn"],
            barcode=DEVICE["barcode"],
            remarks=DEVICE["remarks"],
            model_name=DEVICE["model_name"],
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE["rack"],
            position=DEVICE["position"],
            dc=DATACENTER,
        )
        self.device.name = DEVICE["name"]
        self.device.save()
        self.ip = IPAddress(address=DEVICE["ip"], device=self.device)
        self.ip.save()
        self.db_ip = IPAddress.objects.get(address=DEVICE["ip"])
        self.network_terminator = NetworkTerminator(name="simple_terminator")
        self.network_terminator.save()
        self.network_datacenter = DataCenter(name=DATACENTER)
        self.network_datacenter.save()
        self.network = Network(name=NETWORK["name"], address=NETWORK["address"], data_center=self.network_datacenter)
        self.diskshare_device = Device.create(
            sn=DISKSHARE["sn"], barcode=DISKSHARE["barcode"], model_name="xxx", model_type=DeviceType.storage
        )
        self.diskshare_device.name = DISKSHARE["device"]
        self.diskshare_device.save()
        self.cm_generic = ComponentModel(name="GenericModel")
        self.cm_diskshare = ComponentModel(name="DiskShareModel")
        self.cm_processor = ComponentModel(name="ProcessorModel")
        self.cm_memory = ComponentModel(name="MemoryModel")
        self.cm_storage = ComponentModel(name="ComponentModel")
        self.cm_fibre = ComponentModel(name="FibreChannalMidel")
        self.cm_ethernet = ComponentModel(name="EthernetMidel")
        self.cm_software = ComponentModel(name="SoftwareModel")
        self.cm_splunkusage = ComponentModel(name="SplunkusageModel")
        self.cm_operatingsystem = ComponentModel(name="OperatingSystemModel")
        self.generic_component = GenericComponent(
            device=self.device, model=self.cm_generic, label=COMPONENT["GenericComponent"], sn=GENERIC["sn"]
        )
        self.generic_component.save()
        self.diskshare = DiskShare(
            device=self.device, model=self.cm_diskshare, share_id=self.device.id, size=80, wwn=DISKSHARE["wwn"]
        )
        self.diskshare.save()
        self.disksharemount = DiskShareMount.concurrent_get_or_create(
            share=self.diskshare, device=self.device, volume=COMPONENT["DiskShareMount"]
        )
        self.processor = Processor(device=self.device, model=self.cm_processor, label=COMPONENT["Processor"])
        self.processor.save()
        self.memory = Memory(device=self.device, model=self.cm_memory, label=COMPONENT["Memory"])
        self.memory.save()
        self.storage = Storage(device=self.device, model=self.cm_storage, label=COMPONENT["Storage"])
        self.storage.save()
        self.fibrechannel = FibreChannel(
            device=self.device, model=self.cm_fibre, label=COMPONENT["Fibre"], physical_id="01234"
        )
        self.fibrechannel.save()
        self.ethernet = Ethernet(model=self.cm_ethernet, device=self.device, mac=DEVICE["mac"])
        self.ethernet.save()
        self.software = Software(device=self.device, model=self.cm_software, label=COMPONENT["Software"])
        self.software.save()
        self.splunkusage = SplunkUsage(device=self.device, model=self.cm_splunkusage)
        self.splunkusage.save()
        self.operatingsystem = OperatingSystem(device=self.device, model=self.cm_operatingsystem, label=COMPONENT["OS"])
        self.operatingsystem.save()

    def test_access_to_device(self):
        # User has perm to device list and device details
        device_list = self.client.get("/ui/search/info/")
        self.assertEqual(device_list.status_code, 200)
        url = "/ui/search/info/%s" % self.device.id
        device_details = self.client.get(url, follow=True)
        self.assertEqual(device_details.status_code, 200)

    def test_name_field_old(self):
        # Search objects in response.context
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        self.assertEqual(context.name, self.device.name)

    def test_address_or_network_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        # test ip
        self.assertEqual(context.name, self.device.name)
        ip = context.ipaddress_set.filter(address=DEVICE["ip"]).count()
        self.assertTrue(ip > 0)
        # test network
        """
        FIXME - i can`t tests network, i need more info !
        """

    def test_remarks_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        self.assertEqual(context.remarks, DEVICE["remarks"])

    def test_venture_or_role_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        self.assertEqual(self.device.venture.name, DEVICE["venture"])
        self.assertEqual(context.venture.name, DEVICE["venture"])
        self.assertEqual(context.venture_role_id, self.venture_role.id)
        self.assertEqual(context.venture.symbol, self.venture.symbol)

    def test_model_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        self.assertEqual(self.device.model.type, DeviceType.unknown)
        self.assertEqual(context.model.type, DeviceType.unknown)
        self.assertEqual(context.model.name, DEVICE["model_name"])

    def test_component_or_software_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        processor = context.processor_set.filter(device=self.device.id).count()
        self.assertTrue(processor > 0)

    def test_serial_number_mac_or_wwn_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url)
        context = device_search.context["object"]
        self.assertEqual(self.device.sn, context.sn)
        mac = context.ethernet_set.filter(mac=DEVICE["mac"]).count()
        self.assertTrue(mac > 0)
        diskshare = context.diskshare_set.filter(device=self.device.id).count()
        self.assertTrue(diskshare > 0)
        dsm = context.disksharemount_set.filter(device=self.device.id).count()
        self.assertTrue(dsm > 0)

    def test_barcode_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context["object"]
        self.assertEqual(self.device.barcode, DEVICE["barcode"])
        self.assertEqual(context.barcode, DEVICE["barcode"])

    def test_datacenter_rack_or_position_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context["object"]
        self.assertEqual(self.device.position, DEVICE["position"])
        self.assertEqual(context.position, DEVICE["position"])
        self.assertEqual(self.device.rack, DEVICE["rack"])
        self.assertEqual(context.rack, DEVICE["rack"])
        self.assertEqual(self.device.dc, DATACENTER)
        self.assertEqual(context.dc, DATACENTER)

    def test_history_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context["object"]
        db_query = HistoryChange.objects.get(device=self.device, new_value=DEVICE["name"])
        object_query = context.historychange_set.get(device=self.device, new_value=DEVICE["name"])
        self.assertEqual(db_query.old_value, "")
        self.assertEqual(db_query.new_value, DEVICE["name"])
        self.assertEqual(object_query.old_value, "")
        self.assertEqual(object_query.new_value, DEVICE["name"])

    def test_device_type_field(self):
        url = "/ui/search/info/%s" % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context["object"]
        self.assertEqual(self.device.model.type, DeviceType.unknown.id)
        self.assertEqual(context.model.type, DeviceType.unknown)
Exemple #7
0
 def setUp(self):
     self.client = login_as_su()
     venture = Venture(name=DEVICE['venture'],
                       symbol=DEVICE['ventureSymbol'])
     venture.save()
     self.venture = venture
     venture_role = VentureRole(name=DEVICE['venture_role'],
                                venture=self.venture)
     venture_role.save()
     self.venture_role = venture_role
     self.device = Device.create(
         sn=DEVICE['sn'],
         barcode=DEVICE['barcode'],
         remarks=DEVICE['remarks'],
         model_name=DEVICE['model_name'],
         model_type=DeviceType.unknown,
         venture=self.venture,
         venture_role=self.venture_role,
         rack=DEVICE['rack'],
         position=DEVICE['position'],
         dc=DATACENTER,
     )
     self.device.name = DEVICE['name']
     self.device.save()
     self.ip = IPAddress(address=DEVICE['ip'], device=self.device)
     self.ip.save()
     self.db_ip = IPAddress.objects.get(address=DEVICE['ip'])
     self.network_terminator = NetworkTerminator(name='simple_terminator')
     self.network_terminator.save()
     self.network_datacenter = DataCenter(name=DATACENTER)
     self.network_datacenter.save()
     self.network = Network(
         name=NETWORK['name'],
         address=NETWORK['address'],
         data_center=self.network_datacenter,
     )
     self.diskshare_device = Device.create(
         sn=DISKSHARE['sn'],
         barcode=DISKSHARE['barcode'],
         model_name='xxx',
         model_type=DeviceType.storage,
     )
     self.diskshare_device.name = DISKSHARE['device']
     self.diskshare_device.save()
     self.cm_generic = ComponentModel(name='GenericModel')
     self.cm_diskshare = ComponentModel(name='DiskShareModel')
     self.cm_processor = ComponentModel(name='ProcessorModel')
     self.cm_memory = ComponentModel(name='MemoryModel')
     self.cm_storage = ComponentModel(name='ComponentModel')
     self.cm_fibre = ComponentModel(name='FibreChannalMidel')
     self.cm_ethernet = ComponentModel(name='EthernetMidel')
     self.cm_software = ComponentModel(name='SoftwareModel')
     self.cm_splunkusage = ComponentModel(name='SplunkusageModel')
     self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel')
     self.generic_component = GenericComponent(
         device=self.device,
         model=self.cm_generic,
         label=COMPONENT['GenericComponent'],
         sn=GENERIC['sn'],
     )
     self.generic_component.save()
     self.diskshare = DiskShare(
         device=self.device,
         model=self.cm_diskshare,
         share_id=self.device.id,
         size=80,
         wwn=DISKSHARE['wwn'],
     )
     self.diskshare.save()
     self.disksharemount = DiskShareMount.concurrent_get_or_create(
         share=self.diskshare,
         device=self.device,
         volume=COMPONENT['DiskShareMount'],
     )
     self.processor = Processor(
         device=self.device,
         model=self.cm_processor,
         label=COMPONENT['Processor'],
     )
     self.processor.save()
     self.memory = Memory(
         device=self.device,
         model=self.cm_memory,
         label=COMPONENT['Memory'],
     )
     self.memory.save()
     self.storage = Storage(
         device=self.device,
         model=self.cm_storage,
         label=COMPONENT['Storage'],
     )
     self.storage.save()
     self.fibrechannel = FibreChannel(
         device=self.device,
         model=self.cm_fibre,
         label=COMPONENT['Fibre'],
         physical_id='01234',
     )
     self.fibrechannel.save()
     self.ethernet = Ethernet(
         model=self.cm_ethernet,
         device=self.device,
         mac=DEVICE['mac'],
     )
     self.ethernet.save()
     self.software = Software(
         device=self.device,
         model=self.cm_software,
         label=COMPONENT['Software'],
     )
     self.software.save()
     self.splunkusage = SplunkUsage(
         device=self.device,
         model=self.cm_splunkusage,
     )
     self.splunkusage.save()
     self.operatingsystem = OperatingSystem(
         device=self.device,
         model=self.cm_operatingsystem,
         label=COMPONENT['OS'],
     )
     self.operatingsystem.save()
Exemple #8
0
class TestSearch(TestCase):
    """
    TODO:
    1. when search return more than 1 result
    2. verification objects in html
    """
    def setUp(self):
        self.client = login_as_su()
        venture = Venture(name=DEVICE['venture'],
                          symbol=DEVICE['ventureSymbol'])
        venture.save()
        self.venture = venture
        venture_role = VentureRole(name=DEVICE['venture_role'],
                                   venture=self.venture)
        venture_role.save()
        self.venture_role = venture_role
        self.device = Device.create(
            sn=DEVICE['sn'],
            barcode=DEVICE['barcode'],
            remarks=DEVICE['remarks'],
            model_name=DEVICE['model_name'],
            model_type=DeviceType.unknown,
            venture=self.venture,
            venture_role=self.venture_role,
            rack=DEVICE['rack'],
            position=DEVICE['position'],
            dc=DATACENTER,
        )
        self.device.name = DEVICE['name']
        self.device.save()
        self.ip = IPAddress(address=DEVICE['ip'], device=self.device)
        self.ip.save()
        self.db_ip = IPAddress.objects.get(address=DEVICE['ip'])
        self.network_terminator = NetworkTerminator(name='simple_terminator')
        self.network_terminator.save()
        self.network_datacenter = DataCenter(name=DATACENTER)
        self.network_datacenter.save()
        self.network = Network(
            name=NETWORK['name'],
            address=NETWORK['address'],
            data_center=self.network_datacenter,
        )
        self.diskshare_device = Device.create(
            sn=DISKSHARE['sn'],
            barcode=DISKSHARE['barcode'],
            model_name='xxx',
            model_type=DeviceType.storage,
        )
        self.diskshare_device.name = DISKSHARE['device']
        self.diskshare_device.save()
        self.cm_generic = ComponentModel(name='GenericModel')
        self.cm_diskshare = ComponentModel(name='DiskShareModel')
        self.cm_processor = ComponentModel(name='ProcessorModel')
        self.cm_memory = ComponentModel(name='MemoryModel')
        self.cm_storage = ComponentModel(name='ComponentModel')
        self.cm_fibre = ComponentModel(name='FibreChannalMidel')
        self.cm_ethernet = ComponentModel(name='EthernetMidel')
        self.cm_software = ComponentModel(name='SoftwareModel')
        self.cm_splunkusage = ComponentModel(name='SplunkusageModel')
        self.cm_operatingsystem = ComponentModel(name='OperatingSystemModel')
        self.generic_component = GenericComponent(
            device=self.device,
            model=self.cm_generic,
            label=COMPONENT['GenericComponent'],
            sn=GENERIC['sn'],
        )
        self.generic_component.save()
        self.diskshare = DiskShare(
            device=self.device,
            model=self.cm_diskshare,
            share_id=self.device.id,
            size=80,
            wwn=DISKSHARE['wwn'],
        )
        self.diskshare.save()
        self.disksharemount = DiskShareMount.concurrent_get_or_create(
            share=self.diskshare,
            device=self.device,
            volume=COMPONENT['DiskShareMount'],
        )
        self.processor = Processor(
            device=self.device,
            model=self.cm_processor,
            label=COMPONENT['Processor'],
        )
        self.processor.save()
        self.memory = Memory(
            device=self.device,
            model=self.cm_memory,
            label=COMPONENT['Memory'],
        )
        self.memory.save()
        self.storage = Storage(
            device=self.device,
            model=self.cm_storage,
            label=COMPONENT['Storage'],
        )
        self.storage.save()
        self.fibrechannel = FibreChannel(
            device=self.device,
            model=self.cm_fibre,
            label=COMPONENT['Fibre'],
            physical_id='01234',
        )
        self.fibrechannel.save()
        self.ethernet = Ethernet(
            model=self.cm_ethernet,
            device=self.device,
            mac=DEVICE['mac'],
        )
        self.ethernet.save()
        self.software = Software(
            device=self.device,
            model=self.cm_software,
            label=COMPONENT['Software'],
        )
        self.software.save()
        self.splunkusage = SplunkUsage(
            device=self.device,
            model=self.cm_splunkusage,
        )
        self.splunkusage.save()
        self.operatingsystem = OperatingSystem(
            device=self.device,
            model=self.cm_operatingsystem,
            label=COMPONENT['OS'],
        )
        self.operatingsystem.save()

    def test_access_to_device(self):
        #User has perm to device list and device details
        device_list = self.client.get('/ui/search/info/')
        self.assertEqual(device_list.status_code, 200)
        url = '/ui/search/info/%s' % self.device.id
        device_details = self.client.get(url, follow=True)
        self.assertEqual(device_details.status_code, 200)

    def test_name_field_old(self):
        #Search objects in response.context
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(context.name, self.device.name)

    def test_address_or_network_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        #test ip
        self.assertEqual(context.name, self.device.name)
        ip = context.ipaddress_set.filter(address=DEVICE['ip']).count()
        self.assertTrue(ip > 0)
        #test network
        """
        FIXME - i can`t tests network, i need more info !
        """

    def test_remarks_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(context.remarks, DEVICE['remarks'])

    def test_venture_or_role_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.venture.name, DEVICE['venture'])
        self.assertEqual(context.venture.name, DEVICE['venture'])
        self.assertEqual(context.venture_role_id, self.venture_role.id)
        self.assertEqual(context.venture.symbol, self.venture.symbol)

    def test_model_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.model.type, DeviceType.unknown)
        self.assertEqual(context.model.type, DeviceType.unknown)
        self.assertEqual(context.model.name, DEVICE['model_name'])

    def test_component_or_software_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        processor = context.processor_set.filter(device=self.device.id).count()
        self.assertTrue(processor > 0)

    def test_serial_number_mac_or_wwn_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url)
        context = device_search.context['object']
        self.assertEqual(self.device.sn, context.sn)
        mac = context.ethernet_set.filter(mac=DEVICE['mac']).count()
        self.assertTrue(mac > 0)
        diskshare = context.diskshare_set.filter(device=self.device.id).count()
        self.assertTrue(diskshare > 0)
        dsm = context.disksharemount_set.filter(device=self.device.id).count()
        self.assertTrue(dsm > 0)

    def test_barcode_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.barcode, DEVICE['barcode'])
        self.assertEqual(context.barcode, DEVICE['barcode'])

    def test_datacenter_rack_or_position_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.position, DEVICE['position'])
        self.assertEqual(context.position, DEVICE['position'])
        self.assertEqual(self.device.rack, DEVICE['rack'])
        self.assertEqual(context.rack, DEVICE['rack'])
        self.assertEqual(self.device.dc, DATACENTER)
        self.assertEqual(context.dc, DATACENTER)

    def test_history_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        db_query = HistoryChange.objects.get(
            device=self.device,
            new_value=DEVICE['name'],
        )
        object_query = context.historychange_set.get(
            device=self.device,
            new_value=DEVICE['name'],
        )
        self.assertEqual(db_query.old_value, '')
        self.assertEqual(db_query.new_value, DEVICE['name'])
        self.assertEqual(object_query.old_value, '')
        self.assertEqual(object_query.new_value, DEVICE['name'])

    def test_device_type_field(self):
        url = '/ui/search/info/%s' % self.device.id
        device_search = self.client.get(url, follow=True)
        context = device_search.context['object']
        self.assertEqual(self.device.model.type, DeviceType.unknown.id)
        self.assertEqual(context.model.type, DeviceType.unknown)
Exemple #9
0
 def create(cls, ethernets=None, sn=None, model=None, model_name=None,
            model_type=None, device=None, allow_stub=False, priority=0,
            **kwargs):
     if 'parent' in kwargs and kwargs['parent'] is None:
         del kwargs['parent']
     if not model and (not model_name or not model_type):
         raise ValueError(
             'Either provide model or model_type and model_name.'
         )
     dev = device
     ethernets = [Eth(*e) for e in (ethernets or []) if
                  is_mac_valid(Eth(*e))]
     if ethernets:
         macs = set([MACAddressField.normalize(eth.mac) for
                     eth in ethernets])
         devs = Device.admin_objects.filter(
             ethernet__mac__in=macs
         ).distinct()
         if len(devs) > 1:
             raise ValueError('Multiple devices match MACs: %r' % macs)
         elif len(devs) == 1:
             if dev and devs[0].id != dev.id:
                 raise ValueError('Conflict of devices %r and %r!' % (
                     dev, devs[0]))
             else:
                 dev = devs[0]
     if sn:
         sn = sn.strip()
     if sn in SERIAL_BLACKLIST:
         # we don't raise an exception here because blacklisted/missing sn
         # is not enough to fail device's creation
         sn = None
     if not any((sn, ethernets, allow_stub)):
         if sn in SERIAL_BLACKLIST:
             msg = ("You have provided `sn` which is blacklisted. "
                    "Please use a different one.")
         else:
             msg = ("Neither `sn` nor `ethernets` given.  Use `allow_stub` "
                    "to override.")
         raise ValueError(msg)
     if sn:
         try:
             sndev = Device.admin_objects.get(sn=sn)
         except Device.DoesNotExist:
             pass
         else:
             if dev is None:
                 dev = sndev
             elif sndev.id != dev.id:
                 # both devices are properly placed in the tree
                 if any((
                         sndev.parent and dev.parent,
                         # the device found using ethernets (or explicitly
                         # given as `device`) has different sn than `sn`
                         dev.sn and dev.sn != sn,
                         # the device found using `sn` already has other
                         # ethernets
                         sndev.ethernet_set.exists())):
                     raise ValueError(
                         'Conflict of devices %r and %r!' % (dev, sndev)
                     )
                 sndev.delete()
     if model is None:
         model, model_created = DeviceModel.concurrent_get_or_create(
             name=model_name,
             defaults={
                 'type': model_type.id,
             },
         )
     if dev is None:
         dev, created = Device.concurrent_get_or_create(
             sn=sn,
             defaults={
                 'model': model,
             },
         )
     elif dev.deleted:
         # Ignore the priority and undelete even if it was manually deleted
         priorities = dev.get_save_priorities()
         priorities['deleted'] = 0
         dev.update_save_priorities(priorities)
         dev.deleted = False
     if model and model.type != DeviceType.unknown.id:
         dev.model = model
     if not dev.sn and sn:
         dev.sn = sn
     for k, v in kwargs.iteritems():
         if k in ('name', 'last_seen'):
             continue
         setattr(dev, k, v)
     try:
         user = kwargs.get('user')
     except KeyError:
         user = None
     dev.save(user=user, update_last_seen=True, priority=priority)
     for eth in ethernets:
         ethernet, eth_created = Ethernet.concurrent_get_or_create(
             mac=eth.mac,
             defaults={
                 'device': dev,
             },
         )
         ethernet.device = dev
         ethernet.label = eth.label or 'Autocreated'
         if eth.speed:
             ethernet.speed = eth.speed
         ethernet.save(priority=priority)
     return dev
Exemple #10
0
 def create(
     cls,
     ethernets=None,
     sn=None,
     model=None,
     model_name=None,
     model_type=None,
     device=None,
     allow_stub=False,
     priority=0,
     **kwargs
 ):
     if "parent" in kwargs and kwargs["parent"] is None:
         del kwargs["parent"]
     if not model and (not model_name or not model_type):
         raise ValueError("Either provide model or model_type and model_name.")
     dev = device
     ethernets = [Eth(*e) for e in (ethernets or []) if is_mac_valid(Eth(*e))]
     if ethernets:
         macs = set([MACAddressField.normalize(eth.mac) for eth in ethernets])
         devs = Device.admin_objects.filter(ethernet__mac__in=macs).distinct()
         if len(devs) > 1:
             raise ValueError("Multiple devices match MACs: %r" % macs)
         elif len(devs) == 1:
             if dev and devs[0].id != dev.id:
                 raise ValueError("Conflict of devices %r and %r!" % (dev, devs[0]))
             else:
                 dev = devs[0]
     if sn:
         sn = sn.strip()
     if sn in SERIAL_BLACKLIST:
         sn = None
     if not any((sn, ethernets, allow_stub)):
         raise ValueError("Neither `sn` nor `ethernets` given.  Use `allow_stub` " "to override.")
     if sn:
         try:
             sndev = Device.admin_objects.get(sn=sn)
         except Device.DoesNotExist:
             pass
         else:
             if dev is None:
                 dev = sndev
             elif sndev.id != dev.id:
                 # both devices are properly placed in the tree
                 if any(
                     (
                         sndev.parent and dev.parent,
                         # the device found using ethernets (or explicitly
                         # given as `device`) has different sn than `sn`
                         dev.sn and dev.sn != sn,
                         # the device found using `sn` already has other
                         # ethernets
                         sndev.ethernet_set.exists(),
                     )
                 ):
                     raise ValueError("Conflict of devices %r and %r!" % (dev, sndev))
                 sndev.delete()
     if model is None:
         model, model_created = DeviceModel.concurrent_get_or_create(
             name=model_name, defaults={"type": model_type.id}
         )
     if dev is None:
         dev, created = Device.concurrent_get_or_create(sn=sn, defaults={"model": model})
     elif dev.deleted:
         # Ignore the priority and undelete even if it was manually deleted
         priorities = dev.get_save_priorities()
         priorities["deleted"] = 0
         dev.update_save_priorities(priorities)
         dev.deleted = False
     if model and model.type != DeviceType.unknown.id:
         dev.model = model
     if not dev.sn and sn:
         dev.sn = sn
     for k, v in kwargs.iteritems():
         if k in ("name", "last_seen"):
             continue
         setattr(dev, k, v)
     try:
         user = kwargs.get("user")
     except KeyError:
         user = None
     dev.save(user=user, update_last_seen=True, priority=priority)
     for eth in ethernets:
         ethernet, eth_created = Ethernet.concurrent_get_or_create(mac=eth.mac, defaults={"device": dev})
         ethernet.device = dev
         ethernet.label = eth.label or "Autocreated"
         if eth.speed:
             ethernet.speed = eth.speed
         ethernet.save(priority=priority)
     return dev