Skip to content

Commit 6882c02

Browse files
authored
Merge pull request #17 from dataiku/feature/management-public-api-keys
Management public api keys
2 parents 491a895 + 6db6131 commit 6882c02

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

dataikuapi/dss/admin.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,53 @@ def update_packages(self):
528528
raise Exception('Env update failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
529529
return resp
530530

531+
532+
class DSSGlobalApiKey(object):
533+
"""
534+
A global API key on the DSS instance
535+
"""
536+
def __init__(self, client, key):
537+
self.client = client
538+
self.key = key
539+
540+
########################################################
541+
# Key deletion
542+
########################################################
543+
544+
def delete(self):
545+
"""
546+
Delete the api key
547+
548+
Note: this call requires an API key with admin rights
549+
"""
550+
return self.client._perform_empty(
551+
"DELETE", "/admin/globalAPIKeys/%s" % self.key)
552+
553+
########################################################
554+
# Key description
555+
########################################################
556+
557+
def get_definition(self):
558+
"""
559+
Get the API key's definition
560+
561+
Note: this call requires an API key with admin rights
562+
563+
Returns:
564+
the code env definition, as a JSON object
565+
"""
566+
return self.client._perform_json(
567+
"GET", "/admin/globalAPIKeys/%s" % (self.key))
568+
569+
def set_definition(self, definition):
570+
"""
571+
Set the API key's definition.
572+
573+
Note: this call requires an API key with admin rights
574+
575+
Args:
576+
definition: the definition for the API key, as a JSON object.
577+
"""
578+
return self.client._perform_empty(
579+
"PUT", "/admin/globalAPIKeys/%s" % self.key,
580+
body = definition)

dataikuapi/dssclient.py

Lines changed: 60 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
9+
from dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey
1010
from dss.meaning import DSSMeaning
1111
from dss.sqlquery import DSSSQLQuery
1212
from dss.notebook import DSSNotebook
@@ -412,6 +412,65 @@ 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+
# Global API Keys
417+
########################################################
418+
419+
def list_global_api_keys(self):
420+
"""
421+
List all global API keys set up on the DSS instance
422+
423+
Note: this call requires an API key with admin rights
424+
425+
Returns:
426+
All global API keys, as a list
427+
"""
428+
return self._perform_json(
429+
"GET", "/admin/globalAPIKeys/")
430+
431+
def get_global_api_key(self, key):
432+
"""
433+
Get a handle to interact with a specific Global API key
434+
435+
Args:
436+
key: the secret key of the desired API key
437+
438+
Returns:
439+
A :class:`dataikuapi.dss.admin.DSSGlobalApiKey` API key handle
440+
"""
441+
return DSSGlobalApiKey(self, key)
442+
443+
def create_global_api_key(self, label=None, description=None, admin=False):
444+
"""
445+
Create a Global API key, and return a handle to interact with it
446+
447+
Note: this call requires an API key with admin rights
448+
449+
Args:
450+
label: the label of the new API key
451+
description: the description of the new API key
452+
admin: has the new API key admin rights (True or False)
453+
454+
Returns:
455+
A :class:`dataikuapi.dss.admin.DSSGlobalApiKey` API key handle
456+
"""
457+
resp = self._perform_json(
458+
"POST", "/admin/globalAPIKeys/", body={
459+
"label" : label,
460+
"description" : description,
461+
"globalPermissions": {
462+
"admin": admin
463+
}
464+
})
465+
if resp is None:
466+
raise Exception('API key creation returned no data')
467+
if resp.get('messages', {}).get('error', False):
468+
raise Exception('API key creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
469+
if not resp.get('id', False):
470+
raise Exception('API key creation returned no key')
471+
key = resp.get('key', '')
472+
return DSSGlobalApiKey(self, key)
473+
415474
########################################################
416475
# Meanings
417476
########################################################

0 commit comments

Comments
 (0)