Skip to content

Commit 74ee6fd

Browse files
author
faustas@techconsult.lt
committed
Network and hosts
1 parent a2d4477 commit 74ee6fd

File tree

9 files changed

+312
-2
lines changed

9 files changed

+312
-2
lines changed

upcloud_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
from upcloud_api.server import Server, login_user_block
2323
from upcloud_api.firewall import FirewallRule
2424
from upcloud_api.tag import Tag
25+
from upcloud_api.network import Network
26+
from upcloud_api.interface import Interface
27+
from upcloud_api.router import Router
28+
from upcloud_api.host import Host
2529
from upcloud_api.cloud_manager.cloud_manager import CloudManager

upcloud_api/cloud_manager/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
from upcloud_api.cloud_manager.server_mixin import ServerManager
1010
from upcloud_api.cloud_manager.storage_mixin import StorageManager
1111
from upcloud_api.cloud_manager.tag_mixin import TagManager
12+
from upcloud_api.cloud_manager.network_mixin import NetworkManager
13+
from upcloud_api.cloud_manager.host_mixin import HostManager

upcloud_api/cloud_manager/cloud_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
IPManager,
99
StorageManager,
1010
FirewallManager,
11-
TagManager
11+
TagManager,
12+
NetworkManager,
13+
HostManager
1214
)
1315

1416

