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::encryption_api::{self as api};
use antimatter_api::models::{
    ActiveRootEncryptionKeyId, AvailableRootEncryptionKeyProviders, KeyInfos,
    RootEncryptionKeyIdResponse, RootEncryptionKeyItem, RootEncryptionKeyListResponse,
    RootEncryptionKeyTestResponse, RotateKeyEncryptionKeyResponse,
};
use serde_json::Value;

impl Session {
    /// Fetches the active root encryption key for the session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `RootEncryptionKeyItem` with the root active
    /// encryption key's information.
    pub fn get_active_root_encryption_key(
        &mut self,
    ) -> Result<RootEncryptionKeyItem, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Fetches the list of all root encryption key for the session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `RootEncryptionKeyListResponse` with a vector of
    /// root encryption key information.
    pub fn list_root_encryption_keys(
        &mut self,
    ) -> Result<RootEncryptionKeyListResponse, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Tests a root encryption key in the session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `RootEncryptionKeyTestResponse` with the root
    /// active encryption key's test result information.
    pub fn test_root_encryption_key(
        &mut self,
        root_encryption_key_id: &str,
    ) -> Result<RootEncryptionKeyTestResponse, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Create a root encryption key in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `key_infos` - A RootEncryptionKeyIdResponse containing the information
    ///     for the new root encryption key.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `RootEncryptionKeyIdResponse` with the new root
    /// encryption key's ID.
    pub fn add_root_encryption_key(
        &mut self,
        key_infos: KeyInfos,
    ) -> Result<RootEncryptionKeyIdResponse, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Delete a root encryption key in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `root_encryption_key_id` - A &str containing root encryption key's ID.
    pub fn delete_root_encryption_key(
        &mut self,
        root_encryption_key_id: &str,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(())
    }

    /// Sets the active root encryption key in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `root_encryption_key_id` - A &str containing root encryption key's ID.
    pub fn set_active_root_encryption_key(
        &mut self,
        root_encryption_key_id: ActiveRootEncryptionKeyId,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(())
    }

    /// Rotates key encryption keys with the active root encryption key in the
    /// session's domain.
    ///
    /// Results:
    ///
    /// A `Result` containing a `RotateKeyEncryptionKeyResponse` indicating if
    /// there are more key encryption keys to rotate.
    pub fn rotate_encryption_keys(
        &mut self,
    ) -> Result<RotateKeyEncryptionKeyResponse, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Fetches a list of available root encryption key providers for the Cell.
    ///
    /// Results:
    ///
    /// A `Result` containing a `AvailableRootEncryptionKeyProviders` with a
    /// vector of available root encryption key providers for the Cell.
    pub fn list_key_providers(
        &mut self,
    ) -> Result<AvailableRootEncryptionKeyProviders, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }
}