Skip to content

Commit

Permalink
tools: update description helper for MSW handlers (#2368)
Browse files Browse the repository at this point in the history
tools: update description helper for msw handlers
  • Loading branch information
david-crespo authored Aug 14, 2024
1 parent 60ef745 commit af6c1f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
24 changes: 11 additions & 13 deletions mock-api/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
requireFleetViewer,
requireRole,
unavailableErr,
updateDesc,
} from './util'

// Note the *JSON types. Those represent actual API request and response bodies,
Expand Down Expand Up @@ -99,7 +100,7 @@ export const handlers = makeHandlers({
}
project.name = body.name
}
project.description = body.description || ''
updateDesc(project, body)

return project
},
Expand Down Expand Up @@ -242,7 +243,7 @@ export const handlers = makeHandlers({
},
floatingIpCreate({ body, query }) {
const project = lookup.project(query)
errIfExists(db.floatingIps, { name: body.name })
errIfExists(db.floatingIps, { name: body.name, project_id: project.id })

// TODO: when IP is specified, use ipInAnyRange to check that it is in the pool
const pool = body.pool
Expand Down Expand Up @@ -276,7 +277,7 @@ export const handlers = makeHandlers({
}
floatingIp.name = body.name
}
floatingIp.description = body.description || ''
updateDesc(floatingIp, body)
return floatingIp
},
floatingIpDelete({ path, query }) {
Expand Down Expand Up @@ -644,7 +645,7 @@ export const handlers = makeHandlers({
if (body.name) {
nic.name = body.name
}
nic.description = body.description || ''
updateDesc(nic, body)

if (typeof body.primary === 'boolean' && body.primary !== nic.primary) {
if (nic.primary) {
Expand Down Expand Up @@ -880,7 +881,8 @@ export const handlers = makeHandlers({
}
pool.name = body.name
}
pool.description = body.description || ''

updateDesc(pool, body)

return pool
},
Expand Down Expand Up @@ -923,7 +925,7 @@ export const handlers = makeHandlers({
throw 'Cannot snapshot disk'
}

errIfExists(db.snapshots, { name: body.name })
errIfExists(db.snapshots, { name: body.name, project_id: project.id })

const disk = lookup.disk({ ...query, disk: body.disk })
if (!diskCan.snapshot(disk)) {
Expand Down Expand Up @@ -973,7 +975,7 @@ export const handlers = makeHandlers({
},
vpcCreate({ body, query }) {
const project = lookup.project(query)
errIfExists(db.vpcs, { name: body.name })
errIfExists(db.vpcs, { name: body.name, project_id: project.id })

const newVpc: Json<Api.Vpc> = {
id: uuid(),
Expand Down Expand Up @@ -1011,9 +1013,7 @@ export const handlers = makeHandlers({
vpc.name = body.name
}

if (typeof body.description === 'string') {
vpc.description = body.description
}
updateDesc(vpc, body)

if (body.dns_name) {
vpc.dns_name = body.dns_name
Expand Down Expand Up @@ -1083,9 +1083,7 @@ export const handlers = makeHandlers({
if (body.name) {
subnet.name = body.name
}
if (typeof body.description === 'string') {
subnet.description = body.description
}
updateDesc(subnet, body)

return subnet
},
Expand Down
10 changes: 10 additions & 0 deletions mock-api/msw/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,13 @@ function ipInRange(ip: string, { first, last }: IpRange): boolean {

export const ipInAnyRange = (ip: string, ranges: IpRange[]) =>
ranges.some((range) => ipInRange(ip, range))

export function updateDesc(
resource: { description: string },
update: { description?: string }
) {
// Can't be `if (update.description)` because you could never set it to ''
if (update.description !== undefined) {
resource.description = update.description
}
}

0 comments on commit af6c1f4

Please sign in to comment.