3
3
import json
4
4
from dotenv import load_dotenv
5
5
from azure .identity import DefaultAzureCredential
6
- from azure .mgmt .policyinsights import PolicyInsightsClient
7
6
from azure .core .exceptions import HttpResponseError
8
7
from azure .mgmt .resource import PolicyClient
9
8
from typing import List , Dict , Any
@@ -17,33 +16,34 @@ def list_azure_policy_in_a_subscription_scope(subscription_id:str):
17
16
"""
18
17
try :
19
18
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 ()
22
22
policy_assignments_list = []
23
+ i = 0
23
24
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 } " )
27
29
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 ("++++++++++++++++++++++++++++++++++" )
31
32
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 ,
35
36
"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' ]
38
38
}
39
+ i = i + 1
39
40
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 } ." )
47
47
return policy_assignments_list
48
48
except HttpResponseError as ex :
49
49
print (f"Failed to retrieve policy assignments. Error message: { ex .message } " )
@@ -94,7 +94,9 @@ def main():
94
94
print (f'Subscription id of { subscription_name } is : { subscription_id } ' )
95
95
os .environ ['subscription_id' ] = subscription_id
96
96
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 )} ' )
97
98
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 } ' )
98
100
if policy_name is not None :
99
101
print (f'Removing policy { policy_name } on the scope { policy_assignment_scope } ' )
100
102
remove_azure_policy_from_subscription (credential = credential ,subscription_id = subscription_id , policy_name = policy_name , scope = policy_assignment_scope )
0 commit comments