tilo-actions 0.5.0

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store, search index entries included.
Documentation
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
//! What a host offers a person's account, and why it refuses an action.
//!
//! Every place a workspace runs is a host: the person's own devices, hosts
//! they run themselves, and hosting providers that sell compute. Hosting is
//! not a separate dashboard: a person's account with a host is what this
//! module describes, and a limit is the host accepting or refusing an action.
//! Billing hangs off that acceptance, never off an app.
//!
//! A host answers [`ACCOUNT`] under [`PROVIDER`] with a [`ServerAccount`]:
//!
//! - the plans it offers ([`PlanOffer`]); a host that sells nothing offers
//!   none and answers no [`Subscription`];
//! - the account's [`Subscription`]: its plan, whether a payment method is on
//!   file, what it is entitled to, its usage (Linux VM seconds and bytes
//!   stored per workspace) and its limits, and its invoices;
//! - where the person manages billing ([`ServerAccount::manage_billing`]): the
//!   provider's own page, never a form Tilo draws.
//!
//! The read is live: a host sends a new answer whenever the account changes
//! (a card added, a plan changed, usage recorded), so a view or a waiting
//! command follows it without asking again.
//!
//! When the host refuses an action that needs what the account lacks, it
//! says so with a [`Refusal`]: a typed [`Reason`], the words for whoever ran
//! it, and the [`NextStep`]s that would let it through, each with the
//! provider's URL. The first action that needs a Linux machine on a host
//! that sells them, or a Browser page action past the period's free ones,
//! with no card on file, is refused with [`Reason::NoPaymentMethod`] and
//! [`NextStepKind::AddPaymentMethod`]: nothing asks for a card before that.
//!
//! Every type here is written field for field, with enums tagged by their
//! variant, so it crosses both JSON (the action API) and postcard (a
//! host's own sockets) as the same value.
//!
//! Payment itself (checkout, prices, webhooks, credit, rate limits, fraud
//! checks) is the provider's. All it does here is answer this contract:
//! write the account's subscription where its host reads it, and name its pages.
//! The account is the person's: their Tilo identity carries it to every
//! device, so Settings and the overview of their Tilo show the same subscription.

use serde::{Deserialize, Serialize};

use super::{ActionAddress, RemoteActionTarget};

/// The provider a server answers its accounts under.
pub const PROVIDER: &str = "tilo.server";
/// The resource the account's actions are actions of.
pub const RESOURCE: &str = "server.account";
/// Live: the calling person's account with this host, as a [`ServerAccount`].
pub const ACCOUNT: &str = "account";

/// The target of action `action` of [`RESOURCE`].
#[must_use]
pub fn target(action: &str) -> RemoteActionTarget {
    RemoteActionTarget {
        provider: PROVIDER.to_owned(),
        address: ActionAddress::new(RESOURCE, action),
    }
}

/// What kind of place a host is, as the person thinks of it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ServerKind {
    /// One of the person's own devices.
    #[default]
    Device,
    /// A host the person runs themselves: it sells nothing.
    SelfHosted,
    /// A hosting provider, such as Tilo Cloud.
    Provider,
}

/// The limits of a plan, or of an account on it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Limits {
    /// Linux VMs of the account running at once.
    pub max_running: u32,
    /// Linux VMs (with their disks) the account keeps, running or not.
    pub max_vms: u32,
    /// The VM sizes it may use, by name (`small`, `default`, `large`,
    /// `xlarge`).
    pub sizes: Vec<String>,
    /// VM seconds included per billing period; `None` when the plan bills
    /// every second or sets no such bound.
    pub included_vm_seconds: Option<u64>,
    /// Bytes the account's workspaces may keep on the host together; `None`
    /// when the host sets no such bound.
    pub storage_bytes: Option<u64>,
    /// Workspaces of the account's own the host keeps; `None` when the host
    /// sets no such bound.
    pub max_workspaces: Option<u32>,
    /// Browser page actions an account without a payment method may run in
    /// the billing period; `None` when the host gives none.
    pub free_browser_actions: Option<u32>,
}

/// One plan a host offers.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PlanOffer {
    /// What [`Subscription::plan`] names it by.
    pub id: String,
    /// Its name, in the provider's words ("Pro").
    pub name: String,
    /// Its price, in the provider's words ("$20 a month, then $0.04 per VM
    /// hour"); `None` for a plan without one.
    pub price: Option<String>,
    pub limits: Limits,
}

