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
//! Data types used for managing hosts on a wasmCloud lattice

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::types::component::ComponentDescription;
use crate::types::provider::ProviderDescription;
use crate::Result;

/// A summary representation of a host
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Host {
    /// NATS server host used for regular RPC
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) rpc_host: Option<String>,

    /// NATS server host used for the control interface
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) ctl_host: Option<String>,

    /// Human-friendly name for this host
    #[serde(default)]
    pub(crate) friendly_name: String,

    /// Unique nkey public key for this host
    #[serde(default)]
    pub(crate) id: String,

    /// JetStream domain (if applicable) in use by this host
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) js_domain: Option<String>,

    /// Hash map of label-value pairs for this host
    #[serde(default)]
    pub(crate) labels: BTreeMap<String, String>,

    /// The lattice that this host is a member of
    #[serde(default)]
    pub(crate) lattice: String,

    /// Human-friendly uptime description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) uptime_human: Option<String>,

    /// Uptime in seconds
    #[serde(default)]
    pub(crate) uptime_seconds: u64,

    /// Current wasmCloud Host software version
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) version: Option<String>,
}

impl Host {
    /// Get the NATS server host used for RPC
    pub fn rpc_host(&self) -> Option<&str> {
        self.rpc_host.as_deref()
    }

    /// Get the NATS server host used for control interface commands
    pub fn ctl_host(&self) -> Option<&str> {
        self.ctl_host.as_deref()
    }

    /// Get the friendly name of the host
    pub fn friendly_name(&self) -> &str {
        &self.friendly_name
    }

    /// Get the ID of the host
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the NATS Jetstream domain for the host
    pub fn js_domain(&self) -> Option<&str> {
        self.js_domain.as_deref()
    }

    /// Get the labels on the host
    pub fn labels(&self) -> &BTreeMap<String, String> {
        &self.labels
    }

    /// Get the lattice this host is a member of
    pub fn lattice(&self) -> &str {
        &self.lattice
    }

    /// Get a human friendly host uptime description
    pub fn uptime_human(&self) -> Option<&str> {
        self.uptime_human.as_deref()
    }

    /// Get the number of seconds the host has been up
    pub fn uptime_seconds(&self) -> u64 {
        self.uptime_seconds
    }

    /// Get the version of the host
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

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

#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostBuilder {
    rpc_host: Option<String>,
    ctl_host: Option<String>,
    friendly_name: Option<String>,
    id: Option<String>,
    js_domain: Option<String>,
    labels: Option<BTreeMap<String, String>>,
    lattice: Option<String>,
    uptime_human: Option<String>,
    uptime_seconds: Option<u64>,
    version: Option<String>,
}

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

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

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

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

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

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

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

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

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

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

    pub fn build(self) -> Result<Host> {
        Ok(Host {
            friendly_name: self
                .friendly_name
                .ok_or_else(|| "friendly_name is required".to_string())?,
            labels: self.labels.unwrap_or_default(),
            uptime_human: self.uptime_human,
            uptime_seconds: self
                .uptime_seconds
                .ok_or_else(|| "uptime_seconds is required".to_string())?,
            rpc_host: self.rpc_host,
            ctl_host: self.ctl_host,
            id: self.id.ok_or_else(|| "id is required".to_string())?,
            lattice: self
                .lattice
                .ok_or_else(|| "lattice is required".to_string())?,
            js_domain: self.js_domain,
            version: self.version,
        })
    }
}

/// Describes the known contents of a given host at the time of
/// a query. Also used as a payload for the host heartbeat
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HostInventory {
    /// Components running on this host.
    #[serde(alias = "actors")]
    pub(crate) components: Vec<ComponentDescription>,

    /// Providers running on this host
    pub(crate) providers: Vec<ProviderDescription>,

    /// The host's unique ID
    #[serde(default)]
    pub(crate) host_id: String,

    /// The host's human-readable friendly name
    #[serde(default)]
    pub(crate) friendly_name: String,

    /// The host's labels
    #[serde(default)]
    pub(crate) labels: BTreeMap<String, String>,

    /// The host version
    #[serde(default)]
    pub(crate) version: String,

    /// The host uptime in human-readable form
    #[serde(default)]
    pub(crate) uptime_human: String,

    /// The host uptime in seconds
    #[serde(default)]
    pub(crate) uptime_seconds: u64,
}

impl HostInventory {
    /// Get information about providers in the inventory
    pub fn components(&self) -> &Vec<ComponentDescription> {
        self.components.as_ref()
    }

