-
Notifications
You must be signed in to change notification settings - Fork 0
/
PurviewXLDownload.py
352 lines (251 loc) · 15.6 KB
/
PurviewXLDownload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import cmd
import sys
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
import requests
import xlsxwriter
import json
import argparse
############################### Command Line Parameters or can be set in the file itself #######################################################################################
PurviewAccountName = "" # Purview Account Name
PurviewSearchKeyWords = "" # Keywords to be searched. Mutiple Keywords can be provided comma seperated. Use * for search all.If left Blank , * will be used
PurviewExcelOutputPath = "" # Path in which Excel Output to be return. If left blank , Will be written to current path
PurviewRestAPILimit = 50 # This is API limit to send guids as bulk to get the entity details. Incase if the Entitites API is very slow or failing this can be
# made as a lower number.
########################################### Purview Settings to be done in file ################################################################################################
KeyVaultURI = f"" # Keyvault URI in which Service account SPNId and Password is stored
KeyVaultNameForPurviewAppSPNID = "" # Keyvault Secret Name in which Service account SPNId is stored
KeyVaultNameForPurviewAppSPNSecret = "" # Keyvault URI in which Service account is stored
PurviewTenantID = "" # TenantID in which Purview Account is deployed
########################################### Optional Setting ################################################################################################
# The column sheet will be generated for these Asset Type only.
# In case if column data is missing for any table Asset type , Try adding the asset type here.
PurviewColumnAssetType = [
'AZURE_DATA_EXPLORER_TABLE',
'AZURE_DATALAKE_GEN1_PATH',
'AZURE_DATALAKE_GEN1_RESOURCE_SET',
'AZURE_DATALAKE_GEN2_PATH',
'AZURE_DATALAKE_GEN2_RESOURCE_SET',
'AZURE_SQL_DW_TABLE',
'AZURE_SQL_TABLE',
'AZURE_MARIADB_TABLE',
'AZURE_MYSQL_TABLE',
'AZURE_POSTGRESQL_TABLE',
'AZURE_SQL_DW_TABLE',
'AZURE_SQL_MI_TABLE',
'AZURE_SYNAPSE_DEDICATED_SQL_TABLE',
'AZURE_SYNAPSE_SERVERLESS_SQL_TABLE',
'AZURE_TABLE',
'HBASE_TABLE',
'HIVE_TABLE',
'MSSQL_TABLE',
'RDBMS_TABLE',
'SAP_ECC_TABLE',
'SAP_S4HANA_TABLE',
'TERADATA_TABLE',
]
############################### Command Line Parameters or can be set in the file itself ######################################################
# Initialize parser
parser = argparse.ArgumentParser()
parser.add_argument("-P", "--PurviewAccountName", help = "PurviewAccountName")
parser.add_argument("-S", "--SearchKeyword", help = "SearchKeyword")
parser.add_argument("-E", "--ExcelOutPath", help = "ExcelOutPath")
parser.add_argument("-R", "--RestAPILimit", help = "RestAPILimit")
args = parser.parse_args()
if args.PurviewAccountName is not None:
PurviewAccountName = args.PurviewAccountName
if args.SearchKeyword is not None:
PurviewSearchKeyWords = args.SearchKeyword
if args.ExcelOutPath is not None:
PurviewExcelOutputPath = args.ExcelOutPath
if args.RestAPILimit is not None:
PurviewRestAPILimit = args.RestAPILimite
#################################################### Validate Inputs #####################################
if len(KeyVaultURI) == 0 :
print('Please configure the KeyVaultURI in the script')
sys.exit()
if len(KeyVaultNameForPurviewAppSPNID) == 0 :
print('Please configure the KeyVaultNameForPurviewAppSPNID in the script')
sys.exit()
if len(KeyVaultNameForPurviewAppSPNSecret) == 0 :
print('Please configure the KeyVaultNameForPurviewAppSPNSecret in the script')
sys.exit()
if len(PurviewTenantID) == 0 :
print('Please configure the PurviewTenantID in the script')
sys.exit()
if len(PurviewAccountName) == 0 :
print('Please input the Purview Account Name')
sys.exit()
try:
#################################################### Get SPN ID & Secret from Key Valut #####################################
try :
credential = DefaultAzureCredential()
except Exception as e:
ResultOutput = 'Error Occured :' + str(e)
print(ResultOutput)
client = SecretClient(vault_url=KeyVaultURI, credential=credential)
PurviewAppSPNID = client.get_secret(KeyVaultNameForPurviewAppSPNID).value
PurviewAppSPNSecret = client.get_secret(KeyVaultNameForPurviewAppSPNSecret).value
#################################################### Get Oath Token #########################################################
url = "https://login.microsoftonline.com/" + PurviewTenantID + "/oauth2/token"
payload="grant_type=client_credentials&client_id=" + PurviewAppSPNID + " &client_secret=" + PurviewAppSPNSecret + "&resource=73c2949e-da2d-457a-9607-fcc665198967"
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
OathTokenresponse = requests.request("POST", url, headers=headers, data=payload)
OathTokenresponseJSON = OathTokenresponse.json()
OathBearerToken = OathTokenresponseJSON["access_token"]
print("")
print("")
print("")
#################################################### Create excel and write the header ################################################
if os.path.exists("PurviewOutputExcel.xlsx"):
os.remove("PurviewOutputExcel.xlsx")
if len(PurviewExcelOutputPath) == 0 :
PurviewExcelOutputPath = os.path.dirname(os.path.realpath(__file__))
xlPath = PurviewExcelOutputPath + '\\PurviewOutputExcel.xlsx'
xlWorkBook = xlsxwriter.Workbook(xlPath)
xlWorkBookHeaderFormat = xlWorkBook.add_format()
xlWorkBookHeaderFormat.set_bold()
xlWorkBookHeaderFormat.set_font_color('white')
xlWorkBookHeaderFormat.set_bg_color('#6495ed')
xlAssetWorkSheet = xlWorkBook.add_worksheet(name='PurviewAssets')
xlAssetRowheader =['ID','QualifiedName','Name','Description','EntityType','AssetType','GlossaryTerm','Contact','Owner','Typename']
xlAssetRow = 0
xlAssetCol = 0
xlAssetWorkSheet.write_row(xlAssetRow,xlAssetCol,tuple(xlAssetRowheader),xlWorkBookHeaderFormat)
xlAssetWorkSheet.set_column('A:K', 20)
xlAssetColumnSheet = xlWorkBook.add_worksheet(name='PurviewColumns')
xlAssetColumnRowheader =['TableGuid','TableName','ColumnGuid','QualifiedName','Name','DataType','Length','Precision','Scale','Description','GlossaryTerm','Classification']
xlAssetColumnRow = 0
xlAssetColumnCol = 0
xlAssetColumnSheet.write_row(xlAssetColumnRow,xlAssetColumnCol,tuple(xlAssetColumnRowheader),xlWorkBookHeaderFormat)
xlAssetColumnSheet.set_column('A:L', 20)
#################################################### Loop for offset ################################################
PurviewSearchCount = 0
PurviewRestAPIOffset = 0
while PurviewRestAPIOffset <= PurviewSearchCount:
#################################################### Connect to Purview & Fire REst API ################################################
PurviewURL = "https://" + PurviewAccountName + ".catalog.purview.azure.com"
PurviewRestURL = PurviewURL + "/api/atlas/v2/search/advanced"
payload=("{\r\n \"keywords\": \"" + PurviewSearchKeyWords + "\",\r\n \"offset\": \"" + str(PurviewRestAPIOffset) + "\",\r\n \"limit\": \""+ str(PurviewRestAPILimit) + "\"\r\n}")
headers ={}
headers ['Authorization'] = "Bearer " + OathBearerToken
headers ['Content-Type'] = "application/json"
response = requests.request("POST", PurviewRestURL, headers=headers, data=payload)
PurviewSearchresponseJSON = response.json()
if PurviewSearchCount == 0 :
PurviewSearchCount = PurviewSearchresponseJSON["@search.count"]
if PurviewSearchCount == 0 :
ResultOutput = 'No results found for the keyword'
print(ResultOutput)
sys.exit()
else :
ResultOutput = str(PurviewSearchCount) + ' results found for the keyword'
print(ResultOutput)
ResultOutput = 'Processing ' + str(PurviewRestAPIOffset) + '-' + str(PurviewRestAPIOffset + PurviewRestAPILimit) + ' of ' + str(PurviewSearchCount)
print(ResultOutput)
######################################################################################################################################################
# Write Row by Row to the file
######################################################################################################################################################
PurviewAssetGuids = ""
Name = ""
for PurviewAssetProperties in PurviewSearchresponseJSON["value"] :
Id = PurviewAssetProperties["id"]
Name = Name + PurviewAssetProperties["name"] + ","
QualifiedName = PurviewAssetProperties["qualifiedName"]
# print (Id + " == " + QualifiedName)
Name = PurviewAssetProperties["name"]
Description = PurviewAssetProperties["description"]
Owner = PurviewAssetProperties["owner"]
EntityType = PurviewAssetProperties["entityType"]
#print(PurviewColumnAssetType.count(EntityType.upper()))
#if not EntityType.upper().startswith("ADF_") :
if PurviewColumnAssetType.count(EntityType.upper()) !=0 :
PurviewAssetGuids = PurviewAssetGuids + Id + "&guid="
ArraySeperator =','
Classification = ArraySeperator.join(PurviewAssetProperties["classification"])
if len(PurviewAssetProperties["assetType"]) > 0 :
AssetType = PurviewAssetProperties["assetType"][0]
else :
AssetType = ""
GlossaryTerm = ""
for AssetTerm in PurviewAssetProperties["term"]:
GlossaryTerm = GlossaryTerm + AssetTerm["name"] + ","
GlossaryTerm = GlossaryTerm[:-1]
Contact = ""
# for Contact in PurviewAssetProperties["term"]:
# ContactExpert = Contact[""]
xlAssetDetailsRow =[Id,QualifiedName,Name,Description,EntityType,AssetType,GlossaryTerm,Contact,Owner]
xlAssetRow += 1
xlAssetWorkSheet.write_row(xlAssetRow,xlAssetColumnCol,tuple(xlAssetDetailsRow))
PurviewAssetGuids = PurviewAssetGuids[:-6]
if len(PurviewAssetGuids) != 0 :
PurviewRestURL = PurviewURL + "/api/atlas/v2/entity/bulk?guid=" + PurviewAssetGuids
headers ={}
headers ['Authorization'] = "Bearer " + OathBearerToken
headers ['Content-Type'] = "application/json"
payload=("{}")
RetryCount = 0
PurviewrestAPIStatusCode = 200
while RetryCount <= 3 :
response = requests.request("GET", PurviewRestURL, headers=headers, data=payload)
PurviewrestAPIStatusCode = response.status_code
if response.status_code == 200 :
RetryCount = 0
break
else :
# print(Name)
# print(PurviewRestURL)
# print(ResultOutput)
RetryCount += 1
ResultOutput = "Rest API call returned status code " + str(PurviewrestAPIStatusCode) + '.Running retry attempt ' + str(RetryCount)
print(ResultOutput)
if RetryCount >= 3 :
xlWorkBook.close()
ResultOutput = "Error : Three retry attempts failed for the API. Please change the filter to retrieve less number of output or modify the PurviewRestAPILimit parameter to reduce the limit"
print(ResultOutput)
sys.exit()
else :
PurviewGuidSearchresponseJSON = response.json()
for AssetColumnGuid in PurviewGuidSearchresponseJSON["referredEntities"]:
#print(json.dumps(PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['relationshipAttributes']))
Typename = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['typeName']
if "COLUMN" in Typename.upper() :
TableGuid = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['relationshipAttributes']['table']['guid']
TableName = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['relationshipAttributes']['table']['displayText']
ColumnGuid = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['guid']
QualifiedName = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["qualifiedName"]
Name = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["name"]
DataType = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["data_type"]
Length = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["length"]
Precision = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["precision"]
Scale = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["scale"]
Description = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['attributes']["description"]
Meanings = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['relationshipAttributes']['meanings']
GlossaryTerm = ""
for MeaningsProperties in Meanings:
GlossaryTerm = GlossaryTerm + MeaningsProperties["displayText"] + ","
GlossaryTerm = GlossaryTerm[:-1]
ClassificationAssociated =""
#print(PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid])
if 'classifications' in PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid] :
Classifications = PurviewGuidSearchresponseJSON["referredEntities"][AssetColumnGuid]['classifications']
for Classification in Classifications:
ClassificationAssociated = ClassificationAssociated + Classification["typeName"] + ","
ClassificationAssociated = ClassificationAssociated[:-1]
xlAssetColumnRowDetails =[TableGuid,TableName,ColumnGuid,QualifiedName,Name,DataType,Length,Precision,Scale,Description,GlossaryTerm,ClassificationAssociated]
xlAssetColumnRow += 1
xlAssetColumnSheet.write_row(xlAssetColumnRow,xlAssetColumnCol,tuple(xlAssetColumnRowDetails))
PurviewRestAPIOffset = PurviewRestAPIOffset + PurviewRestAPILimit
xlWorkBook.close()
ResultOutput = 'Processing Completed.'
print(ResultOutput)
except Exception as e:
try :
xlWorkBook.close()
except :
test = 1
ResultOutput = 'Error Occured :' + str(e)
print(ResultOutput)