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
mod events;
#[macro_use]
extern crate serde;
extern crate log;
use crossbeam::Sender;
pub use events::{BusEvent, CloudEvent, BUS_EVENT_SUBJECT};
use std::{collections::HashMap, path::PathBuf, time::Duration};
use wascap::prelude::*;
pub const INVENTORY_ACTORS: &str = "wasmbus.inventory.actors";
pub const INVENTORY_HOSTS: &str = "wasmbus.inventory.hosts";
pub const INVENTORY_BINDINGS: &str = "wasmbus.inventory.bindings";
pub const INVENTORY_CAPABILITIES: &str = "wasmbus.inventory.capabilities";
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum InventoryResponse {
Host(HostProfile),
Actors {
host: String,
actors: Vec<Claims<Actor>>,
},
Bindings {
host: String,
bindings: Vec<Binding>,
},
Capabilities {
host: String,
capabilities: Vec<HostedCapability>,
},
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct HostProfile {
pub id: String,
pub labels: HashMap<String, String>,
pub uptime_ms: u128,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct HostedCapability {
pub binding_name: String,
pub descriptor: wascc_codec::capabilities::CapabilityDescriptor,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Binding {
pub actor: String,
pub capability_id: String,
pub binding_name: String,
pub configuration: HashMap<String, String>,
}
pub struct Client {
nc: nats::Connection,
timeout: Duration,
}
impl Client {
pub fn new(host: &str, credsfile: Option<PathBuf>, call_timeout: Duration) -> Self {
Client {
nc: get_connection(host, credsfile),
timeout: call_timeout,
}
}
pub fn get_hosts(&self) -> std::result::Result<Vec<HostProfile>, Box<dyn std::error::Error>> {
let mut hosts = vec![];
let sub = self.nc.request_multi(INVENTORY_HOSTS, &[])?;
for msg in sub.timeout_iter(self.timeout) {
let ir: InventoryResponse = serde_json::from_slice(&msg.data)?;
if let InventoryResponse::Host(h) = ir {
hosts.push(h);
}
}
Ok(hosts)
}
pub fn get_bindings(
&self,
) -> std::result::Result<HashMap<String, Vec<Binding>>, Box<dyn std::error::Error>> {
let mut host_bindings = HashMap::new();
let sub = self.nc.request_multi(INVENTORY_BINDINGS, &[])?;
for msg in sub.timeout_iter(self.timeout) {
let ir: InventoryResponse = serde_json::from_slice(&msg.data)?;
if let InventoryResponse::Bindings { bindings: b, host } = ir {
host_bindings
.entry(host)
.and_modify(|e: &mut Vec<Binding>| e.extend_from_slice(&b))
.or_insert(b.clone());
}
}
Ok(host_bindings)
}
pub fn get_actors(
&self,
) -> std::result::Result<HashMap<String, Vec<Claims<Actor>>>, Box<dyn std::error::Error>> {
let mut host_actors = HashMap::new();
let sub = self.nc.request_multi(INVENTORY_ACTORS, &[])?;
for msg in sub.timeout_iter(self.timeout) {
let ir: InventoryResponse = serde_json::from_slice(&msg.data)?;
if let InventoryResponse::Actors { host, actors } = ir {
host_actors
.entry(host)
.and_modify(|e: &mut Vec<Claims<Actor>>| e.extend_from_slice(&actors))
.or_insert(actors.clone());
}
}
Ok(host_actors)
}
pub fn get_capabilities(
&self,
) -> std::result::Result<HashMap<String, Vec<HostedCapability>>, Box<dyn std::error::Error>>
{
let mut host_caps = HashMap::new();
let sub = self.nc.request_multi(INVENTORY_CAPABILITIES, &[])?;
for msg in sub.timeout_iter(self.timeout) {
let ir: InventoryResponse = serde_json::from_slice(&msg.data)?;
if let InventoryResponse::Capabilities { host, capabilities } = ir {
host_caps
.entry(host)
.and_modify(|e: &mut Vec<HostedCapability>| e.extend_from_slice(&capabilities))
.or_insert(capabilities.clone());
}
}
Ok(host_caps)
}
pub fn watch_events(&self, sender: Sender<BusEvent>) -> Result<(), Box<dyn std::error::Error>> {
let _sub = self
.nc
.subscribe("wasmbus.events")?
.with_handler(move |msg| {
let ce: CloudEvent = serde_json::from_slice(&msg.data).unwrap();
let be: BusEvent = serde_json::from_str(&ce.data).unwrap();
let _ = sender.send(be);
Ok(())
});
Ok(())
}
}
fn get_connection(host: &str, credsfile: Option<PathBuf>) -> nats::Connection {
let mut opts = if let Some(creds) = credsfile {
nats::Options::with_credentials(creds)
} else {
nats::Options::new()
};
opts = opts.with_name("waSCC Lattice");
opts.connect(host).unwrap()
}