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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Data types used when interacting with the control interface of a wasmCloud lattice

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{ComponentId, Result};

/// A control interface response that wraps a response payload, a success flag, and a message
/// with additional context if necessary.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct CtlResponse<T> {
    /// Whether the request succeeded
    pub(crate) success: bool,
    /// A message with additional context about the response
    pub(crate) message: String,
    /// The response data, if any
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) response: Option<T>,
}

impl<T> CtlResponse<T> {
    /// Create a [`CtlResponse`] with provided response data
    #[must_use]
    pub fn ok(response: T) -> Self {
        CtlResponse {
            success: true,
            message: String::new(),
            response: Some(response),
        }
    }

    /// Get whether the request succeeded
    #[must_use]
    pub fn succeeded(&self) -> bool {
        self.success
    }

    /// Get the message included in the response
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Get the internal data of the response (if any)
    #[must_use]
    pub fn data(&self) -> Option<&T> {
        self.response.as_ref()
    }

    /// Take the internal data
    #[must_use]
    pub fn into_data(self) -> Option<T> {
        self.response
    }
}

impl CtlResponse<()> {
    /// Helper function to return a successful response without
    /// a message or a payload.
    #[must_use]
    pub fn success(message: String) -> Self {
        CtlResponse {
            success: true,
            message,
            response: None,
        }
    }

    /// Helper function to return an unsuccessful response with
    /// a message but no payload. Note that this implicitly is
    /// typing the inner payload as `()` for efficiency.
    #[must_use]
    pub fn error(message: &str) -> Self {
        CtlResponse {
            success: false,
            message: message.to_string(),
            response: None,
        }
    }
}

/// Command a host to scale a component
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ScaleComponentCommand {
    /// Image reference for the component.
    #[serde(default)]
    pub(crate) component_ref: String,
    /// Unique identifier of the component to scale.
    pub(crate) component_id: ComponentId,
    /// Optional set of annotations used to describe the nature of this component scale command. For
    /// example, autonomous agents may wish to "tag" scale requests as part of a given deployment
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) annotations: Option<BTreeMap<String, String>>,
    /// The maximum number of concurrent executing instances of this component. Setting this to `0` will
    /// stop the component.
    // NOTE: renaming to `count` lets us remain backwards compatible for a few minor versions
    #[serde(default, alias = "count", rename = "count")]
    pub(crate) max_instances: u32,
    /// Host ID on which to scale this component
    #[serde(default)]
    pub(crate) host_id: String,
    /// A list of named configs to use for this component. It is not required to specify a config.
    /// Configs are merged together before being given to the component, with values from the right-most
    /// config in the list taking precedence. For example, given ordered configs foo {a: 1, b: 2},
    /// bar {b: 3, c: 4}, and baz {c: 5, d: 6}, the resulting config will be: {a: 1, b: 3, c: 5, d:
    /// 6}
    #[serde(default)]
    pub(crate) config: Vec<String>,
    #[serde(default)]
    /// Whether to perform an update if the details of the component (ex. component ID) change as
    /// part of the scale request.
    ///
    /// Normally this is implemented by the receiver (ex. wasmcloud host) as a *separate* update component call
    /// being made shortly after this command (scale) is processed.
    pub(crate) allow_update: bool,
}

impl ScaleComponentCommand {
    #[must_use]
    pub fn component_ref(&self) -> &str {
        &self.component_ref
    }

    #[must_use]
    pub fn component_id(&self) -> &str {
        &self.component_id
    }

    #[must_use]
    pub fn allow_update(&self) -> bool {
        self.allow_update
    }

    #[must_use]
    pub fn config(&self) -> &Vec<String> {
        &self.config
    }

    #[must_use]
    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
        self.annotations.as_ref()
    }

    #[must_use]
    pub fn max_instances(&self) -> u32 {
        self.max_instances
    }

    #[must_use]
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

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

/// Builder that produces [`ScaleComponentCommand`]s
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct ScaleComponentCommandBuilder {
    component_ref: Option<String>,
    component_id: Option<ComponentId>,
    annotations: Option<BTreeMap<String, String>>,
    max_instances: Option<u32>,
    host_id: Option<String>,
    config: Option<Vec<String>>,
    allow_update: Option<bool>,
}