/// Something an account may do once its host says so.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Entitlement {
    /// Run Linux programs in a VM of a workspace's own: a non-Tilo app, or
    /// a shell command Tilo does not implement itself.
    LinuxVms,
}

/// How long one workspace's VMs ran in the period.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WorkspaceUsage {
    /// The workspace's id.
    pub workspace: String,
    /// Seconds its VMs ran.
    pub vm_seconds: u64,
    /// Seconds its suspended VMs held a snapshot.
    pub snapshot_seconds: u64,
    /// Whether one of its VMs runs now.
    pub running: bool,
    /// Bytes the host keeps of the workspace now: its history and state.
    pub stored_bytes: u64,
}

/// What the account used in the current billing period.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Usage {
    /// When the period started and ends, Unix milliseconds.
    pub period_start_ms: u64,
    pub period_end_ms: u64,
    /// Browser page actions the account ran in the period.
    pub browser_actions: u64,
    /// Per workspace, most used first.
    pub workspaces: Vec<WorkspaceUsage>,
}

impl Usage {
    /// Seconds every workspace's VMs ran.
    #[must_use]
    pub fn vm_seconds(&self) -> u64 {
        self.workspaces
            .iter()
            .fold(0_u64, |sum, usage| sum.saturating_add(usage.vm_seconds))
    }

    /// Bytes every workspace keeps on the host now.
    #[must_use]
    pub fn stored_bytes(&self) -> u64 {
        self.workspaces
            .iter()
            .fold(0_u64, |sum, usage| sum.saturating_add(usage.stored_bytes))
    }

    /// The usage of `workspace`, if its VMs ran or it keeps anything.
    #[must_use]
    pub fn of(&self, workspace: &str) -> Option<&WorkspaceUsage> {
        self.workspaces
            .iter()
            .find(|usage| usage.workspace == workspace)
    }
}

/// Where an invoice stands.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum InvoiceStatus {
    Paid,
    /// Issued and not paid yet.
    #[default]
    Open,
    /// Payment failed; the provider's page says what to do.
    PastDue,
    Void,
}

/// One invoice, as the provider issued it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Invoice {
    pub id: String,
    /// When it was issued, Unix milliseconds.
    pub issued_ms: u64,
    /// Its total, in the provider's words ("$24.10").
    pub amount: String,
    pub status: InvoiceStatus,
    /// The provider's page for it.
    pub url: String,
}

/// A payment method on file, as the provider describes it: enough for the
/// person to recognise it ("Visa ·· 4242"), never enough to charge it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PaymentMethod {
    /// Its brand, in the provider's words ("Visa"); `None` when the
    /// provider does not say.
    pub brand: Option<String>,
    /// The last four digits of its number; `None` when the provider does
    /// not say.
    pub last4: Option<String>,
}

/// The account's subscription with a host that sells something.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Subscription {
    /// The account's id with the host.
    pub account: String,
    /// The [`PlanOffer::id`] it is on; `None` before it chose one.
    pub plan: Option<String>,
    /// The payment method on file; `None` while there is none.
    pub payment_method: Option<PaymentMethod>,
    pub entitlements: Vec<Entitlement>,
    /// What the account may use now; `None` while it is entitled to nothing.
    pub limits: Option<Limits>,
    pub usage: Usage,
    /// Credit the account has left, as the provider writes the amount
    /// ("$12.40"); `None` when it has none.
    pub credit: Option<String>,
    /// Newest first.
    pub invoices: Vec<Invoice>,
}

impl Subscription {
    #[must_use]
    pub fn entitled(&self, entitlement: Entitlement) -> bool {
        self.entitlements.contains(&entitlement)
    }
}

/// A person's account with one server.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ServerAccount {
    /// The server's name, as the person knows it ("Tilo Cloud", "Ada's
    /// laptop").
    pub server: String,
    pub kind: ServerKind,
    /// The plans it offers; none when it sells nothing.
    pub plans: Vec<PlanOffer>,
    /// The account's subscription; `None` on a host that sells nothing.
    pub subscription: Option<Subscription>,
    /// The provider's page where the person manages billing: payment
    /// methods, plan and invoices.
    pub manage_billing: Option<String>,
}

impl ServerAccount {
    /// A server named `server` that sells nothing: its programs run as its
    /// person's own.
    #[must_use]
    pub fn sells_nothing(server: impl Into<String>, kind: ServerKind) -> Self {
        Self {
            server: server.into(),
            kind,
            ..Self::default()
        }
    }

