Skip to content

Commit 5cfd921

Browse files
authored
Merge pull request #74 from TechConsult/networks_and_hosts
Networks and hosts
2 parents 17c28a8 + bb2fff5 commit 5cfd921

30 files changed

+1217
-7
lines changed

docs/host-mixin.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## About
2+
```python
3+
class HostManager():
4+
"""
5+
Functions for managing hosts. Intended to be used as a mixin for CloudManager.
6+
"""
7+
```
8+
`HostManager` 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_hosts(self):
19+
"""
20+
Returns a list of available hosts, along with basic statistics of them when available.
21+
Returns a list of Host objects.
22+
"""
23+
```
24+
25+
```python
26+
def get_host(self, id):
27+
"""
28+
Returns detailed information about a specific host in a Host object.
29+
Id argument must be passed (can be the id of a host or a Host object).
30+
"""
31+
```
32+
33+
```python
34+
def modify_host(self, host, description='new description'):
35+
"""
36+
Modifies description of a specific host.
37+
Host argument must be provided (can be an id or a Host object).
38+
Returns a Host object.
39+
"""
40+
```

docs/network-mixin.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
## About
2+
```python
3+
class NetworkManager():
4+
"""
5+
Functions for managing networks. Intended to be used as a mixin for CloudManager.
6+
"""
7+
```
8+
`NetworkManager` 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_networks(self, zone="fi-hel1"):
19+
"""
20+
Get a list of all networks.
21+
Zone can be passed to return networks in a specific zone but is not mandatory.
22+
"""
23+
```
24+
25+
```python
26+
def get_network(self, uuid):
27+
"""
28+
Retrieves the details of a specific network.
29+
"""
30+
```
31+
32+
```python
33+
def create_network(
34+
name='test network',
35+
zone='fi-hel1',
36+
address='172.16.0.0/22',
37+
dhcp='yes',
38+
family='IPv4',
39+
):
40+
"""
41+
Creates a new SDN private network that cloud servers from the same zone can be attached to.
42+
Name, zone, address, dhcp and family arguments are required.
43+
Router, dhcp_default_route, dhcp_dns, dhcp_bootfile_url and gateway arguments are optional.
44+
Dhcp and dhcp_default_route accept yes or no (string) as a value.
45+
Dhcp_dns accepts an array of addresses.
46+
Returns a Network object.
47+
"""
48+
```
49+
50+
```python
51+
def modify_network(
52+
network='036df3d0-8629-4549-984e-dc86fc3fa1b0',
53+
dhcp='yes',
54+
family='IPv4',
55+
router='04b65749-61e2-4f08-a259-c75afbe81abf',
56+
):
57+
"""
58+
Modifies the details of a specific SDN private network. The Utility and public networks cannot be modified.
59+
Network, dhcp, family and router arguments are required (router can be an id of a router or a router object, same goes for network).
60+
Name, router, dhcp_default_route, dhcp_dns, dhcp_bootfile_url and gateway arguments are optional.
61+
Dhcp and dhcp_default_route accept yes or no (string) as a value.
62+
Dhcp_dns accepts an array of addresses.
63+
Returns a Network object.
64+
"""
65+
```
66+
67+
```python
68+
def delete_network(self, network):
69+
"""
70+
Deletes an SDN private network. All attached cloud servers must first be detached before SDN private networks can be deleted.
71+
Network argument must be provided (can be an id or a Network object).
72+
Returns an empty response.
73+
"""
74+
```
75+
76+
```python
77+
def get_server_networks(self, server):
78+
"""
79+
List all networks the specific cloud server is connected to.
80+
Server argument must be passed (can be an id or a Server object).
81+
Returns a list of Interface objects.
82+
83+
"""
84+
```
85+
86+
```python
87+
def create_network_interface(
88+
server='0082c083-9847-4f9f-ae04-811251309b35',
89+
network='036df3d0-8629-4549-984e-dc86fc3fa1b0',
90+
type='private',
91+
ip_addresses=[{'family': 'IPv4', 'address': '172.16.1.10'}]
92+
):
93+
"""
94+
Creates a new network interface on the specific cloud server and attaches the specified SDN private network to the new interface.
95+
Server, network, type and ip_addresses arguments must be passed.
96+
Index, source_ip_filtering and bootable arguments are optional.
97+
Server and network arguments can be ids or objects.
98+
Ip_addresses argument must be a list of dicts which contain family and address.
99+
Index must be an integer.
100+
Source_ip_filtering and bootable arguments accept a yes or a no string.
101+
Returns an Interface object.
102+
"""
103+
```
104+
105+
```python
106+
def modify_network_interface(
107+
server='0082c083-9847-4f9f-ae04-811251309b35',
108+
index_in_path=7
109+
):
110+
"""
111+
Modifies the network interface at the selected index on the specific cloud server.
112+
Server and index_in_path arguments are mandatory.
113+
Index_in_body, ip_addresses, source_ip_filtering and bootable arguments are optional.
114+
Server argument can be an id or an object.
115+
Index arguments must be integers.
116+
Ip_addresses argument must be a list of dicts which contain family and address.
117+
Source_ip_filtering and bootable arguments accept a yes or a no string.
118+
Returns an Interface object.
119+
"""
120+
```
121+
122+
```python
123+
def delete_network_interface(self, server, index):
124+
"""
125+
Detaches an SDN private network from a cloud server by deleting the network interface at the selected index on the specific cloud server.
126+
Server and index arguments are mandatory.
127+
Server argument can be an id or an object.
128+
Index argument must be an integer.
129+
Returns an empty response
130+
"""
131+
```
132+
133+
```python
134+
def get_routers(self):
135+
"""
136+
Returns a list of all available routers associated with the current account (list of Router objects).
137+
"""
138+
```
139+
140+
```python
141+
def get_router(self, uuid):
142+
"""
143+
Returns detailed information about a specific router (router object).
144+
UUID argument is mandatory
145+
"""
146+
```
147+
148+
```python
149+
def create_router(self, name):
150+
"""
151+
Creates a new router.
152+
Name is a mandatory argument.
153+
Returns a Router object.
154+
"""
155+
```
156+
157+
```python
158+
def modify_router(self, router, name):
159+
"""
160+
Modify an existing router.
161+
Router and name arguments are mandatory.
162+
Router can be an id or a Router object.
163+
Returns a Router object.
164+
"""
165+
```
166+
167+
```python
168+
def delete_router(self, router):
169+
"""
170+
Delete an existing router.
171+
Router argument is mandatory.
172+
Router can be an id or a Router object.
173+
Returns a Router object.
174+
"""
175+
```

