Пример #1
0
def _update_addresses(device, address_data, is_management=False):
    """
    Update management or system ip addresses of a device based on data.

    :param device: Device to be updated
    :param address_data: list of strings with the ip addresses
    :param is_management: whether to update management or system addresses
    """
    ipaddress_ids = []
    for ip in address_data:
        try:
            ipaddress = IPAddress.objects.get(address=ip)
        except IPAddress.DoesNotExist:
            ipaddress = IPAddress(address=ip)
        ipaddress.device = device
        ipaddress.is_management = is_management
        ipaddress.save(update_last_seen=False)
        ipaddress_ids.append(ipaddress.id)
    # Disconnect the rest of addresses from this device
    for ipaddress in IPAddress.objects.filter(
        device=device,
        is_management=is_management,
    ).exclude(id__in=ipaddress_ids):
        ipaddress.device = None
        ipaddress.save(update_last_seen=False)
Пример #2
0
 def management_ip(self, value):
     del self.management_ip
     if isinstance(value, IPAddress):
         ipaddr = value
     elif isinstance(value, basestring):
         ipaddr, _ = IPAddress.concurrent_get_or_create(
             address=value
         )
     elif isinstance(value, Iterable):
         hostname, ip = value
         ipaddr, _ = IPAddress.concurrent_get_or_create(address=ip)
         ipaddr.hostname = hostname
     ipaddr.is_management = True
     self.ipaddress_set.add(ipaddr)
