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
//! The common args effectively represent a "Formation Configuration" which many commands can
//! include.
//!
//! The only additional information is the formation name, which is not part of the configuration,
//! but many commands need as well.

use clap::{builder::PossibleValue, value_parser, Arg};
use seaplane::api::shared::v1::{Provider as ProviderModel, Region as RegionModel};

use crate::cli::validator::{
    validate_endpoint, validate_formation_name, validate_name_id, validate_name_id_path_inline,
    validate_public_endpoint,
};

static LONG_NAME: &str =
    "A human readable name for the Formation (must be unique within the tenant)

Rules for a valid name are as follows:

  - may only include 0-9, a-z, A-Z, and '-' (hyphen)
  - hyphens ('-') may not be repeated (i.e. '--')
  - no more than three (3) total hyphens
  - the total length must be <= 27

Some of these restrictions may be lifted in the future.";

static LONG_AFFINITY: &str = "A Formation Instance that this Formation has an affinity for.

This is a hint to the scheduler to place containers running in each of these
formations \"close\" to eachother (for some version of close including but
not limited to latency).

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_CONNECTION: &str = "A Formation Instance that this Formation is connected to.

Two formations can communicate over their formation endpoints (the endpoints configured via
--formation-endpoints) if and only if both formations opt in to that connection (list
each other in their connections map)

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_PUBLIC_ENDPOINT: &str = r#"An endpoint that will publicly exposed on Instances of this Formation Plan

Public Endpoints take the form '{ROUTE}={FLIGHT}:{PORT}'. Where

ROUTE  := An HTTP URL route
FLIGHT := NAME or ID
PORT   := Network Port (0-65535)

This describes which Flight and port should serve the HTTP traffic arriving at this Formation's
domain URL using the specified route.

For example, consider:

$ seaplane formation edit Foo --public-endpoint /foo/bar=baz:1234

Would mean, all HTTP traffic from the public internet hitting the route '/foo/bar' on the 'Foo'
Formation's domain should be directed to this Formation's Flight named 'baz' on port '1234'

In the future, support for other protocols such as 'tcp:port' or 'udp:port' may be added alongside
'http' routes.

Multiple items can be passed as a comma separated list, or by using the argument
multiple times."#;

static LONG_FORMATION_ENDPOINT: &str = r#"An endpoint that will only exposed privately by Instances of this Formation Plan (only exposed to other Formations)

Formation Endpoints take the form '{PROTO}:{TARGET}={FLIGHT}:{PORT}'. Where

PROTO  := [http | https] | tcp | udp
TARGET := ROUTE | PORT
ROUTE  := with PROTO http, and HTTP URL route, can be elided
PORT   := with PROTO tcp | PROTO udp a Network Port (0-65535)
FLIGHT := NAME or ID
PORT   := Network Port (0-65535)

This describes where traffic arriving at instances of this Formation's domains URL from the private network
should be sent.

For example, consider:

$ seaplane formation edit Foo --formation-endpoint tcp:22=baz:2222

Would mean, route all traffic arriving to the 'Foo' Formation's domain URL on TCP/22 from the
private network to the the Formation's Flight named 'baz' on port '2222'. The PROTO of the incoming
traffic will be used for the PROTO of the outgoing traffic to FLIGHT

Note 'https' can be used interchangeably with 'http' for convenience sake. It does NOT however
require the traffic actually be HTTPS. Here 'http' (or convenience 'https') simply means "Traffic
using the HTTP" protocol.

Multiple items can be passed as a comma separated list, or by using the argument
multiple times."#;

static LONG_FLIGHT_ENDPOINT: &str = r#"An endpoint that will only be exposed privately on Instances of this Formation Plan (only exposed to Flights within this same Formation Instance)

Flight Endpoints take the form '{PROTO}:{TARGET}={FLIGHT}:{PORT}'. Where

PROTO  := [http | https] | tcp | udp
TARGET := ROUTE | PORT
ROUTE  := with PROTO http, and HTTP URL route, can be elided
PORT   := with PROTO tcp | PROTO udp a Network Port (0-65535)
FLIGHT := NAME or ID
PORT   := Network Port (0-65535)

This describes where traffic arriving at this Formation's domain URL from within this Formation's
private network should be sent.

For example, consider:

$ seaplane formation edit Foo --flight-endpoint udp:1234=baz:4321

