Skip to content

Commit 63e7acb

Browse files
committed
ok well I did it
1 parent ce35f6a commit 63e7acb

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

app/api/util.ts

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,53 +89,47 @@ export const genName = (...parts: [string, ...string[]]) => {
8989
)
9090
}
9191

92-
// setting .states is a cute way to make it ergonomic to call the test function
93-
// while also making the states available directly
94-
95-
function makeInstanceTest(states: InstanceState[]) {
96-
const test = (i: { runState: InstanceState }) => states.includes(i.runState)
97-
test.states = states
98-
return test
99-
}
100-
101-
// we used to define this by mapping actions directly to states and then running
102-
// a mapValues on it, but that resulted in TS allowing any string as a key on
103-
// instanceKey
104-
105-
export const instanceCan = {
92+
const instanceActions = {
10693
// NoVmm maps to to Stopped:
10794
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-model/src/instance_state.rs#L55
10895

10996
// https://github.com/oxidecomputer/omicron/blob/0496637/nexus/src/app/instance.rs#L2064
110-
start: makeInstanceTest(['stopped', 'failed']),
97+
start: ['stopped', 'failed'],
11198

11299
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/instance.rs#L865
113-
delete: makeInstanceTest(['stopped', 'failed']),
100+
delete: ['stopped', 'failed'],
114101

115102
// https://github.com/oxidecomputer/omicron/blob/3093818/nexus/db-queries/src/db/datastore/instance.rs#L1030-L1043
116-
update: makeInstanceTest(['stopped', 'failed', 'creating']),
103+
update: ['stopped', 'failed', 'creating'],
117104

118105
// reboot and stop are kind of weird!
119106
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/src/app/instance.rs#L790-L798
120107
// https://github.com/oxidecomputer/propolis/blob/b278193/bin/propolis-server/src/lib/vm/request_queue.rs
121108
// https://github.com/oxidecomputer/console/pull/2387#discussion_r1722368236
122-
reboot: makeInstanceTest(['running']), // technically rebooting allowed but too weird to say i)t
109+
reboot: ['running'], // technically rebooting allowed but too weird to say it
123110
// stopping a failed disk: https://github.com/oxidecomputer/omicron/blob/f0b804818b898bebdb317ac2b000618944c02457/nexus/src/app/instance.rs#L818-L830
124-
stop: makeInstanceTest(['running', 'starting', 'rebooting', 'failed']),
111+
stop: ['running', 'starting', 'rebooting', 'failed'],
125112

126113
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/disk.rs#L323-L327
127-
detachDisk: makeInstanceTest(['creating', 'stopped', 'failed']),
114+
detachDisk: ['creating', 'stopped', 'failed'],
128115
// only Creating and NoVmm
129116
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/disk.rs#L185-L188
130-
attachDisk: makeInstanceTest(['creating', 'stopped']),
131-
117+
attachDisk: ['creating', 'stopped'],
132118
// primary nic: https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/network_interface.rs#L761-L765
133119
// non-primary: https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/network_interface.rs#L806-L810
134-
updateNic: makeInstanceTest(['stopped']),
120+
updateNic: ['stopped'],
135121
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/src/app/instance.rs#L1520-L1522
122+
serialConsole: ['running', 'rebooting', 'migrating', 'repairing'],
123+
} satisfies Record<string, InstanceState[]>
136124

137-
serialConsole: makeInstanceTest(['running', 'rebooting', 'migrating', 'repairing']),
138-
}
125+
// setting .states is a cute way to make it ergonomic to call the test function
126+
// while also making the states available directly
127+
128+
export const instanceCan = R.mapValues(instanceActions, (states: string[]) => {
129+
const test = (i: { runState: InstanceState }) => states.includes(i.runState)
130+
test.states = states
131+
return test
132+
})
139133

140134
export function instanceTransitioning({ runState }: Instance) {
141135
return (
@@ -146,29 +140,29 @@ export function instanceTransitioning({ runState }: Instance) {
146140
)
147141
}
148142

149-
function makeDiskTest(states: DiskState['state'][]) {
150-
// only have to Pick because we want this to work for both Disk and
151-
// Json<Disk>, which we pass to it in the MSW handlers
152-
const test = (d: Pick<Disk, 'state'>) => states.includes(d.state.state)
153-
test.states = states
154-
return test
155-
}
156-
157-
export const diskCan = {
143+
const diskActions = {
158144
// this is a weird one because the list of states is dynamic and it includes
159145
// 'creating' in the unwind of the disk create saga, but does not include
160146
// 'creating' in the disk delete saga, which is what we care about
161147
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/src/app/sagas/disk_delete.rs?plain=1#L110
162-
delete: makeDiskTest(['detached', 'faulted']),
148+
delete: ['detached', 'faulted'],
163149
// TODO: link to API source. It's hard to determine from the saga code what the rule is here.
164-
snapshot: makeDiskTest(['attached', 'detached']),
150+
snapshot: ['attached', 'detached'],
165151
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/disk.rs#L173-L176
166-
attach: makeDiskTest(['creating', 'detached']),
152+
attach: ['creating', 'detached'],
167153
// https://github.com/oxidecomputer/omicron/blob/6dd9802/nexus/db-queries/src/db/datastore/disk.rs#L313-L314
168-
detach: makeDiskTest(['attached']),
154+
detach: ['attached'],
169155
// https://github.com/oxidecomputer/omicron/blob/3093818/nexus/db-queries/src/db/datastore/instance.rs#L1077-L1081
170-
setAsBootDisk: makeDiskTest(['attached']),
171-
}
156+
setAsBootDisk: ['attached'],
157+
} satisfies Record<string, DiskState['state'][]>
158+
159+
export const diskCan = R.mapValues(diskActions, (states: string[]) => {
160+
// only have to Pick because we want this to work for both Disk and
161+
// Json<Disk>, which we pass to it in the MSW handlers
162+
const test = (d: Pick<Disk, 'state'>) => states.includes(d.state.state)
163+
test.states = states
164+
return test
165+
})
172166

173167
/** Hard coded in the API, so we can hard code it here. */
174168
export const FLEET_ID = '001de000-1334-4000-8000-000000000000'

0 commit comments

Comments
 (0)