spiral-sdk 0.1.11

Rust SDK for Spiral resource and table APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Generated by tools/service-contracts/rust.py. Do not edit.
#![doc = "Generated transport-independent contract objects."]

use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use spiral_contract::error::{ErrorEnvelope, ServiceError};
use spiral_contract::strict::{DeserializeStrict, Strict};
use uuid::Uuid;

/// A public JSON Web Key with a key identifier. Private key material is rejected.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateKeyRequest {
    /// Contract field jwk.
    #[serde(rename = "jwk")]
    pub jwk: Value,
}
impl<'de> DeserializeStrict<'de> for CreateKeyRequest {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "jwk")]
            jwk: Strict<Value>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self { jwk: wire.jwk.0 })
    }
}

/// An external issuer and claim conditions trusted by a Workload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreatePolicyRequest {
    /// Contract field audience.
    #[serde(rename = "audience")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub audience: Option<String>,
    /// Contract field conditions.
    #[serde(rename = "conditions")]
    pub conditions: Vec<PolicyCondition>,
    /// Contract field issuer.
    #[serde(rename = "issuer")]
    pub issuer: String,
    /// Contract field name.
    #[serde(rename = "name")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}
impl<'de> DeserializeStrict<'de> for CreatePolicyRequest {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "audience")]
            #[serde(default)]
            audience: Strict<Option<String>>,
            #[serde(rename = "conditions")]
            conditions: Strict<Vec<PolicyCondition>>,
            #[serde(rename = "issuer")]
            issuer: Strict<String>,
            #[serde(rename = "name")]
            #[serde(default)]
            name: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            audience: wire.audience.0,
            conditions: wire.conditions.0,
            issuer: wire.issuer.0,
            name: wire.name.0,
        })
    }
}

/// Properties for a machine identity.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateWorkloadRequest {
    /// Contract field name.
    #[serde(rename = "name")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}
impl<'de> DeserializeStrict<'de> for CreateWorkloadRequest {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "name")]
            #[serde(default)]
            name: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self { name: wire.name.0 })
    }
}

/// A public JSON Web Key whose assertions authenticate the Workload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Key {
    /// Contract field jwk.
    #[serde(rename = "jwk")]
    pub jwk: Value,
    /// Contract field kid.
    #[serde(rename = "kid")]
    pub kid: String,
    /// Contract field workloadId.
    #[serde(rename = "workloadId")]
    pub workload_id: String,
}
impl<'de> DeserializeStrict<'de> for Key {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "jwk")]
            jwk: Strict<Value>,
            #[serde(rename = "kid")]
            kid: Strict<String>,
            #[serde(rename = "workloadId")]
            workload_id: Strict<String>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            jwk: wire.jwk.0,
            kid: wire.kid.0,
            workload_id: wire.workload_id.0,
        })
    }
}

/// One live page of Key records, ordered by ID. Continuations expire after one hour and bind the
/// credential, operation, filters and page size.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyPage {
    /// Contract field items.
    #[serde(rename = "items")]
    pub items: Vec<Key>,
    /// Contract field nextPageToken.
    #[serde(rename = "nextPageToken")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}
impl<'de> DeserializeStrict<'de> for KeyPage {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "items")]
            items: Strict<Vec<Key>>,
            #[serde(rename = "nextPageToken")]
            #[serde(default)]
            next_page_token: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            items: wire.items.0,
            next_page_token: wire.next_page_token.0,
        })
    }
}

/// The registered public key.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyResponse {
    /// Contract field key.
    #[serde(rename = "key")]
    pub key: Key,
}
impl<'de> DeserializeStrict<'de> for KeyResponse {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "key")]
            key: Strict<Key>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self { key: wire.key.0 })
    }
}

/// Continuation request. Page size defaults to 100 and must be 1..=1000. Keep the same credential,
/// filters and page size when following a continuation.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PageRequest {
    /// Contract field pageSize.
    #[serde(rename = "pageSize")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<u32>,
    /// Contract field pageToken.
    #[serde(rename = "pageToken")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}
impl<'de> DeserializeStrict<'de> for PageRequest {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "pageSize")]
            #[serde(default)]
            page_size: Strict<Option<u32>>,
            #[serde(rename = "pageToken")]
            #[serde(default)]
            page_token: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            page_size: wire.page_size.0,
            page_token: wire.page_token.0,
        })
    }
}