    /// Get information about providers in the inventory
    pub fn providers(&self) -> &Vec<ProviderDescription> {
        &self.providers
    }

    /// Get the ID of the host from which this inventory was returned
    pub fn host_id(&self) -> &str {
        &self.host_id
    }

    /// Get the friendly name of the host
    pub fn friendly_name(&self) -> &str {
        &self.friendly_name
    }

    /// Get the labels on the host
    pub fn labels(&self) -> &BTreeMap<String, String> {
        &self.labels
    }

    /// Get the version of the host
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Get a human friendly host uptime description
    pub fn uptime_human(&self) -> &str {
        &self.uptime_human
    }

    /// Get the number of seconds the host has been up
    pub fn uptime_seconds(&self) -> u64 {
        self.uptime_seconds
    }

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

#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostInventoryBuilder {
    components: Option<Vec<ComponentDescription>>,
    providers: Option<Vec<ProviderDescription>>,
    host_id: Option<String>,
    friendly_name: Option<String>,
    labels: Option<BTreeMap<String, String>>,
    version: Option<String>,
    uptime_human: Option<String>,
    uptime_seconds: Option<u64>,
}

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

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

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

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

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

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

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

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

    pub fn build(self) -> Result<HostInventory> {
        Ok(HostInventory {
            components: self.components.unwrap_or_default(),
            providers: self.providers.unwrap_or_default(),
            host_id: self
                .host_id
                .ok_or_else(|| "host_id is required".to_string())?,
            friendly_name: self
                .friendly_name
                .ok_or_else(|| "friendly_name is required".to_string())?,
            labels: self.labels.unwrap_or_default(),
            version: self
                .version
                .ok_or_else(|| "version is required".to_string())?,
            uptime_human: self
                .uptime_human
                .ok_or_else(|| "uptime_human is required".to_string())?,
            uptime_seconds: self
                .uptime_seconds
                .ok_or_else(|| "uptime_seconds is required".to_string())?,
        })
    }
}

/// A label on a given host (ex. "arch=amd64")
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HostLabel {
    /// Key of the label (`arch` in `arch=amd64`)
    pub(crate) key: String,

    /// Value of the label (`amd64` in `arch=amd64`)
    pub(crate) value: String,
}

impl HostLabel {
    /// Create a [`HostLabel`] from a key and value
    pub fn from_kv(key: &str, value: &str) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }

    /// Get the host label key
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Get the host label value
    pub fn value(&self) -> &str {
        &self.value
    }
}

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

    use crate::{ComponentDescription, ProviderDescription};

    use super::{Host, HostInventory};

    #[test]
    fn host_builder() {
        assert_eq!(
            Host {
                rpc_host: Some("rpc_host".into()),
                ctl_host: Some("ctl_host".into()),
                friendly_name: "friendly_name".into(),
                id: "id".into(),
                js_domain: Some("js_domain".into()),
                labels: BTreeMap::from([("a".into(), "b".into())]),
                lattice: "lattice".into(),
                uptime_human: Some("t".into()),
                uptime_seconds: 1,
                version: Some("1.0.0".into()),
            },
            Host::builder()
                .rpc_host("rpc_host".into())
                .ctl_host("ctl_host".into())
                .friendly_name("friendly_name".into())
                .id("id".into())
                .js_domain("js_domain".into())
                .labels(BTreeMap::from([("a".into(), "b".into())]))
                .lattice("lattice".into())
                .uptime_human("t".into())
                .uptime_seconds(1)
                .version("1.0.0".into())
                .build()
                .unwrap()
        )
    }

    #[test]
    fn host_inventory_builder() {
        assert_eq!(
            HostInventory {
                components: Vec::from([ComponentDescription::default()]),
                providers: Vec::from([ProviderDescription::default()]),
                host_id: "host_id".into(),
                friendly_name: "friendly_name".into(),
                labels: BTreeMap::from([("a".into(), "b".into())]),
                version: "1.0.0".into(),
                uptime_human: "t".into(),
                uptime_seconds: 1
            },
            HostInventory::builder()
                .components(Vec::from([ComponentDescription::default()]))
                .providers(Vec::from([ProviderDescription::default()]))
                .host_id("host_id".into())
                .friendly_name("friendly_name".into())
                .labels(BTreeMap::from([("a".into(), "b".into())]))
                .version("1.0.0".into())
                .uptime_human("t".into())
                .uptime_seconds(1)
                .build()
                .unwrap()
        )
    }
}