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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Data types used in RPC calls (usually control-related) on a wasmCloud lattice

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{ComponentId, LinkName, Result, WitNamespace, WitPackage};

/// A host response to a request to start a component.
///
/// This acknowledgement confirms that the host has enough capacity to start the component
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ComponentAuctionAck {
    /// The original component reference used for the auction
    #[serde(default)]
    pub(crate) component_ref: String,
    /// The unique component identifier that the auctioner can use for this component
    #[serde(default)]
    pub(crate) component_id: String,
    /// The host ID of the "bidder" for this auction.
    #[serde(default)]
    pub(crate) host_id: String,
    /// Constraints that were used in the auction
    #[serde(default)]
    pub(crate) constraints: BTreeMap<String, String>,
}

impl ComponentAuctionAck {
    #[must_use]
    pub fn from_component_host_and_constraints(
        component_ref: &str,
        component_id: &str,
        host_id: &str,
        constraints: impl Into<BTreeMap<String, String>>,
    ) -> Self {
        Self {
            component_ref: component_ref.into(),
            component_id: component_id.into(),
            host_id: host_id.into(),
            constraints: constraints.into(),
        }
    }

    /// Get the component ref for the auction acknowledgement
    #[must_use]
    pub fn component_ref(&self) -> &str {
        self.component_ref.as_ref()
    }

    /// Get the component ID for the auction acknowledgement
    #[must_use]
    pub fn component_id(&self) -> &str {
        self.component_ref.as_ref()
    }

    /// Get the host ID for the auction acknowledgement
    #[must_use]
    pub fn host_id(&self) -> &str {
        self.host_id.as_ref()
    }

    /// Get the constraints acknowledged by the auction acknowledgement
    #[must_use]
    pub fn constraints(&self) -> &BTreeMap<String, String> {
        &self.constraints
    }

