Skip to content

Commit 6a5ee56

Browse files
author
Thibaud Baas
committed
FM: format
1 parent 1cc61b7 commit 6a5ee56

File tree

4 files changed

+247
-95
lines changed

4 files changed

+247
-95
lines changed

dataikuapi/fm/future.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
import sys, time
22

3+
34
class FMFuture(object):
45
"""
56
A future on the DSS instance
67
"""
7-
def __init__(self, client, job_id, state=None, result_wrapper=lambda result: result):
8-
self.client = client
9-
self.job_id = job_id
10-
self.state = state
11-
self.state_is_peek = True
12-
self.result_wrapper = result_wrapper
8+
9+
def __init__(
10+
self, client, job_id, state=None, result_wrapper=lambda result: result
11+
):
12+
self.client = client
13+
self.job_id = job_id
14+
self.state = state
15+
self.state_is_peek = True
16+
self.result_wrapper = result_wrapper
1317

1418
@staticmethod
15-
def from_resp(client, resp,result_wrapper=lambda result: result):
19+
def from_resp(client, resp, result_wrapper=lambda result: result):
1620
"""Creates a DSSFuture from a parsed JSON response"""
17-
return FMFuture(client, resp.get('jobId', None), state=resp, result_wrapper=result_wrapper)
21+
return FMFuture(
22+
client, resp.get("jobId", None), state=resp, result_wrapper=result_wrapper
23+
)
1824

1925
@classmethod
2026
def get_result_wait_if_needed(cls, client, ret):
21-
if 'jobId' in ret:
27+
if "jobId" in ret:
2228
future = FMFuture(client, ret["jobId"], ret)
2329
future.wait_for_result()
2430
return future.get_result()
2531
else:
26-
return ret['result']
32+
return ret["result"]
2733

2834
def abort(self):
2935
"""
@@ -36,7 +42,8 @@ def get_state(self):
3642
Get the status of the future, and its result if it's ready
3743
"""
3844
self.state = self.client._perform_tenant_json(
39-
"GET", "/futures/%s" % self.job_id, params={'peek' : False})
45+
"GET", "/futures/%s" % self.job_id, params={"peek": False}
46+
)
4047
self.state_is_peek = False
4148
return self.state
4249

