Skip to main content

spiral/api/workloads/
objects.rs

1// Generated by tools/service-contracts/rust.py. Do not edit.
2#![doc = "Generated transport-independent contract objects."]
3
4use std::error::Error as StdError;
5use std::fmt::{self, Display, Formatter};
6
7use serde::{Deserialize, Deserializer, Serialize};
8use serde_json::Value;
9use spiral_contract::error::{ErrorEnvelope, ServiceError};
10use spiral_contract::strict::{DeserializeStrict, Strict};
11use uuid::Uuid;
12
13/// A public JSON Web Key with a key identifier. Private key material is rejected.
14#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
15pub struct CreateKeyRequest {
16    /// Contract field jwk.
17    #[serde(rename = "jwk")]
18    pub jwk: Value,
19}
20impl<'de> DeserializeStrict<'de> for CreateKeyRequest {
21    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
22        #[derive(Deserialize)]
23        #[serde(deny_unknown_fields)]
24        struct __Wire {
25            #[serde(rename = "jwk")]
26            jwk: Strict<Value>,
27        }
28        let wire = __Wire::deserialize(deserializer)?;
29        Ok(Self { jwk: wire.jwk.0 })
30    }
31}
32
33/// An external issuer and claim conditions trusted by a Workload.
34#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
35pub struct CreatePolicyRequest {
36    /// Contract field audience.
37    #[serde(rename = "audience")]
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub audience: Option<String>,
40    /// Contract field conditions.
41    #[serde(rename = "conditions")]
42    pub conditions: Vec<PolicyCondition>,
43    /// Contract field issuer.
44    #[serde(rename = "issuer")]
45    pub issuer: String,
46    /// Contract field name.
47    #[serde(rename = "name")]
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub name: Option<String>,
50}
51impl<'de> DeserializeStrict<'de> for CreatePolicyRequest {
52    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
53        #[derive(Deserialize)]
54        #[serde(deny_unknown_fields)]
55        struct __Wire {
56            #[serde(rename = "audience")]
57            #[serde(default)]
58            audience: Strict<Option<String>>,
59            #[serde(rename = "conditions")]
60            conditions: Strict<Vec<PolicyCondition>>,
61            #[serde(rename = "issuer")]
62            issuer: Strict<String>,
63            #[serde(rename = "name")]
64            #[serde(default)]
65            name: Strict<Option<String>>,
66        }
67        let wire = __Wire::deserialize(deserializer)?;
68        Ok(Self {
69            audience: wire.audience.0,
70            conditions: wire.conditions.0,
71            issuer: wire.issuer.0,
72            name: wire.name.0,
73        })
74    }
75}
76
77/// Properties for a machine identity.
78#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
79pub struct CreateWorkloadRequest {
80    /// Contract field name.
81    #[serde(rename = "name")]
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub name: Option<String>,
84}
85impl<'de> DeserializeStrict<'de> for CreateWorkloadRequest {
86    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
87        #[derive(Deserialize)]
88        #[serde(deny_unknown_fields)]
89        struct __Wire {
90            #[serde(rename = "name")]
91            #[serde(default)]
92            name: Strict<Option<String>>,
93        }
94        let wire = __Wire::deserialize(deserializer)?;
95        Ok(Self { name: wire.name.0 })
96    }
97}
98
99/// A public JSON Web Key whose assertions authenticate the Workload.
100#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
101pub struct Key {
102    /// Contract field jwk.
103    #[serde(rename = "jwk")]
104    pub jwk: Value,
105    /// Contract field kid.
106    #[serde(rename = "kid")]
107    pub kid: String,
108    /// Contract field workloadId.
109    #[serde(rename = "workloadId")]
110    pub workload_id: String,
111}
112impl<'de> DeserializeStrict<'de> for Key {
113    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
114        #[derive(Deserialize)]
115        #[serde(deny_unknown_fields)]
116        struct __Wire {
117            #[serde(rename = "jwk")]
118            jwk: Strict<Value>,
119            #[serde(rename = "kid")]
120            kid: Strict<String>,
121            #[serde(rename = "workloadId")]
122            workload_id: Strict<String>,
123        }
124        let wire = __Wire::deserialize(deserializer)?;
125        Ok(Self {
126            jwk: wire.jwk.0,
127            kid: wire.kid.0,
128            workload_id: wire.workload_id.0,
129        })
130    }
131}
132
133/// One live page of Key records, ordered by ID. Continuations expire after one hour and bind the
134/// credential, operation, filters and page size.
135#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
136pub struct KeyPage {
137    /// Contract field items.
138    #[serde(rename = "items")]
139    pub items: Vec<Key>,
140    /// Contract field nextPageToken.
141    #[serde(rename = "nextPageToken")]
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub next_page_token: Option<String>,
144}
145impl<'de> DeserializeStrict<'de> for KeyPage {
146    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
147        #[derive(Deserialize)]
148        #[serde(deny_unknown_fields)]
149        struct __Wire {
150            #[serde(rename = "items")]
151            items: Strict<Vec<Key>>,
152            #[serde(rename = "nextPageToken")]
153            #[serde(default)]
154            next_page_token: Strict<Option<String>>,
155        }
156        let wire = __Wire::deserialize(deserializer)?;
157        Ok(Self {
158            items: wire.items.0,
159            next_page_token: wire.next_page_token.0,
160        })
161    }
162}
163
164/// The registered public key.
165#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
166pub struct KeyResponse {
167    /// Contract field key.
168    #[serde(rename = "key")]
169    pub key: Key,
170}
171impl<'de> DeserializeStrict<'de> for KeyResponse {
172    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
173        #[derive(Deserialize)]
174        #[serde(deny_unknown_fields)]
175        struct __Wire {
176            #[serde(rename = "key")]
177            key: Strict<Key>,
178        }
179        let wire = __Wire::deserialize(deserializer)?;
180        Ok(Self { key: wire.key.0 })
181    }
182}
183
184/// Continuation request. Page size defaults to 100 and must be 1..=1000. Keep the same credential,
185/// filters and page size when following a continuation.
186#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
187pub struct PageRequest {
188    /// Contract field pageSize.
189    #[serde(rename = "pageSize")]
190    #[serde(default, skip_serializing_if = "Option::is_none")]
191    pub page_size: Option<u32>,
192    /// Contract field pageToken.
193    #[serde(rename = "pageToken")]
194    #[serde(default, skip_serializing_if = "Option::is_none")]
195    pub page_token: Option<String>,
196}
197impl<'de> DeserializeStrict<'de> for PageRequest {
198    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
199        #[derive(Deserialize)]
200        #[serde(deny_unknown_fields)]
201        struct __Wire {
202            #[serde(rename = "pageSize")]
203            #[serde(default)]
204            page_size: Strict<Option<u32>>,
205            #[serde(rename = "pageToken")]
206            #[serde(default)]
207            page_token: Strict<Option<String>>,
208        }
209        let wire = __Wire::deserialize(deserializer)?;
210        Ok(Self {
211            page_size: wire.page_size.0,
212            page_token: wire.page_token.0,
213        })
214    }
215}
216
217/// A Workload trust policy.
218#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
219pub struct Policy {
220    /// Contract field audience.
221    #[serde(rename = "audience")]
222    #[serde(default, skip_serializing_if = "Option::is_none")]
223    pub audience: Option<String>,
224    /// Contract field conditions.
225    #[serde(rename = "conditions")]
226    pub conditions: Vec<PolicyCondition>,
227    /// Contract field id.
228    #[serde(rename = "id")]
229    pub id: String,
230    /// Contract field issuer.
231    #[serde(rename = "issuer")]
232    pub issuer: String,
233    /// Contract field name.
234    #[serde(rename = "name")]
235    #[serde(default, skip_serializing_if = "Option::is_none")]
236    pub name: Option<String>,
237    /// Contract field workloadId.
238    #[serde(rename = "workloadId")]
239    pub workload_id: String,
240}
241impl<'de> DeserializeStrict<'de> for Policy {
242    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
243        #[derive(Deserialize)]
244        #[serde(deny_unknown_fields)]
245        struct __Wire {
246            #[serde(rename = "audience")]
247            #[serde(default)]
248            audience: Strict<Option<String>>,
249            #[serde(rename = "conditions")]
250            conditions: Strict<Vec<PolicyCondition>>,
251            #[serde(rename = "id")]
252            id: Strict<String>,
253            #[serde(rename = "issuer")]
254            issuer: Strict<String>,
255            #[serde(rename = "name")]
256            #[serde(default)]
257            name: Strict<Option<String>>,
258            #[serde(rename = "workloadId")]
259            workload_id: Strict<String>,
260        }
261        let wire = __Wire::deserialize(deserializer)?;
262        Ok(Self {
263            audience: wire.audience.0,
264            conditions: wire.conditions.0,
265            id: wire.id.0,
266            issuer: wire.issuer.0,
267            name: wire.name.0,
268            workload_id: wire.workload_id.0,
269        })
270    }
271}
272
273/// One JWT claim that an external identity must match.
274#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
275pub struct PolicyCondition {
276    /// Contract field key.
277    #[serde(rename = "key")]
278    pub key: String,
279    /// Contract field value.
280    #[serde(rename = "value")]
281    pub value: String,
282}
283impl<'de> DeserializeStrict<'de> for PolicyCondition {
284    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
285        #[derive(Deserialize)]
286        #[serde(deny_unknown_fields)]
287        struct __Wire {
288            #[serde(rename = "key")]
289            key: Strict<String>,
290            #[serde(rename = "value")]
291            value: Strict<String>,
292        }
293        let wire = __Wire::deserialize(deserializer)?;
294        Ok(Self {
295            key: wire.key.0,
296            value: wire.value.0,
297        })
298    }
299}
300
301/// One live page of Policy records, ordered by ID. Continuations expire after one hour and bind the
302/// credential, operation, filters and page size.
303#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
304pub struct PolicyPage {
305    /// Contract field items.
306    #[serde(rename = "items")]
307    pub items: Vec<Policy>,
308    /// Contract field nextPageToken.
309    #[serde(rename = "nextPageToken")]
310    #[serde(default, skip_serializing_if = "Option::is_none")]
311    pub next_page_token: Option<String>,
312}
313impl<'de> DeserializeStrict<'de> for PolicyPage {
314    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
315        #[derive(Deserialize)]
316        #[serde(deny_unknown_fields)]
317        struct __Wire {
318            #[serde(rename = "items")]
319            items: Strict<Vec<Policy>>,
320            #[serde(rename = "nextPageToken")]
321            #[serde(default)]
322            next_page_token: Strict<Option<String>>,
323        }
324        let wire = __Wire::deserialize(deserializer)?;
325        Ok(Self {
326            items: wire.items.0,
327            next_page_token: wire.next_page_token.0,
328        })
329    }
330}
331
332/// The created trust policy.
333#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
334pub struct PolicyResponse {
335    /// Contract field policy.
336    #[serde(rename = "policy")]
337    pub policy: Policy,
338}
339impl<'de> DeserializeStrict<'de> for PolicyResponse {
340    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
341        #[derive(Deserialize)]
342        #[serde(deny_unknown_fields)]
343        struct __Wire {
344            #[serde(rename = "policy")]
345            policy: Strict<Policy>,
346        }
347        let wire = __Wire::deserialize(deserializer)?;
348        Ok(Self {
349            policy: wire.policy.0,
350        })
351    }
352}
353
354/// A machine identity scoped to one Project.
355#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
356pub struct Workload {
357    /// Contract field id.
358    #[serde(rename = "id")]
359    pub id: String,
360    /// Contract field name.
361    #[serde(rename = "name")]
362    #[serde(default, skip_serializing_if = "Option::is_none")]
363    pub name: Option<String>,
364    /// Contract field projectId.
365    #[serde(rename = "projectId")]
366    pub project_id: String,
367}
368impl<'de> DeserializeStrict<'de> for Workload {
369    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
370        #[derive(Deserialize)]
371        #[serde(deny_unknown_fields)]
372        struct __Wire {
373            #[serde(rename = "id")]
374            id: Strict<String>,
375            #[serde(rename = "name")]
376            #[serde(default)]
377            name: Strict<Option<String>>,
378            #[serde(rename = "projectId")]
379            project_id: Strict<String>,
380        }
381        let wire = __Wire::deserialize(deserializer)?;
382        Ok(Self {
383            id: wire.id.0,
384            name: wire.name.0,
385            project_id: wire.project_id.0,
386        })
387    }
388}
389
390/// One live page of Workload records, ordered by ID. Continuations expire after one hour and bind
391/// the credential, operation, filters and page size.
392#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
393pub struct WorkloadPage {
394    /// Contract field items.
395    #[serde(rename = "items")]
396    pub items: Vec<Workload>,
397    /// Contract field nextPageToken.
398    #[serde(rename = "nextPageToken")]
399    #[serde(default, skip_serializing_if = "Option::is_none")]
400    pub next_page_token: Option<String>,
401}
402impl<'de> DeserializeStrict<'de> for WorkloadPage {
403    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
404        #[derive(Deserialize)]
405        #[serde(deny_unknown_fields)]
406        struct __Wire {
407            #[serde(rename = "items")]
408            items: Strict<Vec<Workload>>,
409            #[serde(rename = "nextPageToken")]
410            #[serde(default)]
411            next_page_token: Strict<Option<String>>,
412        }
413        let wire = __Wire::deserialize(deserializer)?;
414        Ok(Self {
415            items: wire.items.0,
416            next_page_token: wire.next_page_token.0,
417        })
418    }
419}
420
421/// The created machine identity.
422#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
423pub struct WorkloadResponse {
424    /// Contract field workload.
425    #[serde(rename = "workload")]
426    pub workload: Workload,
427}
428impl<'de> DeserializeStrict<'de> for WorkloadResponse {
429    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
430        #[derive(Deserialize)]
431        #[serde(deny_unknown_fields)]
432        struct __Wire {
433            #[serde(rename = "workload")]
434            workload: Strict<Workload>,
435        }
436        let wire = __Wire::deserialize(deserializer)?;
437        Ok(Self {
438            workload: wire.workload.0,
439        })
440    }
441}
442
443/// Typed errors declared by this service contract.
444#[derive(Clone, Debug, PartialEq, Eq)]
445pub enum WorkloadsError {
446    /// Workloads:Conflict.
447    Conflict { reason: String },
448    /// Workloads:InvalidArgument.
449    InvalidArgument { reason: String },
450    /// Workloads:LimitReached.
451    LimitReached { reason: String },
452    /// Workloads:NotFound.
453    NotFound { resource: String },
454}
455impl WorkloadsError {
456    /// HTTP status for this declared error.
457    pub fn status(&self) -> u16 {
458        match self {
459            Self::Conflict { .. } => 409,
460            Self::InvalidArgument { .. } => 400,
461            Self::LimitReached { .. } => 412,
462            Self::NotFound { .. } => 404,
463        }
464    }
465    /// Serialize only contract-declared error parameters.
466    pub fn envelope(&self, instance: String) -> ErrorEnvelope {
467        match self {
468            Self::Conflict { reason } => {
469                let mut envelope = ErrorEnvelope::new("CONFLICT", "Workloads:Conflict", instance);
470                envelope
471                    .parameters
472                    .insert("reason".into(), reason.to_string());
473                envelope
474            }
475            Self::InvalidArgument { reason } => {
476                let mut envelope =
477                    ErrorEnvelope::new("INVALID_ARGUMENT", "Workloads:InvalidArgument", instance);
478                envelope
479                    .parameters
480                    .insert("reason".into(), reason.to_string());
481                envelope
482            }
483            Self::LimitReached { reason } => {
484                let mut envelope =
485                    ErrorEnvelope::new("FAILED_PRECONDITION", "Workloads:LimitReached", instance);
486                envelope
487                    .parameters
488                    .insert("reason".into(), reason.to_string());
489                envelope
490            }
491            Self::NotFound { resource } => {
492                let mut envelope = ErrorEnvelope::new("NOT_FOUND", "Workloads:NotFound", instance);
493                envelope
494                    .parameters
495                    .insert("resource".into(), resource.to_string());
496                envelope
497            }
498        }
499    }
500    /// Decode a declared error and its typed error parameters.
501    pub fn decode_envelope(envelope: &ErrorEnvelope) -> Option<Self> {
502        if envelope.error_instance_id.len() != 36
503            || Uuid::parse_str(&envelope.error_instance_id).is_err()
504        {
505            return None;
506        }
507        match envelope.error_name.as_str() {
508            "Workloads:Conflict" => {
509                if envelope.error_code != "CONFLICT" {
510                    return None;
511                }
512                let reason = envelope.parameters.get("reason")?.clone();
513                Some(Self::Conflict { reason })
514            }
515            "Workloads:InvalidArgument" => {
516                if envelope.error_code != "INVALID_ARGUMENT" {
517                    return None;
518                }
519                let reason = envelope.parameters.get("reason")?.clone();
520                Some(Self::InvalidArgument { reason })
521            }
522            "Workloads:LimitReached" => {
523                if envelope.error_code != "FAILED_PRECONDITION" {
524                    return None;
525                }
526                let reason = envelope.parameters.get("reason")?.clone();
527                Some(Self::LimitReached { reason })
528            }
529            "Workloads:NotFound" => {
530                if envelope.error_code != "NOT_FOUND" {
531                    return None;
532                }
533                let resource = envelope.parameters.get("resource")?.clone();
534                Some(Self::NotFound { resource })
535            }
536            _ => None,
537        }
538    }
539}
540impl Display for WorkloadsError {
541    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
542        match self {
543            Self::Conflict { reason } => {
544                write!(formatter, "Workloads:Conflict (reason: {})", reason)
545            }
546            Self::InvalidArgument { reason } => {
547                write!(formatter, "Workloads:InvalidArgument (reason: {})", reason)
548            }
549            Self::LimitReached { reason } => {
550                write!(formatter, "Workloads:LimitReached (reason: {})", reason)
551            }
552            Self::NotFound { resource } => {
553                write!(formatter, "Workloads:NotFound (resource: {})", resource)
554            }
555        }
556    }
557}
558impl StdError for WorkloadsError {}
559impl ServiceError for WorkloadsError {
560    fn to_envelope(&self, instance: String) -> ErrorEnvelope {
561        Self::envelope(self, instance)
562    }
563    fn from_envelope(envelope: &ErrorEnvelope) -> Option<Self> {
564        Self::decode_envelope(envelope)
565    }
566}
567#[automatically_derived]
568impl Default for CreateWorkloadRequest {
569    fn default() -> Self {
570        Self { name: None }
571    }
572}
573#[automatically_derived]
574impl Default for PageRequest {
575    fn default() -> Self {
576        Self {
577            page_size: None,
578            page_token: None,
579        }
580    }
581}