Would mean, route all traffic arriving to the 'Foo' Formation's domain URL on UDP/1234 from the
Formation's private network to the the Formation's Flight named 'baz' on port '4321'. The PROTO of
the incoming traffic will be used for the PROTO of the outgoing traffic to FLIGHT

Note 'https' can be used interchangeably with 'http' for convenience sake. It does NOT however
require the traffic actually be HTTPS. Here 'http' (or convenience 'https') simply means "Traffic
using the HTTP" protocol.

Multiple items can be passed as a comma separated list, or by using the argument
multiple times."#;

static LONG_REGION: &str =
    "A region in which this Formation's Flights are allowed to run in (See REGION SPEC below)

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_PROVIDER: &str = "A provider that this Formation's Flights are permitted to run on

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_EXCLUDE_PROVIDER: &str =
    "A provider that this Formation's Flights are *NOT* permitted to run on

This will override any values given to --provider

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_EXCLUDE_REGION: &str =
    "A region in which this Formation's Flights are *NOT* allowed to run in (See REGION SPEC below)

This will override any values given to --region

Multiple items can be passed as a comma separated list, or by using the argument
multiple times.";

static LONG_FLIGHT: &str =
    "A Flight Plan to include in this Formation in the form of ID|NAME|@path|@-|INLINE-SPEC (See FLIGHT SPEC below)

Multiple items can be passed as a SEMICOLON (';') separated list or by using the argument multiple
times. Note that when using the INLINE-SPEC it's usually easiest to only place one Flight Plan per
--include-flight-plan argument

$ seaplane formation plan \\
    --include-flight-plan name=flight1,image=nginx:latest \\
    --include-flight-plan name=flight2,image=hello:latest

Which would create, and include, two Flight Plans (flight1, and flight2).";
// NOTE: we can't use `derive(clap::ValueEnum)` because it of how it derives the to_possible_value
// which appears to unconditionally use shish-ka-bob case which we don't want.
/// We provide a shim between the Seaplane Provider so we can do some additional UX work like 'all'
#[derive(Debug, Copy, Clone, PartialEq, Eq, strum::EnumString)]
#[strum(ascii_case_insensitive, serialize_all = "lowercase")]
pub enum Provider {
    Aws,
    Azure,
    DigitalOcean,
    Equinix,
    Gcp,
    All,
}

impl clap::ValueEnum for Provider {
    fn value_variants<'a>() -> &'a [Self] {
        use Provider::*;
        &[Aws, Azure, DigitalOcean, Equinix, Gcp, All]
    }

    fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
        use Provider::*;
        match self {
            Aws => Some(PossibleValue::new("aws")),
            Azure => Some(PossibleValue::new("azure")),
            DigitalOcean => Some(PossibleValue::new("digitalocean")),
            Equinix => Some(PossibleValue::new("equinix")),
            Gcp => Some(PossibleValue::new("gcp")),
            All => Some(PossibleValue::new("all")),
        }
    }

    fn from_str(input: &str, _ignore_case: bool) -> Result<Self, String> {
        // Because we use strum(ascii_ignore_case) for our FromStr impl we unconditionally ignore
        // case and can ignore clap's hint
        input.parse().map_err(|e| format!("{e}"))
    }
}

impl Provider {
    pub fn into_model(&self) -> Option<ProviderModel> {
        if self == &Provider::All {
            None
        } else {
            Some(self.into())
        }
    }
}

#[allow(clippy::from_over_into)]
impl<'a> Into<ProviderModel> for &'a Provider {
    fn into(self) -> ProviderModel {
        use Provider::*;
        match self {
            Aws => ProviderModel::AWS,
            Azure => ProviderModel::Azure,
            DigitalOcean => ProviderModel::DigitalOcean,
            Equinix => ProviderModel::Equinix,
            Gcp => ProviderModel::GCP,
            All => {
                panic!("Provider::All cannot be converted into seaplane::api::shared::v1::Provider")
            }
        }
    }
}

