use crate::ResourceGraphHelper;
use cloud_terrastodon_azure_types::AzureTenantId;
use cloud_terrastodon_azure_types::StorageAccount;
use cloud_terrastodon_command::CacheKey;
use cloud_terrastodon_command::CacheableCommand;
use cloud_terrastodon_command::async_trait;
use eyre::Result;
use indoc::indoc;
use std::path::PathBuf;
#[must_use = "This is a future request, you must .await it"]
pub struct StorageAccountListRequest {
pub tenant_id: AzureTenantId,
}
pub fn fetch_all_storage_accounts(tenant_id: AzureTenantId) -> StorageAccountListRequest {
StorageAccountListRequest { tenant_id }
}
#[async_trait]
impl CacheableCommand for StorageAccountListRequest {
type Output = Vec<StorageAccount>;
fn cache_key(&self) -> CacheKey {
CacheKey::new(PathBuf::from_iter([
"az",
"resource_graph",
"storage_accounts",
self.tenant_id.to_string().as_str(),
]))
}
async fn run(self) -> Result<Self::Output> {
ResourceGraphHelper::new(
self.tenant_id,
indoc! {r#"
Resources
| where type == "microsoft.storage/storageaccounts"
| project id,name,kind,location,sku,properties,tags
"#},
Some(self.cache_key()),
)
.collect_all::<StorageAccount>()
.await
}
}
cloud_terrastodon_command::impl_cacheable_into_future!(StorageAccountListRequest);
#[cfg(test)]
mod test {
use super::fetch_all_storage_accounts;
use crate::get_test_tenant_id;
use crate::is_storage_account_name_available;
use cloud_terrastodon_azure_types::Slug;
#[tokio::test]
pub async fn it_works() -> eyre::Result<()> {
let storage_accounts = fetch_all_storage_accounts(get_test_tenant_id().await?).await?;
assert!(!storage_accounts.is_empty());
let check_name = rand::random::<u64>() % storage_accounts.len() as u64;
for (i, sa) in storage_accounts.into_iter().enumerate() {
sa.name.validate_slug()?;
if i == check_name as usize {
assert!(!is_storage_account_name_available(&sa.name).await?);
}
}
Ok(())
}
}