Skip to content

Commit e667a73

Browse files
authored
Merge pull request #226 from dataiku/feature/dss100-sc-58209-public-api-for-api-keys
[sc58209] Add personal API keys APIs
2 parents 96d33ca + 9d94d96 commit e667a73

File tree

2 files changed

+223
-54
lines changed

2 files changed

+223
-54
lines changed

dataikuapi/dss/admin.py

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
from .future import DSSFuture
44
import json, warnings
55

6+
67
class DSSConnectionInfo(dict):
78
"""A class holding read-only information about a connection.
8-
This class should not be created directly. Instead, use :meth:`DSSConnection.get_info`
9+
Do not create this object directly, use :meth:`DSSConnection.get_info` instead.
910
1011
The main use case of this class is to retrieve the decrypted credentials for a connection,
1112
if allowed by the connection permissions.
1213
1314
Depending on the connection kind, the credential may be available using :meth:`get_basic_credential`
14-
or :meth:`get_aws_credential`
15+
or :meth:`get_aws_credential`.
1516
"""
1617
def __init__(self, data):
17-
"""Do not call this directly, use :meth:`DSSConnection.get_info`"""
1818
super(DSSConnectionInfo, self).__init__(data)
1919

2020
def get_type(self):
@@ -52,9 +52,9 @@ def get_aws_credential(self):
5252
class DSSConnection(object):
5353
"""
5454
A connection on the DSS instance.
55+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_connection` instead.
5556
"""
5657
def __init__(self, client, name):
57-
"""Do not call this directly, use :meth:`dataikuapi.DSSClient.get_connection`"""
5858
self.client = client
5959
self.name = name
6060

@@ -149,10 +149,9 @@ def sync_datasets_acls(self):
149149
class DSSUser(object):
150150
"""
151151
A handle for a user on the DSS instance.
152-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_user`
152+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_user` instead.
153153
"""
154154
def __init__(self, client, login):
155-
"""Do not call this directly, use :meth:`dataikuapi.DSSClient.get_user`"""
156155
self.client = client
157156
self.login = login
158157

@@ -241,7 +240,7 @@ def get_client_as(self):
241240
class DSSOwnUser(object):
242241
"""
243242
A handle to interact with your own user
244-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_own_user`
243+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_own_user` instead.
245244
"""
246245
def __init__(self, client):
247246
self.client = client
@@ -257,9 +256,11 @@ def get_settings(self):
257256

258257

259258
class DSSUserSettingsBase(object):
260-
"""Settings for a DSS user"""
259+
"""
260+
Settings for a DSS user.
261+
Do not create this object directly, use :meth:`DSSUser.get_settings` or :meth:`DSSOwnUser.get_settings` instead.
262+
"""
261263
def __init__(self, settings):
262-
"""Do not call this directly, use :meth:`DSSUser.get_settings` or :meth:`DSSOwnUser.get_settings` """
263264
self.settings = settings
264265

