Skip to content

Commit 0b42420

Browse files
committed
Add ability to obtain a proxy-user client + edit own user + structured user settings
1 parent feafa0a commit 0b42420

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

dataikuapi/dss/admin.py

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def sync_datasets_acls(self):
156156
"POST", "/admin/connections/%s/sync" % self.name,
157157
body = {'root':True})
158158
return DSSFuture(self.client, future_response.get('jobId', None), future_response)
159-
160-
159+
160+
161161
class DSSUser(object):
162162
"""
163163
A handle for a user on the DSS instance.
@@ -167,10 +167,6 @@ def __init__(self, client, login):
167167
self.client = client
168168
self.login = login
169169

170-
########################################################
171-
# User deletion
172-
########################################################
173-
174170
def delete(self):
175171
"""
176172
Deletes the user
@@ -179,6 +175,15 @@ def delete(self):
179175
return self.client._perform_empty(
180176
"DELETE", "/admin/users/%s" % self.login)
181177

178+
def get_settings(self):
179+
"""
180+
Gets the settings of the user
181+
182+
:rtype: :class:`DSSUserSettings`
183+
"""
184+
raw =self.client._perform_json("GET", "/admin/users/%s" % self.login)
185+
return DSSUserSettings(self.client, self.login, raw)
186+
182187
########################################################
183188
# User description
184189
########################################################
@@ -214,7 +219,104 @@ def set_definition(self, definition):
214219
return self.client._perform_json(
215220
"PUT", "/admin/users/%s" % self.login,
216221
body = definition)
217-
222+
223+
def get_client_as(self):
224+
from dataikuapi.dssclient import DSSClient
225+
226+
if self.client.api_key is not None:
227+
return DSSClient(self.client.host, self.client.api_key, extra_headers={"X-DKU-ProxyUser": self.login})
228+
elif self.client.internal_ticket is not None:
229+
return DSSClient(self.client.host, internal_ticket = self.client.internal_ticket,
230+
extra_headers={"X-DKU-ProxyUser": self.login})
231+
else:
232+
raise ValueError("Don't know how to proxy this client")
233+
234+
class DSSOwnUser(object):
235+
"""
236+
A handle to interact with your own user
237+
Do not create this directly, use :meth:`dataikuapi.DSSClient.get_own_user`
238+
"""
239+
def __init__(self, client):
240+
self.client = client
241+
242+
def get_settings(self):
243+
"""
244+
Gets your own settings
245+
246+
:rtype: :class:`DSSOwnUserSettings`
247+
"""
248+
raw = self.client._perform_json("GET", "/current-user")
249+
return DSSOwnUserSettings(self.client, raw)
250+
251+
252+
class DSSUserSettingsBase(object):
253+
254+
def __init__(self, settings):
255+
self.settings = settings
256+
257+
def get_raw(self):
258+
"""
259+
:returns: the raw settings of the user, as a dict. Modifications made to the returned object
260+
are reflected when saving
261+
262+
:rtype: dict
263+
"""
264+
return self.settings
265+
266+
def set_basic_connection_credential(self, connection, user, password):
267+
self.settings["credentials"][connection] = {
268+
"type": "BASIC",
269+
"user": user,
270+
"password": password
271+
}
272+
273+
def remove_connection_credential(self,connection):
274+
if connection in self.settings["credentials"]:
275+
del self.settings["credentials"][connection]
276+
277+
def set_basic_plugin_credential(self, plugin_id, param_set_id, preset_id, param_name, user, password):
278+
name = json.dumps(["PLUGIN", pluginId, paramSetId, presetId, paramName])[1:-1]
279+
280+
self.settings["credentials"][name] = {
281+
"type": "BASIC",
282+
"user": user,
283+
"password": password
284+
}
285+
286+
def set_oauth2_plugin_credential(self, plugin_id, param_set_id, preset_id, param_name, refresh_token):
287+
name = json.dumps(["PLUGIN", pluginId, paramSetId, presetId, paramName])[1:-1]
288+
289+
self.settings["credentials"][name] = {
290+
"type": "OAUTH_REFRESH_TOKEN",
291+
"refreshToken": refresh_token
292+
}
293+
294+
def remove_plugin_credential(self, plugin_id, param_set_id, preset_id, param_name):
295+
name = json.dumps(["PLUGIN", pluginId, paramSetId, presetId, paramName])[1:-1]
296+
297+
if name in self.settings["credentials"]:
298+
del self.settings["credentials"][name]
299+
300+
301+
class DSSUserSettings(DSSUserSettingsBase):
302+
def __init__(self, client, login, settings):
303+
super(DSSUserSettings, self).__init__(settings)
304+
self.client = client
305+
self.login = login
306+
307+
def save(self):
308+
self.client._perform_json("PUT", "/admin/users/%s" % self.login, body = self.settings)
309+
310+
311+
class DSSOwnUserSettings(DSSUserSettingsBase):
312+
def __init__(self, client, settings):
313+
super(DSSOwnUserSettings, self).__init__(settings)
314+
self.client = client
315+
316+
def save(self):
317+
self.client._perform_empty("PUT", "/current-user", body = self.settings)
318+
319+
218320
class DSSGroup(object):
219321
"""
220322
A group on the DSS instance.

dataikuapi/dssclient.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .dss.projectfolder import DSSProjectFolder
88
from .dss.project import DSSProject
99
from .dss.plugin import DSSPlugin
10-
from .dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster
10+
from .dss.admin import DSSUser, DSSOwnUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster
1111
from .dss.meaning import DSSMeaning
1212
from .dss.sqlquery import DSSSQLQuery
1313
from .dss.notebook import DSSNotebook
@@ -19,7 +19,7 @@
1919
class DSSClient(object):
2020
"""Entry point for the DSS API client"""
2121

22-
def __init__(self, host, api_key=None, internal_ticket = None):
22+
def __init__(self, host, api_key=None, internal_ticket = None, extra_headers = None):
2323
"""
2424
Instantiate a new DSS API client on the given host with the given API key.
2525
@@ -38,8 +38,10 @@ def __init__(self, host, api_key=None, internal_ticket = None):
3838
self._session.headers.update({"X-DKU-APITicket" : self.internal_ticket})
3939
else:
4040
raise ValueError("API Key is required")
41-
42-
41+
42+
if extra_headers is not None:
43+
self._session.headers.update(extra_headers)
44+
4345
########################################################
4446
# Futures
4547
########################################################
@@ -320,6 +322,9 @@ def create_user(self, login, password, display_name='', source_type='LOCAL', gro
320322
})
321323
return DSSUser(self, login)
322324

325+
def get_own_user(self):
326+
return DSSOwnUser(self)
327+
323328
########################################################
324329
# Groups
325330
########################################################

0 commit comments

Comments
 (0)