Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit d1193dc

Browse files
Merge pull request #22 from TheAntColony/ips
IPs
2 parents 304ea48 + 5f64f74 commit d1193dc

9 files changed

Lines changed: 43 additions & 48 deletions

File tree

packet/IPAddress.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66

77
class IPAddress:
8-
def __init__(self, data):
9-
self.ip = data["id"]
8+
def __init__(self, data, manager):
9+
self.manager = manager
10+
11+
self.id = data["id"]
1012
self.address_family = data["address_family"]
1113
self.netmask = data["netmask"]
1214
self.created_at = data["created_at"]
@@ -35,8 +37,11 @@ def __init__(self, data):
3537
if "gateway" in data:
3638
self.gateway = data["gateway"]
3739

38-
def __str__(self):
39-
return "%s" % self.code
40+
def delete(self):
41+
return self.manager.call_api("ips/%s" % self.id, type="DELETE")
42+
43+
def __str__(self):
44+
return "%s" % self.code
4045

41-
def __repr__(self):
42-
return "{}: {}".format(self.__class__.__name__, self.id)
46+
def __repr__(self):
47+
return "{}: {}".format(self.__class__.__name__, self.id)

packet/Manager.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ def list_device_ips(self, device_id):
302302
data = self.call_api("devices/%s/ips" % device_id, type="GET")
303303
ips = list()
304304
for jsoned in data["ip_addresses"]:
305-
ip = IPAddress(jsoned)
305+
ip = IPAddress(jsoned, self)
306306
ips.append(ip)
307307
return ips
308308

309309
def get_ip(self, ip_id):
310310
data = self.call_api("ips/%s" % ip_id)
311-
return IPAddress(data)
311+
return IPAddress(data, self)
312312

313313
def delete_ip(self, ip_id):
314314
self.call_api("ips/%s" % ip_id, type="DELETE")
@@ -319,7 +319,7 @@ def list_project_ips(self, project_id, params={}):
319319
params=params)
320320
ips = list()
321321
for jsoned in data["ip_addresses"]:
322-
ip = IPAddress(jsoned)
322+
ip = IPAddress(jsoned, self)
323323
ips.append(ip)
324324
return ips
325325