    /// Whether the host sells anything.
    #[must_use]
    pub fn sells(&self) -> bool {
        !self.plans.is_empty()
    }

    /// The plan the account is on.
    #[must_use]
    pub fn plan(&self) -> Option<&PlanOffer> {
        let id = self.subscription.as_ref()?.plan.as_deref()?;
        self.plans.iter().find(|plan| plan.id == id)
    }
}

/// What would let a refused action through.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NextStepKind {
    /// Add a payment method on the provider's page.
    AddPaymentMethod,
    /// Raise the account's limits: another plan, or a higher bound.
    RaiseLimit,
    /// Choose a VM size the plan includes for the workspace.
    ChooseSize,
    /// Wait for one of the account's VMs to stop on its own, or stop one.
    WaitForIdle,
    /// Use a workspace the account already has.
    UseExisting,
}

/// One way through a refusal.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NextStep {
    pub kind: NextStepKind,
    /// The provider's page for it; `None` for a step taken in Tilo itself.
    #[serde(default)]
    pub url: Option<String>,
}

/// Why a host refused an action.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Reason {
    /// The action needs what the host sells, the account has no payment
    /// method on file, and the free use of it is spent: a Linux machine on a
    /// host that sells them, or Browser past the page actions its billing
    /// period gives.
    NoPaymentMethod,
    /// The workspace's VM size is not one the plan includes.
    SizeNotInPlan { size: String, allowed: Vec<String> },
    /// The account already runs as many VMs at once as its plan allows.
    TooManyRunning {
        limit: u32,
        /// The workspaces whose VMs run now.
        running: Vec<String>,
    },
    /// The account already keeps as many VMs as its plan allows.
    TooManyVms { limit: u32, existing: u32 },
    /// The account's workspaces keep as many bytes on the host as it allows:
    /// the write was not kept.
    StorageFull { limit_bytes: u64, used_bytes: u64 },
    /// The account already has as many workspaces of its own on the host as
    /// it allows.
    TooManyWorkspaces {
        limit: u32,
        /// The ids of the workspaces it has.
        existing: Vec<String>,
    },
}

/// A host's refusal of an action, for whoever ran it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Refusal {
    pub reason: Reason,
    /// What happened and what to do, in plain words for whoever ran the
    /// action, a person or an agent.
    pub message: String,
    /// What would let it through, the likeliest first.
    #[serde(default)]
    pub next: Vec<NextStep>,
}

impl Refusal {
    /// The step of `kind`, if the refusal offers it.
    #[must_use]
    pub fn step(&self, kind: NextStepKind) -> Option<&NextStep> {
        self.next.iter().find(|step| step.kind == kind)
    }

    /// Whether the account's subscription changing on the host is what would let
    /// the action through, so whoever ran it can wait for that rather than
    /// run it again.
    #[must_use]
    pub const fn awaits_the_account(&self) -> bool {
        matches!(self.reason, Reason::NoPaymentMethod)
    }

    /// The refusal as an action's refusal reason
    /// ([`super::ActionCallError::Refused`]): its JSON, which
    /// [`Self::from_reason`] reads back typed, and which a caller that does
    /// not read it still shows as plain data.
    #[must_use]
    pub fn to_reason(&self) -> String {
        serde_json::to_string(self).unwrap_or_else(|_| self.message.clone())
    }

    /// The refusal an action's refusal reason carries, when it carries one
    /// ([`Self::to_reason`]).
    #[must_use]
    pub fn from_reason(reason: &str) -> Option<Self> {
        serde_json::from_str(reason).ok()
    }
}

impl std::fmt::Display for Refusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    tilo_test_env::scenario! {
        fn a_server_account_answers_plans_and_typed_refusals;

        phase a_host_that_sells_nothing_answers_no_plans_and_no_subscription;
        {
            let account = ServerAccount::sells_nothing("Ada's server", ServerKind::SelfHosted);
            assert!(!account.sells());
            assert!(account.plan().is_none());
            let json = serde_json::to_value(&account).unwrap();
            assert_eq!(
                json,
                serde_json::json!({
                    "server": "Ada's server",
                    "kind": "self-hosted",
                    "plans": [],
                    "subscription": null,
                    "manage_billing": null
                })
            );
            assert_eq!(serde_json::from_value::<ServerAccount>(json).unwrap(), account);
        }

