Skip to content

Commit 3a0a43e

Browse files
committed
Task done
1 parent e7bbb84 commit 3a0a43e

16 files changed

+418
-16
lines changed

docs/object_storage-mixin.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## About
2+
```python
3+
class ObjectStorageManager():
4+
"""
5+
Functions for managing Object Storages. Intended to be used as a mixin for CloudManager.
6+
"""
7+
```
8+
`ObjectStorageManager` is a mixed into `CloudManager` and the following methods are available by
9+
10+
```python
11+
manager = CloudManager("api-username", "password")
12+
manager.method()
13+
```
14+
15+
## Methods
16+
17+
```python
18+
def get_object_storages(self):
19+
"""
20+
List all Object Storage devices on the account or those which the subaccount has permissions.
21+
Returns a list of ObjectStorage objects.
22+
"""
23+
```
24+
25+
```python
26+
def create_object_storage(self, zone, access_key, secret_key, size, name=None, description=None):
27+
"""
28+
Used to create a new Object Storage device with a given name, size and location.
29+
Zone, access_key, secret_key and size are mandatory while name and description are optional.
30+
"""
31+
```
32+
33+
```python
34+
def modify_object_storage(self, object_storage, access_key=None, secret_key=None, description=None, size=None):
35+
"""
36+
Modify requests can be used to update the details of an Object Storage including description, access_key and secret_key.
37+
Object_storage is mandatory and can be a uuid or a ObjectStorage object.
38+
Access_key, secret_key, description and size are optional.
39+
If passed access_key needs to be provided with secret_key and vice-versa.
40+
"""
41+
```
42+
43+
```python
44+
def delete_object_storage(self, object_storage):
45+
"""
46+
Object Storage devices can be deleted using the following API request.
47+
Object_storage is mandatory and can be a uuid or a ObjectStorage object.
48+
"""
49+
```
50+
51+
```python
52+
def get_object_storage_network_statistics(
53+
self,
54+
object_storage,
55+
datetime_from,
56+
datetime_to=None,
57+
interval=None,
58+
bucket=[],
59+
filename=[],
60+
method=[],
61+
status=[],
62+
group_by=[],
63+
order_by=[],
64+
limit=None
65+
):
66+
"""
67+
The network usage of an Object Storage device is metered and can be reviewed using the statistics request.
68+
Object_storage is mandatory and can be a uuid or a ObjectStorage object.
69+
Datetime_from is mandatory and needs to be a string example: 2020-11-03 00:00:00
70+
Datetime_to is optional and needs to be a string example: 2020-11-04 00:00:00
71+
Interval is optional and needs to be an integer
72+
Bucket is optional and needs to be a list of bucket name strings
73+
Filename is optional and needs to be a list of filename strings
74+
Method is optional and needs to be a list of method name strings
75+
Status is optional and needs to be a list of http status codes as integers
76+
Group_by is optional and needs to be a list of specified properties as strings
77+
Order_by is optional and needs to be a list of specified properties as strings
78+
Limit is optional and needs to be an integer
79+
"""
80+
```

test/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ def read_from_file(filename):
3939
return read_from_file(filename)
4040

4141
@staticmethod
42-
def mock_get(target, response_file=None):
42+
def mock_get(target, response_file=None, match_querystring=False):
4343
if not response_file:
4444
response_file = target + '.json'
4545

4646
data = Mock.read_from_file(response_file)
4747
responses.add(responses.GET, Mock.base_url + '/' + target,
4848
body=data,
4949
status=200,
50-
content_type='application/json')
50+
content_type='application/json',
51+
match_querystring=True)
5152
return data
5253

5354
@staticmethod

