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
use rmp_serde::{Deserializer, Serializer};
use serde::{Deserialize, Serialize};
use std::io::Cursor;

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ProviderAuctionRequest {
    #[serde(rename = "provider_ref")]
    pub provider_ref: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
    #[serde(rename = "constraints")]
    pub constraints: std::collections::HashMap<String, String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ProviderAuctionAck {
    #[serde(rename = "provider_ref")]
    pub provider_ref: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
    #[serde(rename = "host_id")]
    pub host_id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ActorAuctionRequest {
    #[serde(rename = "actor_ref")]
    pub actor_ref: String,
    #[serde(rename = "constraints")]
    pub constraints: std::collections::HashMap<String, String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ActorAuctionAck {
    #[serde(rename = "actor_ref")]
    pub actor_ref: String,
    #[serde(rename = "constraints")]
    pub constraints: std::collections::HashMap<String, String>,
    #[serde(rename = "host_id")]
    pub host_id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StartActorCommand {
    #[serde(rename = "actor_ref")]
    pub actor_ref: String,
    #[serde(rename = "host_id")]
    pub host_id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StartActorAck {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "actor_ref")]
    pub actor_ref: String,
    #[serde(rename = "failure")]
    pub failure: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StartProviderCommand {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "provider_ref")]
    pub provider_ref: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StartProviderAck {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "provider_ref")]
    pub provider_ref: String,
    #[serde(rename = "failure")]
    pub failure: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StopActorCommand {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "actor_ref")]
    pub actor_ref: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StopProviderCommand {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "provider_ref")]
    pub provider_ref: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
    #[serde(rename = "contract_id")]
    pub contract_id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct UpdateActorCommand {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "actor_id")]
    pub actor_id: String,
    #[serde(rename = "new_actor_ref")]
    pub new_actor_ref: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct UpdateActorAck {
    #[serde(rename = "accepted")]
    pub accepted: bool,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StopActorAck {
    #[serde(rename = "failure")]
    pub failure: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct StopProviderAck {
    #[serde(rename = "failure")]
    pub failure: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct LinkDefinitionList {
    #[serde(rename = "links")]
    pub links: Vec<LinkDefinition>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct LinkDefinition {
    #[serde(rename = "actor_id")]
    pub actor_id: String,
    #[serde(rename = "provider_id")]
    pub provider_id: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
    #[serde(rename = "contract_id")]
    pub contract_id: String,
    #[serde(rename = "values")]
    pub values: std::collections::HashMap<String, String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct HostList {
    #[serde(rename = "hosts")]
    pub hosts: Vec<Host>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct Host {
    #[serde(rename = "id")]
    pub id: String,
    #[serde(rename = "uptime")]
    pub uptime_seconds: u64,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ClaimsList {
    #[serde(rename = "claims")]
    pub claims: Vec<Claims>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct Claims {
    #[serde(rename = "values")]
    pub values: std::collections::HashMap<String, String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct HostInventory {
    #[serde(rename = "host_id")]
    pub host_id: String,
    #[serde(rename = "labels")]
    pub labels: std::collections::HashMap<String, String>,
    #[serde(rename = "actors")]
    pub actors: Vec<ActorDescription>,
    #[serde(rename = "providers")]
    pub providers: Vec<ProviderDescription>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ActorDescription {
    #[serde(rename = "id")]
    pub id: String,
    #[serde(rename = "image_ref")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_ref: Option<String>,
    #[serde(rename = "name")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "revision")]
    pub revision: i32,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
pub struct ProviderDescription {
    #[serde(rename = "id")]
    pub id: String,
    #[serde(rename = "link_name")]
    pub link_name: String,
    #[serde(rename = "image_ref")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_ref: Option<String>,
    #[serde(rename = "name")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "revision")]
    pub revision: i32,
}

/// The standard function for serializing codec structs into a format that can be
/// used for message exchange between actor and host. Use of any other function to
/// serialize could result in breaking incompatibilities.
pub(crate) fn serialize<T>(
    item: T,
) -> ::std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>
where
    T: Serialize,
{
    let mut buf = Vec::new();
    item.serialize(&mut Serializer::new(&mut buf).with_struct_map())?;
    Ok(buf)
}

/// The standard function for de-serializing codec structs from a format suitable
/// for message exchange between actor and host. Use of any other function to
/// deserialize could result in breaking incompatibilities.
pub(crate) fn deserialize<'de, T: Deserialize<'de>>(
    buf: &[u8],
) -> ::std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
    let mut de = Deserializer::new(Cursor::new(buf));
    match Deserialize::deserialize(&mut de) {
        Ok(t) => Ok(t),
        Err(e) => Err(format!("Failed to de-serialize: {}", e).into()),
    }
}