awacke1 commited on
Commit
623522b
·
1 Parent(s): f8f14a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import tempfile
4
+ from azure.identity import DefaultAzureCredential
5
+ from azure.mgmt.resource import ResourceManagementClient
6
+ from python_terraform import Terraform
7
+
8
+ import streamlit as st
9
+
10
+ # Set up Azure credentials
11
+ credential = DefaultAzureCredential()
12
+ subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
13
+
14
+ # Initialize ResourceManagementClient
15
+ resource_client = ResourceManagementClient(credential, subscription_id)
16
+
17
+ # Initialize Terraform
18
+ tf = Terraform(working_dir=tempfile.mkdtemp())
19
+
20
+ # Streamlit app
21
+ st.title("Azure SDK and Terraform Demo")
22
+
23
+ # Get the list of resource groups
24
+ resource_groups = [rg.name for rg in resource_client.resource_groups.list()]
25
+
26
+ # Select a resource group
27
+ selected_rg = st.selectbox("Select a Resource Group", resource_groups)
28
+
29
+ if selected_rg:
30
+ # Display resources in the selected resource group
31
+ resources = resource_client.resources.list_by_resource_group(selected_rg)
32
+ st.subheader(f"Resources in Resource Group: {selected_rg}")
33
+ for resource in resources:
34
+ st.write(f"{resource.type}: {resource.name}")
35
+
36
+ # Terraform configurations
37
+ st.subheader("Terraform Configuration")
38
+
39
+ # Set up the Terraform configuration
40
+ terraform_config = f"""
41
+ provider "azurerm" {{
42
+ features {{}}
43
+ }}
44
+
45
+ resource "azurerm_resource_group" "example" {{
46
+ name = "{selected_rg}"
47
+ location = "East US"
48
+ }}
49
+
50
+ resource "azurerm_virtual_network" "example" {{
51
+ name = "example-network"
52
+ address_space = ["10.0.0.0/16"]
53
+ location = azurerm_resource_group.example.location
54
+ resource_group_name = azurerm_resource_group.example.name
55
+ }}
56
+
57
+ resource "azurerm_subnet" "example" {{
58
+ name = "internal"
59
+ resource_group_name = azurerm_resource_group.example.name
60
+ virtual_network_name = azurerm_virtual_network.example.name
61
+ address_prefix = "10.0.2.0/24"
62
+ }}
63
+ """
64
+
65
+ # Save the Terraform configuration to a file
66
+ with open("main.tf", "w") as f:
67
+ f.write(terraform_config)
68
+
69
+ st.code(terraform_config, language="hcl")
70
+
71
+ if st.button("Apply Terraform"):
72
+ # Initialize and apply Terraform configuration
73
+ tf.init()
74
+ ret_code, stdout, stderr = tf.apply(skip_plan=True, capture_output=True)
75
+
76
+ if ret_code == 0:
77
+ st.success("Terraform applied successfully!")
78
+ st.code(stdout, language="bash")
79
+ else:
80
+ st.error("Error applying Terraform configuration")
81
+ st.code(stderr, language="bash")