Skip to content

Commit 7999343

Browse files
authored
Bump API for IP pools and implement mock API (#1880)
bump API for IP pools and implement mock API
1 parent 367142c commit 7999343

File tree

10 files changed

+586
-102
lines changed

10 files changed

+586
-102
lines changed

OMICRON_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b1ebae8ab2cb7e636f04d847e0ac77aba891ff30
1+
19059b1bedc8bbc7a9d293486842a9a4fd264ea3

libs/api-mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
export * from './disk'
1010
export * from './image'
1111
export * from './instance'
12+
export * from './ip-pool'
1213
export * from './network-interface'
1314
export * from './physical-disk'
1415
export * from './project'

libs/api-mocks/ip-pool.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
9+
import { type IpPool, type IpPoolSilo } from '@oxide/api'
10+
11+
import type { Json } from './json-type'
12+
import { defaultSilo } from './silo'
13+
14+
const ipPool1: Json<IpPool> = {
15+
id: '69b5c583-74a9-451a-823d-0741c1ec66e2',
16+
name: 'ip-pool-1',
17+
description: '',
18+
time_created: new Date().toISOString(),
19+
time_modified: new Date().toISOString(),
20+
}
21+
22+
const ipPool2: Json<IpPool> = {
23+
id: 'af2fbe06-b21d-4364-96b7-a58220bc3242',
24+
name: 'ip-pool-2',
25+
description: '',
26+
time_created: new Date().toISOString(),
27+
time_modified: new Date().toISOString(),
28+
}
29+
30+
export const ipPools: Json<IpPool>[] = [ipPool1, ipPool2]
31+
32+
export const ipPoolSilos: Json<IpPoolSilo>[] = [
33+
{
34+
ip_pool_id: ipPool1.id,
35+
silo_id: defaultSilo.id,
36+
is_default: true,
37+
},
38+
]

libs/api-mocks/msw/db.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ export const lookup = {
130130
if (!image) throw notFoundErr
131131
return image
132132
},
133+
ipPool({ pool: id }: PP.IpPool): Json<Api.IpPool> {
134+
if (!id) throw notFoundErr
135+
136+
if (isUuid(id)) return lookupById(db.ipPools, id)
137+
138+
const pool = db.ipPools.find((p) => p.name === id)
139+
if (!pool) throw notFoundErr
140+
141+
return pool
142+
},
143+
// unusual one because it's a sibling relationship. we look up both the pool and the silo first
144+
ipPoolSilo({ pool: poolId, silo: siloId }: PP.IpPool & PP.Silo): Json<Api.IpPoolSilo> {
145+
const pool = lookup.ipPool({ pool: poolId })
146+
const silo = lookup.silo({ silo: siloId })
147+
148+
const ipPoolSilo = db.ipPoolSilos.find(
149+
(ips) => ips.ip_pool_id === pool.id && ips.silo_id === silo.id
150+
)
151+
if (!ipPoolSilo) throw notFoundErr
152+
153+
return ipPoolSilo
154+
},
133155
samlIdp({
134156
provider: id,
135157
...siloSelector
@@ -203,6 +225,8 @@ const initDb = {
203225
groupMemberships: [...mock.groupMemberships],
204226
images: [...mock.images],
205227
instances: [...mock.instances],
228+
ipPools: [...mock.ipPools],
229+
ipPoolSilos: [...mock.ipPoolSilos],
206230
networkInterfaces: [mock.networkInterface],
207231
physicalDisks: [...mock.physicalDisks],
208232
projects: [...mock.projects],

libs/api-mocks/msw/handlers.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,66 @@ export const handlers = makeHandlers({
547547

548548
return json(instance, { status: 202 })
549549
},
550+
ipPoolList: ({ query }) => paginated(query, db.ipPools),
551+
ipPoolView: ({ path }) => lookup.ipPool(path),
552+
ipPoolSiloList({ path /*query*/ }) {
553+
// TODO: paginated wants an id field, but this is a join table, so it has a
554+
// composite pk
555+
// return paginated(query, db.ipPoolResources)
556+
557+
const pool = lookup.ipPool(path)
558+
const assocs = db.ipPoolSilos.filter((ipr) => ipr.ip_pool_id === pool.id)
559+
return { items: assocs }
560+
},
561+
ipPoolSiloLink({ path, body }) {
562+
const pool = lookup.ipPool(path)
563+
const silo_id = lookup.silo({ silo: body.silo }).id
564+
565+
const assoc = {
566+
ip_pool_id: pool.id,
567+
silo_id,
568+
is_default: body.is_default,
569+
}
570+
571+
const alreadyThere = db.ipPoolSilos.find(
572+
(ips) => ips.ip_pool_id === pool.id && ips.silo_id === silo_id
573+
)
574+
575+
// TODO: this matches current API logic but makes no sense because is_default
576+
// could be different. Need to fix that. Should 400 or 409 on conflict.
577+
if (!alreadyThere) db.ipPoolSilos.push(assoc)
578+
579+
return assoc
580+
},
581+
ipPoolSiloUnlink({ path }) {
582+
const pool = lookup.ipPool(path)
583+
const silo = lookup.silo(path)
584+
585+
// ignore is_default when deleting, it's not part of the pk
586+
db.ipPoolSilos = db.ipPoolSilos.filter(
587+
(ips) => !(ips.ip_pool_id === pool.id && ips.silo_id === silo.id)
588+
)
589+
590+
return 204
591+
},
592+
ipPoolSiloUpdate: ({ path, body }) => {
593+
const ipPoolSilo = lookup.ipPoolSilo(path)
594+
595+
// if we're setting default, we need to set is_default false on the existing default
596+
if (body.is_default) {
597+
const silo = lookup.silo(path)
598+
const existingDefault = db.ipPoolSilos.find(
599+
(ips) => ips.silo_id === silo.id && ips.is_default
600+
)
601+
if (existingDefault) {
602+
existingDefault.is_default = false
603+
}
604+
}
605+
606+
ipPoolSilo.is_default = body.is_default
607+
608+
return ipPoolSilo
609+
},
550610
projectPolicyView({ path }) {
551611
const project = lookup.project(path)
552612

@@ -960,7 +1020,6 @@ export const handlers = makeHandlers({
9601020
siloMetric: handleMetrics,
9611021

9621022
// Misc endpoints we're not using yet in the console
963-
addSledToInitializedRack: NotImplemented,
9641023
certificateCreate: NotImplemented,
9651024
certificateDelete: NotImplemented,
9661025
certificateList: NotImplemented,
@@ -973,7 +1032,6 @@ export const handlers = makeHandlers({
9731032
instanceSerialConsoleStream: NotImplemented,
9741033
ipPoolCreate: NotImplemented,
9751034
ipPoolDelete: NotImplemented,
976-
ipPoolList: NotImplemented,
9771035
ipPoolRangeAdd: NotImplemented,
9781036
ipPoolRangeList: NotImplemented,
9791037
ipPoolRangeRemove: NotImplemented,
@@ -982,7 +1040,6 @@ export const handlers = makeHandlers({
9821040
ipPoolServiceRangeRemove: NotImplemented,
9831041
ipPoolServiceView: NotImplemented,
9841042
ipPoolUpdate: NotImplemented,
985-
ipPoolView: NotImplemented,
9861043
localIdpUserCreate: NotImplemented,
9871044
localIdpUserDelete: NotImplemented,
9881045
localIdpUserSetPassword: NotImplemented,
@@ -1021,12 +1078,13 @@ export const handlers = makeHandlers({
10211078
siloQuotasView: NotImplemented,
10221079
siloUserList: NotImplemented,
10231080
siloUserView: NotImplemented,
1081+
sledAdd: NotImplemented,
1082+
sledListUninitialized: NotImplemented,
10241083
sledSetProvisionState: NotImplemented,
10251084
switchList: NotImplemented,
10261085
switchView: NotImplemented,
10271086
systemPolicyUpdate: NotImplemented,
10281087
systemQuotasList: NotImplemented,
1029-
uninitializedSledList: NotImplemented,
10301088
userBuiltinList: NotImplemented,
10311089
userBuiltinView: NotImplemented,
10321090
})

0 commit comments

Comments
 (0)