Skip to main content

spiral/
workload.rs

1//! Workload identity handles.
2use crate::ids::WorkloadId;
3use crate::resources::workload::{
4    CreateWorkloadKeyRequest, CreateWorkloadKeyResponse, CreateWorkloadPolicyRequest,
5    CreateWorkloadPolicyResponse, Workload as WorkloadInfo, WorkloadKey, WorkloadPolicyResponse,
6};
7use crate::{Client, Error, List};
8
9/// A workload identity bound to an authenticated connection.
10#[derive(Debug, Clone)]
11pub struct Workload {
12    client: Client,
13    id: WorkloadId,
14}
15impl Client {
16    /// Select a workload without making a network request.
17    pub fn workload(&self, id: impl Into<WorkloadId>) -> Workload {
18        Workload {
19            client: self.clone(),
20            id: id.into(),
21        }
22    }
23}
24impl Workload {
25    /// Stable workload identifier.
26    pub fn id(&self) -> &WorkloadId {
27        &self.id
28    }
29    /// Read workload metadata.
30    pub async fn get(&self) -> Result<WorkloadInfo, Error> {
31        Ok(self
32            .client
33            .workloads()
34            .read_workload(self.id.to_string())
35            .await?)
36    }
37    /// Deactivate the workload and revoke all of its credentials.
38    pub async fn deactivate(&self) -> Result<(), Error> {
39        Ok(self
40            .client
41            .workloads()
42            .deactivate_workload(self.id.to_string())
43            .await?)
44    }
45    /// List public authentication keys.
46    pub fn keys(&self) -> List<'_, WorkloadKey> {
47        self.client
48            .workloads()
49            .list_keys_list(self.id.to_string(), Default::default())
50    }
51    /// Register a public JWK with a key ID.
52    pub async fn add_key(
53        &self,
54        request: CreateWorkloadKeyRequest,
55    ) -> Result<CreateWorkloadKeyResponse, Error> {
56        Ok(self
57            .client
58            .workloads()
59            .add_key(self.id.to_string(), request)
60            .await?)
61    }
62    /// Remove a public authentication key.
63    pub async fn remove_key(&self, id: &str) -> Result<(), Error> {
64        Ok(self
65            .client
66            .workloads()
67            .remove_key(self.id.to_string(), id.to_owned())
68            .await?)
69    }
70    /// List policies admitting external workload identities.
71    pub fn policies(&self) -> List<'_, WorkloadPolicyResponse> {
72        self.client
73            .workloads()
74            .list_policies_list(self.id.to_string(), Default::default())
75    }
76    /// Add an external identity trust policy.
77    pub async fn create_policy(
78        &self,
79        request: CreateWorkloadPolicyRequest,
80    ) -> Result<CreateWorkloadPolicyResponse, Error> {
81        Ok(self
82            .client
83            .workloads()
84            .create_policy(self.id.to_string(), request)
85            .await?)
86    }
87}