awacke1 commited on
Commit
1bc0752
Β·
verified Β·
1 Parent(s): 5984e60

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import pandas as pd
4
+ from azure.identity import AzureCliCredential
5
+ from azure.mgmt.resource import ResourceManagementClient
6
+ from azure.cosmos import CosmosClient, exceptions
7
+ from github import Github
8
+ from git import Repo
9
+ import shutil
10
+ from datetime import datetime
11
+ import base64
12
+
13
+ # Azure configuration
14
+ ENDPOINT = "https://acae-afd.documents.azure.com:443/"
15
+ DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
16
+ CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
17
+
18
+ # GitHub configuration
19
+ def download_github_repo(url, local_path):
20
+ if os.path.exists(local_path):
21
+ shutil.rmtree(local_path)
22
+ Repo.clone_from(url, local_path)
23
+
24
+ def create_zip_file(source_dir, output_filename):
25
+ shutil.make_archive(output_filename, 'zip', source_dir)
26
+
27
+ def get_base64_download_link(file_path, file_name):
28
+ with open(file_path, "rb") as file:
29
+ contents = file.read()
30
+ base64_encoded = base64.b64encode(contents).decode()
31
+ return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">Download {file_name}</a>'
32
+
33
+ # Azure Resource Management functions
34
+ def get_azure_resources(credential, subscription_id):
35
+ resource_client = ResourceManagementClient(credential, subscription_id)
36
+ resources = resource_client.resources.list()
37
+ return [{"name": r.name, "type": r.type, "id": r.id} for r in resources]
38
+
39
+ def build_resource_tree(resources):
40
+ tree = {}
41
+ for resource in resources:
42
+ resource_type = resource['type']
43
+ if resource_type not in tree:
44
+ tree[resource_type] = []
45
+ tree[resource_type].append(resource)
46
+ return tree
47
+
48
+ # Cosmos DB functions
49
+ def insert_record(container, record):
50
+ try:
51
+ response = container.create_item(body=record)
52
+ return True, response
53
+ except exceptions.CosmosHttpResponseError as e:
54
+ return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
55
+ except Exception as e:
56
+ return False, f"An unexpected error occurred: {str(e)}"
57
+
58
+ def fetch_all_records(container):
59
+ try:
60
+ query = "SELECT * FROM c"
61
+ items = list(container.query_items(query=query, enable_cross_partition_query=True))
62
+ return pd.DataFrame(items)
63
+ except exceptions.CosmosHttpResponseError as e:
64
+ st.error(f"HTTP error occurred while fetching records: {str(e)}. Status code: {e.status_code}")
65
+ return pd.DataFrame()
66
+ except Exception as e:
67
+ st.error(f"An unexpected error occurred while fetching records: {str(e)}")
68
+ return pd.DataFrame()
69
+
70
+ # Streamlit app
71
+ st.set_page_config(layout="wide")
72
+ st.title("🌟 Azure Resource Manager and GitHub Integration")
73
+
74
+ # Initialize session state
75
+ if 'logged_in' not in st.session_state:
76
+ st.session_state.logged_in = False
77
+
78
+ # Login section
79
+ if not st.session_state.logged_in:
80
+ st.subheader("πŸ” Login")
81
+ subscription_id = st.text_input("Azure Subscription ID")
82
+ if st.button("πŸš€ Login"):
83
+ try:
84
+ credential = AzureCliCredential()
85
+ ResourceManagementClient(credential, subscription_id)
86
+ st.session_state.credential = credential
87
+ st.session_state.subscription_id = subscription_id
88
+ st.session_state.logged_in = True
89
+ st.success("Successfully logged in!")
90
+ st.rerun()
91
+ except Exception as e:
92
+ st.error(f"Failed to authenticate: {str(e)}")
93
+ else:
94
+ # Main app content
95
+ col1, col2 = st.columns([1, 3])
96
+
97
+ with col1:
98
+ st.subheader("πŸ“Š Azure Resources")
99
+ resources = get_azure_resources(st.session_state.credential, st.session_state.subscription_id)
100
+ resource_tree = build_resource_tree(resources)
101
+
102
+ for resource_type, resource_list in resource_tree.items():
103
+ with st.expander(resource_type):
104
+ for resource in resource_list:
105
+ st.write(f"- {resource['name']}")
106
+
107
+ with col2:
108
+ st.subheader("πŸ”§ Resource Management")
109
+
110
+ # Display resources in a table
111
+ df_resources = pd.DataFrame(resources)
112
+ st.dataframe(df_resources)
113
+
114
+ # Save resource to Cosmos DB
115
+ st.subheader("πŸ’Ύ Save Resource to Cosmos DB")
116
+ selected_resource = st.selectbox("Select a resource to save", df_resources['name'])
117
+ if selected_resource:
118
+ resource = df_resources[df_resources['name'] == selected_resource].iloc[0]
119
+ if st.button("Save to Cosmos DB"):
120
+ try:
121
+ cosmos_client = CosmosClient(ENDPOINT, credential=st.session_state.credential)
122
+ database = cosmos_client.get_database_client(DATABASE_NAME)
123
+ container = database.get_container_client(CONTAINER_NAME)
124
+
125
+ record = {
126
+ "id": resource['id'],
127
+ "name": resource['name'],
128
+ "type": resource['type']
129
+ }
130
+
131
+ success, response = insert_record(container, record)
132
+ if success:
133
+ st.success("Resource saved to Cosmos DB successfully!")
134
+ else:
135
+ st.error(f"Failed to save resource: {response}")
136
+ except Exception as e:
137
+ st.error(f"An error occurred: {str(e)}")
138
+
139
+ # GitHub Integration
140
+ st.subheader("πŸ™ GitHub Integration")
141
+ github_token = os.environ.get("GITHUB")
142
+ if github_token:
143
+ source_repo = st.text_input("Source GitHub Repository URL")
144
+ new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"Azure-Resource-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
145
+
146
+ if st.button("πŸ“₯ Clone Repository"):
147
+ if source_repo:
148
+ try:
149
+ local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
150
+ download_github_repo(source_repo, local_path)
151
+ zip_filename = f"{new_repo_name}.zip"
152
+ create_zip_file(local_path, zip_filename[:-4])
153
+ st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
154
+ st.success("Repository cloned successfully!")
155
+ except Exception as e:
156
+ st.error(f"An error occurred: {str(e)}")
157
+ finally:
158
+ if os.path.exists(local_path):
159
+ shutil.rmtree(local_path)
160
+ if os.path.exists(zip_filename):
161
+ os.remove(zip_filename)
162
+ else:
163
+ st.error("Please provide a source repository URL.")
164
+ else:
165
+ st.warning("GitHub token not found in environment variables. GitHub integration is disabled.")
166
+
167
+ # Logout button
168
+ if st.button("πŸšͺ Logout"):
169
+ st.session_state.logged_in = False
170
+ st.session_state.credential = None
171
+ st.session_state.subscription_id = None
172
+ st.rerun()
173
+
174
+ # Display connection info
175
+ st.sidebar.subheader("πŸ”— Connection Information")
176
+ st.sidebar.text(f"Subscription ID: {st.session_state.subscription_id}")
177
+ st.sidebar.text(f"Cosmos DB Endpoint: {ENDPOINT}")
178
+ st.sidebar.text(f"Database: {DATABASE_NAME}")
179
+ st.sidebar.text(f"Container: {CONTAINER_NAME}")