@@ -45,41 +52,50 @@ def peek_state(self):
4552
Get the status of the future, and its result if it's ready
4653
"""
4754
self.state = self.client._perform_tenant_json(
48-
"GET", "/futures/%s" % self.job_id, params={'peek' : True})
55+
"GET", "/futures/%s" % self.job_id, params={"peek": True}
56+
)
4957
self.state_is_peek = True
5058
return self.state
5159

5260
def get_result(self):
5361
"""
5462
Get the future result if it's ready, raises an Exception otherwise
5563
"""
56-
if self.state is None or not self.state.get('hasResult', False) or self.state_is_peek:
64+
if (
65+
self.state is None
66+
or not self.state.get("hasResult", False)
67+
or self.state_is_peek
68+
):
5769
self.get_state()
58-
if self.state.get('hasResult', False):
59-
return self.result_wrapper(self.state.get('result', None))
70+
if self.state.get("hasResult", False):
71+
return self.result_wrapper(self.state.get("result", None))
6072
else:
6173
raise Exception("Result not ready")
6274

6375
def has_result(self):
6476
"""
6577
Checks whether the future has a result ready
6678
"""
67-
if self.state is None or not self.state.get('hasResult', False):
79+
if self.state is None or not self.state.get("hasResult", False):
6880
self.get_state()
69-
return self.state.get('hasResult', False)
81+
return self.state.get("hasResult", False)
7082

7183
def wait_for_result(self):
7284
"""
7385
Wait and get the future result
7486
"""
75-
if self.state.get('hasResult', False):
76-
return self.result_wrapper(self.state.get('result', None))
77-
if self.state is None or not self.state.get('hasResult', False) or self.state_is_peek:
87+
if self.state.get("hasResult", False):
88+
return self.result_wrapper(self.state.get("result", None))
89+
if (
90+
self.state is None
91+
or not self.state.get("hasResult", False)
92+
or self.state_is_peek
93+
):
7894
self.get_state()
79-
while not self.state.get('hasResult', False):
95+
while not self.state.get("hasResult", False):
8096
time.sleep(5)
8197
self.get_state()
82-
if self.state.get('hasResult', False):
83-
return self.result_wrapper(self.state.get('result', None))
98+
if self.state.get("hasResult", False):
99+
return self.result_wrapper(self.state.get("result", None))
84100
else:
85101
raise Exception("No result")

dataikuapi/fm/instances.py

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from enum import Enum
22
from .future import FMFuture
33

4+
45
class FMInstanceCreator(object):
5-
def __init__(self, client, label, instance_settings_template_id, virtual_network_id, image_id):
6+
def __init__(
7+
self, client, label, instance_settings_template_id, virtual_network_id, image_id
8+
):
69
"""
710
Helper to create a DSS Instance
811
@@ -30,7 +33,9 @@ def with_dss_node_type(self, dss_node_type):
3033
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
3134
"""
3235
if dss_node_type not in ["design", "automation", "deployer"]:
33-
raise ValueError("Only \"design\", \"automation\" or \"deployer\" dss_node_type are supported")
36+
raise ValueError(
37+
'Only "design", "automation" or "deployer" dss_node_type are supported'
38+
)
3439
self.data["dssNodeType"] = dss_node_type
3540
return self
3641

@@ -44,7 +49,15 @@ def with_cloud_instance_type(self, cloud_instance_type):
4449
self.data["cloudInstanceType"] = cloud_instance_type
4550
return self
4651

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):
52+
def with_data_volume_options(
53+
self,
54+
data_volume_type=None,
55+
data_volume_size=None,
56+
data_volume_size_max=None,
57+
data_volume_IOPS=None,
58+
data_volume_encryption=None,
59+
data_volume_encryption_key=None,
60+
):
4861
"""
4962
Set the options of the data volume to use with the DSS Instance
5063
@@ -57,7 +70,9 @@ def with_data_volume_options(self, data_volume_type=None, data_volume_size=None,
5770
:rtype: :class:`dataikuapi.fm.instances.FMInstanceCreator`
5871
"""
5972
if type(data_volume_encryption) is not FMInstanceEncryptionMode:
60-
raise TypeError("data_volume encryption needs to be of type FMInstanceEncryptionMode")
73+
raise TypeError(
74+
"data_volume encryption needs to be of type FMInstanceEncryptionMode"
75+
)
6176

6277
self.data["dataVolumeType"] = data_volume_type
6378
self.data["dataVolumeSizeGB"] = data_volume_size
@@ -89,7 +104,12 @@ def with_fm_tags(self, fm_tags):
89104

90105

91106
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):
107+
def with_aws_root_volume_options(
108+
self,
109+
aws_root_volume_size=None,
110+
aws_root_volume_type=None,
111+
aws_root_volume_IOPS=None,
112+
):
93113
"""
94114
Set the options of the root volume of the DSS Instance
95115
@@ -110,7 +130,9 @@ def create(self):
110130
:return: Created DSS Instance
111131
:rtype: :class:`dataikuapi.fm.instances.FMAWSInstance`
112132
"""
113-
instance = self.client._perform_tenant_json("POST", "/instances", body=self.data)
133+
instance = self.client._perform_tenant_json(
134+
"POST", "/instances", body=self.data
135+
)
114136
return FMAWSInstance(self.client, instance)
115137

116138

@@ -122,7 +144,9 @@ def create(self):
122144
:return: Created DSS Instance
123145
:rtype: :class:`dataikuapi.fm.instances.FMAzureInstance`
124146
"""
125-
instance = self.client._perform_tenant_json("POST", "/instances", body=self.data)
147+
instance = self.client._perform_tenant_json(
148+
"POST", "/instances", body=self.data
149+
)
126150
return FMAzureInstance(self.client, instance)
127151

128152

@@ -131,10 +155,11 @@ class FMInstance(object):
131155
A handle to interact with a DSS instance.
132156
Do not create this directly, use :meth:`FMClient.get_instance` or :meth: `FMClient.new_instance_creator`
133157
"""
158+
134159
def __init__(self, client, instance_data):
135-
self.client = client
160+
self.client = client
136161
self.instance_data = instance_data
137-
self.id = instance_data['id']
162+
self.id = instance_data["id"]
138163

139164
def reprovision(self):
140165
"""
@@ -143,7 +168,9 @@ def reprovision(self):
143168
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the reprovision process
144169
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
145170
"""
146-
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/reprovision" % self.id)
171+
future = self.client._perform_tenant_json(
172+
"GET", "/instances/%s/actions/reprovision" % self.id
173+
)
147174
return FMFuture.from_resp(self.client, future)
148175

149176
def deprovision(self):
@@ -153,7 +180,9 @@ def deprovision(self):
153180
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the deprovision process
154181
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
155182
"""
156-
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/deprovision" % self.id)
183+
future = self.client._perform_tenant_json(
184+
"GET", "/instances/%s/actions/deprovision" % self.id
185+
)
157186
return FMFuture.from_resp(self.client, future)
158187

159188
def restart_dss(self):
@@ -163,21 +192,29 @@ def restart_dss(self):
163192
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the restart process
164193
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
165194
"""
166-
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/restart-dss" % self.id)
195+
future = self.client._perform_tenant_json(
196+
"GET", "/instances/%s/actions/restart-dss" % self.id
197+
)
167198
return FMFuture.from_resp(self.client, future)
168199

169200
def save(self):
170201
"""
171202
Update the Instance.
172203
"""
173-
self.client._perform_tenant_empty("PUT", "/instances/%s" % self.id, body=self.instance_data)
174-
self.instance_data = self.client._perform_tenant_json("GET", "/instances/%s" % self.id)
204+
self.client._perform_tenant_empty(
205+
"PUT", "/instances/%s" % self.id, body=self.instance_data
206+
)
207+
self.instance_data = self.client._perform_tenant_json(
208+
"GET", "/instances/%s" % self.id
209+
)
175210

176211
def get_status(self):
177212
"""
178213
Get the physical DSS instance's status
179214
"""
180-
status = self.client._perform_tenant_json("GET", "/instances/%s/status" % self.id)
215+
status = self.client._perform_tenant_json(
216+
"GET", "/instances/%s/status" % self.id
217+
)
181218
return FMInstanceStatus(status)
182219

183220
def delete(self):
@@ -187,7 +224,9 @@ def delete(self):
187224
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the deletion process
188225
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
189226
"""
190-
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/delete" % self.id)
227+
future = self.client._perform_tenant_json(
228+
"GET", "/instances/%s/actions/delete" % self.id
229+
)
191230
return FMFuture.from_resp(self.client, future)
192231

193232
def set_automated_snapshots(self, enable, period, keep=0):
@@ -198,9 +237,9 @@ def set_automated_snapshots(self, enable, period, keep=0):
198237
:param int period: The time period between 2 snapshot in hours
199238
:param int keep: Optional, the number of snapshot to keep. Use 0 to keep all snapshots. Defaults to 0.
200239
"""
201-
self.instance_data['enableAutomatedSnapshot'] = enable
202-
self.instance_data['automatedSnapshotPeriod'] = period
203-
self.instance_data['automatedSnapshotRetention'] = keep
240+
self.instance_data["enableAutomatedSnapshot"] = enable
241+
self.instance_data["automatedSnapshotPeriod"] = period
242+
self.instance_data["automatedSnapshotRetention"] = keep
204243
self.save()
205244

206245
def set_custom_certificate(self, pem_data):
@@ -211,7 +250,7 @@ def set_custom_certificate(self, pem_data):
211250
212251
param: str pem_data: The SSL certificate
213252
"""
214-
self.instance_data['sslCertificatePEM'] = pem_data
253+
self.instance_data["sslCertificatePEM"] = pem_data
215254
self.save()
216255

217256

@@ -223,8 +262,8 @@ def set_elastic_ip(self, enable, elasticip_allocation_id):
223262
:param boolan enable: Enable the elastic ip allocation
224263
:param str elaticip_allocation_id: AWS ElasticIP allocation ID
225264
"""
226-
self.instance_data['awsAssignElasticIP'] = enable
227-
self.instance_data['awsElasticIPAllocationId'] = elasticip_allocation_id
265+
self.instance_data["awsAssignElasticIP"] = enable
266+
self.instance_data["awsElasticIPAllocationId"] = elasticip_allocation_id
228267
self.save()
229268

230269

@@ -236,8 +275,8 @@ def set_elastic_ip(self, enable, public_ip_id):
236275
:param boolan enable: Enable the elastic ip allocation
237276
:param str public_ip_id: Azure Public IP ID
238277
"""
239-
self.instance_data['azureAssignElasticIP'] = enable
240-
self.instance_data['azurePublicIPId'] = public_ip_id
278+
self.instance_data["azureAssignElasticIP"] = enable
279+
self.instance_data["azurePublicIPId"] = public_ip_id
241280
self.save()
242281

243282

@@ -252,6 +291,7 @@ class FMInstanceStatus(dict):
252291
"""A class holding read-only information about an Instance.
253292
This class should not be created directly. Instead, use :meth:`FMInstance.get_info`
254293
"""
294+
255295
def __init__(self, data):
256296
"""Do not call this directly, use :meth:`FMInstance.get_status`"""
257297
super(FMInstanceStatus, self).__init__(data)

0 commit comments

Comments
 (0)