Skip to content

Commit 1cc61b7

Browse files
author
Thibaud Baas
committed
fm: FMVirtualNetworkCreator
1 parent 234341e commit 1cc61b7

File tree

2 files changed

+168
-67
lines changed

2 files changed

+168
-67
lines changed

dataikuapi/fm/virtualnetworks.py

Lines changed: 158 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,123 @@
11
from dataikuapi.fm.future import FMFuture
22

3+
4+
class FMVirtualNetworkCreator(object):
5+
def __init__(self, client, label):
6+
"""
7+
A builder class to create a Virtual Network
8+
9+
:param str label: The label of the Virtual Network
10+
"""
11+
self.client = client
12+
self.data = {}
13+
self.data["label"] = label
14+
self.data["mode"] = "EXISTING_MONOTENANT"
15+
16+
def with_internet_access_mode(self, internet_access_mode):
17+
"""
18+
:param str internet_access_mode: The internet access mode of the instances created in this virtual network. Accepts "YES", "NO", "EGRESS_ONLY". Defaults to "YES"
19+
"""
20+
if internet_access_mode not in ["YES", "NO", "EGRESS_ONLY"]:
21+
raise ValueError(
22+
'internet_access_mode should be either "YES", "NO", or "EGRESS_ONLY"'
23+
)
24+
25+
self.data["internetAccessMode"] = internet_access_mode
26+
return self
27+
28+
29+
class FMAWSVirtualNetworkCreator(FMVirtualNetworkCreator):
30+
def with_vpc(self, aws_vpc_id, aws_subnet_id):
31+
"""
32+
Setup the VPC and Subnet to with the VirtualNetwork
33+
34+
:param str aws_vpc_id: ID of the VPC to use
35+
:param str aws_subnet_id: ID of the subnet to use
36+
"""
37+
self.data["awsVpcId"] = aws_vpc_id
38+
self.data["awsSubnetId"] = aws_subnet_id
39+
return self
40+
41+
def with_auto_create_security_groups(self):
42+
"""
43+
Automatically create the AWS Security Groups when creating this VirtualNetwork
44+
"""
45+
self.data["awsAutoCreateSecurityGroups"] = True
46+
return self
47+
48+
def with_aws_security_groups(self, aws_security_groups):
49+
"""
50+
Use pre-created AWS Security Groups
51+
52+
:param list aws_security_groups: A list of up to 5 security group ids to assign to the instances created in this virtual network.
53+
"""
54+
self.data["awsAutoCreateSecurityGroups"] = False
55+
self.data["awsSecurityGroups"] = aws_security_groups
56+
return self
57+
58+
def create(self):
59+
"""
60+
Create the VirtualNetwork
61+
62+
:return: Created VirtualNetwork
63+
:rtype: :class:`dataikuapi.fm.virtualnetworks.FMAWSVirtualNetwork`
64+
"""
65+
vn = self.client._perform_tenant_json(
66+
"POST", "/virtual-networks", body=self.data
67+
)
68+
return FMAWSVirtualNetwork(self, vn)
69+
70+
71+
class FMAzureVirtualNetworkCreator(FMVirtualNetworkCreator):
72+
def with_virtual_network(self, azure_vn_id, azure_subnet_id):
73+
"""
74+
Setup the Azure Virtual Network and Subnet to with the VirtualNetwork
75+
76+
:param str azure_vn_id: Resource ID of the Azure Virtual Network to use
77+
:param str azure_subnet_id: Resource ID of the subnet to use
78+
"""
79+
self.data["azureVnId"] = azure_vn_id
80+
self.data["azureSubnetId"] = azure_subnet_id
81+
return self
82+
83+
def with_auto_update_security_groups(self, auto_update_security_groups=True):
84+
"""
85+
Auto update the security groups of the Azure Virtual Network
86+
87+
:param boolean auto_update_security_groups: Optional, Auto update the subnet security group. Defaults to True
88+
"""
89+
self.data["azureAutoUpdateSecurityGroups"] = auto_update_security_groups
90+
return self
91+
92+
def create(self):
93+
"""
94+
Create the VirtualNetwork
95+
96+
:return: Created VirtualNetwork
97+
:rtype: :class:`dataikuapi.fm.virtualnetworks.FMAzureVirtualNetwork`
98+
"""
99+
vn = self.client._perform_tenant_json(
100+
"POST", "/virtual-networks", body=self.data
101+
)
102+
return FMAzureVirtualNetwork(self, vn)
103+
104+
3105
class FMVirtualNetwork(object):
4106
def __init__(self, client, vn_data):
5-
self.client = client
107+
self.client = client
6108
self.vn_data = vn_data
7-
self.id = self.vn_data['id']
109+
self.id = self.vn_data["id"]
8110

