Skip to content

Commit 66f6d31

Browse files
committed
update IP related code, add missing fields
1 parent d541a41 commit 66f6d31

File tree

8 files changed

+85
-11
lines changed

8 files changed

+85
-11
lines changed

test/conftest.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ def manager():
2424
return upcloud_api.CloudManager("testuser", "mock-api-password")
2525

2626

27+
def read_from_file(filename):
28+
filename = filename.replace("/", "_")
29+
cwd = os.path.dirname(__file__)
30+
f = open(cwd + '/json_data/'+filename, 'r')
31+
return f.read()
32+
33+
2734
class Mock(object):
2835
base_url = 'https://api.upcloud.com/1.2'
2936

3037
@staticmethod
3138
def read_from_file(filename):
32-
33-
filename = filename.replace("/", "_")
34-
35-
cwd = os.path.dirname(__file__)
36-
f = open(cwd + '/json_data/'+filename, 'r')
37-
return f.read()
39+
return read_from_file(filename)
3840

3941
@staticmethod
4042
def mock_get(target, response_file=None):

test/json_data/ip_address.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
{
55
"access": "private",
66
"address": "10.1.0.101",
7+
"family": "IPv4",
78
"ptr_record": "",
89
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
910
},
1011
{
1112
"access": "private",
1213
"address": "10.1.0.115",
14+
"family": "IPv4",
1315
"ptr_record": "",
1416
"server": "00887721-4fad-4e9c-b781-6f2265753f11"
1517
},
1618
{
1719
"access": "private",
1820
"address": "10.1.1.97",
21+
"family": "IPv4",
1922
"ptr_record": "",
2023
"server": "00ee2cf4-91c9-4e7a-89b9-a8087e32896b"
2124
}

test/json_data/ip_address_10.1.0.101.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"ip_address": {
33
"access": "private",
44
"address": "10.1.0.101",
5+
"family": "IPv4",
6+
"part_of_plan": "yes",
57
"ptr_record": "a.ptr.record",
68
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
79
}

test/json_data/ip_address_post.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"ip_address": {
33
"access": "private",
44
"address": "10.1.0.101",
5+
"family": "IPv4",
56
"ptr_record": "a.ptr.record",
67
"server": "00798b85-efdc-41ca-8021-f6ef457b8531"
78
}

test/json_data/server_00798b85-efdc-41ca-8021-f6ef457b8531.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"ip_address" : [
99
{
1010
"access" : "private",
11-
"address" : "10.0.0.0"
11+
"address" : "10.0.0.0",
12+
"family": "IPv4"
1213
},
1314
{
1415
"access" : "public",
15-
"address" : "0.0.0.0"
16+
"address" : "0.0.0.0",
17+
"family": "IPv4"
1618
}
1719
]
1820
},

test/test_apidoc/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
These tests cover cases from the UpCloud API Documentation.
3+
The client should follow the official doc as closely as possible.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import unicode_literals
2+
from __future__ import print_function
3+
from __future__ import division
4+
from __future__ import absolute_import
5+
6+
7+
from upcloud_api import IPAddress, Server
8+
from conftest import Mock, read_from_file
9+
import json
10+
import responses
11+
12+
class TestIP(object):
13+
14+
def test_ip_in_server_creation(self):
15+
"""IPAddress in server creation.
16+
17+
https://www.upcloud.com/api/8-servers/#create-server
18+
"""
19+
ip1 = IPAddress(family='IPv4', access='public')
20+
ip2 = IPAddress(family='IPv6', access='private')
21+
assert ip1.to_dict() == {'family': 'IPv4', 'access': 'public'}
22+
assert ip2.to_dict() == {'family': 'IPv6', 'access': 'private'}
23+
24+
def test_ip_in_server_details(self):
25+
"""IPAddress in server details.
26+
27+
https://www.upcloud.com/api/8-servers/#get-server-details
28+
"""
29+
ip = IPAddress(access='private', address='10.0.0.0', family='IPv4')
30+
assert ip.to_dict() == {
31+
'access': 'private',
32+
'address': '10.0.0.0',
33+
'family' : 'IPv4'
34+
}
35+
36+
data = read_from_file('server_00798b85-efdc-41ca-8021-f6ef457b8531.json')
37+
s = Server(**json.loads(data))
38+
for ip in s.ip_addresses:
39+
assert set(ip.to_dict().keys()) == set(['address', 'family', 'access'])
40+
41+
def test_ip_details(self):
42+
"""IPAdress LIST/GET.
43+
44+
https://www.upcloud.com/api/10-ip-addresses/#list-ip-addresses
45+
"""
46+
ip = IPAddress(**json.loads(read_from_file('ip_address_10.1.0.101.json'))['ip_address'])
47+
assert ip.to_dict() == {
48+
'access': 'private',
49+
'address': '10.1.0.101',
50+
'family': 'IPv4',
51+
'part_of_plan': 'yes',
52+
'ptr_record': 'a.ptr.record',
53+
'server': '008c365d-d307-4501-8efc-cd6d3bb0e494'
54+
}

upcloud_api/ip_address.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
from __future__ import unicode_literals
22
from upcloud_api import UpCloudResource
33

4+
45
class IPAddress(UpCloudResource):
56
"""
67
Class representation of the API's IP address. Extends UpCloudResource.
78
89
Attributes:
910
access -- "public" or "private"
1011
address -- the actual IPAddress (string)
12+
family -- IPv4 or IPv6
13+
part_of_plan -- yes/no string indicating whether this belongs to a preconfigured plan or not
1114
ptr_record -- the reverse DNS name (string)
1215
server -- the UUID of the server this IP is attached to (string)
1316
1417
The only updateable field is the ptr_record.
15-
ptr_record and server are present only if /server/uuid endpoint was used.
18+
19+
Note that all of the fields are not always available depending on the API call,
20+
consult the official API docs for details.
1621
"""
1722

1823
ATTRIBUTES = {
19-
'family': 'IPv4',
2024
'access': None,
2125
'address': None,
22-
'ptr_record': None
26+
'family': 'IPv4',
27+
'part_of_plan': None,
28+
'ptr_record': None,
29+
'server': None,
2330
}
2431

2532
def save(self):

0 commit comments

Comments
 (0)