15-
class CloudManager(BaseAPI, ServerManager, IPManager, StorageManager, FirewallManager, TagManager):
17+
class CloudManager(BaseAPI, ServerManager, IPManager, StorageManager, FirewallManager, TagManager, NetworkManager, HostManager):
1618
"""
1719
CloudManager contains the core functionality of the upcloud API library.
1820
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
from __future__ import division
4+
from __future__ import absolute_import
5+
6+
from upcloud_api import Host
7+
8+
9+
class HostManager(object):
10+
"""
11+
Functions for managing hosts. Intended to be used as a mixin for CloudManager.
12+
"""
13+
14+
def get_hosts(self):
15+
"""
16+
Returns a list of available hosts, along with basic statistics of them when available.
17+
"""
18+
url = '/host'
19+
res = self.get_request(url)
20+
return [Host(**host) for host in res['hosts']['host']]
21+
22+
def get_host(self, id):
23+
"""
24+
Returns a list of available hosts, along with basic statistics of them when available.
25+
"""
26+
url = '/host/{0}'.format(id)
27+
res = self.get_request(url)
28+
return Host(**res['host'])
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
from __future__ import division
4+
from __future__ import absolute_import
5+
6+
from upcloud_api import Network, Interface, Router
7+
8+
9+
class NetworkManager(object):
10+
"""
11+
Functions for managing networks. Intended to be used as a mixin for CloudManager.
12+
"""
13+
14+
def get_networks(self, zone=None):
15+
"""
16+
Get a list of all networks.
17+
Zone can be passed to return networks in a specific zone
18+
"""
19+
url = '/network/?zone={0}'.format(zone) if zone else '/network/'
20+
res = self.get_request(url)
21+
return [Network(**network) for network in res['networks']['network']]
22+
23+
def get_network(self, uuid):
24+
"""
25+
Retrieves the details of a specific network.
26+
"""
27+
url = '/network/{0}'.format(uuid)
28+
res = self.get_request(url)
29+
return Network(**res['network'])
30+
31+
def create_network(self, name, zone, address, dhcp, family, router=None, dhcp_default_route=None, dhcp_dns=None, dhcp_bootfile_url=None, gateway=None):
32+
"""
33+
Creates a new SDN private network that cloud servers from the same zone can be attached to.
34+
"""
35+
url = '/network/'
36+
body = {
37+
'network': {
38+
'name': name,
39+
'zone': zone,
40+
'ip_networks': {
41+
'ip_network': {
42+
'address': address,
43+
'dhcp': dhcp,
44+
'family': family
45+
}
46+
}
47+
}
48+
}
49+
50+
if router:
51+
body['network']['router'] = router
52+
if dhcp_default_route:
53+
body['network']['ip_networks']['network']['dhcp_default_route'] = dhcp_default_route
54+
if dhcp_dns:
55+
body['network']['ip_networks']['network']['dhcp_dns'] = dhcp_dns
56+
if dhcp_bootfile_url:
57+
body['network']['ip_networks']['network']['dhcp_bootfile_url'] = dhcp_bootfile_url
58+
if gateway:
59+
body['network']['ip_networks']['network']['gateway'] = gateway
60+
res = self.post_request(url, body)
61+
return Network(**res['network'])
62+
63+
def modify_network(self, uuid, dhcp, family, name=None, router=None, dhcp_default_route=None, dhcp_dns=None, dhcp_bootfile_url=None, gateway=None):
64+
"""
65+
Modifies the details of a specific SDN private network. The Utility and public networks cannot be modified.
66+
"""
67+
url = '/network/{}'.format(uuid)
68+
body = {
69+
'network': {
70+
'ip_networks': {
71+
'ip_network': {'family': family}
72+
}
73+
}
74+
}
75+
if name:
76+
body['network']['name'] = name
77+
if dhcp:
78+
body['network']['ip_networks']['ip_network']['dhcp'] = dhcp
79+
if router:
80+
body['network']['router'] = router
81+
if dhcp_default_route:
82+
body['network']['ip_networks']['ip_network']['dhcp_default_route'] = dhcp_default_route
83+
if dhcp_dns:
84+
body['network']['ip_networks']['ip_network']['dhcp_dns'] = dhcp_dns
85+
if dhcp_bootfile_url:
86+
body['network']['ip_networks']['ip_network']['dhcp_bootfile_url'] = dhcp_bootfile_url
87+
if gateway:
88+
body['network']['ip_networks']['ip_network']['gateway'] = gateway
89+
res = self.put_request(url, body)
90+
return Network(**res['network'])
91+
92+
def delete_network(self, uuid):
93+
"""
94+
Deletes an SDN private network. All attached cloud servers must first be detached before SDN private networks can be deleted.
95+
"""
96+
url = '/network/{0}'.format(uuid)
97+
res = self.delete_request(url)
98+
return res
99+
100+
def get_server_networks(self, server):
101+
"""
102+
List all networks the specific cloud server is connected to.
103+
"""
104+
url = '/server/{0}/networking'.format(server)
105+
res = self.get_request(url)
106+
return [Interface(**interface) for interface in res['networking']['interfaces']['interface']]
107+
108+
def create_network_interface(self, server, network, type, ip_addresses, index=None, source_ip_filtering=None, bootable=None):
109+
"""
110+
Creates a new network interface on the specific cloud server and attaches the specified SDN private network to the new interface.
111+
"""
112+
url = '/server/{0}/networking/interface'.format(server)
113+
body = {'interface': {'network': network, 'type': type, 'ip_addresses': {'ip_address': ip_addresses}}}
114+
if index:
115+
body['interface']['index'] = index
116+
if source_ip_filtering:
117+
body['interface']['source_ip_filtering'] = source_ip_filtering
118+
if bootable:
119+
body['interface']['bootable'] = bootable
120+
res = self.post_request(url, body)
121+
return Interface(**res['interface'])
122+
123+
def modify_network_interface(self, server, index_in_path, index_in_body=None, ip_addresses=None, source_ip_filtering=None, bootable=None):
124+
"""
125+
Modifies the network interface at the selected index on the specific cloud server.
126+
"""
127+
url = '/server/{0}/networking/interface/{0}'.format(server, index_in_path)
128+
body = {'interface': {}}
129+
if index_in_body:
130+
body['interface']['index'] = index_in_body
131+
if ip_addresses:
132+
body['interface']['ip_addresses']['ip_address'] = ip_addresses
133+
if source_ip_filtering:
134+
body['interface']['source_ip_filtering'] = source_ip_filtering
135+
if bootable:
136+
body['interface']['bootable'] = bootable
137+
res = self.post_request(url, body)
138+
return Interface(**res['interface'])
139+
140+
def delete_network_interface(self, server, index):
141+
"""
142+
Detaches an SDN private network from a cloud server by deleting the network interface at the selected index on the specific cloud server.
143+
"""
144+
url = '/server/{0}/networking/interface/{0}'.format(server, index_in_path)
145+
res = self.delete_request(url)
146+
return res
147+
148+
def get_routers(self):
149+
"""
150+
Returns a list of all available routers associated with the current account.
151+
"""
152+
url = '/router'
153+
res = self.get_request(url)
154+
return [Router(**router) for router in res['routers']['router']]
155+
156+
def get_router(self, uuid):
157+
"""
158+
Returns detailed information about a specific router.
159+
"""
160+
url = '/router/{0}'.format(uuid)
161+
res = self.get_request(url)
162+
return Router(**res['router'])
163+
164+
def create_router(self, name):
165+
"""
166+
Creates a new router.
167+
"""
168+
url = '/router'
169+
body = {'router': {'name': name}}
170+
res = self.post_request(url, body)
171+
return Router(**res['router'])
172+
173+
def modify_router(self, uuid, name):
174+
"""
175+
Modify an existing router.
176+
"""
177+
url = '/router/{0}'.format(uuid)
178+
body = {'router': {'name': name}}
179+
res = self.put_request(url, body)
180+
return Router(**res['router'])
181+
182+
def delete_router(self, uuid):
183+
"""
184+
Delete an existing router.
185+
"""
186+
url = '/router/{0}'.format(uuid)
187+
res = self.delete_request(url)
188+
return res

upcloud_api/host.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 upcloud_api import UpCloudResource
7+
from upcloud_api.utils import assignIfExists
8+
9+
10+
class Host(UpCloudResource):
11+
"""
12+
Class representation of UpCloud network.
13+
"""
14+
15+
ATTRIBUTES = {
16+
'id': None,
17+
'description': None,
18+
'zone': None,
19+
'windows_enabled': None,
20+
'stats': None
21+
}

upcloud_api/interface.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 upcloud_api import UpCloudResource
7+
from upcloud_api.utils import assignIfExists
8+
9+
10+
class Interface(UpCloudResource):
11+
"""
12+
Class representation of UpCloud network interface.
13+
"""
14+
15+
ATTRIBUTES = {
16+
'index': None,
17+
'ip_addresses': None,
18+
'mac': None,
19+
'network': None,
20+
'source_ip_filtering': None,
21+
'type': None,
22+
'bootable': None
23+
}

upcloud_api/network.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 upcloud_api import UpCloudResource
7+
from upcloud_api.utils import assignIfExists
8+
9+
10+
class Network(UpCloudResource):
11+
"""
12+
Class representation of UpCloud network.
13+
"""
14+
15+
ATTRIBUTES = {
16+
'name': None,
17+
'type': None,
18+
'uuid': None,
19+
'zone': None,
20+
'ip_networks': None,
21+
'servers': None
22+
}

upcloud_api/router.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 upcloud_api import UpCloudResource
7+
from upcloud_api.utils import assignIfExists
8+
9+
10+
class Router(UpCloudResource):
11+
"""
12+
Class representation of UpCloud network.
13+
"""
14+
15+
ATTRIBUTES = {
16+
'name': None,
17+
'type': None,
18+
'uuid': None,
19+
'attached_networks': None
20+
}

0 commit comments

Comments
 (0)