5
5
from upcloud_api .ip_address import IPAddress
6
6
from upcloud_api .server_group import ServerGroup
7
7
from upcloud_api .storage import Storage
8
+ from upcloud_api .upcloud_resource import UpCloudResource
8
9
from upcloud_api .utils import try_it_n_times
9
10
10
11
if TYPE_CHECKING :
@@ -28,6 +29,47 @@ def login_user_block(username, ssh_keys, create_password=False):
28
29
return block
29
30
30
31
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
+
31
73
# TODO: should this inherit from UpcloudResource too?
32
74
class Server :
33
75
"""
@@ -68,6 +110,7 @@ class Server:
68
110
'labels' ,
69
111
'login_user' ,
70
112
'memory_amount' ,
113
+ 'networking' ,
71
114
'nic_model' ,
72
115
'password_delivery' ,
73
116
'plan' ,
@@ -364,6 +407,16 @@ def prepare_post_body(self):
364
407
365
408
body ['server' ]['storage_devices' ] = {'storage_device' : []}
366
409
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
+
367
420
storage_title_id = 0 # running number for unique storage titles
368
421
for storage in self .storage_devices :
369
422
if not hasattr (storage , 'os' ) or storage .os is None :
@@ -420,6 +473,9 @@ def to_dict(self):
420
473
fields ['ip_addresses' ].append (
421
474
{'address' : ip .address , 'access' : ip .access , 'family' : ip .family }
422
475
)
476
+ fields ['networking' ] = []
477
+ for iface in dict .get (dict .get (self .networking , 'interfaces' ), 'interface' ):
478
+ fields ['networking' ].append (ServerNetworkInterface (iface ).to_dict ())
423
479
424
480
for storage in self .storage_devices :
425
481
fields ['storage_devices' ].append (
0 commit comments