33import json
44from dotenv import load_dotenv
55from azure .identity import DefaultAzureCredential
6- from azure .mgmt .policyinsights import PolicyInsightsClient
76from azure .core .exceptions import HttpResponseError
87from azure .mgmt .resource import PolicyClient
98from typing import List , Dict , Any
@@ -17,33 +16,34 @@ def list_azure_policy_in_a_subscription_scope(subscription_id:str):
1716 """
1817 try :
1918 credential = DefaultAzureCredential ()
20- policy_insights_client = PolicyInsightsClient (credential , subscription_id = {subscription_id })
21- policy_assignments = policy_insights_client .policy_states .list_query_results_for_subscription (policy_states_resource = 'latest' ,subscription_id = subscription_id )
19+ # policy_insights_client = PolicyInsightsClient(credential, subscription_id=subscription_id)
20+ policy_client = PolicyClient (credential = credential ,subscription_id = subscription_id )
21+ policy_assignments = policy_client .policy_assignments .list ()
2222 policy_assignments_list = []
23+ i = 0
2324 for assignment in policy_assignments :
24- print (f"Policy Assignment ID: { assignment .policy_assignment_id } " )
25- print (f"Policy Assignment Name: { assignment .policy_assignment_name } " )
26- print (f"Policy Assignment Scope: { assignment .policy_assignment_scope } " )
25+ print (f'Policy no #{ i } ' )
26+ print (f"Policy Assignment ID: { assignment .id } " )
27+ print (f"Policy Assignment Name: { assignment .display_name } " )
28+ print (f"Policy Assignment Scope: { assignment .scope } " )
2729 print (f"Policy Definition ID: { assignment .policy_definition_id } " )
28- print (f"Policy Definition Name: { assignment .policy_definition_name } " )
29- print (f"Policy Assignment Created On: { assignment .timestamp .strftime ('%Y-%m-%d %H:%M:%S' )} " )
30- print ("------------------------------" )
30+ print (f"Policy Assignment Created On: { assignment .metadata ['createdOn' ]} " )
31+ print ("++++++++++++++++++++++++++++++++++" )
3132 assignment_dict = {
32- "policy_assignment_id" : assignment .policy_assignment_id ,
33- "policy_assignment_name" : assignment .policy_assignment_name ,
34- "policy_assignment_scope" : assignment .policy_assignment_scope ,
33+ "policy_assignment_id" : assignment .id ,
34+ "policy_assignment_name" : assignment .display_name ,
35+ "policy_assignment_scope" : assignment .scope ,
3536 "policy_definition_id" : assignment .policy_definition_id ,
36- "policy_definition_name" : assignment .policy_definition_name ,
37- "policy_assignment_created_on" : assignment .timestamp .strftime ('%Y-%m-%d %H:%M:%S' )
37+ "policy_assignment_created_on" : assignment .metadata ['createdOn' ]
3838 }
39+ i = i + 1
3940 policy_assignments_list .append (assignment_dict )
40- file_name = f'azure_policy_assignment_{ subscription_id } .json'
41- print (file_name )
42- # Assuming policy_assignments_list is the list of dictionaries
43- with open (file_name , 'w' ) as json_file :
44- json .dump (policy_assignments_list , json_file , indent = 4 )
45- print (f"Policy assignments successfully retrieved and saved to { file_name } ." )
46-
41+ file_name = f'azure_policy_assignment_{ subscription_id } .json'
42+ print (file_name )
43+ # Assuming policy_assignments_list is the list of dictionaries
44+ with open (file_name , 'w' ) as json_file :
45+ json .dump (policy_assignments_list , json_file , indent = 4 )
46+ print (f"Policy assignments successfully retrieved and saved to { file_name } ." )
4747 return policy_assignments_list
4848 except HttpResponseError as ex :
4949 print (f"Failed to retrieve policy assignments. Error message: { ex .message } " )
@@ -94,7 +94,9 @@ def main():
9494 print (f'Subscription id of { subscription_name } is : { subscription_id } ' )
9595 os .environ ['subscription_id' ] = subscription_id
9696 policy_assignments_list = list_azure_policy_in_a_subscription_scope (subscription_id = subscription_id )
97+ print (f'Total number of policies assigned on { subscription_name } : { len (policy_assignments_list )} ' )
9798 policy_name , policy_assignment_scope = validation_of_policy_name (policy_name = policy_name , policy_assignments_list = policy_assignments_list )
99+ print (f'Policy name and policy assignment scope are : { policy_name } & { policy_assignment_scope } ' )
98100 if policy_name is not None :
99101 print (f'Removing policy { policy_name } on the scope { policy_assignment_scope } ' )
100102 remove_azure_policy_from_subscription (credential = credential ,subscription_id = subscription_id , policy_name = policy_name , scope = policy_assignment_scope )
0 commit comments