Skip to content

Commit

Permalink
Merge pull request #924 from mkurek/feature/pricing-api-get-device
Browse files Browse the repository at this point in the history
Added getting device by ip address or remark to pricing api
  • Loading branch information
xor-xor committed Jun 12, 2014
2 parents 9bd4912 + c5ad86f commit 830be21
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ralph/util/api_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,35 @@ def get_device_by_name(device_name):
return {}


def get_device_by_remarks(remark):
"""Returns device information by remark"""
devices = Device.objects.filter(remarks__icontains=remark)
if devices:
device = devices[0]
return {
'device_id': device.id,
'venture_id': device.venture.id if device.venture else None,
}
return {}


def get_ip_info(ipaddress):
"""Returns device information by IP address"""
result = {}
try:
ip = IPAddress.objects.select_related().get(address=ipaddress)
except IPAddress.DoesNotExist:
pass
else:
if ip.venture is not None:
result['venture_id'] = ip.venture.id
if ip.device is not None:
result['device_id'] = ip.device.id
if ip.device.venture is not None:
result['venture_id'] = ip.device.venture.id
return result


def get_ip_addresses(only_public=False):
"""Yileds available IP addresses"""
ips = IPAddress.objects.filter(is_public=only_public)
Expand Down
24 changes: 24 additions & 0 deletions src/ralph/util/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
DeviceType,
DiskShare,
DiskShareMount,
IPAddress,
MarginKind,
PricingAggregate,
PricingFormula,
Expand Down Expand Up @@ -355,6 +356,12 @@ def setUp(self):
self.device_without_venture, created = Device.objects.get_or_create(
name="Device2",
)

self.ip1 = IPAddress(address='1.1.1.1', device=self.device)
self.ip2 = IPAddress(address='2.2.2.2', venture=self.venture)
self.ip1.save()
self.ip2.save()

self.device.save()
self.device_without_venture.save()

Expand All @@ -375,6 +382,23 @@ def test_get_device_by_name_wrong_name(self):
device3 = api_pricing.get_device_by_name("Device3")
self.assertEqual(device3, {})

def test_get_ip_info(self):
result = api_pricing.get_ip_info(ipaddress='1.1.1.1')
self.assertEquals(result, {
'device_id': self.device.id,
'venture_id': self.venture.id,
})

def test_get_ip_info_without_device(self):
result = api_pricing.get_ip_info(ipaddress='2.2.2.2')
self.assertEquals(result, {
'venture_id': self.venture.id,
})

def test_get_ip_info_empty(self):
result = api_pricing.get_ip_info(ipaddress='3.3.3.3')
self.assertEquals(result, {})


class UncompressBase64DataTest(TestCase):

Expand Down

0 comments on commit 830be21

Please sign in to comment.