#================================================================================ # File: api_get_policies.py # Type: python script # Date: 09-October 2019 # Author: Carlos Cuellar # Ownership: This script is owned and maintained by the user, not by Delphix # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Copyright (c) 2019 by Delphix. All rights reserved. # # Description: # # Script to create a mapping file with objects and policies assigned to them. # # Prerequisites: # Python 2/3 installed # # # Usage # python api_get_policies.py # # # Example # python api_get_policies.py admin delphix delphixengine #================================================================================ # import sys import requests import json import time from datetime import datetime today_date = datetime.today().strftime('%Y-%m-%d') #Variables/Parameters DMUSER=sys.argv[1] DMPASS=sys.argv[2] DX_ENGINE=sys.argv[3] BASEURL='http://' + DX_ENGINE + '/resources/json/delphix' # # Request Headers ... # req_headers = { 'Content-Type': 'application/json' } # # Python session, also handles the cookies ... # session = requests.session() # # Create session ... # formdata = '{ "type": "APISession", "version": { "type": "APIVersion", "major": 1, "minor": 10, "micro": 0 } }' r = session.post(BASEURL+'/session', data=formdata, headers=req_headers, allow_redirects=False) # # Login ... # formdata = '{ "type": "LoginRequest", "username": "' + DMUSER + '", "password": "' + DMPASS + '" }' r = session.post(BASEURL+'/login', data=formdata, headers=req_headers, allow_redirects=False) # # Get Delphix Database, source, sourceconfig and environment data ... # database = session.get(BASEURL+'/database', headers=req_headers, allow_redirects=False) group = session.get(BASEURL+'/group', headers=req_headers, allow_redirects=False) # # JSON Parsing ... # database_j = json.loads(database.text) group_j = json.loads(group.text) ##print (database_j) ##print (group_j) print ("Printing Delphix policies assigned to Dataset groups, dSources and vDBs...") print ("") print ("dSource/vDB/Group, Object Reference, Policy, Policy Reference") # # Look for Delphix Group Reference and Policies applied to them # for dbobj in group_j['result']: policy_group = session.get(BASEURL+'/policy?effective=true&target='+dbobj['reference'], headers=req_headers, allow_redirects=False) policy_group_j = json.loads(policy_group.text) for dbobj2 in policy_group_j['result']: print (dbobj['name'] + ',' + dbobj['reference'] + ',' + dbobj2['name'] + ',' + dbobj2['reference']) file = open("dx_policies_mapping_" + today_date + ".out" ,"a+") file.write(dbobj['name'] + ',' + dbobj['reference'] + ',' + dbobj2['name'] + ',' + dbobj2['reference']) file.write("\n") # # Look for Delphix dSource and vDBs references and their policies applied to them # for dbobj in database_j['result']: policy_database = session.get(BASEURL+'/policy?effective=true&target='+dbobj['reference'], headers=req_headers, allow_redirects=False) policy_database_j = json.loads(policy_database.text) for dbobj2 in policy_database_j['result']: print (str(dbobj['name']) + ',' + str(dbobj['reference']) + ',' + str(dbobj2['name']) + ',' + str(dbobj2['reference'])) file = open("dx_policies_mapping_" + today_date + ".out" ,"a+") file.write(str(dbobj['name']) + ',' + str(dbobj['reference']) + ',' + str(dbobj2['name']) + ',' + str(dbobj2['reference'])) file.write("\n") file.close()