Skip to content

Commit 9a0c990

Browse files
author
Elias Nygren
committed
add IPv6 to IP_address, IP-manager and Server.add_IP
1 parent 0e5d981 commit 9a0c990

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

upcloud/cloud_manager/ip_address_mixin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class IPManager():
44
"""
55
Functions for managing IP-addresses. Intended to be used as a mixin for CloudManager.
66
"""
7-
7+
88
def get_IP(self, address):
99
"""
1010
Get an IP_address object with the IP address (string) from the API.
@@ -21,7 +21,7 @@ def get_IPs(self):
2121
IPs = IP_address._create_ip_address_objs( res["ip_addresses"], cloud_manager=self )
2222
return IPs
2323

24-
def attach_IP(self, server):
24+
def attach_IP(self, server, family="IPv4"):
2525
"""
2626
Attach a new (random) IP_address to the given server (object or UUID)
2727
"""
@@ -31,7 +31,8 @@ def attach_IP(self, server):
3131

3232
body = {
3333
"ip_address": {
34-
"server": server
34+
"server": server,
35+
"family": family
3536
}
3637
}
3738

@@ -40,13 +41,13 @@ def attach_IP(self, server):
4041

4142
def modify_IP(self, IP_addr, ptr_record):
4243
"""
43-
Modify an IP address' ptr-record (Reverse DNS).
44+
Modify an IP address' ptr-record (Reverse DNS).
4445
Accepts an IP_address instance (object) or its address (string).
4546
"""
4647

4748
if not isinstance(IP_addr, str):
4849
IP_addr = IP_addr.address
49-
50+
5051
body = {
5152
"ip_address": {
5253
"ptr_record": ptr_record

upcloud/ip_address.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@
22

33
class IP_address(BaseAPI):
44
"""
5-
Object representation of the IP-address.
5+
Object representation of the IP-address.
66
77
Attributes:
88
access -- "public" or "private"
99
address -- the actual IP_address (string)
1010
ptr_record -- the reverse DNS name (string)
1111
server -- the UUID of the server this IP is attached to (string)
12-
13-
The only updateable field is the ptr_record.
12+
13+
The only updateable field is the ptr_record.
1414
ptr_record and server are present only if /server/uuid endpoint was used.
1515
"""
1616

17-
def __init__(self, access, address, cloud_manager, ptr_record=None, server=None, *args, **kwargs):
17+
def __init__(self, access, address, cloud_manager, family="IPv4",
18+
ptr_record=None, server=None, *args, **kwargs):
1819
"""
1920
ptr_record and server not returned by the API in every case (e.g. when IP is nested).
2021
Only ptr_record is editable due to restrictions of the API.
2122
"""
2223
self._cloud_manager = cloud_manager
23-
self.__reset(access, address, ptr_record, server)
24+
self.__reset(access, address, family, ptr_record, server)
2425

25-
def __reset(self, access, address, ptr_record=None, server=None):
26+
def __reset(self, access, address, family="IPv4", ptr_record=None, server=None):
2627
"""
2728
Reset after repopulating from API.
2829
"""
2930
# Always present
3031
self._access = access
3132
self._address = address
33+
self._family = family
3234

3335
# Present when populated from /server/uuid endpoint
3436
self._server_uuid = server
@@ -41,7 +43,7 @@ def save(self):
4143
body = { "ip_address": { "ptr_record": self.ptr } }
4244
data = self._cloud_manager.request("PUT", "/ip_address/" + self.address, body)
4345
self.__reset(**data["ip_address"])
44-
46+
4547
def destroy(self):
4648
"""
4749
Release the IP_address. DELETE /ip_address/uuid
@@ -54,18 +56,23 @@ def __str__(self):
5456
@property
5557
def address(self):
5658
return self._address
57-
59+
5860
@property
5961
def access(self):
60-
return self._access
61-
62+
return self._access
63+
6264
@property
6365
def server_uuid(self):
6466
return self._server_uuid
6567

68+
@property
69+
def family(self):
70+
return self._family
71+
72+
6673
@staticmethod
6774
def _create_ip_address_objs(IP_addrs, cloud_manager):
6875
IP_objs = list()
6976
for IP_addr in IP_addrs["ip_address"]:
7077
IP_objs.append( IP_address(cloud_manager = cloud_manager, **IP_addr) )
71-
return IP_objs
78+
return IP_objs

upcloud/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ def restart(self):
119119
self.cloud_manager.post_request("/server/" + self.uuid + "/restart" , body)
120120
object.__setattr__(self, "state", "maintenance") # post_request already handles any errors from API
121121

122-
def add_IP(self):
122+
def add_IP(self, family="IPv4"):
123123
"""
124124
Allocate a new (random) IP-address to the Server.
125125
"""
126-
IP = self.cloud_manager.attach_IP(self.uuid)
126+
IP = self.cloud_manager.attach_IP(self.uuid, family)
127127
self.ip_addresses.append(IP)
128128
return IP
129129

0 commit comments

Comments
 (0)