Skip to content

Commit 2fff395

Browse files
committed
add api on general settings
and extras for impersonation rules
1 parent 4eb7b83 commit 2fff395

File tree

3 files changed

+243
-2
lines changed

3 files changed

+243
-2
lines changed

dataikuapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
from apinode_admin_client import APINodeAdminClient
55

66
from dss.recipe import GroupingRecipeCreator, JoinRecipeCreator, StackRecipeCreator, WindowRecipeCreator, SyncRecipeCreator, SamplingRecipeCreator, SQLQueryRecipeCreator, CodeRecipeCreator #, ShakerRecipeCreator
7+
8+
from dss.admin import DSSUserImpersonationRule, DSSGroupImpersonationRule

dataikuapi/dss/admin.py

Lines changed: 224 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,227 @@ def set_definition(self, definition):
190190
"PUT", "/admin/groups/%s" % self.name,
191191
body = definition)
192192

193-
193+
194+
class DSSGeneralSettings(object):
195+
"""
196+
The general settings of the DSS instance
197+
"""
198+
def __init__(self, client):
199+
self.client = client
200+
self.settings = self.client._perform_json("GET", "/admin/general-settings")
201+
202+
########################################################
203+
# Update settings on instance
204+
########################################################
205+
206+
def save(self):
207+
"""
208+
Save the changes that were made to the settings on the DSS instance
209+
210+
Note: this call requires an API key with admin rights
211+
"""
212+
return self.client._perform_empty("PUT", "/admin/general-settings", body = self.settings)
213+
214+
215+
########################################################
216+
# Value accessors
217+
########################################################
218+
219+
def get_raw(self):
220+
"""
221+
Get the settings as a dictionary
222+
"""
223+
return self.settings
224+
225+
def add_impersonation_rule(self, rule, is_user_rule=True):
226+
"""
227+
Add a rule to the impersonation settings
228+
229+
Args:
230+
rule : an impersonation rule, either a :class:`dataikuapi.dss.admin.DSSUserImpersonationRule`
231+
or a :class:`dataikuapi.dss.admin.DSSGroupImpersonationRule`, or a plain dict
232+
is_user_rule : when the rule parameter is a dict, whether the rule is for users or groups
233+
"""
234+
rule_raw = rule
235+
if isinstance(rule, DSSUserImpersonationRule):
236+
rule_raw = rule.raw
237+
is_user_rule = True
238+
elif isinstance(rule, DSSGroupImpersonationRule):
239+
rule_raw = rule.raw
240+
is_user_rule = False
241+
impersonation = self.settings['impersonation']
242+
if is_user_rule:
243+
impersonation['userRules'].append(rule_raw)
244+
else:
245+
impersonation['groupRules'].append(rule_raw)
246+
247+
def get_impersonation_rules(self, dss_user=None, dss_group=None, unix_user=None, hadoop_user=None, project_key=None, scope=None, rule_type=None, is_user=None):
248+
"""
249+
Retrieve the user or group impersonation rules that matches the parameters
250+
251+
Args:
252+
dss_user : a DSS user or regular expression to match DSS user names
253+
dss_group : a DSS group or regular expression to match DSS groups
254+
unix_user : a name to match the target UNIX user
255+
hadoop_user : a name to match the target Hadoop user
256+
project_key : a project_key
257+
scope : project-scoped ('PROJECT') or global ('GLOBAL')
258+
type : the rule user or group matching method ('IDENTITY', 'SINGLE_MAPPING', 'REGEXP_RULE')
259+
is_user : True if only user-level rules should be considered, False for only group-level rules, None to consider both
260+
"""
261+
user_matches = self.settings['impersonation']['userRules'] if is_user == None or is_user == True else []
262+
if dss_user is not None:
263+
user_matches = [m for m in user_matches if dss_user == m.get('dssUser', None)]
264+
if unix_user is not None:
265+
user_matches = [m for m in user_matches if unix_user == m.get('targetUnix', None)]
266+
if hadoop_user is not None:
267+
user_matches = [m for m in user_matches if hadoop_user == m.get('targetHadoop', None)]
268+
if project_key is not None:
269+
user_matches = [m for m in user_matches if project_key == m.get('projectKey', None)]
270+
if rule_type is not None:
271+
user_matches = [m for m in user_matches if rule_type == m.get('type', None)]
272+
if scope is not None:
273+
user_matches = [m for m in user_matches if scope == m.get('scope', None)]
274+
group_matches = self.settings['impersonation']['groupRules'] if is_user == None or is_user == False else []
275+
if dss_group is not None:
276+
group_matches = [m for m in group_matches if dss_group == m.get('dssGroup', None)]
277+
if unix_user is not None:
278+
group_matches = [m for m in group_matches if unix_user == m.get('targetUnix', None)]
279+
if hadoop_user is not None:
280+
group_matches = [m for m in group_matches if hadoop_user == m.get('targetHadoop', None)]
281+
if rule_type is not None:
282+
group_matches = [m for m in group_matches if rule_type == m.get('type', None)]
283+
284+
all_matches = []
285+
for m in user_matches:
286+
all_matches.append(DSSUserImpersonationRule(m))
287+
for m in group_matches:
288+
all_matches.append(DSSGroupImpersonationRule(m))
289+
return all_matches
290+
291+
def remove_impersonation_rules(self, dss_user=None, dss_group=None, unix_user=None, hadoop_user=None, project_key=None, scope=None, rule_type=None, is_user=None):
292+
"""
293+
Remove the user or group impersonation rules that matches the parameters from the settings
294+
295+
Args:
296+
dss_user : a DSS user or regular expression to match DSS user names
297+
dss_group : a DSS group or regular expression to match DSS groups
298+
unix_user : a name to match the target UNIX user
299+
hadoop_user : a name to match the target Hadoop user
300+
project_key : a project_key
301+
scope : project-scoped ('PROJECT') or global ('GLOBAL')
302+
type : the rule user or group matching method ('IDENTITY', 'SINGLE_MAPPING', 'REGEXP_RULE')
303+
is_user : True if only user-level rules should be considered, False for only group-level rules, None to consider both
304+
"""
305+
for m in self.get_impersonation_rules(dss_user, dss_group, unix_user, hadoop_user, project_key, scope, rule_type, is_user):
306+
if isinstance(m, DSSUserImpersonationRule):
307+
self.settings['impersonation']['userRules'].remove(m.raw)
308+
elif isinstance(m, DSSGroupImpersonationRule):
309+
self.settings['impersonation']['groupRules'].remove(m.raw)
310+
311+
312+
class DSSUserImpersonationRule(object):
313+
"""
314+
Helper to build user-level rule items for the impersonation settings
315+
"""
316+
def __init__(self, raw=None):
317+
self.raw = raw if raw is not None else {'scope':'GLOBAL','type':'IDENTITY'}
318+
319+
def scope_global(self):
320+
"""
321+
Make the rule apply to all projects
322+
"""
323+
self.raw['scope'] = 'GLOBAL'
324+
return self
325+
326+
def scope_project(self, project_key):
327+
"""
328+
Make the rule apply to a given project
329+
330+
Args:
331+
project_key : the project this rule applies to
332+
"""
333+
self.raw['scope'] = 'PROJECT'
334+
self.raw['projectKey'] = project_key
335+
return self
336+
337+
def user_identity(self):
338+
"""
339+
Make the rule map each DSS user to a UNIX user of the same name
340+
"""
341+
self.raw['type'] = 'IDENTITY'
342+
return self
343+
344+
def user_single(self, dss_user, unix_user, hadoop_user=None):
345+
"""
346+
Make the rule map a given DSS user to a given UNIX user
347+
348+
Args:
349+
dss_user : a DSS user
350+
unix_user : a UNIX user
351+
hadoop_user : a Hadoop user (optional, defaults to unix_user)
352+
"""
353+
self.raw['type'] = 'SINGLE_MAPPING'
354+
self.raw['dssUser'] = dss_user
355+
self.raw['targetUnix'] = unix_user
356+
self.raw['targetHadoop'] = hadoop_user
357+
return self
358+
359+
def user_regexp(self, regexp, unix_user, hadoop_user=None):
360+
"""
361+
Make the rule map a DSS users matching a given regular expression to a given UNIX user
362+
363+
Args:
364+
regexp : a regular expression to match DSS user names
365+
unix_user : a UNIX user
366+
hadoop_user : a Hadoop user (optional, defaults to unix_user)
367+
"""
368+
self.raw['type'] = 'REGEXP_RULE'
369+
self.raw['ruleFrom'] = regexp
370+
self.raw['targetUnix'] = unix_user
371+
self.raw['targetHadoop'] = hadoop_user
372+
return self
373+
374+
class DSSGroupImpersonationRule(object):
375+
"""
376+
Helper to build group-level rule items for the impersonation settings
377+
"""
378+
def __init__(self, raw=None):
379+
self.raw = raw if raw is not None else {'type':'IDENTITY'}
380+
381+
def group_identity(self):
382+
"""
383+
Make the rule map each DSS user to a UNIX user of the same name
384+
"""
385+
self.raw['type'] = 'IDENTITY'
386+
return self
387+
388+
def group_single(self, dss_group, unix_user, hadoop_user=None):
389+
"""
390+
Make the rule map a given DSS user to a given UNIX user
391+
392+
Args:
393+
dss_group : a DSS group
394+
unix_user : a UNIX user
395+
hadoop_user : a Hadoop user (optional, defaults to unix_user)
396+
"""
397+
self.raw['type'] = 'SINGLE_MAPPING'
398+
self.raw['dssGroup'] = dss_group
399+
self.raw['targetUnix'] = unix_user
400+
self.raw['targetHadoop'] = hadoop_user
401+
return self
402+
403+
def group_regexp(self, regexp, unix_user, hadoop_user=None):
404+
"""
405+
Make the rule map a DSS users matching a given regular expression to a given UNIX user
406+
407+
Args:
408+
regexp : a regular expression to match DSS groups
409+
unix_user : a UNIX user
410+
hadoop_user : a Hadoop user (optional, defaults to unix_user)
411+
"""
412+
self.raw['type'] = 'REGEXP_RULE'
413+
self.raw['ruleFrom'] = regexp
414+
self.raw['targetUnix'] = unix_user
415+
self.raw['targetHadoop'] = hadoop_user
416+
return self

dataikuapi/dssclient.py

Lines changed: 17 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
9+
from dss.admin import DSSUser, DSSGroup, DSSConnection, DSSGeneralSettings
1010
from dss.meaning import DSSMeaning
1111
from dss.sqlquery import DSSSQLQuery
1212
import os.path as osp
@@ -471,6 +471,22 @@ def set_variables(self, variables):
471471
"PUT", "/admin/variables/", body=variables)
472472

473473

474+
########################################################
475+
# General settings
476+
########################################################
477+
478+
def get_general_settings(self):
479+
"""
480+
Get a handle to interact with the general settings.
481+
482+
Note: this call requires an API key with admin rights
483+
484+
Returns:
485+
A :class:`dataikuapi.dss.admin.DSSGeneralSettings`
486+
"""
487+
return DSSGeneralSettings(self)
488+
489+
474490
########################################################
475491
# Bundles / Import (Automation node)
476492
########################################################

0 commit comments

Comments
 (0)