antimatter 2.0.13

antimatter.io Rust library for data control
Documentation
use super::Session;
use crate::session::session::SessionError;
use crate::session::RUNTIME;
use antimatter_api::apis::authentication_api::{self as api};
use antimatter_api::models::{
    DomainIdentityGroupProviderDetails, DomainIdentityProviderDetails, DomainIdentityProviderInfo,
    DomainIdentityProviderList, DomainIdentityProviderPrincipalList,
    DomainIdentityProviderPrincipalParams, DomainInsertIdentityProviderPrincipal200Response,
    PrincipalSummary, UpdatePrincipalParams,
};

impl Session {
    /// Upsert an identity provider's configuration for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `name` - A &str containing the identity provider's name.
    /// * `domain_identity_provider` - A DomainIdentityProviderDetails
    ///     containing the identity providers configuration.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainIdentityProviderInfo` confirming the
    /// upserted identity provider's configuration.
    pub fn upsert_identity_providers(
        &mut self,
        name: &str,
        domain_identity_provider: DomainIdentityProviderDetails,
    ) -> Result<DomainIdentityProviderInfo, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_upsert_identity_provider(
                &conf,
                self.get_domain_id().as_str(),
                name,
                domain_identity_provider,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches a list of identity providers for session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainIdentityProviderList` a vector of
    /// `DomainIdentityProviderInfo`.
    pub fn list_identity_providers(&mut self) -> Result<DomainIdentityProviderList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_list_identity_providers(
                &conf,
                self.get_domain_id().as_str(),
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Lists the identity group providers.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainIdentityGroupProviderDetails` a list of IDP details.
    pub fn list_identity_group_providers(
        &mut self,
    ) -> Result<DomainIdentityGroupProviderDetails, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_identity_group_providers(
                &conf,
                self.get_domain_id().as_str(),
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches an identity providers information in session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainIdentityProviderInfo` confirming the
    /// upserted identity provider's configuration.
    pub fn get_identity_provider(
        &mut self,
        provider_name: &str,
    ) -> Result<DomainIdentityProviderInfo, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_identity_provider(
                &conf,
                self.get_domain_id().as_str(),
                provider_name,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Create an identity provider principal for the session's domain for the
    /// identity provider.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider
    ///     principal's name.
    /// * `domain_identity_provider_principal_params` - A
    ///     DomainIdentityProviderPrincipalParams containing the identity
    ///     providers principal's configuration.
    pub fn insert_identity_provider_principal(
        &mut self,
        identity_provider_name: &str,
        domain_identity_provider_principal_params: DomainIdentityProviderPrincipalParams,
    ) -> Result<DomainInsertIdentityProviderPrincipal200Response, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_insert_identity_provider_principal(
                &conf,
                self.get_domain_id().as_str(),
                identity_provider_name,
                domain_identity_provider_principal_params,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Update an identity provider principal's capabilities in the session's
    /// domain in the identity provider.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider's
    ///     name.
    /// * `principal_id` - A &str containing the identity provider principal's
    ///     name.
    /// * `update_principal_params` - New principal settings to apply to principal.
    pub fn update_identity_provider_principal(
        &mut self,
        identity_provider_name: &str,
        principal_id: &str,
        capability_list: UpdatePrincipalParams,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_update_identity_provider_principal(
                &conf,
                self.get_domain_id().as_str(),
                identity_provider_name,
                principal_id,
                capability_list,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Fetches a list of identity provider principal's of an identity provider
    /// in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider's
    ///     name.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainIdentityProviderPrincipalList` with a
    /// vector of identity provider principals.
    pub fn get_identity_provider_principals(
        &mut self,
        identity_provider_name: &str,
    ) -> Result<DomainIdentityProviderPrincipalList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_identity_provider_principals(
                &conf,
                self.get_domain_id().as_str(),
                identity_provider_name,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches an identity provider principal of an identity provider
    /// in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider's
    ///     name.
    /// * `principal_id` - A &str containing the identity provider principal's
    ///     name.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `PrincipalSummary` containing the identity
    /// provider principal's information.
    pub fn get_identity_provider_principal(
        &mut self,
        identity_provider_name: &str,
        principal_id: &str,
    ) -> Result<PrincipalSummary, SessionError> {
        // TODO: This is generator bugged where the "type" field is incorrectly
        //  consumed by the serdes. This issue was also run into with the Rust
        //  CLI. As a work around we will instead get all principals and ID
        //  match the one we are looking for.
        // let conf = self.get_configuration()?;
        //
        // let res = api::domain_get_identity_provider_principal(
        //     &conf,
        //     self.get_domain_id().as_str(),
        //     identity_provider_name,
        //     principal_id,
        // )
        //     .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        let principals = self
            .get_identity_provider_principals(identity_provider_name)
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        let res = principals
            .principals
            .iter()
            .find(|p| p.principal_id == principal_id);

        if res.is_none() {
            return Err(SessionError::APIError(format!("404 principal not found")));
        }

        Ok(res.unwrap().to_owned())
    }

    /// Deletes an identity provider in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider's
    ///     name.
    pub fn delete_identity_provider(
        &mut self,
        identity_provider_name: &str,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_delete_identity_provider(
                &conf,
                self.get_domain_id().as_str(),
                identity_provider_name,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Deletes an identity provider principal in an identity provider in the
    /// session's domain.
    ///
    /// Arguments:
    ///
    /// * `identity_provider_name` - A &str containing the identity provider's
    ///     name.
    /// * `principal_id` - A &str containing the identity provider principal's
    ///     name.
    pub fn delete_identity_provider_principal(
        &mut self,
        identity_provider_name: &str,
        provider_id: &str,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_delete_identity_provider_principal(
                &conf,
                self.get_domain_id().as_str(),
                identity_provider_name,
                provider_id,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }
}