use crate::fetch_root_management_group;
use cloud_terrastodon_azure_types::AzureTenantId;
use cloud_terrastodon_azure_types::CostManagementQueryDefinition;
use cloud_terrastodon_azure_types::CostManagementQueryResult;
use cloud_terrastodon_rest::RestRequest;
pub async fn fetch_cost_query_results(
tenant_id: AzureTenantId,
query: &CostManagementQueryDefinition,
) -> eyre::Result<CostManagementQueryResult> {
let root = fetch_root_management_group(tenant_id).await?;
let url = format!(
"https://management.azure.com/providers/Microsoft.Management/managementGroups/{}/providers/Microsoft.CostManagement/query?api-version=2021-10-01",
root.tenant_id
);
let resp = RestRequest::new(http::Method::POST, url.as_str())?
.tenant(tenant_id)
.body(serde_json::to_string_pretty(query)?)
.receive::<CostManagementQueryResult>()
.await?;
Ok(resp)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::get_test_tenant_id;
#[tokio::test]
async fn it_works1() -> eyre::Result<()> {
let query = CostManagementQueryDefinition::new_cost_total_this_month();
let resp = fetch_cost_query_results(get_test_tenant_id().await?, &query).await?;
assert_eq!(resp.properties.next_link, None);
assert!(!resp.properties.columns.is_empty());
Ok(())
}
#[tokio::test]
async fn it_works2() -> eyre::Result<()> {
let query = CostManagementQueryDefinition::new_cost_by_day_this_month();
let resp = fetch_cost_query_results(get_test_tenant_id().await?, &query).await?;
assert_eq!(resp.properties.next_link, None);
assert!(!resp.properties.columns.is_empty());
Ok(())
}
#[tokio::test]
async fn it_works3() -> eyre::Result<()> {
let query = CostManagementQueryDefinition::new_cost_by_resource_group_this_month();
let resp = fetch_cost_query_results(get_test_tenant_id().await?, &query).await?;
assert_eq!(resp.properties.next_link, None);
assert!(!resp.properties.columns.is_empty());
Ok(())
}
}