Skip to content

Commit 96fddb3

Browse files
author
Thibaud Baas
committed
FM: future
1 parent 22c6e5a commit 96fddb3

3 files changed

Lines changed: 110 additions & 12 deletions

File tree

dataikuapi/fm/future.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import sys, time
2+
3+
class FMFuture(object):
4+
"""
5+
A future on the DSS instance
6+
"""
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
13+
14+
@staticmethod
15+
def from_resp(client, resp,result_wrapper=lambda result: result):
16+
"""Creates a DSSFuture from a parsed JSON response"""
17+
return FMFuture(client, resp.get('jobId', None), state=resp, result_wrapper=result_wrapper)
18+
19+
@classmethod
20+
def get_result_wait_if_needed(cls, client, ret):
21+
if 'jobId' in ret:
22+
future = FMFuture(client, ret["jobId"], ret)
23+
future.wait_for_result()
24+
return future.get_result()
25+
else:
26+
return ret['result']
27+
28+
def abort(self):
29+
"""
30+
Abort the future
31+
"""
32+
return self.client._perform_tenant_empty("DELETE", "/futures/%s" % self.job_id)
33+
34+
def get_state(self):
35+
"""
36+
Get the status of the future, and its result if it's ready
37+
"""
38+
self.state = self.client._perform_tenant_json(
39+
"GET", "/futures/%s" % self.job_id, params={'peek' : False})
40+
self.state_is_peek = False
41+
return self.state
42+
43+
def peek_state(self):
44+
"""
45+
Get the status of the future, and its result if it's ready
46+
"""
47+
self.state = self.client._perform_tenant_json(
48+
"GET", "/futures/%s" % self.job_id, params={'peek' : True})
49+
self.state_is_peek = True
50+
return self.state
51+
52+
def get_result(self):
53+
"""
54+
Get the future result if it's ready, raises an Exception otherwise
55+
"""
56+
if self.state is None or not self.state.get('hasResult', False) or self.state_is_peek:
57+
self.get_state()
58+
if self.state.get('hasResult', False):
59+
return self.result_wrapper(self.state.get('result', None))
60+
else:
61+
raise Exception("Result not ready")
62+
63+
def has_result(self):
64+
"""
65+
Checks whether the future has a result ready
66+
"""
67+
if self.state is None or not self.state.get('hasResult', False):
68+
self.get_state()
69+
return self.state.get('hasResult', False)
70+
71+
def wait_for_result(self):
72+
"""
73+
Wait and get the future result
74+
"""
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:
78+
self.get_state()
79+
while not self.state.get('hasResult', False):
80+
time.sleep(5)
81+
self.get_state()
82+
if self.state.get('hasResult', False):
83+
return self.result_wrapper(self.state.get('result', None))
84+
else:
85+
raise Exception("No result")

dataikuapi/fm/instances.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from enum import Enum
2+
from .future import FMFuture
23

34
class FMInstance(object):
45
"""
@@ -13,20 +14,32 @@ def __init__(self, client, instance_data):
1314
def reprovision(self):
1415
"""
1516
Reprovision the physical DSS instance
17+
18+
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the reprovision process
19+
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
1620
"""
17-
self.client._perform_tenant_json("GET", "/instances/%s/actions/reprovision" % self.id)
21+
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/reprovision" % self.id)
22+
return FMFuture.from_resp(self.client, future)
1823

1924
def deprovision(self):
2025
"""
2126
Deprovision the physical DSS instance
27+
28+
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the deprovision process
29+
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
2230
"""
23-
self.client._perform_tenant_json("GET", "/instances/%s/actions/deprovision" % self.id)
31+
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/deprovision" % self.id)
32+
return FMFuture.from_resp(self.client, future)
2433

2534
def restart_dss(self):
2635
"""
2736
Restart the DSS running on the physical instance
37+
38+
:return: A :class:`~dataikuapi.fm.future.FMFuture` representing the restart process
39+
:rtype: :class:`~dataikuapi.fm.future.FMFuture`
2840
"""
29-
self.client._perform_tenant_json("GET", "/instances/%s/actions/restart-dss" % self.id)
41+
future = self.client._perform_tenant_json("GET", "/instances/%s/actions/restart-dss" % self.id)
42+
return FMFuture.from_resp(self.client, future)
3043

3144
def save(self):
3245
"""