    pub fn builder() -> ComponentAuctionAckBuilder {
        ComponentAuctionAckBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
pub struct ComponentAuctionAckBuilder {
    component_ref: Option<String>,
    component_id: Option<String>,
    host_id: Option<String>,
    constraints: Option<BTreeMap<String, String>>,
}

impl ComponentAuctionAckBuilder {
    #[must_use]
    pub fn component_ref(mut self, v: String) -> Self {
        self.component_ref = Some(v);
        self
    }

    #[must_use]
    pub fn component_id(mut self, v: String) -> Self {
        self.component_id = Some(v);
        self
    }

    #[must_use]
    pub fn host_id(mut self, v: String) -> Self {
        self.host_id = Some(v);
        self
    }

    #[must_use]
    pub fn constraints(mut self, v: BTreeMap<String, String>) -> Self {
        self.constraints = Some(v);
        self
    }

    pub fn build(self) -> Result<ComponentAuctionAck> {
        Ok(ComponentAuctionAck {
            component_ref: self
                .component_ref
                .ok_or_else(|| "component_ref is required".to_string())?,
            component_id: self
                .component_id
                .ok_or_else(|| "component_id is required".to_string())?,
            host_id: self
                .host_id
                .ok_or_else(|| "host_id is required".to_string())?,
            constraints: self.constraints.unwrap_or_default(),
        })
    }
}

/// A request to locate suitable hosts for a given component
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ComponentAuctionRequest {
    /// The image reference, file or OCI, for this component.
    #[serde(default)]
    pub(crate) component_ref: String,
    /// The unique identifier to be used for this component.
    ///
    /// The host will ensure that no other component with the same ID is running on the host
    pub(crate) component_id: ComponentId,
    /// The set of constraints that must match the labels of a suitable target host
    pub(crate) constraints: BTreeMap<String, String>,
}

impl ComponentAuctionRequest {
    /// Get the component ref for the auction request
    #[must_use]
    pub fn component_ref(&self) -> &str {
        self.component_ref.as_ref()
    }

    /// Get the component ID for the auction request
    #[must_use]
    pub fn component_id(&self) -> &str {
        self.component_ref.as_ref()
    }

    /// Get the constraints for the auction request
    #[must_use]
    pub fn constraints(&self) -> &BTreeMap<String, String> {
        &self.constraints
    }

    pub fn builder() -> ComponentAuctionRequestBuilder {
        ComponentAuctionRequestBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
pub struct ComponentAuctionRequestBuilder {
    component_ref: Option<String>,
    component_id: Option<ComponentId>,
    constraints: Option<BTreeMap<String, String>>,
}

impl ComponentAuctionRequestBuilder {
    #[must_use]
    pub fn component_ref(mut self, v: String) -> Self {
        self.component_ref = Some(v);
        self
    }

    #[must_use]
    pub fn component_id(mut self, v: String) -> Self {
        self.component_id = Some(v);
        self
    }

    #[must_use]
    pub fn constraints(mut self, v: BTreeMap<String, String>) -> Self {
        self.constraints = Some(v);
        self
    }

    pub fn build(self) -> Result<ComponentAuctionRequest> {
        Ok(ComponentAuctionRequest {
            component_ref: self
                .component_ref
                .ok_or_else(|| "component_ref is required".to_string())?,
            component_id: self
                .component_id
                .ok_or_else(|| "component_id is required".to_string())?,
            constraints: self.constraints.unwrap_or_default(),
        })
    }
}

/// A host response to a request to start a provider.
///
/// This acknowledgement confirms the host has enough capacity to
/// start the provider and that the provider is not already running on the host
///
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ProviderAuctionAck {
    /// The host ID of the "bidder" for this auction
    #[serde(default)]
    pub(crate) host_id: String,
    /// The original provider reference provided for the auction
    #[serde(default)]
    pub(crate) provider_ref: String,
    /// The unique component identifier that the auctioner can use for this provider
    #[serde(default)]
    pub(crate) provider_id: String,
    /// The constraints provided for the auction
    #[serde(default)]
    pub(crate) constraints: BTreeMap<String, String>,
}

impl ProviderAuctionAck {
    /// Get the Host ID for the provider auction acknowledgement
    #[must_use]
    pub fn host_id(&self) -> &str {
        self.host_id.as_ref()
    }

    /// Get the provider ref for the provider auction acknowledgement
    #[must_use]
    pub fn provider_ref(&self) -> &str {
        self.provider_ref.as_ref()
    }

    /// Get the provider ID for the provider auction acknowledgement
    #[must_use]
    pub fn provider_id(&self) -> &str {
        self.provider_id.as_ref()
    }

    /// Get the constraints for the provider auction acknowledgement
    #[must_use]
    pub fn constraints(&self) -> &BTreeMap<String, String> {
        &self.constraints
    }

    #[must_use]
    pub fn builder() -> ProviderAuctionAckBuilder {
        ProviderAuctionAckBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
pub struct ProviderAuctionAckBuilder {
    host_id: Option<String>,
    provider_ref: Option<String>,
    provider_id: Option<String>,
    constraints: Option<BTreeMap<String, String>>,
}

impl ProviderAuctionAckBuilder {
    #[must_use]
    pub fn provider_ref(mut self, v: String) -> Self {
        self.provider_ref = Some(v);
        self
    }

    #[must_use]
    pub fn provider_id(mut self, v: String) -> Self {
        self.provider_id = Some(v);
        self
    }

    #[must_use]
    pub fn host_id(mut self, v: String) -> Self {
        self.host_id = Some(v);
        self
    }

    #[must_use]
    pub fn constraints(mut self, v: BTreeMap<String, String>) -> Self {
        self.constraints = Some(v);
        self
    }

    pub fn build(self) -> Result<ProviderAuctionAck> {
        Ok(ProviderAuctionAck {
            provider_ref: self
                .provider_ref
                .ok_or_else(|| "provider_ref is required".to_string())?,
            provider_id: self
                .provider_id
                .ok_or_else(|| "provider_id is required".to_string())?,
            host_id: self
                .host_id
                .ok_or_else(|| "host_id is required".to_string())?,
            constraints: self.constraints.unwrap_or_default(),
        })
    }
}

/// A request to locate a suitable host for a capability provider.
///
/// The provider's unique identity is used to rule out hosts on which the
/// provider is already running.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ProviderAuctionRequest {
    /// The image reference, file or OCI, for this provider.
    #[serde(default)]
    pub(crate) provider_ref: String,

    /// The unique identifier to be used for this provider. The host will ensure
    /// that no other provider with the same ID is running on the host
    pub(crate) provider_id: ComponentId,

    /// The set of constraints that must match the labels of a suitable target host
    pub(crate) constraints: BTreeMap<String, String>,
}

impl ProviderAuctionRequest {
    /// Get the provider ref for the auction request
    #[must_use]
    pub fn provider_ref(&self) -> &str {
        self.provider_ref.as_ref()
    }

    /// Get the provider ID for the auction request
    #[must_use]
    pub fn provider_id(&self) -> &str {
        self.provider_id.as_ref()
    }

    /// Get the constraints acknowledged by the auction request
    #[must_use]
    pub fn constraints(&self) -> &BTreeMap<String, String> {
        &self.constraints
    }

    /// Build a new [`ProviderAuctionRequest`]
    #[must_use]
    pub fn builder() -> ProviderAuctionRequestBuilder {
        ProviderAuctionRequestBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
pub struct ProviderAuctionRequestBuilder {
    provider_ref: Option<String>,
    provider_id: Option<ComponentId>,
    constraints: Option<BTreeMap<String, String>>,
}

impl ProviderAuctionRequestBuilder {
    #[must_use]
    pub fn provider_ref(mut self, v: String) -> Self {
        self.provider_ref = Some(v);
        self
    }

    #[must_use]
    pub fn provider_id(mut self, v: String) -> Self {
        self.provider_id = Some(v);
        self
    }

    #[must_use]
    pub fn constraints(mut self, v: BTreeMap<String, String>) -> Self {
        self.constraints = Some(v);
        self
    }

    pub fn build(self) -> Result<ProviderAuctionRequest> {
        Ok(ProviderAuctionRequest {
            provider_ref: self
                .provider_ref
                .ok_or_else(|| "provider_ref is required".to_string())?,
            provider_id: self
                .provider_id
                .ok_or_else(|| "provider_id is required".to_string())?,
            constraints: self.constraints.unwrap_or_default(),
        })
    }
}

/// A request to remove a link definition and detach the relevant component
/// from the given provider
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct DeleteInterfaceLinkDefinitionRequest {
    /// The source component's identifier.
    pub(crate) source_id: ComponentId,

    /// Name of the link. Not providing this is equivalent to specifying Some("default")
    #[serde(default = "default_link_name")]
    pub(crate) name: LinkName,

    /// WIT namespace of the link, e.g. `wasi` in `wasi:keyvalue/readwrite.get`
    pub(crate) wit_namespace: WitNamespace,

    /// WIT package of the link, e.g. `keyvalue` in `wasi:keyvalue/readwrite.get`
    pub(crate) wit_package: WitPackage,
}

impl DeleteInterfaceLinkDefinitionRequest {
    pub fn from_source_and_link_metadata(
        source_id: &str,
        name: &str,
        wit_ns: &str,
        wit_pkg: &str,
    ) -> Self {
        Self {
            source_id: source_id.into(),
            name: name.into(),
            wit_namespace: wit_ns.into(),
            wit_package: wit_pkg.into(),
        }
    }
    /// Get the source (component/provider) ID for delete request
    #[must_use]
    pub fn source_id(&self) -> &str {
        self.source_id.as_ref()
    }

    /// Get the link name for the link deletion request(or "default")
    #[must_use]
    pub fn link_name(&self) -> &str {
        self.name.as_ref()
    }

    /// Get the WIT namespace relevant to the link deletion request
    #[must_use]
    pub fn wit_namespace(&self) -> &str {
        self.wit_namespace.as_ref()
    }

    /// Get the WIT package relevant to the link deletion request
    #[must_use]
    pub fn wit_package(&self) -> &str {
        self.wit_package.as_ref()
    }

    #[must_use]
    pub fn builder() -> DeleteInterfaceLinkDefinitionRequestBuilder {
        DeleteInterfaceLinkDefinitionRequestBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
pub struct DeleteInterfaceLinkDefinitionRequestBuilder {
    source_id: Option<ComponentId>,
    name: Option<LinkName>,
    wit_namespace: Option<WitNamespace>,
    wit_package: Option<WitPackage>,
}

impl DeleteInterfaceLinkDefinitionRequestBuilder {
    pub fn source_id(mut self, v: String) -> Self {
        self.source_id = Some(v);
        self
    }

    pub fn name(mut self, v: String) -> Self {
        self.name = Some(v);
        self
    }

    pub fn wit_namespace(mut self, v: String) -> Self {
        self.wit_namespace = Some(v);
        self
    }

    pub fn wit_package(mut self, v: String) -> Self {
        self.wit_package = Some(v);
        self
    }

    pub fn build(self) -> Result<DeleteInterfaceLinkDefinitionRequest> {
        Ok(DeleteInterfaceLinkDefinitionRequest {
            source_id: self
                .source_id
                .ok_or_else(|| "source_id is required".to_string())?,
            name: self.name.ok_or_else(|| "name is required".to_string())?,
            wit_namespace: self
                .wit_namespace
                .ok_or_else(|| "wit_namespace is required".to_string())?,
            wit_package: self
                .wit_package
                .ok_or_else(|| "wit_package is required".to_string())?,
        })
    }
}

/// Helper function to provide a default link name
fn default_link_name() -> LinkName {
    "default".to_string()
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::{
        ComponentAuctionAck, ComponentAuctionRequest, DeleteInterfaceLinkDefinitionRequest,
        ProviderAuctionAck, ProviderAuctionRequest,
    };

    #[test]
    fn component_auction_ack_builder() {
        assert_eq!(
            ComponentAuctionAck {
                component_ref: "component_ref".into(),
                component_id: "component_id".into(),
                host_id: "host_id".into(),
                constraints: BTreeMap::from([("a".into(), "b".into())])
            },
            ComponentAuctionAck::builder()
                .component_ref("component_ref".into())
                .component_id("component_id".into())
                .host_id("host_id".into())
                .constraints(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }

    #[test]
    fn component_auction_request_builder() {
        assert_eq!(
            ComponentAuctionRequest {
                component_ref: "component_ref".into(),
                component_id: "component_id".into(),
                constraints: BTreeMap::from([("a".into(), "b".into())])
            },
            ComponentAuctionRequest::builder()
                .component_ref("component_ref".into())
                .component_id("component_id".into())
                .constraints(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }

    #[test]
    fn provider_auction_ack_builder() {
        assert_eq!(
            ProviderAuctionAck {
                provider_ref: "provider_ref".into(),
                provider_id: "provider_id".into(),
                host_id: "host_id".into(),
                constraints: BTreeMap::from([("a".into(), "b".into())])
            },
            ProviderAuctionAck::builder()
                .provider_ref("provider_ref".into())
                .provider_id("provider_id".into())
                .host_id("host_id".into())
                .constraints(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }

    #[test]
    fn provider_auction_request_builder() {
        assert_eq!(
            ProviderAuctionRequest {
                provider_ref: "provider_ref".into(),
                provider_id: "provider_id".into(),
                constraints: BTreeMap::from([("a".into(), "b".into())])
            },
            ProviderAuctionRequest::builder()
                .provider_ref("provider_ref".into())
                .provider_id("provider_id".into())
                .constraints(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }

    #[test]
    fn delete_interface_link_definition_request_builder() {
        assert_eq!(
            DeleteInterfaceLinkDefinitionRequest {
                source_id: "source_id".into(),
                name: "name".into(),
                wit_namespace: "wit_namespace".into(),
                wit_package: "wit_package".into(),
            },
            DeleteInterfaceLinkDefinitionRequest::builder()
                .source_id("source_id".into())
                .name("name".into())
                .wit_namespace("wit_namespace".into())
                .wit_package("wit_package".into())
                .build()
                .unwrap()
        )
    }
}