1
1
import os
2
2
import argparse
3
+ import streamlit as st
3
4
from dotenv import load_dotenv
4
5
from azure .identity import EnvironmentCredential
5
6
from azure .mgmt .resource .policy .v2022_06_01 import PolicyClient
@@ -17,6 +18,12 @@ class PolicyAssignmentList(BaseModel):
17
18
policy_definition_id : str
18
19
scope : str
19
20
21
+ def get_policies (subscription_id : str ):
22
+ # Retrieve all policies in the subscription
23
+ credential = EnvironmentCredential ()
24
+ client = PolicyClient (credential = credential , subscription_id = subscription_id )
25
+ policy_assignment_list = client .policy_assignments .list ()
26
+ return [policy .display_name for policy in policy_assignment_list ]
20
27
21
28
def extract_policy_data (subscription_id : str ) -> PolicyAssignmentList :
22
29
"""
@@ -57,6 +64,7 @@ def verify_policy_is_available(subscription_id:str, policy_name: str):
57
64
for policy in policy_assignment_list :
58
65
if policy_name == policy .display_name :
59
66
print (f"Found policy assignment for '{ policy_name } ' in scope { subscription_id } " )
67
+ st .write (f"Found policy assignment for '{ policy_name } ' in scope { subscription_id } " )
60
68
# convert policy obj to dict
61
69
policy_to_be_exempted = policy .__dict__
62
70
break # Exit the loop once the policy is found
@@ -80,13 +88,13 @@ def create_exemption_for_policy(subscription_id: str, policy_name:str, expires_a
80
88
policy_to_be_exempted = verify_policy_is_available (subscription_id = subscription_id , policy_name = policy_name )
81
89
82
90
scope = f"/subscriptions/{ subscription_id } "
83
-
91
+ st . write ( f"Scope of exemption is : /subscriptions/ { subscription_id } " )
84
92
policy_exemption_name = f'exemption for { policy_name } ' # <policy name> - <yyyy-<month short form>-<day> HH:MM")>
85
93
print (f'Policy Exemption name will be "{ policy_exemption_name } "' )
94
+ st .write (f'Policy Exemption name will be "{ policy_exemption_name } "' )
86
95
expiry_date = calculate_expiry (expires_after = expires_after , unit = unit )
87
96
policy_exemption_description = f'exemption for { policy_name } '
88
97
89
-
90
98
try :
91
99
92
100
parameters = PolicyExemption (
@@ -99,6 +107,7 @@ def create_exemption_for_policy(subscription_id: str, policy_name:str, expires_a
99
107
100
108
exemption = client .policy_exemptions .create_or_update (scope = scope ,policy_exemption_name = policy_exemption_name , parameters = parameters )
101
109
print ("Policy exemption created or updated successfully." )
110
+ st .write (f"Policy exemption created or updated successfully. Policy Exemption will expire at { expiry_date } " )
102
111
print (f'Policy Exemption will expire at { expiry_date } ' )
103
112
104
113
except HttpResponseError as err :
0 commit comments