11from ..utils import DataikuException
22import 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
436class 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
2598class 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
0 commit comments