/// A Workload trust policy.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Policy {
    /// Contract field audience.
    #[serde(rename = "audience")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub audience: Option<String>,
    /// Contract field conditions.
    #[serde(rename = "conditions")]
    pub conditions: Vec<PolicyCondition>,
    /// Contract field id.
    #[serde(rename = "id")]
    pub id: String,
    /// Contract field issuer.
    #[serde(rename = "issuer")]
    pub issuer: String,
    /// Contract field name.
    #[serde(rename = "name")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Contract field workloadId.
    #[serde(rename = "workloadId")]
    pub workload_id: String,
}
impl<'de> DeserializeStrict<'de> for Policy {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "audience")]
            #[serde(default)]
            audience: Strict<Option<String>>,
            #[serde(rename = "conditions")]
            conditions: Strict<Vec<PolicyCondition>>,
            #[serde(rename = "id")]
            id: Strict<String>,
            #[serde(rename = "issuer")]
            issuer: Strict<String>,
            #[serde(rename = "name")]
            #[serde(default)]
            name: Strict<Option<String>>,
            #[serde(rename = "workloadId")]
            workload_id: Strict<String>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            audience: wire.audience.0,
            conditions: wire.conditions.0,
            id: wire.id.0,
            issuer: wire.issuer.0,
            name: wire.name.0,
            workload_id: wire.workload_id.0,
        })
    }
}

/// One JWT claim that an external identity must match.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyCondition {
    /// Contract field key.
    #[serde(rename = "key")]
    pub key: String,
    /// Contract field value.
    #[serde(rename = "value")]
    pub value: String,
}
impl<'de> DeserializeStrict<'de> for PolicyCondition {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "key")]
            key: Strict<String>,
            #[serde(rename = "value")]
            value: Strict<String>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            key: wire.key.0,
            value: wire.value.0,
        })
    }
}

/// One live page of Policy records, ordered by ID. Continuations expire after one hour and bind the
/// credential, operation, filters and page size.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyPage {
    /// Contract field items.
    #[serde(rename = "items")]
    pub items: Vec<Policy>,
    /// Contract field nextPageToken.
    #[serde(rename = "nextPageToken")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}
impl<'de> DeserializeStrict<'de> for PolicyPage {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "items")]
            items: Strict<Vec<Policy>>,
            #[serde(rename = "nextPageToken")]
            #[serde(default)]
            next_page_token: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            items: wire.items.0,
            next_page_token: wire.next_page_token.0,
        })
    }
}

/// The created trust policy.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyResponse {
    /// Contract field policy.
    #[serde(rename = "policy")]
    pub policy: Policy,
}
impl<'de> DeserializeStrict<'de> for PolicyResponse {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "policy")]
            policy: Strict<Policy>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            policy: wire.policy.0,
        })
    }
}

/// A machine identity scoped to one Project.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Workload {
    /// Contract field id.
    #[serde(rename = "id")]
    pub id: String,
    /// Contract field name.
    #[serde(rename = "name")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Contract field projectId.
    #[serde(rename = "projectId")]
    pub project_id: String,
}
impl<'de> DeserializeStrict<'de> for Workload {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "id")]
            id: Strict<String>,
            #[serde(rename = "name")]
            #[serde(default)]
            name: Strict<Option<String>>,
            #[serde(rename = "projectId")]
            project_id: Strict<String>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            id: wire.id.0,
            name: wire.name.0,
            project_id: wire.project_id.0,
        })
    }
}

/// One live page of Workload records, ordered by ID. Continuations expire after one hour and bind
/// the credential, operation, filters and page size.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkloadPage {
    /// Contract field items.
    #[serde(rename = "items")]
    pub items: Vec<Workload>,
    /// Contract field nextPageToken.
    #[serde(rename = "nextPageToken")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}
impl<'de> DeserializeStrict<'de> for WorkloadPage {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "items")]
            items: Strict<Vec<Workload>>,
            #[serde(rename = "nextPageToken")]
            #[serde(default)]
            next_page_token: Strict<Option<String>>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            items: wire.items.0,
            next_page_token: wire.next_page_token.0,
        })
    }
}

/// The created machine identity.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkloadResponse {
    /// Contract field workload.
    #[serde(rename = "workload")]
    pub workload: Workload,
}
impl<'de> DeserializeStrict<'de> for WorkloadResponse {
    fn deserialize_strict<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct __Wire {
            #[serde(rename = "workload")]
            workload: Strict<Workload>,
        }
        let wire = __Wire::deserialize(deserializer)?;
        Ok(Self {
            workload: wire.workload.0,
        })
    }
}

