def set_usages(date, usage, usage_type, host, splunk_venture): device_info = get_device_by_name(host) device_id = device_info.get('device_id') venture_id = device_info.get('venture_id') if device_id is None: logger.warning('Device with host %s not found in ralph', host) # device try: device = device_id and Device.objects.get(device_id=device_id) except Device.DoesNotExist: device = None # venture try: if venture_id is None: raise VentureIdNone() venture = Venture.objects.get(venture_id=venture_id) except (VentureIdNone, Venture.DoesNotExist): logger.warning( 'Device with host %s attached to splunk unknown venture', host) venture = splunk_venture # save device information if found in pricing if device is not None: daily_usage, created = DailyUsage.objects.get_or_create( date=date, type=usage_type, pricing_device=device, pricing_venture=venture, ) daily_usage.remarks = host else: # if device not found in pricing, try to find DailyUsage by adding # remarks as filter - in case of DailyUsage, remark should always be # hostname (grouping by hostname in main plugin) daily_usage, created = DailyUsage.objects.get_or_create( date=date, type=usage_type, pricing_venture=venture, remarks=host, ) daily_usage.value = usage daily_usage.save()
def test_get_device_by_name_wrong_name(self): # device does not exist device3 = api_pricing.get_device_by_name("Device3") self.assertEqual(device3, {})
def test_get_device_by_name_without_venture(self): # device without venture device2 = api_pricing.get_device_by_name("Device2") self.assertEqual(device2['device_id'], self.device_without_venture.id) self.assertIsNone(device2['venture_id'])
def test_get_device_by_name_with_venture(self): # device with venture device1 = api_pricing.get_device_by_name("Device1") self.assertEqual(device1['device_id'], self.device.id) self.assertEqual(device1['venture_id'], self.venture.id)