9111
def save(self):
10112
"""
11113
Update the Virtual Network.
12114
"""
13-
self.client._perform_tenant_empty("PUT", "/virtual-networks/%s" % self.id, body=self.vn_data)
14-
self.vn_data = self.client._perform_tenant_json("GET", "/virtual-networks/%s" % self.id)
115+
self.client._perform_tenant_empty(
116+
"PUT", "/virtual-networks/%s" % self.id, body=self.vn_data
117+
)
118+
self.vn_data = self.client._perform_tenant_json(
119+
"GET", "/virtual-networks/%s" % self.id
120+
)
15121

16122
def delete(self):
17123
"""
@@ -20,10 +126,14 @@ def delete(self):
20126
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the deletion process
21127
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
22128
"""
23-
future = self.client._perform_tenant_json("DELETE", "/virtual-networks/%s" % self.id)
129+
future = self.client._perform_tenant_json(
130+
"DELETE", "/virtual-networks/%s" % self.id
131+
)
24132
return FMFuture.from_resp(self.client, future)
25133

26-
def set_fleet_management(self, enable, event_server=None, deployer_management="NO_MANAGED_DEPLOYER"):
134+
def set_fleet_management(
135+
self, enable, event_server=None, deployer_management="NO_MANAGED_DEPLOYER"
136+
):
27137
"""
28138
When enabled, all instances in this virtual network know each other and can centrally manage deployer and logs centralization
29139
@@ -36,40 +146,64 @@ def set_fleet_management(self, enable, event_server=None, deployer_management="N
36146
- "EACH_DESIGN_NODE": Deployer from design. Recommanded if you have a single design node and want a simpler setup.
37147
"""
38148

39-
self.vn_data['managedNodesDirectory'] = enable
40-
self.vn_data['eventServerNodeLabel'] = event_server
41-
self.vn_data['nodesDirectoryDeployerMode'] = deployer_management
149+
self.vn_data["managedNodesDirectory"] = enable
150+
self.vn_data["eventServerNodeLabel"] = event_server
151+
self.vn_data["nodesDirectoryDeployerMode"] = deployer_management
152+
self.save()
153+
154+
def set_https_strategy(self, https_strategy):
155+
"""
156+
Set the HTTPS strategy for this virtual network
157+
158+
:param object: a :class:`dataikuapi.fm.virtualnetworks.FMHTTPSStrategy`
159+
"""
160+
self.vn_data.update(https_strategy)
42161
self.save()
43162

44-
def set_dns_strategy(self, assign_domain_name, aws_private_ip_zone53_id=None, aws_public_ip_zone53_id=None, azure_dns_zone_id=None):
163+
164+
class FMAWSVirtualNetwork(FMVirtualNetwork):
165+
def set_dns_strategy(
166+
self,
167+
assign_domain_name,
168+
aws_private_ip_zone53_id=None,
169+
aws_public_ip_zone53_id=None,
170+
):
45171
"""
46172
Set the DNS strategy for this virtual network
47173
48174
:param boolean assign_domain_name: If false, don't assign domain names, use ip_only
49175
:param str aws_private_ip_zone53_id: Optional, AWS Only, the ID of the AWS Route53 Zone to use for private ip
50176
:param str aws_public_ip_zone53_id: Optional, AWS Only, the ID of the AWS Route53 Zone to use for public ip
51-
:param str azure_dns_zone_id: Optional, Azure Only, the ID of the Azure DNS zone to use
52177
"""
53178

54179
if assign_domain_name:
55-
self.vn_data['dnsStrategy'] = "VN_SPECIFIC_CLOUD_DNS_SERVICE"
56-
self.vn_data['awsRoute53PrivateIPZoneId'] = aws_private_ip_zone53_id
57-
self.vn_data['awsRoute53PublicIPZoneId'] = aws_public_ip_zone53_id
58-
self.vn_data['azureDnsZoneId'] = azure_dns_zone_id
59-
else :
60-
self.vn_data['dnsStrategy'] = "NONE"
180+
self.vn_data["dnsStrategy"] = "VN_SPECIFIC_CLOUD_DNS_SERVICE"
181+
self.vn_data["awsRoute53PrivateIPZoneId"] = aws_private_ip_zone53_id
182+
self.vn_data["awsRoute53PublicIPZoneId"] = aws_public_ip_zone53_id
183+
else:
184+
self.vn_data["dnsStrategy"] = "NONE"
61185

62186
self.save()
63187

64-
def set_https_strategy(self, https_strategy):
188+
189+
class FMAzureVirtualNetwork(FMVirtualNetwork):
190+
def set_dns_strategy(self, assign_domain_name, azure_dns_zone_id=None):
65191
"""
66-
Set the HTTPS strategy for this virtual network
192+
Set the DNS strategy for this virtual network
67193
68-
:param object: a :class:`dataikuapi.fm.virtualnetworks.FMHTTPSStrategy`
194+
:param boolean assign_domain_name: If false, don't assign domain names, use ip_only
195+
:param str azure_dns_zone_id: Optional, Azure Only, the ID of the Azure DNS zone to use
69196
"""
70-
self.vn_data.update(https_strategy)
197+
198+
if assign_domain_name:
199+
self.vn_data["dnsStrategy"] = "VN_SPECIFIC_CLOUD_DNS_SERVICE"
200+
self.vn_data["azureDnsZoneId"] = azure_dns_zone_id
201+
else:
202+
self.vn_data["dnsStrategy"] = "NONE"
203+
71204
self.save()
72205