265266
def get_raw(self):
@@ -331,10 +332,11 @@ def remove_plugin_credential(self, plugin_id, param_set_id, preset_id, param_nam
331332

332333

333334
class DSSUserSettings(DSSUserSettingsBase):
334-
"""Settings for a DSS user"""
335-
335+
"""
336+
Settings for a DSS user.
337+
Do not create this object directly, use :meth:`DSSUser.get_settings` instead.
338+
"""
336339
def __init__(self, client, login, settings):
337-
"""Do not call this directly, use :meth:`DSSUser.get_settings`"""
338340
super(DSSUserSettings, self).__init__(settings)
339341
self.client = client
340342
self.login = login
@@ -366,10 +368,11 @@ def save(self):
366368

367369

368370
class DSSOwnUserSettings(DSSUserSettingsBase):
369-
"""Settings for the current DSS user"""
370-
371+
"""
372+
Settings for the current DSS user.
373+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_own_user` instead.
374+
"""
371375
def __init__(self, client, settings):
372-
"""Do not call this directly, use :meth:`dataikuapi.DSSClient.get_own_user`"""
373376
super(DSSOwnUserSettings, self).__init__(settings)
374377
self.client = client
375378

@@ -381,9 +384,8 @@ def save(self):
381384
class DSSUserActivity(object):
382385
"""
383386
Activity for a DSS user.
384-
Do not call this directly, use :meth:`DSSUser.get_activity` or :meth:`DSSClient.list_users_activity`
387+
Do not create this object directly, use :meth:`DSSUser.get_activity` or :meth:`DSSClient.list_users_activity` instead.
385388
"""
386-
387389
def __init__(self, client, login, activity):
388390
self.client = client
389391
self.login = login
@@ -442,10 +444,9 @@ def last_session_activity(self):
442444
class DSSGroup(object):
443445
"""
444446
A group on the DSS instance.
445-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_group`
447+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_group` instead.
446448
"""
447449
def __init__(self, client, name):
448-
"""Do not call this directly, use :meth:`dataikuapi.DSSClient.get_group`"""
449450
self.client = client
450451
self.name = name
451452

@@ -488,10 +489,9 @@ def set_definition(self, definition):
488489
class DSSGeneralSettings(object):
489490
"""
490491
The general settings of the DSS instance.
491-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_general_settings`
492+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_general_settings` instead.
492493
"""
493494
def __init__(self, client):
494-
"""Do not call this directly, use :meth:`dataikuapi.DSSClient.get_general_settings`"""
495495
self.client = client
496496
self.settings = self.client._perform_json("GET", "/admin/general-settings")
497497

@@ -614,6 +614,7 @@ def push_container_exec_base_images(self):
614614
raise Exception('Container exec base image push failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
615615
return resp
616616

617+
617618
class DSSUserImpersonationRule(object):
618619
"""
619620
Helper to build user-level rule items for the impersonation settings
@@ -676,6 +677,7 @@ def user_regexp(self, regexp, unix_user, hadoop_user=None):
676677
self.raw['targetHadoop'] = hadoop_user
677678
return self
678679

680+
679681
class DSSGroupImpersonationRule(object):
680682
"""
681683
Helper to build group-level rule items for the impersonation settings
@@ -720,10 +722,11 @@ def group_regexp(self, regexp, unix_user, hadoop_user=None):
720722
self.raw['targetHadoop'] = hadoop_user
721723
return self
722724

725+
723726
class DSSCodeEnv(object):
724727
"""
725728
A code env on the DSS instance.
726-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_code_env`
729+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_code_env` instead.
727730
"""
728731
def __init__(self, client, env_lang, env_name):
729732
self.client = client
@@ -923,7 +926,7 @@ def get_log(self, log_name):
923926
class DSSCodeEnvSettings(object):
924927
"""
925928
Base settings class for a DSS code env.
926-
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
929+
Do not create this object directly, use :meth:`DSSCodeEnv.get_settings` instead.
927930
928931
Use :meth:`save` to save your changes
929932
"""
@@ -948,6 +951,7 @@ def save(self):
948951
self.codeenv.client._perform_json(
949952
"PUT", "/admin/code-envs/%s/%s" % (self.env_lang, self.env_name), body=self.settings)
950953

954+
951955
class DSSCodeEnvPackageListBearer(object):
952956
def get_required_packages(self, as_list=False):
953957
"""
@@ -1025,27 +1029,24 @@ def set_built_spark_kubernetes_confs(self, *configs, **kwargs):
10251029
class DSSDesignCodeEnvSettings(DSSCodeEnvSettings, DSSCodeEnvPackageListBearer, DSSCodeEnvContainerConfsBearer):
10261030
"""
10271031
Base settings class for a DSS code env on a design node.
1028-
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
1032+
Do not create this object directly, use :meth:`DSSCodeEnv.get_settings` instead.
10291033
10301034
Use :meth:`save` to save your changes
10311035
"""
1032-
10331036
def __init__(self, codeenv, settings):
10341037
super(DSSDesignCodeEnvSettings, self).__init__(codeenv, settings)
10351038

10361039

10371040
class DSSAutomationCodeEnvSettings(DSSCodeEnvSettings, DSSCodeEnvContainerConfsBearer):
10381041
"""
10391042
Base settings class for a DSS code env on an automation node.
1040-
Do not instantiate this class directly, use :meth:`DSSCodeEnv.get_settings`
1043+
Do not create this object directly, use :meth:`DSSCodeEnv.get_settings` instead.
10411044
10421045
Use :meth:`save` to save your changes
10431046
"""
1044-
10451047
def __init__(self, codeenv, settings):
10461048
super(DSSAutomationCodeEnvSettings, self).__init__(codeenv, settings)
10471049

1048-
10491050
def get_version(self, version_id=None):
10501051
"""
10511052
Get a specific code env version (for versioned envs) or the single
@@ -1075,11 +1076,10 @@ def get_version(self, version_id=None):
10751076
class DSSAutomationCodeEnvVersionSettings(DSSCodeEnvPackageListBearer):
10761077
"""
10771078
Base settings class for a DSS code env version on an automation node.
1078-
Do not instantiate this class directly, use :meth:`DSSAutomationCodeEnvSettings.get_version`
1079+
Do not create this object directly, use :meth:`DSSAutomationCodeEnvSettings.get_version` instead.
10791080
10801081
Use :meth:`save` on the :class:`DSSAutomationCodeEnvSettings` to save your changes
10811082
"""
1082-
10831083
def __init__(self, codeenv_settings, version_settings):
10841084
self.codeenv_settings = codeenv_settings
10851085
self.settings = version_settings
@@ -1136,12 +1136,89 @@ def set_definition(self, definition):
11361136
body = definition)
11371137

11381138

1139+
class DSSPersonalApiKey(object):
1140+
"""
1141+
A personal API key on the DSS instance.
1142+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_personal_api_key` instead.
1143+
"""
1144+
def __init__(self, client, id_):
1145+
self.client = client
1146+
self.id_ = id_
1147+
1148+
########################################################
1149+
# Key description
1150+
########################################################
1151+
1152+
def get_definition(self):
1153+
"""
1154+
Get the API key's definition
1155+
1156+
:returns: the personal API key definition, as a JSON object
1157+
"""
1158+
return self.client._perform_json(
1159+
"GET", "/personal-api-keys/%s" % (self.id_))
1160+
1161+
########################################################
1162+
# Key deletion
1163+
########################################################
1164+
1165+
def delete(self):
1166+
"""
1167+
Delete the API key
1168+
"""
1169+
return self.client._perform_empty(
1170+
"DELETE", "/personal-api-keys/%s" % self.id_)
1171+
1172+
1173+
class DSSPersonalApiKeyListItem(dict):
1174+
"""
1175+
An item in a list of personal API key.
1176+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.list_personal_api_keys` or :meth:`dataikuapi.DSSClient.list_all_personal_api_keys` instead.
1177+
"""
1178+
def __init__(self, client, data):
1179+
super(DSSPersonalApiKeyListItem, self).__init__(data)
1180+
self.client = client
1181+
1182+
def to_personal_api_key(self):
1183+
"""Gets the :class:`DSSPersonalApiKey` corresponding to this item"""
1184+
return DSSPersonalApiKey(self.client, self["id"])
1185+
1186+
@property
1187+
def id(self):
1188+
return self["id"]
1189+
1190+
@property
1191+
def user(self):
1192+
return self["user"]
1193+
1194+
@property
1195+
def key(self):
1196+
return self["key"]
1197+
1198+
@property
1199+
def label(self):
1200+
return self["label"]
1201+
1202+
@property
1203+
def description(self):
1204+
return self["description"]
1205+
1206+
@property
1207+
def created_on(self):
1208+
timestamp = self["createdOn"]
1209+
return datetime.datetime.fromtimestamp(timestamp / 1000) if timestamp > 0 else None
1210+
1211+
@property
1212+
def created_by(self):
1213+
return self["createdBy"]
1214+
1215+
11391216
class DSSCluster(object):
11401217
"""
1141-
A handle to interact with a cluster on the DSS instance
1218+
A handle to interact with a cluster on the DSS instance.
1219+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_cluster` instead.
11421220
"""
11431221
def __init__(self, client, cluster_id):
1144-
"""Do not call that directly, use :meth:`dataikuapi.DSSClient.get_cluster`"""
11451222
self.client = client
11461223
self.cluster_id = cluster_id
11471224

@@ -1307,10 +1384,10 @@ def delete_all_pods(self, namespace=None, label_filter=None, dry_run=False):
13071384

13081385
class DSSClusterSettings(object):
13091386
"""
1310-
The settings of a cluster
1387+
The settings of a cluster.
1388+
Do not create this object directly, use :meth:`DSSCluster.get_settings` instead.
13111389
"""
13121390
def __init__(self, client, cluster_id, settings):
1313-
"""Do not call directly, use :meth:`DSSCluster.get_settings`"""
13141391
self.client = client
13151392
self.cluster_id = cluster_id
13161393
self.settings = settings
@@ -1343,10 +1420,10 @@ def save(self):
13431420

13441421
class DSSClusterStatus(object):
13451422
"""
1346-
The status of a cluster
1423+
The status of a cluster.
1424+
Do not create this object directly, use :meth:`DSSCluster.get_status` instead.
13471425
"""
13481426
def __init__(self, client, cluster_id, status):
1349-
"""Do not call directly, use :meth:`DSSCluster.get_Status`"""
13501427
self.client = client
13511428
self.cluster_id = cluster_id
13521429
self.status = status
@@ -1362,9 +1439,8 @@ class DSSInstanceVariables(dict):
13621439
"""
13631440
Dict containing the instance variables. The variables can be modified directly in the dict and persisted using its :meth:`save` method.
13641441
1365-
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_global_variables`
1442+
Do not create this object directly, use :meth:`dataikuapi.DSSClient.get_global_variables` instead.
13661443
"""
1367-
13681444
def __init__(self, client, variables):
13691445
super(dict, self).__init__()
13701446
self.update(variables)
@@ -1382,7 +1458,7 @@ def save(self):
13821458
class DSSGlobalUsageSummary(object):
13831459
"""
13841460
The summary of the usage of the DSS instance.
1385-
Do not create this directly, use :meth:`dataikuapi.dss.DSSClient.get_global_usage_summary`
1461+
Do not create this object directly, use :meth:`dataikuapi.dss.DSSClient.get_global_usage_summary` instead.
13861462
"""
13871463
def __init__(self, data):
13881464
self.data = data

0 commit comments

Comments
 (0)