Skip to content

Commit cfdbc6e

Browse files
committed
RANGER-3812: Python client updated to support multiple resource sets in a policy
(cherry picked from commit 711a266)
1 parent d7e6db4 commit cfdbc6e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

intg/src/main/python/apache_ranger/model/ranger_policy.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, attrs=None):
4040
self.resourceSignature = attrs.get('resourceSignature')
4141
self.isAuditEnabled = attrs.get('isAuditEnabled')
4242
self.resources = attrs.get('resources')
43+
self.additionalResources = attrs.get('additionalResources')
4344
self.policyItems = attrs.get('policyItems')
4445
self.denyPolicyItems = attrs.get('denyPolicyItems')
4546
self.allowExceptions = attrs.get('allowExceptions')
@@ -58,6 +59,7 @@ def type_coerce_attrs(self):
5859
super(RangerPolicy, self).type_coerce_attrs()
5960

6061
self.resources = type_coerce_dict(self.resources, RangerPolicyResource)
62+
self.additionalResources = type_coerce_list(self.additionalResources, dict)
6163
self.policyItems = type_coerce_list(self.policyItems, RangerPolicyItem)
6264
self.denyPolicyItems = type_coerce_list(self.denyPolicyItems, RangerPolicyItem)
6365
self.allowExceptions = type_coerce_list(self.allowExceptions, RangerPolicyItem)
@@ -66,6 +68,25 @@ def type_coerce_attrs(self):
6668
self.rowFilterPolicyItems = type_coerce_list(self.rowFilterPolicyItems, RangerRowFilterPolicyItem)
6769
self.validitySchedules = type_coerce_list(self.validitySchedules, RangerValiditySchedule)
6870

71+
if isinstance(self.additionalResources, list):
72+
additionalResources = []
73+
74+
for entry in self.additionalResources:
75+
additionalResources.append(type_coerce_dict(entry, RangerPolicyResource))
76+
77+
self.additionalResources = additionalResources
78+
else:
79+
self.additionalResources = None
80+
81+
def add_resource(self, resource):
82+
if resource is not None:
83+
if self.resources is None:
84+
self.resources = resource
85+
else:
86+
if self.additionalResources is None:
87+
self.additionalResources = []
88+
89+
self.additionalResources.append(resource)
6990

7091
class RangerPolicyResource(RangerBase):
7192
def __init__(self, attrs=None):

ranger-examples/sample-client/src/main/python/sample_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@
156156
policy.description = 'test description'
157157
policy.resources = { 'database': RangerPolicyResource({ 'values': ['test_db'] }),
158158
'table': RangerPolicyResource({ 'values': ['test_tbl'] }),
159-
'column': RangerPolicyResource({ 'values': ['*'] }) }
159+
'column': RangerPolicyResource({ 'values': ['*'] }) }
160+
policy.add_resource({ 'database': RangerPolicyResource({ 'values': ['test_db1'] }),
161+
'table': RangerPolicyResource({ 'values': ['test_tbl1'] }),
162+
'column': RangerPolicyResource({ 'values': ['*'] }) })
163+
policy.add_resource({ 'database': RangerPolicyResource({ 'values': ['test_db2'] }),
164+
'table': RangerPolicyResource({ 'values': ['test_tbl2'] }),
165+
'column': RangerPolicyResource({ 'values': ['*'] }) })
166+
167+
160168

161169
allowItem1 = RangerPolicyItem()
162170
allowItem1.users = [ 'admin' ]
@@ -189,7 +197,7 @@
189197
data_mask_policy.description = 'test description'
190198
data_mask_policy.resources = { 'database': RangerPolicyResource({ 'values': ['test_db'] }),
191199
'table': RangerPolicyResource({ 'values': ['test_tbl'] }),
192-
'column': RangerPolicyResource({ 'values': ['test_col'] }) }
200+
'column': RangerPolicyResource({ 'values': ['test_col'] }) }
193201

194202
policyItem1 = RangerDataMaskPolicyItem()
195203
policyItem1.users = [ 'admin' ]

0 commit comments

Comments
 (0)