Skip to content

Commit c2f3397

Browse files
author
Antti Myyrä
committed
feat(server): Server network interfaces to support modern UpCloud networking, and to address #116
1 parent 88dd41a commit c2f3397

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

upcloud_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from upcloud_api.network import Network
2222
from upcloud_api.object_storage import ObjectStorage
2323
from upcloud_api.router import Router
24-
from upcloud_api.server import Server, login_user_block
24+
from upcloud_api.server import Server, ServerNetworkInterface, login_user_block
2525
from upcloud_api.server_group import ServerGroup, ServerGroupAffinityPolicy
2626
from upcloud_api.storage import Storage
2727
from upcloud_api.storage_import import StorageImport

upcloud_api/server.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from upcloud_api.ip_address import IPAddress
66
from upcloud_api.server_group import ServerGroup
77
from upcloud_api.storage import Storage
8+
from upcloud_api.upcloud_resource import UpCloudResource
89
from upcloud_api.utils import try_it_n_times
910

1011
if TYPE_CHECKING:
@@ -28,6 +29,47 @@ def login_user_block(username, ssh_keys, create_password=False):
2829
return block
2930

3031

32+
class ServerNetworkInterface(UpCloudResource):
33+
"""
34+
Class representation of server network interface
35+
"""
36+
37+
ATTRIBUTES = {
38+
'ip_addresses': [],
39+
'type': 'public',
40+
'network': None,
41+
'source_ip_filtering': 'yes',
42+
}
43+
44+
def __init__(self, raw_dict, **kwargs):
45+
"""
46+
Initialize network interface and set sane defaults
47+
"""
48+
super().__init__(**kwargs)
49+
for k, v in raw_dict.items():
50+
if k in ServerNetworkInterface.ATTRIBUTES:
51+
setattr(self, k, v)
52+
53+
if not raw_dict.get('ip_addresses'):
54+
self.ip_addresses = [{'family': 'IPv4'}]
55+
56+
def to_dict(self):
57+
"""
58+
Returns a dict implementation of a network interface to support server creation.
59+
"""
60+
body = {
61+
'type': self.type,
62+
'ip_addresses': {
63+
'ip_address': self.ip_addresses,
64+
},
65+
}
66+
67+
if hasattr(self, 'network'):
68+
body['network'] = self.network
69+
70+
return body
71+
72+
3173
# TODO: should this inherit from UpcloudResource too?
3274
class Server:
3375
"""
@@ -68,6 +110,7 @@ class Server:
68110
'labels',
69111
'login_user',
70112
'memory_amount',
113+
'networking',
71114
'nic_model',
72115
'password_delivery',
73116
'plan',
@@ -364,6 +407,16 @@ def prepare_post_body(self):
364407

365408
body['server']['storage_devices'] = {'storage_device': []}
366409

410+
if hasattr(self, 'networking') and isinstance(self.networking, list):
411+
interfaces = []
412+
for iface in self.networking:
413+
if isinstance(iface, ServerNetworkInterface):
414+
interfaces.append(iface.to_dict())
415+
else:
416+
interfaces.append(iface)
417+
418+
body['server']['networking'] = {'interfaces': {'interface': interfaces}}
419+
367420
storage_title_id = 0 # running number for unique storage titles
368421
for storage in self.storage_devices:
369422
if not hasattr(storage, 'os') or storage.os is None:
@@ -420,6 +473,9 @@ def to_dict(self):
420473
fields['ip_addresses'].append(
421474
{'address': ip.address, 'access': ip.access, 'family': ip.family}
422475
)
476+
fields['networking'] = []
477+
for iface in dict.get(dict.get(self.networking, 'interfaces'), 'interface'):
478+
fields['networking'].append(ServerNetworkInterface(iface).to_dict())
423479

424480
for storage in self.storage_devices:
425481
fields['storage_devices'].append(

0 commit comments

Comments
 (0)