test/conftest.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def mock_get(target, response_file=None):
5151
return data
5252

5353
@staticmethod
54-
def __put_post_callback(request, target, data, ignore_data_field=False, empty_payload=False):
54+
def __put_patch_post_callback(request, target, data, ignore_data_field=False, empty_payload=False):
5555
data_field = target.split("/")[0]
5656

5757
if not empty_payload:
@@ -69,7 +69,7 @@ def mock_post(target, empty_content=False, ignore_data_field=False, empty_payloa
6969
def callback(request):
7070
if not empty_content:
7171
data = json.loads(Mock.read_from_file(target + '_post.json'))
72-
return Mock.__put_post_callback(request, target, data, ignore_data_field, empty_payload)
72+
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
7373
else:
7474
return(200, {}, '{}')
7575

@@ -82,13 +82,25 @@ def mock_put(target, ignore_data_field=False, empty_payload=False, call_api=True
8282
data = json.loads(Mock.read_from_file(target + '.json'))
8383

8484
def callback(request):
85-
return Mock.__put_post_callback(request, target, data, ignore_data_field, empty_payload)
85+
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
8686

8787
url = Mock.base_url + '/' + target if call_api else target
8888
responses.add_callback(responses.PUT, url,
8989
callback=callback,
9090
content_type='application/json')
9191

92+
@staticmethod
93+
def mock_patch(target, ignore_data_field=False, empty_payload=False, call_api=True):
94+
data = json.loads(Mock.read_from_file(target + '.json'))
95+
96+
def callback(request):
97+
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
98+
99+
url = Mock.base_url + '/' + target if call_api else target
100+
responses.add_callback(responses.PATCH, url,
101+
callback=callback,
102+
content_type='application/json')
103+
92104
@staticmethod
93105
def mock_delete(target):
94106
responses.add(responses.DELETE, Mock.base_url + '/' + target,

test/json_data/host.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"hosts": {
3+
"host": [
4+
{
5+
"id": 7653311107,
6+
"description": "My Host #1",
7+
"zone": "private-zone-id",
8+
"windows_enabled": "no",
9+
"stats": {
10+
"stat": [
11+
{
12+
"name": "cpu_idle",
13+
"timestamp": "2019-08-09T12:46:57Z",
14+
"value": 95.2
15+
},
16+
{
17+
"name": "memory_free",
18+
"timestamp": "2019-08-09T12:46:57Z",
19+
"value": 102
20+
}
21+
]
22+
}
23+
},
24+
{
25+
"id": 8055964291,
26+
"description": "My Host #2",
27+
"zone": "private-zone-id",
28+
"windows_enabled": "no",
29+
"stats": {
30+
"stat": [
31+
{
32+
"name": "cpu_idle",
33+
"timestamp": "2019-08-09T12:46:57Z",
34+
"value": 80.1
35+
},
36+
{
37+
"name": "memory_free",
38+
"timestamp": "2019-08-09T12:46:57Z",
39+
"value": 61
40+
}
41+
]
42+
}
43+
}
44+
]
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"host": {
3+
"id": 7653311107,
4+
"description": "My Host #1",
5+
"zone": "private-zone-id",
6+
"windows_enabled": "no",
7+
"stats": {
8+
"stat": [
9+
{
10+
"name": "cpu_idle",
11+
"timestamp": "2019-08-09T12:46:57Z",
12+
"value": 95.2
13+
},
14+
{
15+
"name": "memory_free",
16+
"timestamp": "2019-08-09T12:46:57Z",
17+
"value": 102
18+
}
19+
]
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)