-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathremove_udf_solution.py
More file actions
192 lines (166 loc) · 7.47 KB
/
remove_udf_solution.py
File metadata and controls
192 lines (166 loc) · 7.47 KB
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
from fabric_api import create_fabric_client, FabricApiError
import sys
import os
def get_required_env_var(var_name: str) -> str:
"""Get a required environment variable or exit with error.
Args:
var_name: Name of the environment variable to retrieve
Returns:
Value of the environment variable
Raises:
SystemExit: If the environment variable is not set
"""
value = os.getenv(var_name)
if not value:
print(f"❌ Missing environment variable: {var_name}")
sys.exit(1)
return value
####################
# Variables set up #
####################
SOLUTION_NAME = "Unified_Data_Foundation"
script_dir = os.path.dirname(os.path.abspath(__file__))
# Go up three levels from infra/scripts/fabric to repo root
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(script_dir)))
##############################
# Environment Variable Setup #
##############################
# Load configuration from environment variables
solution_suffix = get_required_env_var("SOLUTION_SUFFIX")
# Use custom workspace name if provided; fall back to auto-generated name.
# 'or' handles both None (unset) and '' (set to empty by CI/CD when var is not configured).
workspace_name = os.getenv("FABRIC_WORKSPACE_NAME") or f"{SOLUTION_NAME}_{solution_suffix}"
workspace_id = os.getenv("FABRIC_WORKSPACE_ID")
if workspace_name and workspace_id:
print("⚠️ WARNING: Both FABRIC_WORKSPACE_NAME and FABRIC_WORKSPACE_ID are set")
print(" Using workspace name and ignoring workspace ID...")
workspace_id = None
print(f"🗑️ Starting {SOLUTION_NAME} workspace removal from Microsoft Fabric")
print(f"📋 Solution suffix: {solution_suffix}")
if workspace_name:
print(f"📋 Target workspace name: {workspace_name}")
else:
print(f"📋 Target workspace ID: {workspace_id}")
print("-" * 60)
##########################
# Clients authentication #
##########################
print("🔐 Authenticating Fabric client...")
# Initialize Fabric API client
try:
fabric_client = create_fabric_client()
print("✅ Fabric API client authenticated successfully")
except Exception as e:
print(f"⚠️ WARNING: Failed to authenticate with Fabric APIs")
print(f" Details: {str(e)}")
print(" Solution: Please ensure you are logged in with Azure CLI: az login")
print(" Exiting gracefully...")
sys.exit(0)
###########################
# Workspace lookup/verify #
###########################
try:
# If workspace name is provided, look it up to get the ID
if workspace_name:
print(f"🔍 Looking up workspace: '{workspace_name}'")
workspaces = fabric_client.list_workspaces()
workspace = next(
(w for w in workspaces if w['displayName'].lower() == workspace_name.lower()), None)
if not workspace:
print(f"⚠️ WARNING: Workspace '{workspace_name}' not found")
print(" Available workspaces:")
for ws in workspaces:
print(f" - {ws['displayName']} (ID: {ws['id']})")
print(" Exiting gracefully...")
sys.exit(0)
workspace_id = workspace['id']
workspace_display_name = workspace['displayName']
print(f"✅ Found workspace: '{workspace_display_name}' (ID: {workspace_id})")
else:
# If workspace ID is provided, verify it exists
print(f"🔍 Verifying workspace ID: '{workspace_id}'")
workspaces = fabric_client.list_workspaces()
workspace = next(
(w for w in workspaces if w['id'].lower() == workspace_id.lower()), None)
if not workspace:
print(f"⚠️ WARNING: Workspace with ID '{workspace_id}' not found")
print(" Available workspaces:")
for ws in workspaces:
print(f" - {ws['displayName']} (ID: {ws['id']})")
print(" Exiting gracefully...")
sys.exit(0)
workspace_display_name = workspace['displayName']
print(f"✅ Found workspace: '{workspace_display_name}' (ID: {workspace_id})")
except FabricApiError as e:
if e.status_code == 401:
print(f"⚠️ WARNING: Unauthorized access to Fabric APIs")
print(" ⚠️ WARNING: Please review your Fabric permissions and licensing:")
print(" 📋 Check these resources:")
print(" • Fabric licenses: https://learn.microsoft.com/en-us/fabric/enterprise/licenses")
print(" • Identity support: https://learn.microsoft.com/en-us/rest/api/fabric/articles/identity-support")
print(" • Create Entra app: https://learn.microsoft.com/en-us/rest/api/fabric/articles/get-started/create-entra-app")
print(" Solution: Ensure you have proper Fabric licensing and permissions")
print(" Exiting gracefully...")
sys.exit(0)
elif e.status_code == 404:
print(f"⚠️ WARNING: Resource not found")
elif e.status_code == 403:
print(f"⚠️ WARNING: Access denied")
print(" Solution: Ensure you have appropriate permissions")
else:
print(f"⚠️ WARNING: Fabric API error")
print(f" Status Code: {e.status_code}")
print(f" Details: {str(e)}")
print(" Exiting gracefully...")
sys.exit(0)
except Exception as e:
print(f"⚠️ WARNING: Unexpected error during workspace lookup: {str(e)}")
print(" Exiting gracefully...")
sys.exit(0)
####################
# Confirmation #
####################
# Proceeding with deletion in unattended mode
print(f"✅ Proceeding with workspace deletion...")
######################
# Workspace deletion #
######################
try:
print(f"🗑️ Deleting workspace: '{workspace_display_name}'")
fabric_client.delete_workspace(workspace_id)
print(f"✅ Workspace '{workspace_display_name}' deleted successfully")
except FabricApiError as e:
if e.status_code == 401:
print(f"⚠️ WARNING: Unauthorized access to Fabric APIs")
print(" ⚠️ WARNING: Please review your Fabric permissions and licensing:")
print(" 📋 Check these resources:")
print(" • Fabric licenses: https://learn.microsoft.com/en-us/fabric/enterprise/licenses")
print(" • Identity support: https://learn.microsoft.com/en-us/rest/api/fabric/articles/identity-support")
print(" • Create Entra app: https://learn.microsoft.com/en-us/rest/api/fabric/articles/get-started/create-entra-app")
print(" Solution: Ensure you have proper Fabric licensing and permissions")
print(" Exiting gracefully...")
sys.exit(0)
elif e.status_code == 404:
print(f"⚠️ WARNING: Workspace not found (may have already been deleted)")
print(" This is typically not an issue during cleanup operations")
elif e.status_code == 403:
print(f"⚠️ WARNING: Access denied")
print(" Solution: Ensure you have Admin permissions on this workspace")
else:
print(f"⚠️ WARNING: Fabric API error")
print(f" Status Code: {e.status_code}")
print(f" Details: {str(e)}")
print(" Exiting gracefully...")
sys.exit(0)
except Exception as e:
print(f"⚠️ WARNING: Unexpected error during workspace deletion: {str(e)}")
print(" Exiting gracefully...")
sys.exit(0)
##################
# End of program #
##################
print("-" * 60)
print(f"🎉 {SOLUTION_NAME} workspace removal completed successfully!")
print(f"✅ Deleted workspace: {workspace_display_name}")
print(f"✅ Workspace ID: {workspace_id}")
print("-" * 60)