// TODO: @needs-decision @pre-1.0 we need to come back and address how many aliases there are and
// their sort order for the CLI's "possible values" message. They're currently "grouped" in region
// aliases, but for one it's too many values, also it makes the sort look wild in the help message.
// The Compute API only uses the XA, XC, XE values, but those are the least user friendly.
/// We provide a shim between the Seaplane Region so we can do some additional UX work
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, strum::EnumString)]
#[strum(ascii_case_insensitive, serialize_all = "lowercase")]
pub enum Region {
    XA,
    Asia,
    XC,
    PRC,
    PeoplesRepublicOfChina,
    XE,
    Europe,
    EU,
    XF,
    Africa,
    XN,
    NorthAmerica,
    NAmerica,
    XO,
    Oceania,
    XQ,
    Antarctica,
    XS,
    SAmerica,
    SouthAmerica,
    XU,
    UK,
    UnitedKingdom,
    All,
}

impl clap::ValueEnum for Region {
    fn value_variants<'a>() -> &'a [Self] {
        use Region::*;
        &[
            XA, // Asia,
            XC, // PRC, PeoplesRepublicOfChina,
            XE, // Europe, EU,
            XF, // Africa,
            XN, // NorthAmerica, NAmerica,
            XO, // Oceania,
            XQ, // Antarctica,
            XS, // SAmerica, SouthAmerica,
            XU, // UK, UnitedKingdom,
            All,
        ]
    }

    fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
        use Region::*;
        match self {
            XA => Some(PossibleValue::new("xa").alias("asia")),
            Asia => None,
            XC => Some(PossibleValue::new("xc").aliases([
                "prc",
                "china",
                "peoples-republic-of-china",
                "peoples_republic_of_china",
                "peoplesrepublicofchina",
            ])),
            PRC => None,
            PeoplesRepublicOfChina => None,
            XE => Some(PossibleValue::new("xe").aliases(["europe", "eu"])),
            Europe => None,
            EU => None,
            XF => Some(PossibleValue::new("xf").alias("africa")),
            Africa => None,
            XN => Some(PossibleValue::new("xn").aliases([
                "namerica",
                "northamerica",
                "n-america",
                "north-america",
                "n_america",
                "north_america",
            ])),
            NorthAmerica => None,
            NAmerica => None,
            XO => Some(PossibleValue::new("xo").alias("oceania")),
            Oceania => None,
            XQ => Some(PossibleValue::new("xq").alias("antarctica")),
            Antarctica => None,
            XS => Some(PossibleValue::new("xs").aliases([
                "samerica",
                "southamerica",
                "s-america",
                "south-america",
                "s_america",
                "south_america",
            ])),
            SAmerica => None,
            SouthAmerica => None,
            XU => Some(PossibleValue::new("xu").aliases([
                "uk",
                "unitedkingdom",
                "united-kingdom",
                "united_kingdom",
            ])),
            UK => None,
            UnitedKingdom => None,
            All => Some(PossibleValue::new("all")),
        }
    }

    fn from_str(input: &str, _ignore_case: bool) -> Result<Self, String> {
        // Because we use strum(ascii_ignore_case) for our FromStr impl we unconditionally ignore
        // case and can ignore clap's hint
        input.parse().map_err(|e| format!("{e}"))
    }
}

impl Region {
    pub fn into_model(&self) -> Option<RegionModel> {
        if self == &Region::All {
            None
        } else {
            Some(self.into())
        }
    }
}

#[allow(clippy::from_over_into)]
impl<'a> Into<RegionModel> for &'a Region {
    fn into(self) -> RegionModel {
        use Region::*;
        match self {
            XA | Asia => RegionModel::XA,
            XC | PRC | PeoplesRepublicOfChina => RegionModel::XC,
            XE | Europe | EU => RegionModel::XE,
            XF | Africa => RegionModel::XF,
            XN | NorthAmerica | NAmerica => RegionModel::XN,
            XO | Oceania => RegionModel::XO,
            XQ | Antarctica => RegionModel::XQ,
            XS | SAmerica | SouthAmerica => RegionModel::XS,
            XU | UK | UnitedKingdom => RegionModel::XU,
            All => panic!("Region::All cannot be converted into seaplane::api::shared::v1::Region"),
        }
    }
}

