Skip to content

Commit 27f7d7e

Browse files
author
Elias Nygren
committed
First commit
0 parents  commit 27f7d7e

35 files changed

+1395
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
ENV/
3+
.DS_Store
4+
UpCloud.egg-info/
5+
*.pyc

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mock==1.0.1
2+
py==1.4.26
3+
pytest==2.6.4
4+
requests==2.6.0
5+
responses==0.3.0
6+
six==1.9.0

setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
from distutils.core import setup
4+
5+
setup(name='UpCloud',
6+
version='0.1',
7+
description='UpCloud API Client',
8+
author='Elias Nygren',
9+
author_email='elias.nygren@upcloud.com',
10+
url='https://www.upcloud.com',
11+
packages=['upcloud'],
12+
)

tests/conftest.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import pytest
3+
import responses
4+
import json
5+
6+
7+
@pytest.fixture(scope='module')
8+
def manager():
9+
import upcloud
10+
return upcloud.CloudManager("testuser", "mock-api-password")
11+
12+
13+
class Mock():
14+
base_url = 'https://api.upcloud.com/1.1'
15+
16+
@staticmethod
17+
def read_from_file(filename):
18+
19+
filename = filename.replace("/", "_")
20+
21+
cwd = os.path.dirname(__file__)
22+
f = open(cwd + '/json_data/'+filename, 'r')
23+
return f.read()
24+
25+
@staticmethod
26+
def mock_get(target):
27+
data = Mock.read_from_file(target + '.json')
28+
responses.add(responses.GET, Mock.base_url + '/' + target,
29+
body=data,
30+
status=200,
31+
content_type='application/json')
32+
return data
33+
34+
@staticmethod
35+
def __put_post_callback(request, target, data):
36+
data_field = target.split("/")[0]
37+
payload = json.loads(request.body)
38+
39+
for field in data[data_field]:
40+
if field in payload[data_field]:
41+
data[data_field][field] = payload[data_field][field]
42+
print(data)
43+
return(200, {}, json.dumps(data))
44+
45+
@staticmethod
46+
def mock_post(target):
47+
data = json.loads( Mock.read_from_file(target + '_post.json') )
48+
49+
def callback(request):
50+
return Mock.__put_post_callback(request, target, data)
51+
52+
responses.add_callback(responses.POST, Mock.base_url + '/' + target,
53+
callback=callback,
54+
content_type='application/json')
55+
56+
@staticmethod
57+
def mock_put(target):
58+
data = json.loads( Mock.read_from_file(target + '.json') )
59+
60+
def callback(request):
61+
return Mock.__put_post_callback(request, target, data)
62+
63+
responses.add_callback(responses.PUT, Mock.base_url + '/' + target,
64+
callback=callback,
65+
content_type='application/json')
66+
@staticmethod
67+
def mock_delete(target):
68+
# print(Mock.base_url + "/" + target)
69+
responses.add(responses.DELETE, Mock.base_url + "/" + target,
70+
status = 204 )
71+
72+
@staticmethod
73+
def mock_server_operation(target):
74+
# drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
75+
targetsplit = target.split("/")
76+
targetfile = "/".join( targetsplit[:2] )
77+
78+
data = json.loads( Mock.read_from_file(targetfile + '.json') )
79+
80+
# API will always respond state: "started", see: Server.stop, Server.start, Server,restart
81+
data["server"]["state"] = "started"
82+
83+
data = json.dumps( data )
84+
responses.add(responses.POST, Mock.base_url + "/" + target, status=200, body = data, content_type='application/json')
85+
86+
87+
88+

tests/json_data/account.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"account":
3+
{
4+
"credits": 70750.074,
5+
"username": "testuser"
6+
}
7+
}

