1use serde::{Deserialize, Serialize};
6use serde_yaml;
7use serde_json::json;
8use std::fs;
9use std::path::PathBuf;
10use crate::rest_helper;
11use crate::models::{Authentication, AzureConfig, ResourceGroup, Subscription};
12use crate::subscriptions::list_subscriptions;
13
14
15
16
17pub async fn parse(file_path: &PathBuf, access_token: &str) -> Result<AzureConfig, Box<dyn std::error::Error>> {
34 print!("Starting to parse the Azure configuration file...\n");
35 let auth = Authentication {
37 access_token: access_token.to_string(),
38 };
39
40 let yaml_content = fs::read_to_string(file_path)?;
42 println!("YAML content read from file: \n{}", yaml_content);
43 let config: AzureConfig = match serde_yaml::from_str(&yaml_content) {
44 Ok(c) => c,
45 Err(e) => {
46 println!("Failed to parse YAML: {:?}", e);
47 return Err(Box::new(e));
48 }
49 };
50
51 let azure_subscriptions = list_subscriptions(&auth.access_token).await?;
53
54 for subscription in &config.subscriptions {
56 if !azure_subscriptions.iter().any(|sub| sub.id == subscription.id) {
58 println!("Subscription ID {} not found in Azure. Skipping...", subscription.id);
59 continue;
60 }
61 println!("Processing subscription: {:?}", subscription);
63 parse_rg_call(subscription, &auth).await;
64 }
65
66
67 Ok(config)
68}
69
70
71async fn parse_rg_call(subscription: &Subscription, access_token: &Authentication) {
72 let resource_groups = match &subscription.resource_groups {
73 Some(rg) if !rg.is_empty() => rg,
74 Some(_) => {
75 println!("The resource_groups vector is empty!");
76 return;
77 }
78 None => {
79 println!("No resource_groups found!");
80 return;
81 }
82 };
83
84 for ResourceGroup in resource_groups {
86 println!("Processing Resource Group: {:?}", ResourceGroup);
87
88 let endpoint = format!(
89 "https://management.azure.com/subscriptions/{}/resourcegroups/{}?api-version=2021-04-01",
90 subscription.id,
91 ResourceGroup.name.as_deref().unwrap_or("unknown")
92 );
93
94 let body = json!({
95 "location": ResourceGroup.region.as_deref().unwrap_or("eastus"),
96 });
97
98 println!("Endpoint: {}", endpoint);
99 println!("Body: {}", body);
100
101 match rest_helper::call_azure_api(&endpoint, &access_token.access_token, &body).await {
102 Ok(response) => {
103 println!("API call successful. Response: {:?}", response);
104 }
105 Err(e) => {
106 println!("API call failed. Error: {:?}", e);
107 }
108 }
109
110 }
111
112}
113
114