Пример #3
0
def set_device_data(device, data, save_priority=SAVE_PRIORITY, warnings=[]):
    """
    Go through the submitted data, and update the Device object
    in accordance with the meaning of the particular fields.
    """

    keys = {
        'sn': 'serial_number',
        'name': 'hostname',
        'dc': 'data_center',
        'rack': 'rack',
        'barcode': 'barcode',
        'chassis_position': 'chassis_position',
    }
    for field_name, key_name in keys.iteritems():
        if key_name in data:
            setattr(device, field_name, data[key_name])
    if 'model_name' in data and (data['model_name'] or '').strip():
        try:
            model_type = _get_choice_by_name(
                DeviceType,
                data.get('type', 'unknown')
            )
        except ValueError:
            model_type = DeviceType.unknown
        try:
            # Don't use get_or_create, because we are in transaction
            device.model = DeviceModel.objects.get(
                name=data['model_name'],
                type=model_type,
            )
        except DeviceModel.DoesNotExist:
            model = DeviceModel(
                name=data['model_name'],
                type=model_type,
            )
            try:
                model.save()
            except IntegrityError:
                if model_type != DeviceType.unknown:
                    try:
                        device.model = DeviceModel.objects.get(
                            name='%s (%s)' % (
                                data['model_name'], model_type.raw
                            ),
                            type=model_type,
                        )
                    except DeviceModel.DoesNotExist:
                        model = DeviceModel(
                            type=model_type,
                            name='%s (%s)' % (
                                data['model_name'], model_type.raw
                            ),
                        )
                        try:
                            model.save()
                        except IntegrityError:
                            pass
                        else:
                            device.model = model
            else:
                device.model = model
    if 'disks' in data:
        _update_component_data(
            device,
            data['disks'],
            Storage,
            {
                'sn': 'serial_number',
                'device': 'device',
                'size': 'size',
                'speed': 'speed',
                'mount_point': 'mount_point',
                'label': 'label',
                'family': 'family',
                'model_name': 'model_name',
            },
            [
                ('sn',),
                ('device', 'mount_point'),
            ],
            ComponentType.disk,
            {'name'},
            save_priority=save_priority,
        )
    if 'processors' in data:
        for index, processor in enumerate(data['processors']):
            processor['index'] = index
        _update_component_data(
            device,
            data['processors'],
            Processor,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'cores': 'cores',
                'family': 'family',
                'index': 'index',
                'model_name': 'model_name',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.processor,
            save_priority=save_priority,
        )
    if 'memory' in data:
        for index, memory in enumerate(data['memory']):
            memory['index'] = index
            memory['speed'] = memory.get('speed', None) or None
        _update_component_data(
            device,
            data['memory'],
            Memory,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'size': 'size',
                'index': 'index',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.memory,
            {'name'},
            save_priority=save_priority,
        )
    if 'mac_addresses' in data:
        _update_component_data(
            device,
            [{'mac': mac} for mac in data['mac_addresses']],
            Ethernet,
            {
                'mac': 'mac',
                'device': 'device',
            },
            [('mac',)],
            None,
            save_priority=save_priority,
        )
    if 'management_ip_addresses' in data:
        _update_addresses(device, data['management_ip_addresses'], True)
    if 'system_ip_addresses' in data:
        _update_addresses(device, data['system_ip_addresses'], False)
    if 'management' in data:
        device.management, created = IPAddress.concurrent_get_or_create(
            address=data['management']
        )
    if 'fibrechannel_cards' in data:
        _update_component_data(
            device,
            data['fibrechannel_cards'],
            FibreChannel,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'physical_id': 'physical_id',
            },
            [
                ('physical_id', 'device'),
            ],
            ComponentType.fibre,
            save_priority=save_priority,
        )
    if 'parts' in data:
        _update_component_data(
            device,
            data['parts'],
            GenericComponent,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'sn': 'serial_number',
                'type': 'type',
            },
            [
                ('sn',),
            ],
            save_priority=save_priority,
        )
    if 'disk_exports' in data:
        _update_component_data(
            device,
            data['disk_exports'],
            DiskShare,
            {
                'device': 'device',
                'label': 'label',
                'wwn': 'serial_number',
                'size': 'size',
                'full': 'full',
                'snapshot_size': 'snapshot_size',
                'share_id': 'share_id',
                'model_name': 'model_name',
            },
            [
                ('wwn',),
            ],
            ComponentType.share,
            save_priority=save_priority,
        )
    if 'disk_shares' in data:
        shares = []
        for share in data['disk_shares']:
            if share.get('server'):
                servers = find_devices({
                    'server': share['server'],
                })
                if len(servers) > 1:
                    raise ValueError(
                        "Multiple servers found for share mount %r" % share,
                    )
                elif len(servers) <= 0:
                    raise ValueError(
                        "No server found for share mount %r" % share,
                    )
                share['server'] = servers[0]
            else:
                share['server'] = None
            try:
                share['share'] = DiskShare.objects.get(
                    wwn=share['serial_number']
                )
            except DiskShare.DoesNotExist:
                warnings.append(
                    'No share found for share mount: %r' % share
                )
                continue
            if share.get('address'):
                try:
                    share['address'] = IPAddress.objects.get(
                        address=share['address'],
                    )
                except IPAddress.DoesNotExist:
                    warnings.append(
                        'No IP address found for share mount: %r' % share
                    )
                    continue
            elif 'address' in share:
                del share['address']
            shares.append(share)
        _update_component_data(
            device,
            shares,
            DiskShareMount,
            {
                'share': 'share',
                'size': 'size',
                'address': 'address',
                'is_virtual': 'is_virtual',
                'volume': 'volume',
                'server': 'server',
                'device': 'device',
            },
            [
                ('device', 'share'),
            ],
            save_priority=save_priority,
        )
    if 'installed_software' in data:
        _update_component_data(
            device,
            data['installed_software'],
            Software,
            {
                'device': 'device',
                'path': 'path',
                'label': 'label',
                'version': 'version',
                'model_name': 'model_name',
                'sn': 'serial_number',
            },
            [
                ('device', 'path'),
            ],
            ComponentType.software,
            save_priority=save_priority,
        )
    if (
        'system_label' in data or
        'system_memory' in data or
        'system_storage' in data or
        'system_cores_count' in data or
        'system_family' in data or
        'system_model_name' in data
    ):
        _update_component_data(
            device,
            [data],
            OperatingSystem,
            {
                'device': 'device',
                'memory': 'system_memory',
                'storage': 'system_storage',
                'cores_count': 'system_cores_count',
                'family': 'system_family',
                'label': 'system_label',
                'model_name': 'system_model_name',
            },
            [
                ('device',),
            ],
            ComponentType.os,
            save_priority=save_priority,
        )
    if 'subdevices' in data:
        subdevice_ids = []
        for subdevice_data in data['subdevices']:
            subdevice = device_from_data(
                subdevice_data,
                save_priority=save_priority,
                warnings=warnings
            )
            if (
                device.model and
                device.model.type in (DeviceType.switch_stack,)
            ):
                subdevice.logical_parent = device
                if subdevice.parent and subdevice.parent.id == device.id:
                    subdevice.parent = None
            else:
                subdevice.parent = device
            subdevice.save(priority=save_priority)
            subdevice_ids.append(subdevice.id)
        for subdevice in device.child_set.exclude(id__in=subdevice_ids):
            subdevice.parent = None
            subdevice.save(priority=save_priority)
    if 'asset' in data and 'ralph_assets' in settings.INSTALLED_APPS:
        from ralph_assets.api_ralph import assign_asset
        if data['asset']:
            try:
                asset_id = data['asset'].id
            except AttributeError:
                pass
            else:
                assign_asset(device.id, asset_id)