@@ -338,7 +338,7 @@ def create_device_ip(self, device_id, address,
338338

339339
data = self.call_api("/devices/%s/ips" % device_id,
340340
params=params, type="POST")
341-
return IPAddress(data)
341+
return IPAddress(data, self)
342342

343343
def reserve_ip_address(self, project_id, type, quantity, facility,
344344
details=None, comments=None, tags=list()):
@@ -351,12 +351,9 @@ def reserve_ip_address(self, project_id, type, quantity, facility,
351351
"tags": tags
352352
}
353353

354-
data = self.call_api("/projects/%s/ips" % project_id, params=request)
355-
ips = list()
356-
for i in data["ip_addresses"]:
357-
ip = IPAddress(i)
358-
ips.append(ip)
359-
return ips
354+
data = self.call_api("/projects/%s/ips" % project_id,
355+
params=request, type="POST")
356+
return IPAddress(data, self)
360357

361358
# Batches
362359
def create_batch(self, project_id, params):

test/test_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setUpClass(self):
1414
org_id = self.manager.list_organizations()[0].id
1515
self.project = self.manager.create_organization_project(
1616
org_id=org_id,
17-
name="Int-Tests-Batch_{}".format(datetime.utcnow().timestamp())
17+
name="Int-Tests-Batch_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
1818
)
1919
self.batches = list()
2020

test/test_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setUpClass(self):
1414
org_id = self.manager.list_organizations()[0].id
1515
self.project = self.manager.create_organization_project(
1616
org_id=org_id,
17-
name="Int-Tests-Device_{}".format(datetime.utcnow().timestamp())
17+
name="Int-Tests-Device_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
1818
)
1919

2020
self.manager.enable_project_bgp_config(

test/test_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def setUpClass(cls):
1616
org_id = cls.manager.list_organizations()[0].id
1717
cls.project = cls.manager.create_organization_project(
1818
org_id=org_id,
19-
name="Int-Tests-Events_{}".format(datetime.utcnow().timestamp())
19+
name="Int-Tests-Events_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
2020
)
2121

2222
def test_list_events(self):

test/test_ips.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import time
34
import unittest
45
import packet
@@ -14,14 +15,14 @@ def setUpClass(cls):
1415
org_id = cls.manager.list_organizations()[0].id
1516
cls.project = cls.manager.create_organization_project(
1617
org_id=org_id,
17-
name="Int-Tests-IPs_{}".format(datetime.utcnow().timestamp())
18+
name="Int-Tests-IPs_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
1819
)
1920

20-
cls.ipaddresses = cls.manager\
21+
cls.ip_block = cls.manager \
2122
.reserve_ip_address(project_id=cls.project.id,
22-
type="global_ipv4",
23+
type="public_ipv4",
2324
quantity=1,
24-
facility="EWR1",
25+
facility="ewr1",
2526
details="delete me",
2627
tags=["deleteme"])
2728

@@ -34,34 +35,26 @@ def setUpClass(cls):
3435
break
3536
time.sleep(2)
3637

37-
cls.manager.reserve_ip_address(project_id=cls.project.id,
38-
type="global_ipv4",
39-
quantity=1,
40-
facility="ewr1")
38+
def test_reserve_ip_address(self):
39+
self.assertEqual(32, self.ip_block.cidr)
40+
self.assertEqual("delete me", self.ip_block.details)
4141

42-
def list_project_ips(self):
42+
def test_list_project_ips(self):
4343
ips = self.manager.list_project_ips(self.project.id)
44-
45-
self.assertIsNotNone(ips)
44+
self.assertGreater(len(ips), 0)
4645

4746
def test_create_device_ip(self):
48-
ip = None
49-
params = {
50-
"include": ["facility"]
51-
}
52-
ips = self.manager.list_project_ips(self.project.id,
53-
params=params)
54-
for i in ips:
55-
if i.facility.code == "ewr1" \
56-
and i.address_family == 4:
57-
ip = i
58-
break
59-
60-
ipaddress = self.manager.create_device_ip(self.device.id,
61-
address=ip.address)
62-
self.assertIsNotNone(ipaddress)
47+
ip = self.manager.create_device_ip(self.device.id,
48+
address=self.ip_block.address)
49+
self.assertIsNotNone(ip)
50+
self.assertEqual(ip.address, self.ip_block.address)
6351

6452
@classmethod
6553
def tearDownClass(cls):
6654
cls.device.delete()
55+
cls.ip_block.delete()
6756
cls.project.delete()
57+
58+
59+
if __name__ == "__main__":
60+
sys.exit(unittest.main())

test/test_ports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setUpClass(self):
1515
org_id = self.manager.list_organizations()[0].id
1616
self.project = self.manager.create_organization_project(
1717
org_id=org_id,
18-
name="Int-Tests-Ports_{}".format(datetime.utcnow().timestamp())
18+
name="Int-Tests-Ports_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
1919
)
2020

2121
self.device = self.manager.create_device(

test/test_vlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setUpClass(self):
1515
org_id = self.manager.list_organizations()[0].id
1616
self.project = self.manager.create_organization_project(
1717
org_id=org_id,
18-
name="Int-Tests-VLAN_{}".format(datetime.utcnow().timestamp())
18+
name="Int-Tests-VLAN_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
1919
)
2020

2121
self.device = self.manager.create_device(

test/test_volume.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setUpClass(self):
1717
org_id = self.manager.list_organizations()[0].id
1818
self.project = self.manager.create_organization_project(
1919
org_id=org_id,
20-
name="Int-Tests-Volume_{}".format(datetime.utcnow().timestamp())
20+
name="Int-Tests-Volume_{}".format(datetime.utcnow().strftime("%Y%m%dT%H%M%S.%f")[:-3])
2121
)
2222

2323
self.volume = self.manager.create_volume(self.project.id,

0 commit comments

Comments
 (0)