cloud_terrastodon_azure 0.35.1

Helpers for interacting with Azure for the Cloud Terrastodon project
use cloud_terrastodon_azure_types::RoleDefinitionId;
use cloud_terrastodon_azure_types::RoleManagementPolicyAssignment;
use cloud_terrastodon_azure_types::Scope;
use cloud_terrastodon_command::CacheKey;
use cloud_terrastodon_command::CommandBuilder;
use cloud_terrastodon_command::CommandKind;
use cloud_terrastodon_command::async_trait;
use cloud_terrastodon_hcl_types::Sanitizable;
use eyre::Result;
use serde::Deserialize;
use std::path::PathBuf;

pub struct RoleManagementPolicyAssignmentsForRoleRequest<T: Scope + Send> {
    pub scope: T,
    pub role_definition_id: RoleDefinitionId,
}

pub fn fetch_role_management_policy_assignments<T: Scope + Send>(
    scope: T,
    role_definition_id: RoleDefinitionId,
) -> RoleManagementPolicyAssignmentsForRoleRequest<T> {
    RoleManagementPolicyAssignmentsForRoleRequest {
        scope,
        role_definition_id,
    }
}

#[async_trait]
impl<T: Scope + Send> cloud_terrastodon_command::CacheableCommand
    for RoleManagementPolicyAssignmentsForRoleRequest<T>
{
    type Output = Vec<RoleManagementPolicyAssignment>;

    fn cache_key(&self) -> CacheKey {
        CacheKey::new(PathBuf::from_iter([
            "az",
            "rest",
            "GET",
            self.scope.expanded_form().sanitize().as_ref(),
            "roleManagementPolicyAssignments",
            "roleDefinitionId",
            self.role_definition_id.short_form().as_ref(),
        ]))
    }

    async fn run(self) -> Result<Self::Output> {
        let url = format!(
            "https://management.azure.com/{}/providers/Microsoft.Authorization/roleManagementPolicyAssignments?api-version=2020-10-01&$filter=roleDefinitionId+eq+%27{}%27",
            self.scope.expanded_form(),
            self.role_definition_id.expanded_form()
        );

        let mut cmd = CommandBuilder::new(CommandKind::CloudTerrastodon);
        cmd.args(["rest", "--method", "GET", "--url", &url]);
        cmd.cache(self.cache_key());

        #[derive(Deserialize)]
        struct Response {
            value: Vec<RoleManagementPolicyAssignment>,
        }

        let mut result: Result<Response, _> = cmd.run().await;
        if result.is_err() {
            // single retry - sometimes this returns a gateway error
            result = cmd.run().await;
        }
        Ok(result?.value)
    }
}

cloud_terrastodon_command::impl_cacheable_into_future!(RoleManagementPolicyAssignmentsForRoleRequest<T>, T: Scope + Send + 'static);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::role_eligibility_schedules::fetch_my_role_eligibility_schedules;
    use crate::test_helpers::expect_aad_premium_p2_license;

    #[tokio::test]
    async fn it_works() -> Result<()> {
        let Some(mut my_roles) =
            expect_aad_premium_p2_license(fetch_my_role_eligibility_schedules().await).await?
        else {
            return Ok(());
        };
        let role = my_roles.swap_remove(0);
        let scope = role.properties.scope;
        let role_definition_id = role.properties.role_definition_id;
        let found_policy_assignments =
            fetch_role_management_policy_assignments(scope, role_definition_id).await?;
        assert!(!found_policy_assignments.is_empty());
        assert!(
            found_policy_assignments
                .iter()
                .any(|assignment| assignment.get_maximum_activation_duration().is_some())
        );
        Ok(())
    }
}