Skip to content

Commit 012292f

Browse files
committed
add code envs
1 parent cd2b20d commit 012292f

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

dataikuapi/dss/admin.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .future import DSSFuture
2+
import json
23

34
class DSSConnection(object):
45
"""
@@ -417,3 +418,85 @@ def group_regexp(self, regexp, unix_user, hadoop_user=None):
417418
self.raw['targetUnix'] = unix_user
418419
self.raw['targetHadoop'] = hadoop_user
419420
return self
421+
422+
class DSSCodeEnv(object):
423+
"""
424+
A code env on the DSS instance
425+
"""
426+
def __init__(self, client, env_lang, env_name):
427+
self.client = client
428+
self.env_lang = env_lang
429+
self.env_name = env_name
430+
431+
########################################################
432+
# Env deletion
433+
########################################################
434+
435+
def delete(self):
436+
"""
437+
Delete the connection
438+
439+
Note: this call requires an API key with admin rights
440+
"""
441+
resp = self.client._perform_json(
442+
"DELETE", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name))
443+
if resp is None:
444+
raise Exception('Env deletion returned no data')
445+
if resp.get('messages', {}).get('error', False):
446+
raise Exception('Env deletion failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
447+
448+
449+
########################################################
450+
# Code env description
451+
########################################################
452+
453+
def get_definition(self):
454+
"""
455+
Get the code env's definition
456+
457+
Note: this call requires an API key with admin rights
458+
459+
Returns:
460+
the code env definition, as a JSON object
461+
"""
462+
return self.client._perform_json(
463+
"GET", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name))
464+
465+
########################################################
466+
# Code env modification
467+
########################################################
468+
def update(self, external_conda_name=None, conda_spec=None, spec=None):
469+
"""
470+
Update the code env definition and apply the changes
471+
472+
Note: this call requires an API key with admin rights
473+
474+
:param external_conda_name: name of an external conda env
475+
:param conda_spec: spec of the conda env
476+
:param spec: list of pip or R packages
477+
"""
478+
modifications = {'externalCondaEnvName':external_conda_name,'condaSpec':conda_spec,'spec':spec}
479+
resp = self.client._perform_json(
480+
"PUT", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name),
481+
body = modifications)
482+
if resp is None:
483+
raise Exception('Env update returned no data')
484+
if resp.get('messages', {}).get('error', False):
485+
raise Exception('Env update failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
486+
487+
def set_jupyter_support(self, active):
488+
"""
489+
Update the code env jupyter support
490+
491+
Note: this call requires an API key with admin rights
492+
493+
:param active: True to activate jupyter support, False to deactivate
494+
"""
495+
resp = self.client._perform_json(
496+
"POST", "/admin/code-envs/%s/%s/jupyter" % (self.env_lang, self.env_name),
497+
params = {'active':active})
498+
if resp is None:
499+
raise Exception('Env update returned no data')
500+
if resp.get('messages', {}).get('error', False):
501+
raise Exception('Env update failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
502+

dataikuapi/dssclient.py

Lines changed: 54 additions & 2 deletions
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
9+
from dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv
1010
from dss.meaning import DSSMeaning
1111
from dss.sqlquery import DSSSQLQuery
1212
from dss.notebook import DSSNotebook
@@ -316,7 +316,7 @@ def list_connections(self):
316316
Note: this call requires an API key with admin rights
317317
318318
Returns:
319-
All connections, as a map of connection name to connection definition
319+
All connections, as a map of connection name to connection definition
320320
"""
321321
return self._perform_json(
322322
"GET", "/admin/connections/")
@@ -360,6 +360,58 @@ def create_connection(self, name, type=None, params=None, usable_by='ALL', allow
360360
})
361361
return DSSConnection(self, name)
362362

363+
########################################################
364+
# Code envs
365+
########################################################
366+
367+
def list_code_envs(self):
368+
"""
369+
List all code envs setup on the DSS instance
370+
371+
Note: this call requires an API key with admin rights
372+
373+
Returns:
374+
List code envs (name, type, language)
375+
"""
376+
return self._perform_json(
377+
"GET", "/admin/code-envs/")
378+
379+
def get_code_env(self, env_lang, env_name):
380+
"""
381+
Get a handle to interact with a specific code env
382+
383+
Args:
384+
name: the name of the desired code env
385+
386+
Returns:
387+
A :class:`dataikuapi.dss.admin.DSSCodeEnv` code env handle
388+
"""
389+
return DSSCodeEnv(self, env_lang, env_name)
390+
391+
def create_code_env(self, env_lang, env_name, deployment_mode, params=None):
392+
"""
393+
Create a code env, and return a handle to interact with it
394+
395+
Note: this call requires an API key with admin rights
396+
397+
:param env_lang: the language (Python, R) of the new code env
398+
:param env_name: the name of the new code env
399+
:param deployment_mode: the type of the new code env
400+
:param params: the parameters of the new connection, as a JSON object
401+
402+
:returns: A :class:`dataikuapi.dss.admin.DSSCodeEnv` code env handle
403+
404+
"""
405+
params = params if params is not None else {}
406+
params['deploymentMode'] = deployment_mode
407+
resp = self._perform_json(
408+
"POST", "/admin/code-envs/%s/%s" % (env_lang, env_name), body=params)
409+
if resp is None:
410+
raise Exception('Env creation returned no data')
411+
if resp.get('messages', {}).get('error', False):
412+
raise Exception('Env creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
413+
return DSSCodeEnv(self, env_lang, env_name)
414+
363415
########################################################
364416
# Meanings
365417
########################################################

0 commit comments

Comments
 (0)