use crate::ids::WorkloadId;
use crate::resources::workload::{
CreateWorkloadKeyRequest, CreateWorkloadKeyResponse, CreateWorkloadPolicyRequest,
CreateWorkloadPolicyResponse, Workload as WorkloadInfo, WorkloadKey, WorkloadPolicyResponse,
};
use crate::{Client, Error, List};
#[derive(Debug, Clone)]
pub struct Workload {
client: Client,
id: WorkloadId,
}
impl Client {
pub fn workload(&self, id: impl Into<WorkloadId>) -> Workload {
Workload {
client: self.clone(),
id: id.into(),
}
}
}
impl Workload {
pub fn id(&self) -> &WorkloadId {
&self.id
}
pub async fn get(&self) -> Result<WorkloadInfo, Error> {
Ok(self
.client
.workloads()
.read_workload(self.id.to_string())
.await?)
}
pub async fn deactivate(&self) -> Result<(), Error> {
Ok(self
.client
.workloads()
.deactivate_workload(self.id.to_string())
.await?)
}
pub fn keys(&self) -> List<'_, WorkloadKey> {
self.client
.workloads()
.list_keys_list(self.id.to_string(), Default::default())
}
pub async fn add_key(
&self,
request: CreateWorkloadKeyRequest,
) -> Result<CreateWorkloadKeyResponse, Error> {
Ok(self
.client
.workloads()
.add_key(self.id.to_string(), request)
.await?)
}
pub async fn remove_key(&self, id: &str) -> Result<(), Error> {
Ok(self
.client
.workloads()
.remove_key(self.id.to_string(), id.to_owned())
.await?)
}
pub fn policies(&self) -> List<'_, WorkloadPolicyResponse> {
self.client
.workloads()
.list_policies_list(self.id.to_string(), Default::default())
}
pub async fn create_policy(
&self,
request: CreateWorkloadPolicyRequest,
) -> Result<CreateWorkloadPolicyResponse, Error> {
Ok(self
.client
.workloads()
.create_policy(self.id.to_string(), request)
.await?)
}
}