Skip to content

Commit af6c1f4

Browse files
authored
tools: update description helper for MSW handlers (#2368)
tools: update description helper for msw handlers
1 parent 60ef745 commit af6c1f4

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

mock-api/msw/handlers.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
requireFleetViewer,
5252
requireRole,
5353
unavailableErr,
54+
updateDesc,
5455
} from './util'
5556

5657
// Note the *JSON types. Those represent actual API request and response bodies,
@@ -99,7 +100,7 @@ export const handlers = makeHandlers({
99100
}
100101
project.name = body.name
101102
}
102-
project.description = body.description || ''
103+
updateDesc(project, body)
103104

104105
return project
105106
},
@@ -242,7 +243,7 @@ export const handlers = makeHandlers({
242243
},
243244
floatingIpCreate({ body, query }) {
244245
const project = lookup.project(query)
245-
errIfExists(db.floatingIps, { name: body.name })
246+
errIfExists(db.floatingIps, { name: body.name, project_id: project.id })
246247

247248
// TODO: when IP is specified, use ipInAnyRange to check that it is in the pool
248249
const pool = body.pool
@@ -276,7 +277,7 @@ export const handlers = makeHandlers({
276277
}
277278
floatingIp.name = body.name
278279
}
279-
floatingIp.description = body.description || ''
280+
updateDesc(floatingIp, body)
280281
return floatingIp
281282
},
282283
floatingIpDelete({ path, query }) {
@@ -644,7 +645,7 @@ export const handlers = makeHandlers({
644645
if (body.name) {
645646
nic.name = body.name
646647
}
647-
nic.description = body.description || ''
648+
updateDesc(nic, body)
648649

649650
if (typeof body.primary === 'boolean' && body.primary !== nic.primary) {
650651
if (nic.primary) {
@@ -880,7 +881,8 @@ export const handlers = makeHandlers({
880881
}
881882
pool.name = body.name
882883
}
883-
pool.description = body.description || ''
884+
885+
updateDesc(pool, body)
884886

885887
return pool
886888
},
@@ -923,7 +925,7 @@ export const handlers = makeHandlers({
923925
throw 'Cannot snapshot disk'
924926
}
925927

926-
errIfExists(db.snapshots, { name: body.name })
928+
errIfExists(db.snapshots, { name: body.name, project_id: project.id })
927929

928930
const disk = lookup.disk({ ...query, disk: body.disk })
929931
if (!diskCan.snapshot(disk)) {
@@ -973,7 +975,7 @@ export const handlers = makeHandlers({
973975
},
974976
vpcCreate({ body, query }) {
975977
const project = lookup.project(query)
976-
errIfExists(db.vpcs, { name: body.name })
978+
errIfExists(db.vpcs, { name: body.name, project_id: project.id })
977979

978980
const newVpc: Json<Api.Vpc> = {
979981
id: uuid(),
@@ -1011,9 +1013,7 @@ export const handlers = makeHandlers({
10111013
vpc.name = body.name
10121014
}
10131015

1014-
if (typeof body.description === 'string') {
1015-
vpc.description = body.description
1016-
}
1016+
updateDesc(vpc, body)
10171017

10181018
if (body.dns_name) {
10191019
vpc.dns_name = body.dns_name
@@ -1083,9 +1083,7 @@ export const handlers = makeHandlers({
10831083
if (body.name) {
10841084
subnet.name = body.name
10851085
}
1086-
if (typeof body.description === 'string') {
1087-
subnet.description = body.description
1088-
}
1086+
updateDesc(subnet, body)
10891087

10901088
return subnet
10911089
},

mock-api/msw/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,13 @@ function ipInRange(ip: string, { first, last }: IpRange): boolean {
399399

400400
export const ipInAnyRange = (ip: string, ranges: IpRange[]) =>
401401
ranges.some((range) => ipInRange(ip, range))
402+
403+
export function updateDesc(
404+
resource: { description: string },
405+
update: { description?: string }
406+
) {
407+
// Can't be `if (update.description)` because you could never set it to ''
408+
if (update.description !== undefined) {
409+
resource.description = update.description
410+
}
411+
}

0 commit comments

Comments
 (0)