206+
73207
class FMHTTPSStrategy(dict):
74208
def __init__(self, data, https_strategy, http_redirect=False):
75209
"""
@@ -82,11 +216,11 @@ def __init__(self, data, https_strategy, http_redirect=False):
82216
- :meth:`dataikuapi.fm.virtualnetwork.FMHTTPSStrategy.lets_encrypt` to use Let's Encrypt
83217
"""
84218
super(FMHTTPSStrategy, self).__init__(data)
85-
self['httpsStrategy'] = https_strategy
219+
self["httpsStrategy"] = https_strategy
86220
if http_redirect:
87-
self['httpStrategy'] = "REDIRECT"
221+
self["httpStrategy"] = "REDIRECT"
88222
else:
89-
self['httpStrategy'] = "DISABLE"
223+
self["httpStrategy"] = "DISABLE"
90224

91225
@staticmethod
92226
def disable():

dataikuapi/fmclient.py

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .utils import DataikuException
99

1010
from .fm.tenant import FMCloudCredentials
11-
from .fm.virtualnetworks import FMVirtualNetwork
11+
from .fm.virtualnetworks import FMVirtualNetwork, FMAWSVirtualNetworkCreator, FMAzureVirtualNetworkCreator
1212
from .fm.instances import FMInstance, FMInstanceEncryptionMode, FMAWSInstanceCreator, FMAzureInstanceCreator
1313
from .fm.instancesettingstemplates import FMInstanceSettingsTemplate, FMAWSInstanceSettingsTemplateCreator, FMAzureInstanceSettingsTemplateCreator
1414

@@ -85,51 +85,18 @@ def get_virtual_network(self, virtual_network_id):
8585
vn = self._perform_tenant_json("GET", "/virtual-networks/%s" % virtual_network_id)
8686
return FMVirtualNetwork(self, vn)
8787

88-
def create_virtual_network(self,
89-
label,
90-
awsVpcId=None,
91-
awsSubnetId=None,
92-
awsAutoCreateSecurityGroups=False,
93-
awsSecurityGroups=None,
94-
azureVnId=None,
95-
azureSubnetId=None,
96-
azureAutoUpdateSecurityGroups=None,
97-
internetAccessMode = "YES"):
98-
"""
99-
Create a Virtual Network
100-
101-
:param str label: The label of the Virtual Network
102-
103-
:param str awsVpcId: AWS Only, ID of the VPC to use
104-
:param str awsSubnetId: AWS Only, ID of the subnet to use
105-
:param boolean awsAutoCreateSecurityGroups: Optional, AWS Only, If false, do not create security groups automatically. Defaults to false
106-
:param list awsSecurityGroups: Optional, AWS Only, A list of up to 5 security group ids to assign to the instances created in this virtual network. Ignored if awsAutoCreateSecurityGroups is true
10788

108-
:param str azureVnId: Azure Only, ID of the Azure Virtual Network to use
109-
:param str azureSubnetId: Azure Only, ID of the subnet to use
110-
:param boolean azureAutoUpdateSecurityGroups: Azure Only, Auto update the subnet security group
111-
112-
:param str internetAccessMode: Optional, The internet access mode of the instances created in this virtual network. Accepts "YES", "NO", "EGRESS_ONLY". Defaults to "YES"
113-
114-
:return: requested instance settings template
115-
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplate`
89+
def new_virtual_network_creator(self, label):
11690
"""
91+
Instantiate a new virtual network creator
11792
118-
data = {
119-
"label": label,
120-
"awsVpcId": awsVpcId,
121-
"awsSubnetId": awsSubnetId,
122-
"awsAutoCreateSecurityGroups": awsAutoCreateSecurityGroups,
123-
"awsSecurityGroups": awsSecurityGroups,
124-
"azureVnId": azureVnId,
125-
"azureSubnetId": azureSubnetId,
126-
"azureAutoUpdateSecurityGroups": azureAutoUpdateSecurityGroups,
127-
"internetAccessMode": internetAccessMode,
128-
"mode": "EXISTING_MONOTENANT"
129-
}
130-
131-
vn = self._perform_tenant_json("POST", "/virtual-networks", body=data)
132-
return FMVirtualNetwork(self, vn)
93+
:param str label: The label of the
94+
:rtype: :class:`dataikuapi.fm.virtualnetworks.FMVirtualNetworkCreator`
95+
"""
96+
if self.cloud == "AWS":
97+
return FMAWSVirtualNetworkCreator(self, label)
98+
elif self.cloud == "Azure":
99+
return FMAzureVirtualNetworkCreator(self, label)
133100

134101

135102
########################################################

0 commit comments

Comments
 (0)