Skip to content

Commit 9b03b5c

Browse files
committed
add clusters
1 parent 1554c25 commit 9b03b5c

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

dataikuapi/dss/admin.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,96 @@ def set_definition(self, definition):
578578
return self.client._perform_empty(
579579
"PUT", "/admin/globalAPIKeys/%s" % self.key,
580580
body = definition)
581+
582+
class DSSCluster(object):
583+
"""
584+
A cluster on the DSS instance
585+
"""
586+
def __init__(self, client, cluster_id):
587+
self.client = client
588+
self.cluster_id = cluster_id
589+
590+
########################################################
591+
# Cluster deletion
592+
########################################################
593+
594+
def delete(self):
595+
"""
596+
Delete the cluster (not stopping it)
597+
"""
598+
self.client._perform_empty(
599+
"DELETE", "/admin/clusters/%s" % (self.cluster_id))
600+
601+
602+
########################################################
603+
# Cluster description
604+
########################################################
605+
606+
def get_definition(self):
607+
"""
608+
Get the cluster's definition
609+
610+
Returns:
611+
the cluster definition, as a JSON object
612+
"""
613+
return self.client._perform_json(
614+
"GET", "/admin/clusters/%s" % (self.cluster_id))
615+
616+
def set_definition(self, cluster):
617+
"""
618+
Set the cluster's definition. The definition should come from a call to the get_definition()
619+
method.
620+
621+
Fields that can be updated :
622+
623+
* cluster.permissions, cluster.usableByAll, cluster.owner
624+
* cluster.params
625+
626+
:param cluster: a cluster definition
627+
628+
Returns:
629+
the updated cluster definition, as a JSON object
630+
"""
631+
return self.client._perform_json(
632+
"PUT", "/admin/clusters/%s" % (self.cluster_id), body=cluster)
633+
634+
def get_status(self):
635+
"""
636+
Get the cluster's status and usages
637+
638+
Returns:
639+
the cluster status, as a JSON object
640+
"""
641+
return self.client._perform_json(
642+
"GET", "/admin/clusters/%s/status" % (self.cluster_id))
643+
644+
645+
########################################################
646+
# Cluster actions
647+
########################################################
648+
649+
def start(self):
650+
"""
651+
Starts the cluster
652+
"""
653+
resp = self.client._perform_json(
654+
"POST", "/admin/clusters/%s/start" % (self.cluster_id))
655+
if resp is None:
656+
raise Exception('Env update returned no data')
657+
if resp.get('messages', {}).get('error', False):
658+
raise Exception('Cluster operation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
659+
return resp
660+
661+
662+
def stop(self):
663+
"""
664+
Stops the cluster
665+
"""
666+
resp = self.client._perform_json(
667+
"POST", "/admin/clusters/%s/stop" % (self.cluster_id))
668+
if resp is None:
669+
raise Exception('Env update returned no data')
670+
if resp.get('messages', {}).get('error', False):
671+
raise Exception('Cluster operation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
672+
return resp
673+

dataikuapi/dssclient.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .dss.future import DSSFuture
77
from .dss.project import DSSProject
88
from .dss.plugin import DSSPlugin
9-
from .dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey
9+
from .dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster
1010
from .dss.meaning import DSSMeaning
1111
from .dss.sqlquery import DSSSQLQuery
1212
from .dss.notebook import DSSNotebook
@@ -412,6 +412,55 @@ def create_code_env(self, env_lang, env_name, deployment_mode, params=None):
412412
raise Exception('Env creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
413413
return DSSCodeEnv(self, env_lang, env_name)
414414

415+
########################################################
416+
# Clusters
417+
########################################################
418+
419+
def list_clusters(self):
420+
"""
421+
List all clusters setup on the DSS instance
422+
423+
Returns:
424+
List clusters (name, type, state)
425+
"""
426+
return self._perform_json(
427+
"GET", "/admin/clusters/")
428+
429+
def get_cluster(self, cluster_id):
430+
"""
431+
Get a handle to interact with a specific cluster
432+
433+
Args:
434+
name: the name of the desired cluster
435+
436+
Returns:
437+
A :class:`dataikuapi.dss.admin.DSSCluster` cluster handle
438+
"""
439+
return DSSCluster(self, cluster_id)
440+
441+
def create_cluster(self, cluster_name, cluster_type='manual', params=None):
442+
"""
443+
Create a cluster, and return a handle to interact with it
444+
445+
:param cluster_name: the name of the new cluster
446+
:param cluster_type: the type of the new cluster
447+
:param params: the parameters of the new cluster, as a JSON object
448+
449+
:returns: A :class:`dataikuapi.dss.admin.DSSCluster` cluster handle
450+
451+
"""
452+
definition = {}
453+
definition['name'] = cluster_name
454+
definition['type'] = cluster_type
455+
definition['params'] = params if params is not None else {}
456+
resp = self._perform_json(
457+
"POST", "/admin/clusters/", body=definition)
458+
if resp is None:
459+
raise Exception('Cluster creation returned no data')
460+
if resp.get('messages', {}).get('error', False):
461+
raise Exception('Cluster creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
462+
return DSSCluster(self, resp['id'])
463+
415464
########################################################
416465
# Global API Keys
417466
########################################################

0 commit comments

Comments
 (0)