Skip to content

Commit 5509911

Browse files
authored
Merge pull request #16 from dataiku/feature/dss41-venv
Feature/dss41 venv
2 parents bf07561 + da9c507 commit 5509911

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

dataikuapi/dss/admin.py

Lines changed: 111 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,113 @@ 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+
return resp
448+
449+
450+
########################################################
451+
# Code env description
452+
########################################################
453+
454+
def get_definition(self):
455+
"""
456+
Get the code env's definition
457+
458+
Note: this call requires an API key with admin rights
459+
460+
Returns:
461+
the code env definition, as a JSON object
462+
"""
463+
return self.client._perform_json(
464+
"GET", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name))
465+
466+
def set_definition(self, env):
467+
"""
468+
Set the code env's definition. The definition should come from a call to the get_definition()
469+
method.
470+
471+
Fields that can be updated in design node:
472+
473+
* env.permissions, env.usableByAll, env.desc.owner
474+
* env.specCondaEnvironment, env.specPackageList, env.externalCondaEnvName, env.desc.installCorePackages,
475+
env.desc.installJupyterSupport, env.desc.yarnPythonBin
476+
477+
Fields that can be updated in automation node (where {version} is the updated version):
478+
479+
* env.permissions, env.usableByAll, env.owner
480+
* env.{version}.specCondaEnvironment, env.{version}.specPackageList, env.{version}.externalCondaEnvName,
481+
env.{version}.desc.installCorePackages, env.{version}.desc.installJupyterSupport, env.{version}.desc.yarnPythonBin
482+
483+
484+
485+
Note: this call requires an API key with admin rights
486+
487+
:param data: a code env definition
488+
489+
Returns:
490+
the updated code env definition, as a JSON object
491+
"""
492+
return self.client._perform_json(
493+
"PUT", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name), body=env)
494+
495+
496+
########################################################
497+
# Code env actions
498+
########################################################
499+
500+
def set_jupyter_support(self, active):
501+
"""
502+
Update the code env jupyter support
503+
504+
Note: this call requires an API key with admin rights
505+
506+
:param active: True to activate jupyter support, False to deactivate
507+
"""
508+
resp = self.client._perform_json(
509+
"POST", "/admin/code-envs/%s/%s/jupyter" % (self.env_lang, self.env_name),
510+
params = {'active':active})
511+
if resp is None:
512+
raise Exception('Env update returned no data')
513+
if resp.get('messages', {}).get('error', False):
514+
raise Exception('Env update failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
515+
return resp
516+
517+
def update_packages(self):
518+
"""
519+
Update the code env packages so that it matches its spec
520+
521+
Note: this call requires an API key with admin rights
522+
"""
523+
resp = self.client._perform_json(
524+
"POST", "/admin/code-envs/%s/%s/packages" % (self.env_lang, self.env_name))
525+
if resp is None:
526+
raise Exception('Env update returned no data')
527+
if resp.get('messages', {}).get('error', False):
528+
raise Exception('Env update failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
529+
return resp
530+

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 code env, 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)