Skip to content

Commit 04300ae

Browse files
committed
refactor(wit): Extract regions interface
1 parent 79b74ec commit 04300ae

6 files changed

Lines changed: 112 additions & 101 deletions

File tree

fly-http/src/machine.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::activity_flyio::fly_http::regions::Region;
12
use crate::exports::activity_flyio::fly_http::machines::{
2-
ExecResponse, Guest, Machine, MachineConfig, MachineRegion,
3+
ExecResponse, Guest, Machine, MachineConfig,
34
};
5+
46
use crate::machine::ser::{MachineSer, ToLowerWrapper};
57
use crate::{API_BASE_URL, Component, request_with_api_token};
68
use anyhow::{Context, anyhow, bail, ensure};
@@ -15,9 +17,10 @@ use wstd::runtime::block_on;
1517
// These structs are internal implementation details. They are designed to serialize
1618
// into the exact JSON format expected by the Fly.io Machines API.
1719
pub(crate) mod ser {
20+
use crate::activity_flyio::fly_http::regions::Region;
1821
use crate::exports::activity_flyio::fly_http::machines::{
1922
CpuKind, ExecResponse, GuestConfig, HostStatus, InitConfig, Machine, MachineConfig,
20-
MachineRegion, MachineRestart, RestartPolicy, StopConfig,
23+
MachineRestart, RestartPolicy, StopConfig,
2124
};
2225
use serde::de::DeserializeOwned;
2326
use serde::{Deserialize, Serialize};
@@ -28,13 +31,13 @@ pub(crate) mod ser {
2831
pub(crate) struct MachineCreateRequestSer {
2932
pub(crate) name: String,
3033
pub(crate) config: MachineConfigSer,
31-
pub(crate) region: Option<ToLowerWrapper<MachineRegion>>,
34+
pub(crate) region: Option<ToLowerWrapper<Region>>,
3235
}
3336

3437
#[derive(Serialize, Debug)]
3538
pub(crate) struct MachineUpdateRequestSer {
3639
pub(crate) config: MachineConfigSer,
37-
pub(crate) region: Option<ToLowerWrapper<MachineRegion>>,
40+
pub(crate) region: Option<ToLowerWrapper<Region>>,
3841
}
3942

4043
#[derive(Deserialize, Debug)]
@@ -46,7 +49,7 @@ pub(crate) mod ser {
4649
instance_id: String,
4750
name: String,
4851
state: String,
49-
region: ToLowerWrapper<MachineRegion>,
52+
region: ToLowerWrapper<Region>,
5053
host_status: ToLowerWrapper<HostStatus>,
5154
}
5255
impl From<MachineSer> for Machine {
@@ -226,19 +229,17 @@ pub(crate) mod ser {
226229
where
227230
D: serde::Deserializer<'de>,
228231
{
229-
deserializer.deserialize_string(MachineRegionVisitor {
232+
deserializer.deserialize_string(RegionVisitor {
230233
_phantom_data: Default::default(),
231234
})
232235
}
233236
}
234237

235-
struct MachineRegionVisitor<T: Debug + Serialize + DeserializeOwned> {
238+
struct RegionVisitor<T: Debug + Serialize + DeserializeOwned> {
236239
_phantom_data: std::marker::PhantomData<T>,
237240
}
238241

239-
impl<'de, T: Debug + Serialize + DeserializeOwned> serde::de::Visitor<'de>
240-
for MachineRegionVisitor<T>
241-
{
242+
impl<'de, T: Debug + Serialize + DeserializeOwned> serde::de::Visitor<'de> for RegionVisitor<T> {
242243
type Value = ToLowerWrapper<T>;
243244

244245
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
@@ -385,7 +386,7 @@ async fn create(
385386
app_name: String,
386387
machine_name: String,
387388
machine_config: MachineConfig,
388-
region: Option<MachineRegion>,
389+
region: Option<Region>,
389390
) -> Result<String, anyhow::Error> {
390391
{
391392
let region = region.map(ToLowerWrapper);
@@ -444,7 +445,7 @@ async fn update(
444445
app_name: String,
445446
machine_id: String,
446447
machine_config: MachineConfig,
447-
region: Option<MachineRegion>,
448+
region: Option<Region>,
448449
) -> Result<(), anyhow::Error> {
449450
{
450451
let region = region.map(ToLowerWrapper);
@@ -556,7 +557,7 @@ impl Guest for Component {
556557
app_name: String,
557558
machine_name: String,
558559
machine_config: MachineConfig,
559-
region: Option<MachineRegion>,
560+
region: Option<Region>,
560561
) -> Result<String, String> {
561562
block_on(create(app_name, machine_name, machine_config, region))
562563
.map_err(|err| err.to_string())
@@ -566,7 +567,7 @@ impl Guest for Component {
566567
app_name: String,
567568
machine_id: String,
568569
machine_config: MachineConfig,
569-
region: Option<MachineRegion>,
570+
region: Option<Region>,
570571
) -> Result<(), String> {
571572
block_on(update(app_name, machine_id, machine_config, region))
572573
.map_err(|err| err.to_string())
@@ -602,7 +603,7 @@ impl Guest for Component {
602603
mod tests {
603604
use super::ser::ResponseErrorSer;
604605
use crate::{
605-
exports::activity_flyio::fly_http::machines::MachineRegion,
606+
exports::activity_flyio::fly_http::machines::Region,
606607
machine::ser::{MachineSer, ToLowerWrapper},
607608
};
608609
use insta::assert_debug_snapshot;
@@ -612,15 +613,15 @@ mod tests {
612613
fn region_ser() {
613614
assert_eq!(
614615
"\"ams\"",
615-
serde_json::to_string(&ToLowerWrapper(MachineRegion::Ams)).unwrap()
616+
serde_json::to_string(&ToLowerWrapper(Region::Ams)).unwrap()
616617
);
617618
}
618619

619620
#[test]
620621
fn region_de() {
621622
assert_matches::assert_matches!(
622623
serde_json::from_str("\"ams\"").unwrap(),
623-
ToLowerWrapper(MachineRegion::Ams)
624+
ToLowerWrapper(Region::Ams)
624625
);
625626
}
626627

fly-http/src/snapshots/fly_http__machine__tests__machine_deserialization.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ MachineSer {
4747
instance_id: "01K4SR42ZPDHHCN70QNZKVPK48",
4848
name: "machine",
4949
state: "started",
50-
region: MachineRegion::Ams,
50+
region: Region::Ams,
5151
host_status: HostStatus::Ok,
5252
}

fly-http/src/snapshots/fly_http__volume__tests__volume_deserialization.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ VolumeSer {
66
id: "vol_vjeylkgg6gll7j94",
77
name: "my_app_vol",
88
state: "created",
9-
region: MachineRegion::Ams,
9+
region: Region::Ams,
1010
size_gb: 1,
1111
encrypted: true,
1212
attached_machine_id: None,

fly-http/src/volume.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ use wstd::runtime::block_on;
1010
// These structs are internal implementation details. They are designed to serialize
1111
// into the exact JSON format expected by the Fly.io Volumes API.
1212
pub(crate) mod ser {
13-
use crate::exports::activity_flyio::fly_http::{machines::MachineRegion, volumes::Volume};
1413
use crate::machine::ser::ToLowerWrapper;
14+
use crate::{
15+
activity_flyio::fly_http::regions::Region,
16+
exports::activity_flyio::fly_http::volumes::Volume,
17+
};
1518
use serde::{Deserialize, Serialize};
1619

1720
#[derive(Serialize, Debug)]
1821
pub(crate) struct VolumeCreateRequestSer {
1922
pub(crate) name: String,
2023
pub(crate) size_gb: u32,
21-
pub(crate) region: ToLowerWrapper<MachineRegion>,
24+
pub(crate) region: ToLowerWrapper<Region>,
2225
#[serde(rename = "require_unique_zone")]
2326
pub(crate) require_unique_zone: Option<bool>,
2427
}
@@ -28,7 +31,7 @@ pub(crate) mod ser {
2831
pub(crate) id: String,
2932
pub(crate) name: String,
3033
pub(crate) state: String,
31-
pub(crate) region: ToLowerWrapper<MachineRegion>,
34+
pub(crate) region: ToLowerWrapper<Region>,
3235
pub(crate) size_gb: u32,
3336
pub(crate) encrypted: bool,
3437
pub(crate) attached_machine_id: Option<String>,

fly-http/wit/activity-flyio_fly-http@1.0.0-beta/fly.wit

Lines changed: 9 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface apps {
1717

1818
/// [Machines API](https://docs.machines.dev/#tag/machines/get/apps/{app_name}/machines)
1919
interface machines {
20+
use regions.{region};
2021

2122
enum machine-state {
2223
created,
@@ -46,7 +47,7 @@ interface machines {
4647
instance-id: string,
4748
name: string,
4849
state: string,
49-
region: machine-region,
50+
region: region,
5051
host-status: host-status,
5152
}
5253

@@ -72,79 +73,6 @@ interface machines {
7273
performance,
7374
}
7475

75-
enum machine-region {
76-
/// Amsterdam, Netherlands
77-
ams,
78-
/// Stockholm, Sweden
79-
arn,
80-
/// Atlanta, Georgia (US)
81-
atl,
82-
/// Bogotá, Colombia
83-
bog,
84-
/// Mumbai, India
85-
bom,
86-
/// Boston, Massachusetts (US)
87-
bos,
88-
/// Paris, France
89-
cdg,
90-
/// Denver, Colorado (US)
91-
den,
92-
/// Dallas, Texas (US)
93-
dfw,
94-
/// Secaucus, NJ (US)
95-
ewr,
96-
/// Ezeiza, Argentina
97-
eze,
98-
/// Frankfurt, Germany
99-
fra,
100-
/// Guadalajara, Mexico
101-
gdl,
102-
/// Rio de Janeiro, Brazil
103-
gig,
104-
/// Sao Paulo, Brazil
105-
gru,
106-
/// Hong Kong, Hong Kong
107-
hkg,
108-
/// Ashburn, Virginia (US)
109-
iad,
110-
/// Johannesburg, South Africa
111-
jnb,
112-
/// Los Angeles, California (US)
113-
lax,
114-
/// London, United Kingdom
115-
lhr,
116-
/// Madrid, Spain
117-
mad,
118-
/// Miami, Florida (US)
119-
mia,
120-
/// Tokyo, Japan
121-
nrt,
122-
/// Chicago, Illinois (US)
123-
ord,
124-
/// Bucharest, Romania
125-
otp,
126-
/// Phoenix, Arizona (US)
127-
phx,
128-
/// Querétaro, Mexico
129-
qro,
130-
/// Santiago, Chile
131-
scl,
132-
/// Seattle, Washington (US)
133-
sea,
134-
/// Singapore, Singapore
135-
sin,
136-
/// San Jose, California (US)
137-
sjc,
138-
/// Sydney, Australia
139-
syd,
140-
/// Warsaw, Poland
141-
waw,
142-
/// Montreal, Canada
143-
yul,
144-
/// Toronto, Canada
145-
yyz,
146-
}
147-
14876
enum restart-policy {
14977
no,
15078
always,
@@ -181,9 +109,9 @@ interface machines {
181109

182110
/// Create and start a machine under `app-name` with name `machine-name` and return its ID.
183111
/// If machine already exists, attributes of `machine-config` are not updated, just the machine ID is returned.
184-
create: func(app-name: string, machine-name: string, machine-config: machine-config, region: option<machine-region>) -> result<string, string>;
112+
create: func(app-name: string, machine-name: string, machine-config: machine-config, region: option<region>) -> result<string, string>;
185113

186-
update: func(app-name: string, machine-id: string, machine-config: machine-config, region: option<machine-region>) -> result<_, string>;
114+
update: func(app-name: string, machine-id: string, machine-config: machine-config, region: option<region>) -> result<_, string>;
187115

188116
suspend: func(app-name: string, machine-id: string) -> result<_, string>;
189117

@@ -217,13 +145,13 @@ interface secrets {
217145

218146
/// [Volumes API](https://docs.machines.dev/#tag/volumes)
219147
interface volumes {
220-
use machines.{machine-region};
148+
use regions.{region};
221149

222150
record volume {
223151
id: string,
224152
name: string,
225153
state: string,
226-
region: machine-region,
154+
region: region,
227155
size-gb: u32,
228156
encrypted: bool,
229157
attached-machine-id: option<string>,
@@ -240,7 +168,7 @@ interface volumes {
240168
record volume-create-request {
241169
name: string,
242170
size-gb: u32,
243-
region: machine-region,
171+
region: region,
244172
require-unique-zone: option<bool>,
245173
}
246174

@@ -267,3 +195,5 @@ world exports {
267195
export secrets;
268196
export volumes;
269197
}
198+
199+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package activity-flyio:fly-http@1.0.0-beta;
2+
3+
interface regions {
4+
5+
enum region {
6+
/// Amsterdam, Netherlands
7+
ams,
8+
/// Stockholm, Sweden
9+
arn,
10+
/// Atlanta, Georgia (US)
11+
atl,
12+
/// Bogotá, Colombia
13+
bog,
14+
/// Mumbai, India
15+
bom,
16+
/// Boston, Massachusetts (US)
17+
bos,
18+
/// Paris, France
19+
cdg,
20+
/// Denver, Colorado (US)
21+
den,
22+
/// Dallas, Texas (US)
23+
dfw,
24+
/// Secaucus, NJ (US)
25+
ewr,
26+
/// Ezeiza, Argentina
27+
eze,
28+
/// Frankfurt, Germany
29+
fra,
30+
/// Guadalajara, Mexico
31+
gdl,
32+
/// Rio de Janeiro, Brazil
33+
gig,
34+
/// Sao Paulo, Brazil
35+
gru,
36+
/// Hong Kong, Hong Kong
37+
hkg,
38+
/// Ashburn, Virginia (US)
39+
iad,
40+
/// Johannesburg, South Africa
41+
jnb,
42+
/// Los Angeles, California (US)
43+
lax,
44+
/// London, United Kingdom
45+
lhr,
46+
/// Madrid, Spain
47+
mad,
48+
/// Miami, Florida (US)
49+
mia,
50+
/// Tokyo, Japan
51+
nrt,
52+
/// Chicago, Illinois (US)
53+
ord,
54+
/// Bucharest, Romania
55+
otp,
56+
/// Phoenix, Arizona (US)
57+
phx,
58+
/// Querétaro, Mexico
59+
qro,
60+
/// Santiago, Chile
61+
scl,
62+
/// Seattle, Washington (US)
63+
sea,
64+
/// Singapore, Singapore
65+
sin,
66+
/// San Jose, California (US)
67+
sjc,
68+
/// Sydney, Australia
69+
syd,
70+
/// Warsaw, Poland
71+
waw,
72+
/// Montreal, Canada
73+
yul,
74+
/// Toronto, Canada
75+
yyz,
76+
}
77+
}

0 commit comments

Comments
 (0)