use crate::client::Client;
use crate::error::SailError;
use crate::sailbox::object::Sailbox;
use time::OffsetDateTime;
use super::types::{
CredentialInjectionPolicyInfo, CredentialInjectionPolicyPage, InjectionRule,
ListCredentialInjectionPoliciesQuery, SecretInfo,
};
pub struct Credentials<'a> {
client: &'a Client,
}
impl std::fmt::Debug for Credentials<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Credentials").finish()
}
}
impl Credentials<'_> {
pub async fn set_secret(&self, name: &str, value: &str) -> Result<Secret, SailError> {
self.client
.set_secret(name, value)
.await
.map(|info| Secret::bind(self.client.clone(), info))
}
pub async fn get_secret(&self, name: &str) -> Result<Secret, SailError> {
self.client
.get_secret(name)
.await
.map(|info| Secret::bind(self.client.clone(), info))
}
pub async fn list_secrets(&self) -> Result<Vec<Secret>, SailError> {
Ok(self
.client
.list_secrets()
.await?
.into_iter()
.map(|info| Secret::bind(self.client.clone(), info))
.collect())
}
pub async fn delete_secret(&self, name: &str) -> Result<(), SailError> {
self.client.delete_secret(name).await
}
pub async fn create_policy(
&self,
name: &str,
rules: Vec<InjectionRule>,
) -> Result<CredentialInjectionPolicy, SailError> {
self.client
.create_credential_policy(name, &rules)
.await
.map(|info| CredentialInjectionPolicy::bind(self.client.clone(), info))
}
pub async fn get_policy(
&self,
policy_id: &str,
) -> Result<CredentialInjectionPolicy, SailError> {
self.client
.get_credential_policy(policy_id)
.await
.map(|info| CredentialInjectionPolicy::bind(self.client.clone(), info))
}
pub async fn list_policies(
&self,
query: &ListCredentialInjectionPoliciesQuery,
) -> Result<CredentialInjectionPolicyPage, SailError> {
self.client.list_credential_policies(query).await
}
}
#[derive(Clone)]
pub struct Secret {
client: Client,
info: SecretInfo,
}
impl std::fmt::Debug for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Secret")
.field("name", &self.info.name)
.finish_non_exhaustive()
}
}
impl Secret {
pub(crate) fn bind(client: Client, info: SecretInfo) -> Secret {
Secret { client, info }
}
pub fn name(&self) -> &str {
&self.info.name
}
pub fn created_at(&self) -> OffsetDateTime {
self.info.created_at
}
pub fn updated_at(&self) -> OffsetDateTime {
self.info.updated_at
}
pub fn info(&self) -> &SecretInfo {
&self.info
}
pub fn into_info(self) -> SecretInfo {
self.info
}
pub async fn delete(&self) -> Result<(), SailError> {
self.client.delete_secret(&self.info.name).await
}
}
#[derive(Clone)]
pub struct CredentialInjectionPolicy {
client: Client,
info: CredentialInjectionPolicyInfo,
}
impl std::fmt::Debug for CredentialInjectionPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CredentialInjectionPolicy")
.field("id", &self.info.id)
.field("name", &self.info.name)
.finish_non_exhaustive()
}
}
impl CredentialInjectionPolicy {
pub(crate) fn bind(
client: Client,
info: CredentialInjectionPolicyInfo,
) -> CredentialInjectionPolicy {
CredentialInjectionPolicy { client, info }
}
pub fn id(&self) -> &str {
&self.info.id
}
pub fn name(&self) -> &str {
&self.info.name
}
pub fn rules(&self) -> &[InjectionRule] {
&self.info.rules
}
pub fn info(&self) -> &CredentialInjectionPolicyInfo {
&self.info
}
pub fn into_info(self) -> CredentialInjectionPolicyInfo {
self.info
}
pub async fn rename(&mut self, name: &str) -> Result<(), SailError> {
self.info = self
.client
.rename_credential_policy(&self.info.id, name)
.await?;
Ok(())
}
pub async fn delete(&self) -> Result<(), SailError> {
self.client.delete_credential_policy(&self.info.id).await
}
}
#[doc(hidden)]
impl Client {
pub fn credentials(&self) -> Credentials<'_> {
Credentials { client: self }
}
}
#[doc(hidden)]
impl Sailbox {
pub async fn set_credential_policy(
&self,
policy: &CredentialInjectionPolicy,
) -> Result<(), SailError> {
self.client()
.set_sailbox_credential_policy(self.sailbox_id(), policy.id())
.await
}
pub async fn credential_policy(&self) -> Result<Option<CredentialInjectionPolicy>, SailError> {
Ok(self
.client()
.sailbox_credential_policy(self.sailbox_id())
.await?
.map(|info| CredentialInjectionPolicy::bind(self.client().clone(), info)))
}
pub async fn clear_credential_policy(&self) -> Result<(), SailError> {
self.client()
.clear_sailbox_credential_policy(self.sailbox_id())
.await
}
}