tests/json_data/ip_address.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"ip_addresses": {
3+
"ip_address": [
4+
{
5+
"access": "private",
6+
"address": "10.1.0.101",
7+
"ptr_record": "",
8+
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
9+
},
10+
{
11+
"access": "private",
12+
"address": "10.1.0.115",
13+
"ptr_record": "",
14+
"server": "00887721-4fad-4e9c-b781-6f2265753f11"
15+
},
16+
{
17+
"access": "private",
18+
"address": "10.1.1.97",
19+
"ptr_record": "",
20+
"server": "00ee2cf4-91c9-4e7a-89b9-a8087e32896b"
21+
}
22+
]
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ip_address": {
3+
"access": "private",
4+
"address": "10.1.0.101",
5+
"ptr_record": "a.ptr.record",
6+
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ip_address": {
3+
"access": "private",
4+
"address": "10.1.0.101",
5+
"ptr_record": "a.ptr.record",
6+
"server": "00798b85-efdc-41ca-8021-f6ef457b8531"
7+
}
8+
}

tests/json_data/price.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"prices": {"zone": [{"storage_backup": {"amount": 1, "price": 0.007}, "io_request_backup": {"amount": 1000000, "price": 0}, "io_request_maxiops": {"amount": 1000000, "price": 0}, "server_memory": {"amount": 256, "price": 0.45}, "public_bandwidth_in": {"amount": 1, "price": 0}, "io_request_ssd": {"amount": 1000000, "price": 0}, "server_core": {"amount": 1, "price": 1.3}, "storage_ssd": {"amount": 1, "price": 0.05}, "storage_template": {"amount": 1, "price": 0.028}, "public_bandwidth_out": {"amount": 1, "price": 5}, "name": "fi-hel1", "storage_maxiops": {"amount": 1, "price": 0.028}, "ip_address": {"amount": 1, "price": 0.3}, "storage_hdd": {"amount": 1, "price": 0.013}, "io_request_hdd": {"amount": 1000000, "price": 0}, "firewall": {"amount": 1, "price": 0.5}}, {"storage_backup": {"amount": 1, "price": 0.007}, "io_request_backup": {"amount": 1000000, "price": 0}, "io_request_maxiops": {"amount": 1000000, "price": 0}, "server_memory": {"amount": 256, "price": 0.125}, "public_bandwidth_in": {"amount": 1, "price": 0}, "io_request_ssd": {"amount": 1000000, "price": 0}, "server_core": {"amount": 1, "price": 1}, "storage_ssd": {"amount": 1, "price": 0.028}, "storage_template": {"amount": 1, "price": 0.028}, "public_bandwidth_out": {"amount": 1, "price": 5}, "name": "uk-lon1", "storage_maxiops": {"amount": 1, "price": 0.028}, "ip_address": {"amount": 1, "price": 0.3}, "storage_hdd": {"amount": 1, "price": 0.007}, "io_request_hdd": {"amount": 1000000, "price": 0}, "firewall": {"amount": 1, "price": 0.5}}, {"storage_maxiops": {"amount": 1, "price": 0.028}, "io_request_backup": {"amount": 1000000, "price": 0}, "server_core": {"amount": 1, "price": 1}, "storage_backup": {"amount": 1, "price": 0.007}, "public_bandwidth_in": {"amount": 1, "price": 0}, "io_request_maxiops": {"amount": 1000000, "price": 0}, "server_memory": {"amount": 256, "price": 0.125}, "storage_template": {"amount": 1, "price": 0.028}, "public_bandwidth_out": {"amount": 1, "price": 5}, "name": "us-chi1", "ip_address": {"amount": 1, "price": 0.3}, "firewall": {"amount": 1, "price": 0.5}}]}}

tests/json_data/server.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"servers" : {
3+
"server" : [
4+
{
5+
"zone" : "fi-hel1",
6+
"core_number" : "0",
7+
"title" : "Helsinki server",
8+
"hostname" : "fi.example.com",
9+
"memory_amount" : "512",
10+
"uuid" : "00798b85-efdc-41ca-8021-f6ef457b8531",
11+
"state" : "started"
12+
},
13+
{
14+
"zone" : "uk-lon1",
15+
"core_number" : "0",
16+
"title" : "London server",
17+
"hostname" : "uk.example.com",
18+
"memory_amount" : "512",
19+
"uuid" : "009d64ef-31d1-4684-a26b-c86c955cbf46",
20+
"state" : "stopped"
21+
}
22+
]
23+
}
24+
}

0 commit comments

Comments
 (0)