Skip to content

Commit bb2fff5

Browse files
author
Faustas
committed
Finished networks and hosts
1 parent 4f545a2 commit bb2fff5

11 files changed

+376
-20
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+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"networking":
3+
{
4+
"interfaces":
5+
{
6+
"interface":
7+
[
8+
{
9+
"bootable": "no",
10+
"index": 1,
11+
"ip_addresses": {"ip_address": [{"address": "185.20.136.92", "family": "IPv4", "floating": "no"}]},
12+
"mac": "da:4e:74:bd:2b:29",
13+
"network": "03000000-0000-4000-8002-000000000000",
14+
"source_ip_filtering": "yes",
15+
"type": "public"
16+
},
17+
{
18+
"bootable": "no",
19+
"index": 2,
20+
"ip_addresses": {"ip_address": [{"address": "10.1.6.245", "family": "IPv4", "floating": "no"}]},
21+
"mac": "da:4e:74:bd:90:93",
22+
"network": "03b34bc2-5adf-4fc4-8c44-83f869058f5a",
23+
"source_ip_filtering": "yes",
24+
"type": "utility"
25+
},
26+
{
27+
"bootable": "no",
28+
"index": 3,
29+
"ip_addresses": {"ip_address": [{"address": "2a04:3540:1000:310:d84e:74ff:febd:0760", "family": "IPv6", "floating": "no"}]},
30+
"mac": "da:4e:74:bd:07:60",
31+
"network": "03000000-0000-4000-8015-000000000000",
32+
"source_ip_filtering": "yes",
33+
"type": "public"
34+
},
35+
{
36+
"bootable": "yes",
37+
"index": 7,
38+
"ip_addresses": {"ip_address": [{"address": "172.16.1.10", "family": "IPv4", "floating": "no"}]},
39+
"mac": "da:4e:74:bd:ce:18",
40+
"network": "036df3d0-8629-4549-984e-dc86fc3fa1b0",
41+
"source_ip_filtering": "yes",
42+
"type": "private"
43+
}
44+
]
45+
}
46+
}
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"interface":
3+
{
4+
"bootable": "no",
5+
"index": 8,
6+
"ip_addresses": {"ip_address": [{"address": "172.16.1.10", "family": "IPv4", "floating": "no"}]},
7+
"mac": "da:4e:74:bd:ce:18",
8+
"network": "036df3d0-8629-4549-984e-dc86fc3fa1b0",
9+
"source_ip_filtering": "no",
10+
"type": "private"
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"interface":
3+
{
4+
"bootable": "yes",
5+
"index": 7,
6+
"ip_addresses":
7+
{
8+
"ip_address":
9+
[
10+
{
11+
"address": "172.16.1.10",
12+
"family": "IPv4",
13+
"floating": "no"}]},
14+
"mac": "da:4e:74:bd:ce:18",
15+
"network": "036df3d0-8629-4549-984e-dc86fc3fa1b0",
16+
"source_ip_filtering": "yes",
17+
"type": "private"
18+
}
19+
}

test/test_network.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_get_network(self, manager):
2828

2929
@responses.activate
3030
def test_create_network(self, manager):
31-
data = Mock.mock_post('network')
31+
data = Mock.mock_post('network', ignore_data_field=True)
3232
network = manager.create_network(
3333
name='test network',
3434
zone='fi-hel1',
@@ -49,9 +49,9 @@ def test_create_network(self, manager):
4949

5050
@responses.activate
5151
def test_modify_network(self, manager):
52-
data = Mock.mock_put('network/036df3d0-8629-4549-984e-dc86fc3fa1b0')
52+
data = Mock.mock_put('network/036df3d0-8629-4549-984e-dc86fc3fa1b0', ignore_data_field=True)
5353
network = manager.modify_network(
54-
uuid='036df3d0-8629-4549-984e-dc86fc3fa1b0',
54+
network='036df3d0-8629-4549-984e-dc86fc3fa1b0',
5555
dhcp='yes',
5656
family='IPv4',
5757
router='04b65749-61e2-4f08-a259-c75afbe81abf',
@@ -62,11 +62,54 @@ def test_modify_network(self, manager):
6262
gateway='172.16.0.1'
6363
)
6464

65-
assert type(network).__name__ == "test network modify"
65+
assert type(network).__name__ == "Network"
66+
assert network.name == 'test network modify'
6667
assert network.uuid == '036df3d0-8629-4549-984e-dc86fc3fa1b0'
6768
assert network.type == 'private'
6869
assert network.zone == 'fi-hel1'
6970

71+
@responses.activate
72+
def test_get_server_networks(self, manager):
73+
data = Mock.mock_get('server/0082c083-9847-4f9f-ae04-811251309b35/networking')
74+
networks = manager.get_server_networks('0082c083-9847-4f9f-ae04-811251309b35')
75+
76+
for network in networks:
77+
assert type(network).__name__ == "Interface"
78+
79+
@responses.activate
80+
def test_create_network_interface(self, manager):
81+
data = Mock.mock_post('server/0082c083-9847-4f9f-ae04-811251309b35/networking/interface', ignore_data_field=True)
82+
network_interface = manager.create_network_interface(
83+
server='0082c083-9847-4f9f-ae04-811251309b35',
84+
network='036df3d0-8629-4549-984e-dc86fc3fa1b0',
85+
type='private',
86+
ip_addresses=[{'family': 'IPv4', 'address': '172.16.1.10'}],
87+
index=7,
88+
source_ip_filtering='yes',
89+
bootable='yes'
90+
)
91+
assert type(network_interface).__name__=="Interface"
92+
93+
@responses.activate
94+
def test_modify_network_interface(self, manager):
95+
data = Mock.mock_put('server/0082c083-9847-4f9f-ae04-811251309b35/networking/interface/7', ignore_data_field=True)
96+
network_interface = manager.modify_network_interface(
97+
server='0082c083-9847-4f9f-ae04-811251309b35',
98+
index_in_path=7,
99+
index_in_body=8,
100+
ip_addresses=[{'family': 'IPv4', 'address': '172.16.1.10'}],
101+
source_ip_filtering='no',
102+
bootable='no'
103+
)
104+
assert type(network_interface).__name__=="Interface"
105+
106+
@responses.activate
107+
def test_delete_network_interface(self, manager):
108+
data = Mock.mock_delete('server/0082c083-9847-4f9f-ae04-811251309b35/networking/interface/8')
109+
res = manager.delete_network_interface('0082c083-9847-4f9f-ae04-811251309b35', 8)
110+
111+
assert res == {}
112+
70113
@responses.activate
71114
def test_delete_network(sekf, manager):
72115
data = Mock.mock_delete('network/03000000-0000-4000-8001-000000000000')

upcloud_api/cloud_manager/host_mixin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def get_hosts(self):
2121

2222
def get_host(self, id):
2323
"""
24-
Returns a list of available hosts, along with basic statistics of them when available.
24+
Returns detailed information about a specific host.
2525
"""
2626
url = '/host/{0}'.format(id)
2727
res = self.get_request(url)
2828
return Host(**res['host'])
2929

30-
def modify_host(self, id, description):
30+
def modify_host(self, host, description):
3131
"""
3232
Modifies description of a specific host.
3333
"""
34-
url = '/host/{0}'.format(id)
34+
url = '/host/{0}'.format(host)
3535
body = {'host': {'description': description}}
3636
res = self.patch_request(url, body)
3737
return Host(**res['host'])

0 commit comments

Comments
 (0)