impl ScaleComponentCommandBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn component_ref(mut self, v: &str) -> Self {
        self.component_ref = Some(v.into());
        self
    }

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

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

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

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

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

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

    pub fn build(self) -> Result<ScaleComponentCommand> {
        Ok(ScaleComponentCommand {
            component_ref: self
                .component_ref
                .ok_or_else(|| "component ref is required for scaling components".to_string())?,
            component_id: self
                .component_id
                .ok_or_else(|| "component id is required for scaling components".to_string())?,
            annotations: self.annotations,
            max_instances: self.max_instances.unwrap_or(0),
            host_id: self
                .host_id
                .ok_or_else(|| "host id is required for scaling hosts host".to_string())?,
            config: self.config.unwrap_or_default(),
            allow_update: self.allow_update.unwrap_or_default(),
        })
    }
}

/// A command sent to a host requesting a capability provider be started with the
/// given link name and optional configuration.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct StartProviderCommand {
    /// Unique identifier of the provider to start.
    provider_id: ComponentId,
    /// The image reference of the provider to be started
    #[serde(default)]
    provider_ref: String,
    /// The host ID on which to start the provider
    #[serde(default)]
    host_id: String,
    /// A list of named configs to use for this provider. It is not required to specify a config.
    /// Configs are merged together before being given to the provider, with values from the right-most
    /// config in the list taking precedence. For example, given ordered configs foo {a: 1, b: 2},
    /// bar {b: 3, c: 4}, and baz {c: 5, d: 6}, the resulting config will be: {a: 1, b: 3, c: 5, d:
    /// 6}
    #[serde(default)]
    config: Vec<String>,
    /// Optional set of annotations used to describe the nature of this provider start command. For
    /// example, autonomous agents may wish to "tag" start requests as part of a given deployment
    #[serde(default, skip_serializing_if = "Option::is_none")]
    annotations: Option<BTreeMap<String, String>>,
}

impl StartProviderCommand {
    #[must_use]
    pub fn provider_id(&self) -> &str {
        &self.provider_id
    }

    #[must_use]
    pub fn provider_ref(&self) -> &str {
        &self.provider_ref
    }

    #[must_use]
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

    #[must_use]
    pub fn config(&self) -> &Vec<String> {
        &self.config
    }

    #[must_use]
    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
        self.annotations.as_ref()
    }

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

/// A builder that produces [`StartProviderCommand`]s
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StartProviderCommandBuilder {
    host_id: Option<String>,
    provider_id: Option<ComponentId>,
    provider_ref: Option<String>,
    annotations: Option<BTreeMap<String, String>>,
    config: Option<Vec<String>>,
}

impl StartProviderCommandBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn provider_ref(mut self, v: &str) -> Self {
        self.provider_ref = Some(v.into());
        self
    }

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

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

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

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

    pub fn build(self) -> Result<StartProviderCommand> {
        Ok(StartProviderCommand {
            provider_ref: self
                .provider_ref
                .ok_or_else(|| "provider ref is required for starting providers".to_string())?,
            provider_id: self
                .provider_id
                .ok_or_else(|| "provider id is required for starting providers".to_string())?,
            annotations: self.annotations,
            host_id: self
                .host_id
                .ok_or_else(|| "host id is required for starting providers".to_string())?,
            config: self.config.unwrap_or_default(),
        })
    }
}

/// A command sent to request that the given host purge and stop
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct StopHostCommand {
    /// The ID of the target host
    #[serde(default)]
    pub(crate) host_id: String,
    /// An optional timeout, in seconds
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) timeout: Option<u64>,
}

impl StopHostCommand {
    #[must_use]
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

    #[must_use]
    pub fn timeout(&self) -> Option<u64> {
        self.timeout
    }

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

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StopHostCommandBuilder {
    host_id: Option<String>,
    timeout: Option<u64>,
}

impl StopHostCommandBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

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

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

    pub fn build(self) -> Result<StopHostCommand> {
        Ok(StopHostCommand {
            host_id: self
                .host_id
                .ok_or_else(|| "host id is required for stopping host".to_string())?,
            timeout: self.timeout,
        })
    }
}

/// A request to stop the given provider on the indicated host
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct StopProviderCommand {
    /// Host ID on which to stop the provider
    #[serde(default)]
    pub(crate) host_id: String,
    /// Unique identifier for the provider to stop.
    #[serde(default, alias = "provider_ref")]
    pub(crate) provider_id: ComponentId,
}

impl StopProviderCommand {
    #[must_use]
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

    #[must_use]
    pub fn provider_id(&self) -> &str {
        &self.provider_id
    }

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

/// Builder for [`StopProviderCommand`]s
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StopProviderCommandBuilder {
    host_id: Option<String>,
    provider_id: Option<ComponentId>,
}

impl StopProviderCommandBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

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

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

