forked from microsoft/AI-for-Operations-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_app.py
568 lines (472 loc) · 24.9 KB
/
function_app.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import os
import logging
import docx
import csv
import requests
import collections.abc
import azure.functions as func
from azure.identity import ManagedIdentityCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.subscription import SubscriptionClient
# Configurazione logging avanzato
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Endpoint OpenAI
QUESTION_ENDPOINT = os.getenv("QUESTION_ENDPOINT")
logging.info(f"Question endpoint set to: {QUESTION_ENDPOINT}")
# Autenticazione tramite identità gestita della Function App
credential = ManagedIdentityCredential()
subscription_client = SubscriptionClient(credential)
tokenai = credential.get_token("https://cognitiveservices.azure.com/.default")
# Funzione per appiattire un dizionario annidato in una struttura piana
def flatten_dict(d, parent_key='', sep='_'):
#logging.info(f"start function flatten_dict")
items = []
for k, v in d.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
if isinstance(v, collections.abc.MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
elif isinstance(v, list):
for i, item in enumerate(v):
if isinstance(item, collections.abc.MutableMapping):
items.extend(flatten_dict(item, f"{new_key}{sep}{i}", sep=sep).items())
else:
items.append((f"{new_key}{sep}{i}", item))
else:
items.append((new_key, v))
return dict(items)
# Funzione per ottenere tutte le risorse con un determinato tag in una sottoscrizione
def get_resources_by_tag_in_subscription(subscription_id, tag_key, tag_value):
logging.info(f"start get_resources_by_tag_in_subscription")
resource_client = ResourceManagementClient(credential, subscription_id)
tag_filter = f"tagName eq '{tag_key}' and tagValue eq '{tag_value}'"
logging.info(f"Cercando risorse con il tag: {tag_key}={tag_value} nella sottoscrizione {subscription_id}")
resources = resource_client.resources.list(filter=tag_filter)
resources_list = []
for resource in resources:
resource_details = {
'name': resource.name,
'id': resource.id,
'location': resource.location,
'type': resource.type,
'tags': resource.tags
}
resources_list.append(resource_details)
logging.info(f"Trovata risorsa: {resource.name} - {resource.type}")
logging.info(str(resources_list))
return resources_list
# Funzione per ottenere i Resource Groups con un determinato tag in una sottoscrizione
def get_resource_groups_by_tag_in_subscription(subscription_id, tag_key, tag_value):
logging.info(f"start get_resource_groups_by_tag_in_subscription")
resource_client = ResourceManagementClient(credential, subscription_id)
logging.info(f"Cercando resource groups con il tag: {tag_key}={tag_value} nella sottoscrizione {subscription_id}")
resource_groups = resource_client.resource_groups.list()
matching_resource_groups = []
for rg in resource_groups:
if rg.tags and tag_key in rg.tags and rg.tags[tag_key] == tag_value:
logging.info(f"Trovato resource group: {rg.name}")
matching_resource_groups.append(rg)
logging.info(str(matching_resource_groups))
return matching_resource_groups
# Funzione per ottenere tutte le risorse con un determinato tag da tutte le sottoscrizioni
def get_all_resources(tag_key, tag_value):
logging.info(f"start get_all_resources")
all_resources = []
added_resource_ids = set()
# Elenca tutte le sottoscrizioni a cui hai accesso
for subscription in subscription_client.subscriptions.list():
subscription_id = subscription.subscription_id
logging.info(f"Processando la sottoscrizione: {subscription_id}")
# Cerca le risorse con il tag specificato in questa sottoscrizione
resources = get_resources_by_tag_in_subscription(subscription_id, tag_key, tag_value)
for resource in resources:
logging.info(f"Analizzando risorsa {resource}")
if resource['id'] not in added_resource_ids:
all_resources.append(resource)
added_resource_ids.add(resource['id'])
# Cerca i resource group con il tag specificato in questa sottoscrizione
resource_groups = get_resource_groups_by_tag_in_subscription(subscription_id, tag_key, tag_value)
for rg in resource_groups:
logging.info(f"Analizzando {rg}")
# Cerca risorse all'interno di ciascun resource group
rg_resources = get_resources_in_resource_group_in_subscription(subscription_id, rg.name)
for resource in rg_resources:
if resource['id'] not in added_resource_ids:
all_resources.append(resource)
added_resource_ids.add(resource['id'])
logging.info(str(all_resources))
return all_resources
# Funzione per ottenere le risorse all'interno di un Resource Group in una sottoscrizione
def get_resources_in_resource_group_in_subscription(subscription_id, resource_group_name):
logging.info(f"start get_resources_in_resource_group_in_subscription")
resource_client = ResourceManagementClient(credential, subscription_id)
logging.info(f"Recuperando risorse dal resource group: {resource_group_name} nella sottoscrizione {subscription_id}")
resources = resource_client.resources.list_by_resource_group(resource_group_name)
resources_list = []
for resource in resources:
resource_details = {
'name': resource.name,
'id': resource.id,
'location': resource.location,
'type': resource.type,
'tags': resource.tags
}
resources_list.append(resource_details)
logging.info(f"Trovata risorsa nel resource group {resource_group_name}: {resource.name} - {resource.type}")
logging.info(str(resources_list))
return resources_list
# Funzione per ottenere l'API più recente per una risorsa specifica
def get_latest_api_version(resource_client, resource_type):
logging.info(f"start get_latest_api_version")
provider_namespace, resource_type_name = resource_type.split('/', 1)
provider = resource_client.providers.get(provider_namespace)
resource_type_info = next(
(t for t in provider.resource_types if t.resource_type == resource_type_name), None
)
if resource_type_info:
return sorted(resource_type_info.api_versions, reverse=True)[0]
return None
# Funzione per ottenere i metadati completi di una risorsa specifica
def get_resource_metadata(resource_client, resource):
logging.info(f"start get_resource_metadata")
resource_type = resource['type']
api_version = get_latest_api_version(resource_client, resource_type)
if api_version:
logging.info(f"Usando l'API version: {api_version} per la risorsa {resource['name']}")
resource_metadata = resource_client.resources.get_by_id(resource['id'], api_version=api_version)
return resource_metadata
else:
logging.info(f"Impossibile trovare l'API per la risorsa {resource['name']} con tipo {resource['type']}")
return None
# Funzione per generare la overview del workload leggendo il file CSV
def generate_workload_overview():
logging.info(f"start generate_workload_overview")
resources_info = []
with open("/tmp/resources_with_expanded_metadata.csv", 'r', encoding='utf-8') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
resources_info.append(row)
resources_str = "\n".join([
f"Name: {row['Name']}, Type: {row['Type']}, Location: {row['Location']}, Resource Group: {row['Resource ID'].split('/')[4]}"
for row in resources_info])
payload = {
"messages": [
{"role": "system",
"content": "You are an expert Azure Architect and Documentation Writer. Your job is to create a clear and detailed overview of an Azure workload."},
{"role": "user",
"content": f"Here is the list of resources in the workload:\n{resources_str}.\nGenerate a detailed and human-readable overview. Explain at the end how the workload is splitted, so if is multi-regional or not and so on."}
],
"temperature": 0.7,
"max_tokens": 16000
}
response = requests.post(
QUESTION_ENDPOINT,
headers={'content-type': 'application/json', 'Authorization': 'Bearer ' + tokenai.token},
json=payload
)
response.raise_for_status()
response_from_copilot = response.json()['choices'][0]['message']['content'].strip()
logging.info(str(response_from_copilot))
return response_from_copilot
# Funzione per generare la documentazione con OpenAI
def generate_infra_config(metadata_list):
logging.info("Inizio della generazione della configurazione dell'infrastruttura.")
document_content = ""
for index, metadata in enumerate(metadata_list):
logging.debug(f"Elaborazione del metadato {index + 1}/{len(metadata_list)}: {metadata}")
metadata_str = str(metadata)
payload = {
"messages": [
{"role": "system", "content": "You are an expert Azure Architect and Documentation Writer."},
{"role": "user", "content": f"Here is the metadata for an Azure resource: \n{metadata_str}.\nPlease generate a detailed and human-readable documentation."}
],
"temperature": 0.7,
"max_tokens": 16000
}
try:
# Invio richiesta a OpenAI
logging.debug("Invio della richiesta a OpenAI con payload.")
response = requests.post(
QUESTION_ENDPOINT,
headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {tokenai.token}'},
json=payload
)
# Gestione della risposta
response.raise_for_status()
response_data = response.json()
response_from_copilot = response_data['choices'][0]['message']['content'].strip()
logging.info(f"Risposta da OpenAI per il metadato {index + 1}: {response_from_copilot}")
except requests.exceptions.RequestException as e:
logging.error(f"Errore nella richiesta a OpenAI per il metadato {index + 1}: {e}")
continue # Salta al prossimo elemento se c'è un errore
# Revisione del contenuto tramite ArchitecturalReviewer e DocCreator
response_from_DocCreator = response_from_copilot
previousdoc = response_from_DocCreator
for i in range(3):
try:
ArchitecturalReviewer_response = ArchitecturalReviewer(response_from_DocCreator)
logging.debug(f"Risposta ArchitecturalReviewer (ciclo {i + 1}): {ArchitecturalReviewer_response}")
response_from_DocCreator, previousdoc = DocCreator(ArchitecturalReviewer_response, previousdoc)
logging.debug(f"Documento revisionato (ciclo {i + 1}): {response_from_DocCreator}")
except Exception as e:
logging.error(f"Errore durante la revisione della documentazione (ciclo {i + 1}): {e}")
break # Interrompe la revisione in caso di errore
# Aggiunge la documentazione revisionata al contenuto finale
document_content += response_from_DocCreator + "\n\n"
# Scrittura del contenuto finale in un file
try:
logging.info("Scrittura del documento finale nel file architecture.txt.")
with open("/tmp/architecture.txt", "a", encoding="utf-8") as architecturefile:
architecturefile.write(document_content)
logging.info("Documento architecture.txt generato con successo.")
except IOError as e:
logging.error(f"Errore durante la scrittura del file architecture.txt: {e}")
return document_content
logging.info(f"start generate_infra_config")
document_content = ""
for metadata in metadata_list:
metadata_str = str(metadata)
payload = {
"messages": [
{"role": "system",
"content": "You are an expert Azure Architect and Documentation Writer."},
{"role": "user",
"content": f"Here is the metadata for an Azure resource: \n{metadata_str}.\nPlease generate a detailed and human-readable documentation."}
],
"temperature": 0.7,
"max_tokens": 16000
}
response = requests.post(
QUESTION_ENDPOINT,
headers={'content-type': 'application/json', 'Authorization': 'Bearer ' + tokenai.token},
json=payload
)
response.raise_for_status()
response_from_copilot = response.json()['choices'][0]['message']['content'].strip()
logging.info(str(response_from_copilot))
# Passa il contenuto attraverso 3 cicli di revisione con ArchitecturalReviewer e DocCreator
response_from_DocCreator = response_from_copilot
previousdoc = response_from_DocCreator
for i in range(3):
ArchitecturalReviewer_response = ArchitecturalReviewer(response_from_DocCreator)
response_from_DocCreator, previousdoc = DocCreator(ArchitecturalReviewer_response, previousdoc)
# Aggiunge la documentazione revisionata al contenuto finale
document_content += response_from_DocCreator + "\n\n"
with open("architecture.txt", "a", encoding="utf-8") as architecturefile:
architecturefile.write(document_content)
return document_content
# Funzione per convertire il file txt in docx e aggiungere l'overview
def txt_to_docx():
logging.info(f"start txt_to_docx")
logging.info("Generazione dei file in corso...")
doc = docx.Document()
# Genera l'overview del workload
overview = generate_workload_overview()
# Aggiungi "Workload Overview" come Titolo 1
doc.add_heading("Workload Overview", level=1)
doc.add_paragraph(overview)
# Aggiungi un'interruzione di pagina per iniziare i dettagli su una nuova pagina
doc.add_page_break()
# Aggiungi "Workload Details" come Titolo 1
doc.add_heading("Workload Details", level=1)
# Aggiungi il contenuto del file architecture.txt
with open("architecture.txt", 'r', encoding='utf-8', errors='ignore') as openfile:
line = openfile.read()
doc.add_paragraph(line)
# Salva il documento Word
doc.save("Output.docx")
logging.info("Il file Output.docx è stato creato con successo.")
def cleanup_files():
logging.info(f"start cleanup_files")
logging.info("Cleaning up temporary files")
if os.path.exists("architecture.txt"):
os.remove("architecture.txt")
logging.debug("architecture.txt deleted")
if os.path.exists("resources_with_expanded_metadata.csv"):
os.remove("resources_with_expanded_metadata.csv")
logging.debug("resources_with_expanded_metadata.csv")
if os.path.exists("Output.docx"):
os.remove("Output.docx")
logging.debug("Output.docx deleted")
if os.path.exists("/tmp/architecture.txt"):
os.remove("/tmp/architecture.txt")
logging.debug("architecture.txt deleted")
if os.path.exists("/tmp/resources_with_expanded_metadata.csv"):
os.remove("/tmp/resources_with_expanded_metadata.csv")
logging.debug("resources_with_expanded_metadata.csv")
if os.path.exists("/tmp/Output.docx"):
os.remove("/tmp/Output.docx")
logging.debug("Output.docx deleted")
logging.info("Temporary files cleanup completed")
def save_resources_with_expanded_metadata_to_csv(resources, metadata_list):
logging.info("Start save_resources_with_expanded_metadata_to_csv")
all_keys = set()
# Raccogli tutte le chiavi disponibili nei metadati, incluse quelle dei dizionari annidati
try:
for metadata in metadata_list:
flat_metadata = flatten_dict(metadata.__dict__) # Appiattiamo il dizionario dei metadati
logging.debug(f"Flat metadata for item: {flat_metadata}")
all_keys.update(flat_metadata.keys())
except AttributeError as e:
logging.error(f"Errore durante l'appiattimento dei metadati: {e}")
return
except Exception as e:
logging.error(f"Errore sconosciuto durante la raccolta delle chiavi: {e}")
return
# Trasforma il set delle chiavi in una lista ordinata per mantenere ordine nelle colonne
all_keys = list(all_keys)
logging.info(f"All unique metadata keys: {all_keys}")
# Scrivi le risorse e i loro metadati in un file CSV
try:
with open("/tmp/resources_with_expanded_metadata.csv", mode="w", newline='', encoding="utf-8") as csv_file:
# Creiamo l'header del CSV con tutte le chiavi uniche
fieldnames = ['Name', 'Resource ID', 'Location', 'Type', 'Tags'] + all_keys
logging.debug(f"Fieldnames for CSV: {fieldnames}")
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
# Scriviamo l'intestazione
writer.writeheader()
logging.info("CSV header written successfully.")
# Scrivi i dettagli di ogni risorsa e i suoi metadati
for resource, metadata in zip(resources, metadata_list):
logging.debug(f"Processing resource: {resource['name']}")
# Crea la riga iniziale per la risorsa
resource_row = {
'Name': resource['name'],
'Resource ID': resource['id'],
'Location': resource['location'],
'Type': resource['type'],
'Tags': resource['tags']
}
# Appiattiamo i metadati annidati prima di scriverli nel CSV
try:
flat_metadata = flatten_dict(metadata.__dict__)
logging.debug(f"Flat metadata for resource {resource['name']}: {flat_metadata}")
except AttributeError as e:
logging.error(f"Errore durante l'appiattimento dei metadati per la risorsa {resource['name']}: {e}")
continue
except Exception as e:
logging.error(f"Errore sconosciuto durante l'appiattimento dei metadati per {resource['name']}: {e}")
continue
# Aggiungi i metadati alle colonne, gestendo eventuali valori mancanti
for key in all_keys:
resource_row[key] = flat_metadata.get(key, 'N/A') # Usa 'N/A' per valori mancanti
# Scrivi la riga nel CSV
writer.writerow(resource_row)
logging.debug(f"Row written for resource {resource['name']}")
except IOError as e:
logging.error(f"Errore durante la creazione del file CSV: {e}")
except Exception as e:
logging.error(f"Errore sconosciuto durante la scrittura nel file CSV: {e}")
logging.info("Il file resources_with_expanded_metadata.csv è stato creato con successo.")
logging.info(f"start save_resources_with_expanded_metadata_to_csv")
all_keys = set()
# Raccogli tutte le chiavi disponibili nei metadati, incluse quelle dei dizionari annidati
for metadata in metadata_list:
flat_metadata = flatten_dict(metadata.__dict__) # Appiattiamo il dizionario dei metadati
all_keys.update(flat_metadata.keys())
# Trasforma il set delle chiavi in una lista ordinata per mantenere ordine nelle colonne
all_keys = list(all_keys)
logging.info(f"all_keys: {all_keys}")
# Scrivi le risorse e i loro metadati in un file CSV
with open("/tmp/resources_with_expanded_metadata.csv", mode="w", newline='', encoding="utf-8") as csv_file:
# Creiamo l'header del CSV con tutte le chiavi uniche
fieldnames = ['Name', 'Resource ID', 'Location', 'Type', 'Tags'] + all_keys
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
# Scriviamo l'intestazione
writer.writeheader()
# Scrivi i dettagli di ogni risorsa e i suoi metadati
for resource, metadata in zip(resources, metadata_list):
resource_row = {
'Name': resource['name'],
'Resource ID': resource['id'],
'Location': resource['location'],
'Type': resource['type'],
'Tags': resource['tags']
}
# Appiattiamo i metadati annidati prima di scriverli nel CSV
flat_metadata = flatten_dict(metadata.__dict__)
# Aggiungi i metadati alle colonne, gestendo eventuali valori mancanti
for key in all_keys:
resource_row[key] = flat_metadata.get(key, 'N/A') # Usa 'N/A' per valori mancanti
# Scrivi la riga nel CSV
writer.writerow(resource_row)
logging.info("Il file resources_with_expanded_metadata.csv è stato creato con successo.")
def ArchitecturalReviewer(response_from_DocCreator):
logging.info(f"start ArchitecturalReviewer")
payload = {
"messages": [
{"role": "system",
"content": "You are the Azure architectural reviewer of the enterprise. Our team is creating documentation on our Azure architecture for the existing dedicated workload. The team will pass a specific piece of documentation to you each time. Make suggestions on how to make it more user-friendly the part without suggesting to add graph, diagrams, table of contents, feedback and so on. Suggest how to write the doc in a user-friendly readable way."},
{"role": "user",
"content": f"{response_from_DocCreator}"}
],
"temperature": 0.7,
"max_tokens": 16000
}
response = requests.post(
QUESTION_ENDPOINT,
headers={'content-type': 'application/json', 'Authorization': 'Bearer ' + tokenai.token},
json=payload
)
response.raise_for_status()
ArchitecturalReviewer_response = response.json()['choices'][0]['message']['content'].strip()
logging.info(f"Architectural Reviewer Comments: {ArchitecturalReviewer_response}")
return ArchitecturalReviewer_response
def DocCreator(ArchitecturalReviewer_response,previousdoc):
logging.info(f"start DocCreator")
payload = {
"messages": [
{"role": "system",
"content": "You have created a document about your Azure infrastructure related a workload. Your supervisor is reviewing the documentation. Generate a new documentation output based on his suggestions as an output."},
{"role": "user",
"content": f"source:{previousdoc}. Suggestion: {ArchitecturalReviewer_response}"}
],
"temperature": 0.7,
"max_tokens": 16000
}
response = requests.post(
QUESTION_ENDPOINT,
headers={'content-type': 'application/json', 'Authorization': 'Bearer ' + tokenai.token},
json=payload
)
response.raise_for_status()
response_from_DocCreator = response.json()['choices'][0]['message']['content'].strip()
logging.info(f"Doc Creator Repsonse: {response_from_DocCreator}")
return response_from_DocCreator, response_from_DocCreator
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="smartdocs")
def smartdocs(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Received request for smartdocs function")
tag_key = req.params.get("tag_key")
tag_value = req.params.get("tag_value")
if not tag_key or not tag_value:
try:
req_body = req.get_json()
tag_key = tag_key or req_body.get("tag_key")
tag_value = tag_value or req_body.get("tag_value")
except ValueError:
logging.error("Invalid request body")
return func.HttpResponse("Missing 'tag_key' or 'tag_value' parameters.", status_code=400)
if not tag_key or not tag_value:
logging.error("Missing required parameters: 'tag_key' and 'tag_value'")
return func.HttpResponse("Missing 'tag_key' or 'tag_value' parameters.", status_code=400)
all_resources = get_all_resources(tag_key, tag_value)
metadata_list = []
processed_resource_ids = set()
for subscription in subscription_client.subscriptions.list():
subscription_id = subscription.subscription_id
resource_client = ResourceManagementClient(credential, subscription_id)
for resource in all_resources:
if resource['id'] not in processed_resource_ids:
metadata = get_resource_metadata(resource_client, resource)
if metadata:
metadata_list.append(metadata)
processed_resource_ids.add(resource['id'])
save_resources_with_expanded_metadata_to_csv(all_resources, metadata_list)
document_content = generate_infra_config(metadata_list)
with open("architecture.txt", "w", encoding="utf-8") as output_file:
output_file.write(document_content)
logging.info("Architecture text file saved")
txt_to_docx()
cleanup_files()
logging.info("Function smartdocs completed successfully")
return func.HttpResponse("Resource data processed successfully.", status_code=200)