Пример #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,
         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()
Пример #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'śćź')
Пример #6
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()
Пример #7
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)
Пример #8
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()
Пример #9
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)
Пример #10
0
def set_device_data(device, data, save_priority=SAVE_PRIORITY, warnings=[]):
    """
    Go through the submitted data, and update the Device object
    in accordance with the meaning of the particular fields.
    """

    keys = {
        'sn': 'serial_number',
        'name': 'hostname',
        'dc': 'data_center',
        'rack': 'rack',
        'barcode': 'barcode',
        'chassis_position': 'chassis_position',
    }
    can_edit_position = check_if_can_edit_position(data)
    for field_name, key_name in keys.iteritems():
        if key_name in data:
            if all((
                    not can_edit_position,
                    field_name in ('dc', 'rack', 'chassis_position'),
            )):
                warnings.append(
                    'You can not set data for `{}` here - skipped. Use assets '
                    'module.'.format(key_name), )
                continue
            setattr(device, field_name, data[key_name])
    if 'model_name' in data and (data['model_name'] or '').strip():
        try:
            model_type = get_choice_by_name(DeviceType,
                                            data.get('type', 'unknown'))
        except ValueError:
            model_type = DeviceType.unknown
        try:
            # Don't use get_or_create, because we are in transaction
            device.model = DeviceModel.objects.get(
                name=data['model_name'],
                type=model_type,
            )
        except DeviceModel.DoesNotExist:
            model = DeviceModel(
                name=data['model_name'],
                type=model_type,
            )
            try:
                model.save()
            except IntegrityError:
                if model_type != DeviceType.unknown:
                    try:
                        device.model = DeviceModel.objects.get(
                            name='%s (%s)' %
                            (data['model_name'], model_type.raw),
                            type=model_type,
                        )
                    except DeviceModel.DoesNotExist:
                        model = DeviceModel(
                            type=model_type,
                            name='%s (%s)' %
                            (data['model_name'], model_type.raw),
                        )
                        try:
                            model.save()
                        except IntegrityError:
                            pass
                        else:
                            device.model = model
            else:
                device.model = model
    if 'disks' in data:
        _update_component_data(
            device,
            data['disks'],
            Storage,
            {
                'sn': 'serial_number',
                'device': 'device',
                'size': 'size',
                'speed': 'speed',
                'mount_point': 'mount_point',
                'label': 'label',
                'family': 'family',
                'model_name': 'model_name',
            },
            [
                ('sn', ),
                ('device', 'mount_point'),
            ],
            ComponentType.disk,
            {'name'},
            save_priority=save_priority,
        )
    if 'processors' in data:
        for index, processor in enumerate(data['processors']):
            processor['index'] = index
        _update_component_data(
            device,
            data['processors'],
            Processor,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'cores': 'cores',
                'family': 'family',
                'index': 'index',
                'model_name': 'model_name',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.processor,
            save_priority=save_priority,
        )
    if 'memory' in data:
        for index, memory in enumerate(data['memory']):
            memory['index'] = index
            memory['speed'] = memory.get('speed', None) or None
        _update_component_data(
            device,
            data['memory'],
            Memory,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'size': 'size',
                'index': 'index',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.memory,
            {'name'},
            save_priority=save_priority,
        )
    if 'mac_addresses' in data:
        _update_component_data(
            device,
            [{
                'mac': mac
            } for mac in data['mac_addresses']],
            Ethernet,
            {
                'mac': 'mac',
                'device': 'device',
            },
            [('mac', )],
            None,
            save_priority=save_priority,
        )
    if 'management_ip_addresses' in data:
        if not data.get('asset'):
            _update_addresses(device, data['management_ip_addresses'], True)
        else:
            warnings.append(
                'Management IP addresses ({}) have been ignored. To change '
                'them, please use the Assets module.'.format(
                    ', '.join(data['management_ip_addresses']), ), )
    if 'system_ip_addresses' in data:
        _update_addresses(device, data['system_ip_addresses'], False)
    if 'management' in data:
        if not data.get('asset'):
            device.management, created = IPAddress.concurrent_get_or_create(
                address=data['management'],
                defaults={'is_management': True},
            )
        else:
            warnings.append(
                'Management IP address ({}) has been ignored. To change '
                'them, please use the Assets module.'.format(
                    data['management'], ), )
    if 'fibrechannel_cards' in data:
        _update_component_data(
            device,
            data['fibrechannel_cards'],
            FibreChannel,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'physical_id': 'physical_id',
            },
            [
                ('physical_id', 'device'),
            ],
            ComponentType.fibre,
            save_priority=save_priority,
        )
    if 'parts' in data:
        _update_component_data(
            device,
            data['parts'],
            GenericComponent,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'sn': 'serial_number',
                'type': 'type',
            },
            [
                ('sn', ),
            ],
            save_priority=save_priority,
        )
    if 'disk_exports' in data:
        _update_component_data(
            device,
            data['disk_exports'],
            DiskShare,
            {
                'device': 'device',
                'label': 'label',
                'wwn': 'serial_number',
                'size': 'size',
                'full': 'full',
                'snapshot_size': 'snapshot_size',
                'share_id': 'share_id',
                'model_name': 'model_name',
            },
            [
                ('wwn', ),
            ],
            ComponentType.share,
            save_priority=save_priority,
        )
    if 'disk_shares' in data:
        shares = []
        for share in data['disk_shares']:
            if share.get('server'):
                servers = find_devices({
                    'server': share['server'],
                })
                if len(servers) > 1:
                    raise ValueError(
                        "Multiple servers found for share mount %r" % share, )
                elif len(servers) <= 0:
                    raise ValueError(
                        "No server found for share mount %r" % share, )
                share['server'] = servers[0]
            else:
                share['server'] = None
            try:
                share['share'] = DiskShare.objects.get(
                    wwn=share['serial_number'])
            except DiskShare.DoesNotExist:
                warnings.append('No share found for share mount: %r' % share)
                continue
            if share.get('address'):
                try:
                    share['address'] = IPAddress.objects.get(
                        address=share['address'], )
                except IPAddress.DoesNotExist:
                    warnings.append('No IP address found for share mount: %r' %
                                    share)
                    continue
            elif 'address' in share:
                del share['address']
            shares.append(share)
        _update_component_data(
            device,
            shares,
            DiskShareMount,
            {
                'share': 'share',
                'size': 'size',
                'address': 'address',
                'is_virtual': 'is_virtual',
                'volume': 'volume',
                'server': 'server',
                'device': 'device',
            },
            [
                ('device', 'share'),
            ],
            save_priority=save_priority,
        )
    if 'installed_software' in data:
        _update_component_data(
            device,
            data['installed_software'],
            Software,
            {
                'device': 'device',
                'path': 'path',
                'label': 'label',
                'version': 'version',
                'model_name': 'model_name',
                'sn': 'serial_number',
            },
            [
                ('device', 'path'),
            ],
            ComponentType.software,
            save_priority=save_priority,
        )
    if ('system_label' in data or 'system_memory' in data
            or 'system_storage' in data or 'system_cores_count' in data
            or 'system_family' in data or 'system_model_name' in data):
        _update_component_data(
            device,
            [data],
            OperatingSystem,
            {
                'device': 'device',
                'memory': 'system_memory',
                'storage': 'system_storage',
                'cores_count': 'system_cores_count',
                'family': 'system_family',
                'label': 'system_label',
                'model_name': 'system_model_name',
            },
            [
                ('device', ),
            ],
            ComponentType.os,
            save_priority=save_priority,
        )
    if 'subdevices' in data:
        subdevice_ids = []
        for subdevice_data in data['subdevices']:
            subdevice = device_from_data(subdevice_data,
                                         save_priority=save_priority,
                                         warnings=warnings)
            if has_logical_children(device):
                subdevice.logical_parent = device
                if subdevice.parent and subdevice.parent.id == device.id:
                    subdevice.parent = None
            else:
                subdevice.parent = device
            subdevice.save(priority=save_priority)
            subdevice_ids.append(subdevice.id)
        set_, parent_attr = ((device.logicalchild_set, 'logical_parent')
                             if has_logical_children(device) else
                             (device.child_set, 'parent'))
        for subdevice in set_.exclude(id__in=subdevice_ids):
            setattr(subdevice, parent_attr, None)
            subdevice.save(priority=save_priority)
    if 'connections' in data:
        parsed_connections = set()
        for connection_data in data['connections']:
            connection = connection_from_data(device, connection_data)
            if connection.connection_type == ConnectionType.network:
                connetion_details = connection_data.get('details', {})
                if connetion_details:
                    outbound_port = connetion_details.get('outbound_port')
                    inbound_port = connetion_details.get('inbound_port')
                    try:
                        details = NetworkConnection.objects.get(
                            connection=connection)
                    except NetworkConnection.DoesNotExist:
                        details = NetworkConnection(connection=connection)
                    if outbound_port:
                        details.outbound_port = outbound_port
                    if inbound_port:
                        details.inbound_port = inbound_port
                    details.save()
            parsed_connections.add(connection.pk)
        device.outbound_connections.exclude(pk__in=parsed_connections).delete()
    if 'asset' in data and 'ralph_assets' in settings.INSTALLED_APPS:
        from ralph_assets.api_ralph import assign_asset
        asset = data['asset']
        if asset and not isinstance(asset, Asset):
            asset = get_asset_by_name(asset)
        if asset:
            assign_asset(device.id, asset.id)