pub fn args() -> Vec<Arg<'static>> {
    let validator = |s: &str| validate_name_id(validate_formation_name, s);
    #[cfg_attr(not(feature = "unstable"), allow(unused_mut))]
    let mut hide = true;
    let _ = hide;
    #[cfg(feature = "unstable")]
    {
        hide = false;
    }

    // TODO: add --from with support for @file and @- (stdin)
    vec![
        arg!(name_id --name -('n') =["STRING"])
            .help("A human readable name for the Formation (must be unique within the tenant) if omitted a pseudo random name will be assigned")
            .long_help(LONG_NAME)
            .validator(validate_formation_name),
        arg!(--launch|active)
            .overrides_with("launch") // Override with self so someone can do `--launch --active` which isn't needed, but people will do it
            .help("This Formation Plan should be deployed and set as active right away (requires a formation configuration)"),
        arg!(--grounded|("no-active"))
            .help("This Formation Plan should be deployed but NOT set as active (requires a formation configuration)"),
        arg!(--("include-flight-plan")|("include-flight-plans") -('I') =["SPEC"]...)
            .help("Use local Flight Plan in this Formation in the form of ID|NAME|@path|@-|INLINE-SPEC (supports SEMICOLON (';') separated list, or multiple uses) (See FLIGHT SPEC below)")
            .value_delimiter(';')
            .required(true)
            .long_help(LONG_FLIGHT)
            .validator(validate_name_id_path_inline),
        arg!(--provider|providers =["PROVIDER"=>"all"]... ignore_case)
            .help("A provider that this Formation's Flights are permitted to run on (supports comma separated list, or multiple uses)")
            .long_help(LONG_PROVIDER)
            .value_parser(value_parser!(Provider)),
        arg!(--("exclude-provider")|("exclude-providers") =["PROVIDER"]... ignore_case)
            .help("A provider that this Formation's Flights are *NOT* permitted to run on (supports comma separated list, or multiple uses)")
            .long_help(LONG_EXCLUDE_PROVIDER)
            .value_parser(value_parser!(Provider)),
        arg!(--region|regions =["REGION"=>"all"]... ignore_case)
            .help("A region in which this Formation's Flights are allowed to run in (supports comma separated list, or multiple uses) (See REGION SPEC below)")
            .long_help(LONG_REGION)
            .value_parser(value_parser!(Region)),
        arg!(--("exclude-region")|("exclude-regions") =["REGION"]... ignore_case)
            .help("A region in which this Formation's Flights are *NOT* allowed to run in (supports comma separated list, or multiple uses) (See REGION SPEC below)")
            .long_help(LONG_EXCLUDE_REGION)
            .value_parser(value_parser!(Region)),
        // TODO: maybe allow omitting http:
        arg!(--("public-endpoint")|("public-endpoints") =["SPEC"]...)
            .help("An endpoint that will be publicly exposed by instances of this Formation Plan in the form of 'ROUTE=FLIGHT:PORT' (supports comma separated list, or multiple uses)")
            .long_help(LONG_PUBLIC_ENDPOINT)
            .validator(validate_public_endpoint),
        // TODO: maybe allow omitting the Flight's port if it's the same
        arg!(--("flight-endpoint")|("flight-endpoints") =["SPEC"]...)
            .validator(validate_endpoint)
            .help("An endpoint that will only be privately exposed on Instances of this Formation Plan to Flights within the same Formation Instance. In the form of 'PROTO:TARGET=FLIGHT:PORT' (supports comma separated list, or multiple uses)")
            .long_help(LONG_FLIGHT_ENDPOINT),
        arg!(--affinity|affinities =["NAME|ID"]...)
            .help("A Formation that this Formation has an affinity for (supports comma separated list, or multiple uses)")
            .long_help(LONG_AFFINITY)
            .validator(validator)
            .hide(hide), // Hidden on feature = unstable
        arg!(--connection|connections =["NAME|ID"]...)
            .help("A Formations that this Formation is connected to (supports comma separated list, or multiple uses)")
            .long_help(LONG_CONNECTION)
            .validator(validator)
            .hide(hide), // Hidden on feature = unstable
        // TODO: maybe allow omitting the Flight's port if it's the same
        arg!(--("formation-endpoint")|("formation-endpoints") =["SPEC"]...)
            .validator(validate_endpoint)
            .help("An endpoints that will only be exposed privately on Instances of this Formation Plan to other Formations within the same tenant and who have declared mutual connections. In the form of 'PROTO:TARGET=FLIGHT:PORT' (supports comma separated list, or multiple uses)")
            .long_help(LONG_FORMATION_ENDPOINT)
            .hide(hide), // Hidden on feature = unstable
    ]
}