dataikuapi/fmclient.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_virtual_networks(self):
6161
List all Virtual Networks
6262
6363
:return: list of virtual networks
64-
:rtype: list of :class:`dataikuapi.fm.tenant.FMVirtualNetwork`
64+
:rtype: list of :class:`dataikuapi.fm.virtualnetworks.FMVirtualNetwork`
6565
"""
6666
vns = self._perform_tenant_json("GET", "/virtual-networks")
6767
return [ FMVirtualNetwork(self, x) for x in vns]
@@ -73,7 +73,7 @@ def get_virtual_network(self, virtual_network_id):
7373
:param str virtual_network_id
7474
7575
:return: requested virtual network
76-
:rtype: :class:`dataikuapi.fm.tenant.FMVirtualNetwork`
76+
:rtype: :class:`dataikuapi.fm.virtualnetworks.FMVirtualNetwork`
7777
"""
7878
template = self._perform_tenant_json("GET", "/virtual-networks/%s" % virtual_network_id)
7979
return FMVirtualNetwork(self, template)
@@ -100,7 +100,7 @@ def get_instance_template(self, template_id):
100100
:param str template_id
101101
102102
:return: requested instance settings template
103-
:rtype: :class:`dataikuapi.fm.tenant.FMInstanceSettingsTemplate`
103+
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplate`
104104
"""
105105
template = self._perform_tenant_json("GET", "/instance-settings-templates/%s" % template_id)
106106
return FMInstanceSettingsTemplate(self, template)
@@ -118,7 +118,7 @@ def create_instance_template(self, label,
118118
119119
:param str label: The label of the Instance Settings Template
120120
121-
:param list setupActions: Optional, a list of `:class: FMSetupAction` to be played on an instance
121+
:param list setupActions: Optional, a list of :class:`dataikuapi.fm.instancesettingstemplates.FMSetupAction` to be played on an instance
122122
:param str license: Optional, overrides the license set in Cloud Setup
123123
124124
:param str awsKeyPairName: Optional, AWS Only, the name of an AWS key pair to add to the instance. Needed to get SSH access to the DSS instance, using the centos user.
@@ -137,7 +137,7 @@ def create_instance_template(self, label,
137137
:param str runtimeManagedIdentity: Optional, Azure Only, the managed identity assigned to the DSS instance at runtime
138138
139139
:return: requested instance settings template
140-
:rtype: :class:`dataikuapi.fm.tenant.FMInstanceSettingsTemplate`
140+
:rtype: :class:`dataikuapi.fm.instancesettingstemplates.FMInstanceSettingsTemplate`
141141
"""
142142

143143
data = {
@@ -172,7 +172,7 @@ def get_instances(self):
172172
List all DSS Instances
173173
174174
:return: list of instances
175-
:rtype: list of :class:`dataikuapi.fm.tenant.FMInstance`
175+
:rtype: list of :class:`dataikuapi.fm.instances.FMInstance`
176176
"""
177177
instances = self._perform_tenant_json("GET", "/instances")
178178
return [ FMInstance(self, **x) for x in instances]
@@ -184,7 +184,7 @@ def get_instance(self, instance_id):
184184
:param str instance_id
185185
186186
:return: Instance
187-
:rtype: :class:`dataikuapi.fm.tenant.FMInstance`
187+
:rtype: :class:`dataikuapi.fm.instances.FMInstance`
188188
"""
189189
instance = self._perform_tenant_json("GET", "/instances/%s" % instance_id)
190190
return FMInstance(self, instance)
@@ -209,7 +209,7 @@ def create_instance(self, instance_settings_template, virtual_network, label, im
209209
:param int data_volume_size: Optional, Data volume initial size
210210
:param int data_volume_size_max: Optional, Data volume maximum size
211211
:param int data_volume_IOPS: Optional, Data volume IOPS
212-
:param object data_volume_encryption: Optional, a :class:`FMInstanceEncryptionMode` setting the encryption mode of the data volume
212+
:param object data_volume_encryption: Optional, a :class:`dataikuapi.fm.instances.FMInstanceEncryptionMode` setting the encryption mode of the data volume
213213
:param str data_volume_encryption_key: Optional, the encryption key to use when data_volume_encryption_key is FMInstanceEncryptionMode.CUSTOM
214214
:param int aws_root_volume_size: Optional, the root volume size
215215
:param str aws_root_volume_type: Optional, the root volume type
@@ -218,7 +218,7 @@ def create_instance(self, instance_settings_template, virtual_network, label, im
218218
:param list fm_tags: Optional, list of tags to be applied on the instance in the Fleet Manager
219219
220220
:return: Instance
221-
:rtype: :class:`dataikuapi.fm.tenant.FMInstance`
221+
:rtype: :class:`dataikuapi.fm.instances.FMInstance`
222222
"""
223223
data = {
224224
"virtualNetworkId": virtual_network.id,

0 commit comments

Comments
 (0)