[ https://issues.apache.org/jira/browse/RANGER-2798?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Pradeep Agrawal resolved RANGER-2798. ------------------------------------- Resolution: Cannot Reproduce > existing policy is getting replaced when using update_policy from a python > script > --------------------------------------------------------------------------------- > > Key: RANGER-2798 > URL: https://issues.apache.org/jira/browse/RANGER-2798 > Project: Ranger > Issue Type: Bug > Components: Ranger > Reporter: Bhargavi > Assignee: Pradeep Agrawal > Priority: Major > > #!/usr/bin/env python > """ > Input file is a CSV: > Policy name, Resource path, AD groups, Read, Write, Execute, Comment > """ > policy_id ='' > import time > import requests > import csv > import json > from httplib import HTTPSConnection > from base64 import b64encode > import sys > ranger_host = sys.argv[2] > ranger_port = sys.argv[3] > policy_api = '/service/public/api/policy' > ranger_user = 'admin' > ranger_password = > repository_name = sys.argv[4] > repository_name += '_hadoop' > description_template = 'Policy for %s' > policy_template = { > 'policyType': 'access', > 'policyName': '', > 'resourceName': '', > 'description': '', > 'repositoryName': repository_name, > 'repositoryType': 'hdfs', > 'isEnabled': True, > 'isRecursive': True, > 'isAuditEnabled': True, > 'permMapList': [] > } > > > def get_policy(policy_name): > """Retrieve the policy definition > """ > global policyid > conn = HTTPSConnection(host=ranger_host, port=ranger_port) > headers = { > 'Authorization' : 'Basic %s' % b64encode('%s:%s' % (ranger_user, > ranger_password)), > 'Content-Type': 'application/json' > } > conn.request('GET', policy_api + '?policyName=' + policy_name, > headers=headers) > response = conn.getresponse() > json_data = response.read() > result = json.loads(json_data) > count = result['totalCount'] > print count > if count == 1: > for data in result['vXPolicies']: > policy_id = data['id'] > print(policy_id) > update_policy(policy,policy_id) > print(policy) > else: > create_policy(policy) > print(policy) > def update_policy(data,policy_id): > conn = HTTPSConnection(host=ranger_host, port=ranger_port) > headers = { > 'Authorization' : 'Basic %s' % b64encode('%s:%s' % (ranger_user, > ranger_password)), > 'Content-Type': 'application/json' > } > conn.request('PUT', policy_api + '/' + str(policy_id), headers=headers, > body=json.dumps(data)) > response = conn.getresponse() > if response.status != 200: > print 'Error updating policy' > print response.read() > else: > print 'Policy %s updated' > > > > def create_policy(data): > conn = HTTPSConnection(host=ranger_host, port=ranger_port) > headers = { > 'Authorization' : 'Basic %s' % b64encode('%s:%s' % (ranger_user, > ranger_password)), > 'Content-Type': 'application/json' > } > conn.request('POST', policy_api, headers=headers, body=json.dumps(data)) > response = conn.getresponse() > if response.status != 200: > print 'Error creating policy %s: %s' % (data['policyName'], data) > print response.read() > else: > print 'Policy %s created' % data['policyName'] > > with open(sys.argv[1]) as csvfile: > reader = csv.DictReader(csvfile, delimiter=',') > policy = policy_template > i=0 > for row in reader: > if row['Policy name']: > policy['policyName'] = row['Policy name'] > policy['resourceName'] = row['Resource path'] > policy['description'] = description_template % row['Resource path'] > policy['permMapList'] = [ { > 'groupList': [row['AD groups']], > 'permList': [] > }] > if row['Read']: policy['permMapList'][int(i)]['permList'].append('Read') > if row['Write']: policy['permMapList'][int(i)]['permList'].append('Write') > if row['Execute']: > policy['permMapList'][int(i)]['permList'].append('Execute') > else: > policy['permMapList'].append({ > 'groupList': [row['AD groups']], > 'permList': [] > }) > i=i+1 > print i > if row['Read']: policy['permMapList'][int(i)]['permList'].append('Read') > if row['Write']: policy['permMapList'][int(i)]['permList'].append('Write') > if row['Execute']: > policy['permMapList'][int(i)]['permList'].append('Execute') > get_policy(policy['policyName']) > policy=policy_template -- This message was sent by Atlassian Jira (v8.3.4#803005)