Пример #11
0
def set_device_data(device, data, save_priority=SAVE_PRIORITY, warnings=[]):
    """
    Go through the submitted data, and update the Device object
    in accordance with the meaning of the particular fields.
    """

    keys = {
        'sn': 'serial_number',
        'name': 'hostname',
        'dc': 'data_center',
        'rack': 'rack',
        'barcode': 'barcode',
        'chassis_position': 'chassis_position',
    }
    can_edit_position = check_if_can_edit_position(data)
    for field_name, key_name in keys.iteritems():
        if key_name in data:
            if all((
                not can_edit_position,
                field_name in ('dc', 'rack', 'chassis_position'),
            )):
                warnings.append(
                    'You can not set data for `{}` here - skipped. Use assets '
                    'module.'.format(key_name),
                )
                continue
            setattr(device, field_name, data[key_name])
    if 'model_name' in data and (data['model_name'] or '').strip():
        try:
            model_type = get_choice_by_name(
                DeviceType,
                data.get('type', 'unknown')
            )
        except ValueError:
            model_type = DeviceType.unknown
        try:
            # Don't use get_or_create, because we are in transaction
            device.model = DeviceModel.objects.get(
                name=data['model_name'],
                type=model_type,
            )
        except DeviceModel.DoesNotExist:
            model = DeviceModel(
                name=data['model_name'],
                type=model_type,
            )
            try:
                model.save()
            except IntegrityError:
                if model_type != DeviceType.unknown:
                    try:
                        device.model = DeviceModel.objects.get(
                            name='%s (%s)' % (
                                data['model_name'], model_type.raw
                            ),
                            type=model_type,
                        )
                    except DeviceModel.DoesNotExist:
                        model = DeviceModel(
                            type=model_type,
                            name='%s (%s)' % (
                                data['model_name'], model_type.raw
                            ),
                        )
                        try:
                            model.save()
                        except IntegrityError:
                            pass
                        else:
                            device.model = model
            else:
                device.model = model
    if 'disks' in data:
        _update_component_data(
            device,
            data['disks'],
            Storage,
            {
                'sn': 'serial_number',
                'device': 'device',
                'size': 'size',
                'speed': 'speed',
                'mount_point': 'mount_point',
                'label': 'label',
                'family': 'family',
                'model_name': 'model_name',
            },
            [
                ('sn',),
                ('device', 'mount_point'),
            ],
            ComponentType.disk,
            {'name'},
            save_priority=save_priority,
        )
    if 'processors' in data:
        for index, processor in enumerate(data['processors']):
            processor['index'] = index
        _update_component_data(
            device,
            data['processors'],
            Processor,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'cores': 'cores',
                'family': 'family',
                'index': 'index',
                'model_name': 'model_name',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.processor,
            save_priority=save_priority,
        )
    if 'memory' in data:
        for index, memory in enumerate(data['memory']):
            memory['index'] = index
            memory['speed'] = memory.get('speed', None) or None
        _update_component_data(
            device,
            data['memory'],
            Memory,
            {
                'device': 'device',
                'label': 'label',
                'speed': 'speed',
                'size': 'size',
                'index': 'index',
            },
            [
                ('device', 'index'),
            ],
            ComponentType.memory,
            {'name'},
            save_priority=save_priority,
        )
    if 'mac_addresses' in data:
        _update_component_data(
            device,
            [{'mac': mac} for mac in data['mac_addresses']],
            Ethernet,
            {
                'mac': 'mac',
                'device': 'device',
            },
            [('mac',)],
            None,
            save_priority=save_priority,
        )
    if 'management_ip_addresses' in data:
        if not data.get('asset'):
            _update_addresses(device, data['management_ip_addresses'], True)
        else:
            warnings.append(
                'Management IP addresses ({}) have been ignored. To change '
                'them, please use the Assets module.'.format(
                    ', '.join(data['management_ip_addresses']),
                ),
            )
    if 'system_ip_addresses' in data:
        _update_addresses(device, data['system_ip_addresses'], False)
    if 'management' in data:
        if not data.get('asset'):
            device.management, created = IPAddress.concurrent_get_or_create(
                address=data['management'], defaults={'is_management': True},
            )
        else:
            warnings.append(
                'Management IP address ({}) has been ignored. To change '
                'them, please use the Assets module.'.format(
                    data['management'],
                ),
            )
    if 'fibrechannel_cards' in data:
        _update_component_data(
            device,
            data['fibrechannel_cards'],
            FibreChannel,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'physical_id': 'physical_id',
            },
            [
                ('physical_id', 'device'),
            ],
            ComponentType.fibre,
            save_priority=save_priority,
        )
    if 'parts' in data:
        _update_component_data(
            device,
            data['parts'],
            GenericComponent,
            {
                'device': 'device',
                'label': 'label',
                'model_name': 'model_name',
                'sn': 'serial_number',
                'type': 'type',
            },
            [
                ('sn',),
            ],
            save_priority=save_priority,
        )
    if 'disk_exports' in data:
        _update_component_data(
            device,
            data['disk_exports'],
            DiskShare,
            {
                'device': 'device',
                'label': 'label',
                'wwn': 'serial_number',
                'size': 'size',
                'full': 'full',
                'snapshot_size': 'snapshot_size',
                'share_id': 'share_id',
                'model_name': 'model_name',
            },
            [
                ('wwn',),
            ],
            ComponentType.share,
            save_priority=save_priority,
        )
    if 'disk_shares' in data:
        shares = []
        for share in data['disk_shares']:
            if share.get('server'):
                servers = find_devices({
                    'server': share['server'],
                })
                if len(servers) > 1:
                    raise ValueError(
                        "Multiple servers found for share mount %r" % share,
                    )
                elif len(servers) <= 0:
                    raise ValueError(
                        "No server found for share mount %r" % share,
                    )
                share['server'] = servers[0]
            else:
                share['server'] = None
            try:
                share['share'] = DiskShare.objects.get(
                    wwn=share['serial_number']
                )
            except DiskShare.DoesNotExist:
                warnings.append(
                    'No share found for share mount: %r' % share
                )
                continue
            if share.get('address'):
                try:
                    share['address'] = IPAddress.objects.get(
                        address=share['address'],
                    )
                except IPAddress.DoesNotExist:
                    warnings.append(
                        'No IP address found for share mount: %r' % share
                    )
                    continue
            elif 'address' in share:
                del share['address']
            shares.append(share)
        _update_component_data(
            device,
            shares,
            DiskShareMount,
            {
                'share': 'share',
                'size': 'size',
                'address': 'address',
                'is_virtual': 'is_virtual',
                'volume': 'volume',
                'server': 'server',
                'device': 'device',
            },
            [
                ('device', 'share'),
            ],
            save_priority=save_priority,
        )
    if 'installed_software' in data:
        _update_component_data(
            device,
            data['installed_software'],
            Software,
            {
                'device': 'device',
                'path': 'path',
                'label': 'label',
                'version': 'version',
                'model_name': 'model_name',
                'sn': 'serial_number',
            },
            [
                ('device', 'path'),
            ],
            ComponentType.software,
            save_priority=save_priority,
        )
    if (
        'system_label' in data or
        'system_memory' in data or
        'system_storage' in data or
        'system_cores_count' in data or
        'system_family' in data or
        'system_model_name' in data
    ):
        _update_component_data(
            device,
            [data],
            OperatingSystem,
            {
                'device': 'device',
                'memory': 'system_memory',
                'storage': 'system_storage',
                'cores_count': 'system_cores_count',
                'family': 'system_family',
                'label': 'system_label',
                'model_name': 'system_model_name',
            },
            [
                ('device',),
            ],
            ComponentType.os,
            save_priority=save_priority,
        )
    if 'subdevices' in data:
        subdevice_ids = []
        for subdevice_data in data['subdevices']:
            subdevice = device_from_data(
                subdevice_data,
                save_priority=save_priority,
                warnings=warnings
            )
            if has_logical_children(device):
                subdevice.logical_parent = device
                if subdevice.parent and subdevice.parent.id == device.id:
                    subdevice.parent = None
            else:
                subdevice.parent = device
            subdevice.save(priority=save_priority)
            subdevice_ids.append(subdevice.id)
        set_, parent_attr = (
            (device.logicalchild_set, 'logical_parent')
            if has_logical_children(device)
            else (device.child_set, 'parent')
        )
        for subdevice in set_.exclude(id__in=subdevice_ids):
            setattr(subdevice, parent_attr, None)
            subdevice.save(priority=save_priority)
    if 'connections' in data:
        parsed_connections = set()
        for connection_data in data['connections']:
            connection = connection_from_data(device, connection_data)
            if connection.connection_type == ConnectionType.network:
                connetion_details = connection_data.get('details', {})
                if connetion_details:
                    outbound_port = connetion_details.get('outbound_port')
                    inbound_port = connetion_details.get('inbound_port')
                    try:
                        details = NetworkConnection.objects.get(
                            connection=connection
                        )
                    except NetworkConnection.DoesNotExist:
                        details = NetworkConnection(connection=connection)
                    if outbound_port:
                        details.outbound_port = outbound_port
                    if inbound_port:
                        details.inbound_port = inbound_port
                    details.save()
            parsed_connections.add(connection.pk)
        device.outbound_connections.exclude(
            pk__in=parsed_connections
        ).delete()
    if 'asset' in data and 'ralph_assets' in settings.INSTALLED_APPS:
        from ralph_assets.api_ralph import assign_asset
        asset = data['asset']
        if asset and not isinstance(asset, Asset):
            asset = get_asset_by_name(asset)
        if asset:
            assign_asset(device.id, asset.id)