test/json_data/object-storage.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"object_storages":
3+
{
4+
"object_storage":
5+
[
6+
{
7+
"created": "2020-11-03T15:28:59Z",
8+
"description": "test for python api",
9+
"name": "pyapi-test3",
10+
"size": 250,
11+
"state": "started",
12+
"url": "https://pyapi-test3.fi-hel2.upcloudobjects.com/",
13+
"uuid": "06b0e4fc-d74b-455e-a373-60cd6ca84022",
14+
"zone": "fi-hel2"
15+
},
16+
{
17+
"created": "2020-11-03T15:16:15Z",
18+
"description": "test-python-api",
19+
"name": "test-python-api",
20+
"size": 250,
21+
"state": "started",
22+
"url": "https://test-python-api.fi-hel2.upcloudobjects.com/",
23+
"uuid": "06f12ae8-8531-44c9-9529-acdd278c7408",
24+
"zone": "fi-hel2"
25+
}
26+
]
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"object_storage": {
3+
"created": "2020-11-03T15:28:59Z",
4+
"description": "new description",
5+
"name": "test-os",
6+
"size": 500,
7+
"state": "started",
8+
"url": "https://pyapi-test3.fi-hel2.upcloudobjects.com/",
9+
"uuid": "0608edc4-d4a3-4b01-abe4-e147bd7ffe45",
10+
"zone": "fi-hel2"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"object_storage": {
3+
"created": "2020-11-03T15:28:59Z",
4+
"description": "test for python api",
5+
"name": "pyapi-test3",
6+
"size": 250,
7+
"state": "started",
8+
"url": "https://pyapi-test3.fi-hel2.upcloudobjects.com/",
9+
"uuid": "06b0e4fc-d74b-455e-a373-60cd6ca84022",
10+
"zone": "fi-hel2"
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"stats":
3+
{
4+
"stat":
5+
[
6+
{
7+
"bytes_received": 1041,
8+
"bytes_transmitted": 3011,
9+
"requests": 5,
10+
"timestamp": "2020-11-03T15:00:00Z"
11+
},
12+
{
13+
"bytes_received": 583,
14+
"bytes_transmitted": 1296,
15+
"requests": 2,
16+
"timestamp": "2020-11-04T10:00:00Z"
17+
},
18+
{
19+
"bytes_received": 279,
20+
"bytes_transmitted": 648,
21+
"requests": 1,
22+
"timestamp": "2020-11-04T11:00:00Z"
23+
}
24+
]
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"object_storage":
3+
{
4+
"created": "2020-11-05T13:30:21Z",
5+
"description": "for tests",
6+
"name": "test-os",
7+
"size": 250,
8+
"state": "started",
9+
"url": "https://test-os.fi-hel2.upcloudobjects.com/",
10+
"used_space": 0,
11+
"uuid": "0608edc4-d4a3-4b01-abe4-e147bd7ffe45",
12+
"zone": "fi-hel2"
13+
}
14+
}

test/test_object_storage.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
from conftest import Mock
7+
import responses
8+
9+
10+
class TestObjectStorage(object):
11+
@responses.activate
12+
def test_get_object_storages(self, manager):
13+
data = Mock.mock_get('object-storage')
14+
object_storages = manager.get_object_storages()
15+
16+
for object_storage in object_storages:
17+
assert type(object_storage).__name__ == 'ObjectStorage'
18+
19+
@responses.activate
20+
def test_create_object_storage(self, manager):
21+
data = Mock.mock_post('object-storage', ignore_data_field=True)
22+
object_storage = manager.create_object_storage('fi-hel2', 'access_key', 'secret_key', 250, 'test-os', 'for tests')
23+
24+
assert type(object_storage).__name__ == 'ObjectStorage'
25+
assert object_storage.name == 'test-os'
26+
assert object_storage.description == 'for tests'
27+
assert object_storage.zone == 'fi-hel2'
28+
assert object_storage.size == 250
29+
30+
@responses.activate
31+
def test_get_object_storage(self, manager):
32+
data = Mock.mock_get('object-storage/06b0e4fc-d74b-455e-a373-60cd6ca84022')
33+
object_storage = manager.get_object_storage('06b0e4fc-d74b-455e-a373-60cd6ca84022')
34+
35+
assert type(object_storage).__name__ == 'ObjectStorage'
36+
assert object_storage.name == 'pyapi-test3'
37+
assert object_storage.description == 'test for python api'
38+
assert object_storage.zone == 'fi-hel2'
39+
assert object_storage.size == 250
40+
41+
@responses.activate
42+
def test_modify_object_storage(self, manager):
43+
data = Mock.mock_patch('object-storage/0608edc4-d4a3-4b01-abe4-e147bd7ffe45', ignore_data_field=True)
44+
object_storage = manager.modify_object_storage('0608edc4-d4a3-4b01-abe4-e147bd7ffe45', 'access_key', 'secret_key', 'new description', 500)
45+
46+
assert type(object_storage).__name__ == 'ObjectStorage'
47+
assert object_storage.name == 'test-os'
48+
assert object_storage.description == 'new description'
49+
assert object_storage.zone == 'fi-hel2'
50+
assert object_storage.size == 500
51+
52+
@responses.activate
53+
def test_delete_object_storage(self, manager):
54+
data = Mock.mock_delete('object-storage/0608edc4-d4a3-4b01-abe4-e147bd7ffe45')
55+
res = manager.delete_object_storage('0608edc4-d4a3-4b01-abe4-e147bd7ffe45')
56+
57+
assert res == {}
58+
59+
@responses.activate
60+
def test_get_object_storage_network_statistics(self, manager):
61+
data = Mock.mock_get('object-storage/06b0e4fc-d74b-455e-a373-60cd6ca84022/stats/network/?from=2020-11-03T00%3A00%3A00%2B02%3A00', match_querystring=True)
62+
res = manager.get_object_storage_network_statistics('06b0e4fc-d74b-455e-a373-60cd6ca84022', '2020-11-03 00:00:00')
63+
64+
assert 'stats' in res
65+
assert len(res.get('stats').get('stat')) == 3

upcloud_api/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from __future__ import absolute_import
99

1010

11-
__version__ = '0.4.6'
11+
__version__ = '0.4.6'
1212
__author__ = 'Elias Nygren'
13-
__author_email__ = 'elias.nygren@upcloud.com'
13+
__author_email__ = 'elias.nygren@upcloud.com'
1414
__maintainer__ = 'UpCloud'
1515
__maintainer_email__ = 'hello@upcloud.com'
16-
__license__ = 'MIT'
16+
__license__ = 'MIT'
1717
__copyright__ = 'Copyright (c) 2015 UpCloud'
1818

1919
from upcloud_api.upcloud_resource import UpCloudResource
@@ -30,4 +30,5 @@
3030
from upcloud_api.router import Router
3131
from upcloud_api.host import Host
3232
from upcloud_api.ip_network import IpNetwork
33+
from upcloud_api.object_storage import ObjectStorage
3334
from upcloud_api.cloud_manager.cloud_manager import CloudManager

upcloud_api/cloud_manager/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
from upcloud_api.cloud_manager.tag_mixin import TagManager
1212
from upcloud_api.cloud_manager.network_mixin import NetworkManager
1313
from upcloud_api.cloud_manager.host_mixin import HostManager
14+
from upcloud_api.cloud_manager.object_storage_mixin import ObjectStorageManager

0 commit comments

Comments
 (0)