Skip to content

Commit 00f698c

Browse files
author
Thibaud Baas
committed
FM: Split Instance and InstanceCreator per cloud
1 parent 3ea7e81 commit 00f698c

2 files changed

Lines changed: 84 additions & 82 deletions

File tree

dataikuapi/fm/instances.py

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ def __init__(self, client, label, instance_settings_template_id, virtual_network
66
"""
77
Helper to create a DSS Instance
88
9+
:param object client: :class:`dataikuapi.fm.fmclient`
10+
:param str label: The label of the instance
911
:param str instance_settings_template: The instance settings template id this instance should be based on
1012
:param str virtual_network: The virtual network where the instance should be spawned
11-
:param str label: The label of the instance
1213
:param str image_id: The ID of the DSS runtime image (ex: dss-9.0.3-default)
1314
"""
1415
self.client = client
@@ -21,33 +22,29 @@ def __init__(self, client, label, instance_settings_template_id, virtual_network
2122
# Set the default value for dssNodeType
2223
self.data["dssNodeType"] = "design"
2324

24-
def create(self):
25-
"""
26-
Create the DSS instance
27-
28-
:return: Instance
29-
:rtype: :class:`dataikuapi.fm.instances.FMInstance`
30-
"""
31-
instance = self.client._perform_tenant_json("POST", "/instances", body=self.data)
32-
return FMInstance(self.client, instance)
33-
34-
def set_dss_node_type(self, dss_node_type):
25+
def with_dss_node_type(self, dss_node_type):
3526
"""
3627
Set the DSS Node type of the instance to create
3728
3829
:param str dss_node_type: Optional , the type of the dss node to create. Supports "design", "automation ordeployer". Defaults to "design"
30+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
3931
"""
4032
if dss_node_type not in ["design", "automation", "deployer"]:
4133
raise ValueError("Only \"design\", \"automation\" or \"deployer\" dss_node_type are supported")
4234
self.data["dssNodeType"] = dss_node_type
35+
return self
4336

44-
def set_cloud_instance_type(self, cloud_instance_type):
37+
def with_cloud_instance_type(self, cloud_instance_type):
4538
"""
4639
Set the machine type for the DSS Instance
40+
41+
:param str cloud_instance_type
42+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
4743
"""
4844
self.data["cloudInstanceType"] = cloud_instance_type
45+
return self
4946

50-
def set_data_volume_options(self, data_volume_type=None, data_volume_size=None, data_volume_size_max=None, data_volume_IOPS=None, data_volume_encryption=None, data_volume_encryption_key=None):
47+
def with_data_volume_options(self, data_volume_type=None, data_volume_size=None, data_volume_size_max=None, data_volume_IOPS=None, data_volume_encryption=None, data_volume_encryption_key=None):
5148
"""
5249
Set the options of the data volume to use with the DSS Instance
5350
@@ -57,6 +54,7 @@ def set_data_volume_options(self, data_volume_type=None, data_volume_size=None,
5754
:param int data_volume_IOPS: Optional, Data volume IOPS
5855
:param object data_volume_encryption: Optional, a :class:`dataikuapi.fm.instances.FMInstanceEncryptionMode` setting the encryption mode of the data volume
5956
:param str data_volume_encryption_key: Optional, the encryption key to use when data_volume_encryption_key is FMInstanceEncryptionMode.CUSTOM
57+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
6058
"""
6159
if type(data_volume_encryption) is not FMInstanceEncryptionMode:
6260
raise TypeError("data_volume encryption needs to be of type FMInstanceEncryptionMode")
@@ -67,42 +65,71 @@ def set_data_volume_options(self, data_volume_type=None, data_volume_size=None,
6765
self.data["dataVolumeIOPS"] = data_volume_IOPS
6866
self.data["dataVolumeEncryption"] = data_volume_encryption.value
6967
self.data["dataVolumeEncryptionKey"] = data_volume_encryption_key
68+
return self
69+
70+
def with_cloud_tags(self, cloud_tags):
71+
"""
72+
Set the tags to be applied to the cloud resources created for this DSS instance
7073
71-
def set_aws_root_volume_options(self, aws_root_volume_size=None, aws_root_volume_type=None, aws_root_volume_IOPS=None):
74+
:param dict cloud_tags: a key value dictionary of tags to be applied on the cloud resources
75+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
7276
"""
73-
AWS Only: Set the options of the root volume of the DSS Instance
77+
self.data["cloudTags"] = cloud_tags
78+
return self
79+
80+
def with_fm_tags(self, fm_tags):
81+
"""
82+
A list of tags to add on the DSS Instance in Fleet Manager
83+
84+
:param list fm_tags: Optional, list of tags to be applied on the instance in the Fleet Manager
85+
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
86+
"""
87+
self.data["fmTags"] = fm_tags
88+
return self
89+
90+
91+
class FMAWSInstanceCreator(FMInstanceCreator):
92+
def with_aws_root_volume_options(self, aws_root_volume_size=None, aws_root_volume_type=None, aws_root_volume_IOPS=None):
93+
"""
94+
Set the options of the root volume of the DSS Instance
7495
7596
:param int aws_root_volume_size: Optional, the root volume size
7697
:param str aws_root_volume_type: Optional, the root volume type
7798
:param int aws_root_volume_IOPS: Optional, the root volume IOPS
99+
:rtype: :class:`dataikuapi.fm.instances.FMAWSInstanceCreator`
78100
"""
79-
if self.client.cloud != "AWS":
80-
raise BaseException("set_aws_root_volume_options is only usable on AWS tenants")
81101
self.data["awsRootVolumeSizeGB"] = aws_root_volume_size
82102
self.data["awsRootVolumeType"] = aws_root_volume_type
83103
self.data["awsRootVolumeIOPS"] = aws_root_volume_IOPS
104+
return self
84105

85-
def set_cloud_tags(self, cloud_tags):
106+
def create(self):
86107
"""
87-
Set the tags to be applied to the cloud resources created for this DSS instance
108+
Create the DSS instance
88109
89-
:param dict cloud_tags: a key value dictionary of tags to be applied on the cloud resources
110+
:return: Created DSS Instance
111+
:rtype: :class:`dataikuapi.fm.instances.FMAWSInstance`
90112
"""
91-
self.data["cloudTags"] = cloud_tags
113+
instance = self.client._perform_tenant_json("POST", "/instances", body=self.data)
114+
return FMAWSInstance(self.client, instance)
92115

93-
def set_fm_tags(self, fm_tags):
116+
117+
class FMAzureInstanceCreator(FMInstanceCreator):
118+
def create(self):
94119
"""
95-
A list of tags to add on the DSS Instance in Fleet Manager
120+
Create the DSS instance
96121
97-
:param list fm_tags: Optional, list of tags to be applied on the instance in the Fleet Manager
122+
:return: Created DSS Instance
123+
:rtype: :class:`dataikuapi.fm.instances.FMAzureInstance`
98124
"""
99-
self.data["fmTags"] = fm_tags
125+
instance = self.client._perform_tenant_json("POST", "/instances", body=self.data)
126+
return FMAzureInstance(self.client, instance)
100127

101128

102129
class FMInstance(object):
103130
"""
104131
A handle to interact with a DSS instance.
105-
Do not create this directly, use :meth:`FMClient.get_instance` or :meth: `FMClient.create_instance`
132+
Do not create this directly, use :meth:`FMClient.get_instance` or :meth: `FMClient.new_instance_creator`
106133
"""
107134
def __init__(self, client, instance_data):
108135
self.client = client
@@ -176,28 +203,41 @@ def set_automated_snapshots(self, enable, period, keep=0):
176203
self.instance_data['automatedSnapshotRetention'] = keep
177204
self.save()
178205

206+
def set_custom_certificate(self, pem_data):
207+
"""
208+
Set the custom certificate for this instance
209+
210+
Only needed when Virtual Network HTTPS Strategy is set to Custom Certificate
211+
212+
param: str pem_data: The SSL certificate
213+
"""
214+
self.instance_data['sslCertificatePEM'] = pem_data
215+
self.save()
216+
217+
218+
class FMAWSInstance(FMInstance):
179219
def set_elastic_ip(self, enable, elasticip_allocation_id):
180220
"""
181221
Set a public elastic ip for this instance
182222
183223
:param boolan enable: Enable the elastic ip allocation
184-
:param str elaticip_allocation_id: The AWS ElasticIP allocation ID or the Azure Public IP ID
224+
:param str elaticip_allocation_id: AWS ElasticIP allocation ID
185225
"""
186226
self.instance_data['awsAssignElasticIP'] = enable
187227
self.instance_data['awsElasticIPAllocationId'] = elasticip_allocation_id
188-
self.instance_data['azureAssignElasticIP'] = enable
189-
self.instance_data['azurePublicIPId'] = elasticip_allocation_id
190228
self.save()
191229

192-
def set_custom_certificate(self, pem_data):
193-
"""
194-
Set the custom certificate for this instance
195230

196-
Only needed when Virtual Network HTTPS Strategy is set to Custom Certificate
231+
class FMAzureInstance(FMInstance):
232+
def set_elastic_ip(self, enable, public_ip_id):
233+
"""
234+
Set a public elastic ip for this instance
197235
198-
param: str pem_data: The SSL certificate
236+
:param boolan enable: Enable the elastic ip allocation
237+
:param str public_ip_id: Azure Public IP ID
199238
"""
200-
self.instance_data['sslCertificatePEM'] = pem_data
239+
self.instance_data['azureAssignElasticIP'] = enable
240+
self.instance_data['azurePublicIPId'] = public_ip_id
201241
self.save()
202242

203243

dataikuapi/fmclient.py

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .fm.virtualnetworks import FMVirtualNetwork
1212
from .fm.instances import FMInstance, FMInstanceEncryptionMode
1313
from .fm.instancesettingstemplates import FMInstanceSettingsTemplate
14+
from dataikuapi.fm.instances import FMAWSInstanceCreator, FMAzureInstanceCreator
1415

1516
class FMClient(object):
1617
"""Entry point for the FM API client"""
@@ -242,58 +243,19 @@ def get_instance(self, instance_id):
242243
instance = self._perform_tenant_json("GET", "/instances/%s" % instance_id)
243244
return FMInstance(self, instance)
244245

245-
def create_instance(self, instance_settings_template, virtual_network, label, image_id,
246-
dss_node_type="design",
247-
cloud_instance_type=None, data_volume_type=None, data_volume_size=None,
248-
data_volume_size_max=None, data_volume_IOPS=None, data_volume_encryption=FMInstanceEncryptionMode.NONE,
249-
data_volume_encryption_key=None, aws_root_volume_size=None, aws_root_volume_type=None, aws_root_volume_IOPS=None,
250-
cloud_tags=None, fm_tags=None):
246+
def new_instance_creator(self, label, instance_settings_template_id, virtual_network_id, image_id):
251247
"""
252-
Create a DSS Instance
248+
Instantiate a new instance creator
253249
250+
:param str label: The label of the instance
254251
:param str instance_settings_template: The instance settings template id this instance should be based on
255252
:param str virtual_network: The virtual network where the instance should be spawned
256-
:param str label: The label of the instance
257253
:param str image_id: The ID of the DSS runtime image (ex: dss-9.0.3-default)
258-
259-
:param str dss_node_type: Optional , the type of the dss node to create. Defaults to "design"
260-
:param str cloud_instance_type: Optional, Machine type
261-
:param str data_volume_type: Optional, Data volume type
262-
:param int data_volume_size: Optional, Data volume initial size
263-
:param int data_volume_size_max: Optional, Data volume maximum size
264-
:param int data_volume_IOPS: Optional, Data volume IOPS
265-
:param object data_volume_encryption: Optional, a :class:`dataikuapi.fm.instances.FMInstanceEncryptionMode` setting the encryption mode of the data volume
266-
:param str data_volume_encryption_key: Optional, the encryption key to use when data_volume_encryption_key is FMInstanceEncryptionMode.CUSTOM
267-
:param int aws_root_volume_size: Optional, the root volume size
268-
:param str aws_root_volume_type: Optional, the root volume type
269-
:param int aws_root_volume_IOPS: Optional, the root volume IOPS
270-
:param dict cloud_tags: Optional, a key value dictionary of tags to be applied on the cloud resources
271-
:param list fm_tags: Optional, list of tags to be applied on the instance in the Fleet Manager
272-
273-
:return: Instance
274-
:rtype: :class:`dataikuapi.fm.instances.FMInstance`
275254
"""
276-
data = {
277-
"virtualNetworkId": virtual_network.id,
278-
"instanceSettingsTemplateId": instance_settings_template.id,
279-
"label": label,
280-
"dssNodeType": dss_node_type,
281-
"imageId": image_id,
282-
"cloudInstanceType": cloud_instance_type,
283-
"dataVolumeType": data_volume_type,
284-
"dataVolumeSizeGB": data_volume_size,
285-
"dataVolumeSizeMaxGB": data_volume_size_max,
286-
"dataVolumeIOPS": data_volume_IOPS,
287-
"dataVolumeEncryption": data_volume_encryption.value,
288-
"dataVolumeEncryptionKey": data_volume_encryption_key,
289-
"awsRootVolumeSizeGB": aws_root_volume_size,
290-
"awsRootVolumeType": aws_root_volume_type,
291-
"awsRootVolumeIOPS": aws_root_volume_IOPS,
292-
"cloudTags": cloud_tags,
293-
"fmTags": fm_tags
294-
}
295-
instance = self._perform_tenant_json("POST", "/instances", body=data)
296-
return FMInstance(self, instance)
255+
if self.cloud == "AWS":
256+
return FMAWSInstanceCreator(self, label, instance_settings_template_id, virtual_network_id, image_id)
257+
elif self.cloud == "Azure":
258+
return FMAzureInstanceCreator(self, label, instance_settings_template_id, virtual_network_id, image_id)
297259

298260

299261
########################################################

0 commit comments

Comments
 (0)