    pub fn build(self) -> Result<StopProviderCommand> {
        Ok(StopProviderCommand {
            host_id: self
                .host_id
                .ok_or_else(|| "host id is required for stopping provider".to_string())?,
            provider_id: self
                .provider_id
                .ok_or_else(|| "provider id is required for stopping provider".to_string())?,
        })
    }
}

/// A command instructing a specific host to perform a live update
/// on the indicated component by supplying a new image reference. Note that
/// live updates are only possible through image references
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct UpdateComponentCommand {
    /// The component's 56-character unique ID
    #[serde(default)]
    pub(crate) component_id: ComponentId,
    /// Optional set of annotations used to describe the nature of this
    /// update request. Only component instances that have matching annotations
    /// will be upgraded, allowing for instance isolation by
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) annotations: Option<BTreeMap<String, String>>,
    /// The host ID of the host to perform the live update
    #[serde(default)]
    pub(crate) host_id: String,
    /// The new image reference of the upgraded version of this component
    #[serde(default)]
    pub(crate) new_component_ref: String,
}

impl UpdateComponentCommand {
    #[must_use]
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

    #[must_use]
    pub fn component_id(&self) -> &str {
        &self.component_id
    }

    #[must_use]
    pub fn new_component_ref(&self) -> &str {
        &self.new_component_ref
    }

    #[must_use]
    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
        self.annotations.as_ref()
    }

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

/// Builder for [`UpdateComponentCommand`]s
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct UpdateComponentCommandBuilder {
    host_id: Option<String>,
    component_id: Option<ComponentId>,
    new_component_ref: Option<String>,
    annotations: Option<BTreeMap<String, String>>,
}

impl UpdateComponentCommandBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

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

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

    #[must_use]
    pub fn new_component_ref(mut self, v: &str) -> Self {
        self.new_component_ref = Some(v.into());
        self
    }

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

    pub fn build(self) -> Result<UpdateComponentCommand> {
        Ok(UpdateComponentCommand {
            host_id: self
                .host_id
                .ok_or_else(|| "host id is required for updating components".to_string())?,
            component_id: self
                .component_id
                .ok_or_else(|| "component id is required for updating components".to_string())?,
            new_component_ref: self.new_component_ref.ok_or_else(|| {
                "new component ref is required for updating components".to_string()
            })?,
            annotations: self.annotations,
        })
    }
}

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

    use super::{
        ScaleComponentCommand, StartProviderCommand, StopHostCommand, StopProviderCommand,
        UpdateComponentCommand,
    };

    #[test]
    fn scale_component_command_builder() {
        assert_eq!(
            ScaleComponentCommand {
                component_ref: "component_ref".into(),
                component_id: "component_id".into(),
                host_id: "host_id".into(),
                config: vec!["c".into()],
                allow_update: true,
                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
                max_instances: 1,
            },
            ScaleComponentCommand::builder()
                .component_ref("component_ref")
                .component_id("component_id")
                .host_id("host_id")
                .config(vec!["c".into()])
                .allow_update(true)
                .annotations(BTreeMap::from([("a".into(), "b".into())]))
                .max_instances(1)
                .build()
                .unwrap()
        )
    }

    #[test]
    fn start_provider_command_builder() {
        assert_eq!(
            StartProviderCommand {
                provider_id: "provider_id".into(),
                provider_ref: "provider_ref".into(),
                host_id: "host_id".into(),
                config: vec!["p".into()],
                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
            },
            StartProviderCommand::builder()
                .provider_id("provider_id")
                .provider_ref("provider_ref")
                .host_id("host_id")
                .config(vec!["p".into()])
                .annotations(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }

    #[test]
    fn stop_host_command_builder() {
        assert_eq!(
            StopHostCommand {
                host_id: "host_id".into(),
                timeout: Some(1),
            },
            StopHostCommand::builder()
                .host_id("host_id")
                .timeout(1)
                .build()
                .unwrap()
        )
    }

    #[test]
    fn stop_provider_command_builder() {
        assert_eq!(
            StopProviderCommand {
                host_id: "host_id".into(),
                provider_id: "provider_id".into(),
            },
            StopProviderCommand::builder()
                .provider_id("provider_id")
                .host_id("host_id")
                .build()
                .unwrap()
        )
    }

    #[test]
    fn update_component_command_builder() {
        assert_eq!(
            UpdateComponentCommand {
                host_id: "host_id".into(),
                component_id: "component_id".into(),
                new_component_ref: "new_component_ref".into(),
                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
            },
            UpdateComponentCommand::builder()
                .host_id("host_id")
                .component_id("component_id")
                .new_component_ref("new_component_ref")
                .annotations(BTreeMap::from([("a".into(), "b".into())]))
                .build()
                .unwrap()
        )
    }
}