        phase a_refusal_names_its_reason_and_next_steps_on_the_wire;
        {
            let refusal = Refusal {
                reason: Reason::NoPaymentMethod,
                message: "This needs a Linux machine.".into(),
                next: vec![NextStep {
                    kind: NextStepKind::AddPaymentMethod,
                    url: Some("https://billing.example/card".into()),
                }],
            };
            let json = serde_json::to_value(&refusal).unwrap();
            assert_eq!(
                json,
                serde_json::json!({
                    "reason": "no_payment_method",
                    "message": "This needs a Linux machine.",
                    "next": [{"kind": "add_payment_method", "url": "https://billing.example/card"}]
                })
            );
            assert!(refusal.awaits_the_account());
            let bytes = postcard::to_allocvec(&refusal).unwrap();
            assert_eq!(postcard::from_bytes::<Refusal>(&bytes).unwrap(), refusal);
            assert_eq!(
                refusal
                    .step(NextStepKind::AddPaymentMethod)
                    .and_then(|step| step.url.as_deref()),
                Some("https://billing.example/card")
            );
            let limit = Refusal {
                reason: Reason::TooManyRunning {
                    limit: 1,
                    running: vec!["ws1".into()],
                },
                message: String::new(),
                next: Vec::new(),
            };
            assert!(!limit.awaits_the_account());
            assert_eq!(
                serde_json::to_value(&limit.reason).unwrap(),
                serde_json::json!({"too_many_running": {"limit": 1, "running": ["ws1"]}})
            );
        }

        phase storage_refusals_cross_an_actions_reason_typed;
        {
            let full = Refusal {
                reason: Reason::StorageFull {
                    limit_bytes: 10,
                    used_bytes: 12,
                },
                message: "Your workspace is full.".into(),
                next: vec![NextStep {
                    kind: NextStepKind::RaiseLimit,
                    url: None,
                }],
            };
            assert_eq!(
                serde_json::to_value(&full.reason).unwrap(),
                serde_json::json!({"storage_full": {"limit_bytes": 10, "used_bytes": 12}})
            );
            assert_eq!(Refusal::from_reason(&full.to_reason()), Some(full.clone()));
            assert!(!full.awaits_the_account());
            assert_eq!(Refusal::from_reason("the host refused"), None);
            let second = Reason::TooManyWorkspaces {
                limit: 1,
                existing: vec!["w".into()],
            };
            let bytes = postcard::to_allocvec(&second).unwrap();
            assert_eq!(postcard::from_bytes::<Reason>(&bytes).unwrap(), second);
            let usage = Usage {
                workspaces: vec![
                    WorkspaceUsage {
                        workspace: "a".into(),
                        stored_bytes: 7,
                        ..WorkspaceUsage::default()
                    },
                    WorkspaceUsage {
                        workspace: "b".into(),
                        stored_bytes: 5,
                        ..WorkspaceUsage::default()
                    },
                ],
                ..Usage::default()
            };
            assert_eq!(usage.stored_bytes(), 12);
        }

        phase the_plan_an_account_is_on_is_one_of_the_offers;
        {
            let host = ServerAccount {
                server: "Tilo Cloud".into(),
                kind: ServerKind::Provider,
                plans: vec![PlanOffer {
                    id: "pro".into(),
                    name: "Pro".into(),
                    ..PlanOffer::default()
                }],
                subscription: Some(Subscription {
                    plan: Some("pro".into()),
                    usage: Usage {
                        workspaces: vec![
                            WorkspaceUsage {
                                workspace: "a".into(),
                                vm_seconds: 60,
                                ..WorkspaceUsage::default()
                            },
                            WorkspaceUsage {
                                workspace: "b".into(),
                                vm_seconds: 30,
                                ..WorkspaceUsage::default()
                            },
                        ],
                        ..Usage::default()
                    },
                    ..Subscription::default()
                }),
                manage_billing: Some("https://billing.example".into()),
            };
            assert!(host.sells());
            let bytes = postcard::to_allocvec(&host).unwrap();
            assert_eq!(postcard::from_bytes::<ServerAccount>(&bytes).unwrap(), host);
            assert_eq!(host.plan().map(|plan| plan.name.as_str()), Some("Pro"));
            let usage = &host.subscription.as_ref().unwrap().usage;
            assert_eq!(usage.vm_seconds(), 90);
            assert_eq!(usage.of("b").map(|usage| usage.vm_seconds), Some(30));
        }
    }
}