/// Typed errors declared by this service contract.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WorkloadsError {
    /// Workloads:Conflict.
    Conflict { reason: String },
    /// Workloads:InvalidArgument.
    InvalidArgument { reason: String },
    /// Workloads:LimitReached.
    LimitReached { reason: String },
    /// Workloads:NotFound.
    NotFound { resource: String },
}
impl WorkloadsError {
    /// HTTP status for this declared error.
    pub fn status(&self) -> u16 {
        match self {
            Self::Conflict { .. } => 409,
            Self::InvalidArgument { .. } => 400,
            Self::LimitReached { .. } => 412,
            Self::NotFound { .. } => 404,
        }
    }
    /// Serialize only contract-declared error parameters.
    pub fn envelope(&self, instance: String) -> ErrorEnvelope {
        match self {
            Self::Conflict { reason } => {
                let mut envelope = ErrorEnvelope::new("CONFLICT", "Workloads:Conflict", instance);
                envelope
                    .parameters
                    .insert("reason".into(), reason.to_string());
                envelope
            }
            Self::InvalidArgument { reason } => {
                let mut envelope =
                    ErrorEnvelope::new("INVALID_ARGUMENT", "Workloads:InvalidArgument", instance);
                envelope
                    .parameters
                    .insert("reason".into(), reason.to_string());
                envelope
            }
            Self::LimitReached { reason } => {
                let mut envelope =
                    ErrorEnvelope::new("FAILED_PRECONDITION", "Workloads:LimitReached", instance);
                envelope
                    .parameters
                    .insert("reason".into(), reason.to_string());
                envelope
            }
            Self::NotFound { resource } => {
                let mut envelope = ErrorEnvelope::new("NOT_FOUND", "Workloads:NotFound", instance);
                envelope
                    .parameters
                    .insert("resource".into(), resource.to_string());
                envelope
            }
        }
    }
    /// Decode a declared error and its typed error parameters.
    pub fn decode_envelope(envelope: &ErrorEnvelope) -> Option<Self> {
        if envelope.error_instance_id.len() != 36
            || Uuid::parse_str(&envelope.error_instance_id).is_err()
        {
            return None;
        }
        match envelope.error_name.as_str() {
            "Workloads:Conflict" => {
                if envelope.error_code != "CONFLICT" {
                    return None;
                }
                let reason = envelope.parameters.get("reason")?.clone();
                Some(Self::Conflict { reason })
            }
            "Workloads:InvalidArgument" => {
                if envelope.error_code != "INVALID_ARGUMENT" {
                    return None;
                }
                let reason = envelope.parameters.get("reason")?.clone();
                Some(Self::InvalidArgument { reason })
            }
            "Workloads:LimitReached" => {
                if envelope.error_code != "FAILED_PRECONDITION" {
                    return None;
                }
                let reason = envelope.parameters.get("reason")?.clone();
                Some(Self::LimitReached { reason })
            }
            "Workloads:NotFound" => {
                if envelope.error_code != "NOT_FOUND" {
                    return None;
                }
                let resource = envelope.parameters.get("resource")?.clone();
                Some(Self::NotFound { resource })
            }
            _ => None,
        }
    }
}
impl Display for WorkloadsError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Conflict { reason } => {
                write!(formatter, "Workloads:Conflict (reason: {})", reason)
            }
            Self::InvalidArgument { reason } => {
                write!(formatter, "Workloads:InvalidArgument (reason: {})", reason)
            }
            Self::LimitReached { reason } => {
                write!(formatter, "Workloads:LimitReached (reason: {})", reason)
            }
            Self::NotFound { resource } => {
                write!(formatter, "Workloads:NotFound (resource: {})", resource)
            }
        }
    }
}
impl StdError for WorkloadsError {}
impl ServiceError for WorkloadsError {
    fn to_envelope(&self, instance: String) -> ErrorEnvelope {
        Self::envelope(self, instance)
    }
    fn from_envelope(envelope: &ErrorEnvelope) -> Option<Self> {
        Self::decode_envelope(envelope)
    }
}
#[automatically_derived]
impl Default for CreateWorkloadRequest {
    fn default() -> Self {
        Self { name: None }
    }
}
#[automatically_derived]
impl Default for PageRequest {
    fn default() -> Self {
        Self {
            page_size: None,
            page_token: None,
        }
    }
}