Skip to content

Commit 5a1cf0d

Browse files
committed
add wrapper for kubikles
1 parent de101a2 commit 5a1cf0d

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

dataikuapi/dss/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def total_active_with_trigger_scenarios_count(self):
12751275

12761276

12771277
class DSSKubikleTemplateListItem(object):
1278-
"""An item in a list of datasets. Do not instantiate this class, use :meth:`dataikuapi.dss.project.DSSProject.list_datasets`"""
1278+
"""An item in a list of kubikle templates. Do not instantiate this class, use :meth:`dataikuapi.DSSClient.list_kubikle_templates`"""
12791279
def __init__(self, client, data):
12801280
self.client = client
12811281
self._data = data

dataikuapi/dss/kubikle.py

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
from ..utils import DataikuException
22
import json
3+
from datetime import datetime
4+
5+
class DSSKubikleObjectListItem(object):
6+
"""An item in a list of kubikles. Do not instantiate this class, use :meth:`dataikuapi.dss.project.DSSProject.list_kubikle_objects`"""
7+
def __init__(self, client, project_key, data):
8+
self.client = client
9+
self.project_key = project_key
10+
self._data = data
11+
12+
def to_kubikle_object(self):
13+
"""Gets the :class:`DSSKubikleTemplate` corresponding to this kubikle object """
14+
return DSSKubikleObject(self.client, self.project_key, self._data["id"])
15+
16+
@property
17+
def name(self):
18+
return self._data["name"]
19+
@property
20+
def id(self):
21+
return self._data["id"]
22+
@property
23+
def owner(self):
24+
return self._data["owner"]
25+
@property
26+
def template_id(self):
27+
return self._data["templateId"]
28+
@property
29+
def template_label(self):
30+
return self._data.get('desc', {}).get('label', self._data['id'])
31+
@property
32+
def template_description(self):
33+
return self._data.get('desc', {}).get('shortDesc', '')
34+
335

436
class DSSKubikleObject(object):
537
"""
@@ -11,16 +43,57 @@ def __init__(self, client, project_key, kubikle_object_id):
1143
self.project_key = project_key
1244
self.kubikle_object_id = kubikle_object_id
1345

46+
def delete(self):
47+
"""
48+
Delete the kubikle
49+
"""
50+
self.client._perform_empty("DELETE", "/projects/%s/kubikles/%s" % (self.project_key, self.kubikle_object_id))
51+
1452
def get_settings(self):
1553
"""
1654
Get the kubikle object's settings
1755
18-
:returns: a handle to manage the wiki settings (taxonomy, home article)
56+
:returns: a handle to manage the kubikle settings
1957
:rtype: :class:`dataikuapi.dss.kubikle.DSSKubikleObjectSettings`
2058
"""
2159
settings = self.client._perform_json("GET", "/projects/%s/kubikles/%s" % (self.project_key, self.kubikle_object_id))
2260
return DSSKubikleObjectSettings(self.client, self.project_key, self.kubikle_object_id, settings)
2361

62+
def get_status(self):
63+
"""
64+
Get the kubikle object's state
65+
66+
:returns: a handle to inspect the kubikle state
67+
:rtype: :class:`dataikuapi.dss.kubikle.DSSKubikleObjectStatus`
68+
"""
69+
status = self.client._perform_json("GET", "/projects/%s/kubikles/%s/status" % (self.project_key, self.kubikle_object_id))
70+
return DSSKubikleObjectStatus(self.client, self.project_key, self.kubikle_object_id, status)
71+
72+
def stop(self):
73+
"""
74+
Stop a running kubikle
75+
76+
:returns: a future to wait on the stop, or None if already stopped
77+
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
78+
"""
79+
ret = self.client._perform_json("POST", "/projects/%s/kubikles/%s/stop" % (self.project_key, self.kubikle_object_id))
80+
if 'jobId' in ret:
81+
return self.client.get_future(ret["jobId"])
82+
else:
83+
return None
84+
85+
def restart(self):
86+
"""
87+
Restart a kubikle
88+
89+
:returns: a future to wait on the start
90+
:rtype: :class:`dataikuapi.dss.future.DSSFuture`
91+
"""
92+
ret = self.client._perform_json("POST", "/projects/%s/kubikles/%s/restart" % (self.project_key, self.kubikle_object_id))
93+
if 'jobId' in ret:
94+
return self.client.get_future(ret["jobId"])
95+
else:
96+
return None
2497

2598
class DSSKubikleObjectSettings(object):
2699
"""
@@ -39,3 +112,30 @@ def get_raw(self):
39112
"""
40113
return self.settings
41114

115+
class DSSKubikleObjectStatus(object):
116+
"""
117+
Status of a kubikle object
118+
"""
119+
def __init__(self, client, project_key, kubikle_object_id, status):
120+
"""Do not call directly, use :meth:`dataikuapi.dss.kubikle.DSSKubikleObject.get_state`"""
121+
self.client = client
122+
self.project_key = project_key
123+
self.kubikle_object_id = kubikle_object_id
124+
self.status = status
125+
126+
def get_raw(self):
127+
"""
128+
Gets the status as a raw dictionary. This returns a reference to the raw status, not a copy,
129+
"""
130+
return self.status
131+
132+
@property
133+
def state(self):
134+
return self.status["state"]
135+
@property
136+
def last_built(self):
137+
ts = self.status.get("lastStateChange", 0)
138+
if ts > 0:
139+
return datetime.fromtimestamp(ts / 1000)
140+
else:
141+
return None

dataikuapi/dss/project.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .analysis import DSSAnalysis
2626
from .flow import DSSProjectFlow
2727
from .app import DSSAppManifest
28-
from .kubikle import DSSKubikleObject
28+
from .kubikle import DSSKubikleObject, DSSKubikleObjectListItem
2929

3030
class DSSProject(object):
3131
"""
@@ -1632,15 +1632,21 @@ def get_mlflow_extension(self):
16321632
########################################################
16331633
# Kubikles
16341634
########################################################
1635-
def list_kubikle_objects(self):
1635+
def list_kubikle_objects(self, as_type="listitems"):
16361636
"""
16371637
List the kubikle objects in this project
16381638
16391639
Returns:
16401640
the list of the kubikle objects, each one as a JSON object
16411641
"""
1642-
return self.client._perform_json(
1642+
items = self.client._perform_json(
16431643
"GET", "/projects/%s/kubikles/" % self.project_key)
1644+
if as_type == "listitems" or as_type == "listitem":
1645+
return [DSSKubikleObjectListItem(self.client, self.project_key, item) for item in items]
1646+
elif as_type == "objects" or as_type == "object":
1647+
return [DSSKubikleObject(self.client, self.project_key, item["id"]) for item in items]
1648+
else:
1649+
raise ValueError("Unknown as_type")
16441650

16451651
def get_kubikle_object(self, kubikle_object_id):
16461652
"""

0 commit comments

Comments
 (0)