Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions mock-api/msw/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { commaSeries } from '~/util/str'
import type { Json } from '../json-type'
import { projects, type DbProject } from '../project'
import { defaultSilo, siloSettings } from '../silo'
import { internalError } from './util'
import { internalError, invalidRequest } from './util'

export const notFoundErr = (msg: string) => {
const message = `not found: ${msg}`
Expand Down Expand Up @@ -52,7 +52,7 @@ function ensureNoParentSelectors(
.map(([k]) => k)
if (keysWithValues.length > 0) {
const message = `when ${resourceLabel} is specified by ID, ${commaSeries(keysWithValues, 'and')} should not be specified`
throw json({ error_code: 'InvalidRequest', message }, { status: 400 })
throw invalidRequest(message)
}
}

Expand Down Expand Up @@ -101,13 +101,7 @@ export const resolvePoolSelector = (
!poolSelector.ip_version &&
candidateLinks.length > 1
) {
throw json(
{
error_code: 'InvalidRequest',
message: 'ip_version required when multiple default pools exist',
},
{ status: 400 }
)
throw invalidRequest('ip_version required when multiple default pools exist')
}

const link = candidateLinks[0]
Expand Down
49 changes: 12 additions & 37 deletions mock-api/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
getBlockSize,
handleMetrics,
handleOxqlMetrics,
invalidRequest,
ipRangeLen,
NotImplemented,
paginated,
Expand Down Expand Up @@ -558,21 +559,13 @@ export const handlers = makeHandlers({
// Based on Omicron validation in nexus/db-queries/src/db/datastore/external_ip.rs:544-661
const ipVersion = pool.ip_version
if (ipVersion === 'v4' && !hasIpv4Nic) {
throw json(
{
error_code: 'InvalidRequest',
message: `The ephemeral external IP is an IPv4 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`,
},
{ status: 400 }
throw invalidRequest(
`The ephemeral external IP is an IPv4 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`
)
}
if (ipVersion === 'v6' && !hasIpv6Nic) {
throw json(
{
error_code: 'InvalidRequest',
message: `The ephemeral external IP is an IPv6 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`,
},
{ status: 400 }
throw invalidRequest(
`The ephemeral external IP is an IPv6 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`
)
}
}
Expand Down Expand Up @@ -888,35 +881,21 @@ export const handlers = makeHandlers({
const primaryNic = nics.find((n) => n.primary)

if (!primaryNic) {
throw json(
{
error_code: 'InvalidRequest',
message: `Instance ${instance.name} has no primary network interface`,
},
{ status: 400 }
)
throw invalidRequest(`Instance ${instance.name} has no primary network interface`)
}

const ipVersion = pool.ip_version
const stackType = primaryNic.ip_stack.type

if (ipVersion === 'v4' && stackType !== 'v4' && stackType !== 'dual_stack') {
throw json(
{
error_code: 'InvalidRequest',
message: `The ephemeral external IP is an IPv4 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`,
},
{ status: 400 }
throw invalidRequest(
`The ephemeral external IP is an IPv4 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`
)
}

if (ipVersion === 'v6' && stackType !== 'v6' && stackType !== 'dual_stack') {
throw json(
{
error_code: 'InvalidRequest',
message: `The ephemeral external IP is an IPv6 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`,
},
{ status: 400 }
throw invalidRequest(
`The ephemeral external IP is an IPv6 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`
)
}

Expand Down Expand Up @@ -949,12 +928,8 @@ export const handlers = makeHandlers({
)

if (attachedVersions.size > 1 && !ipVersion) {
throw json(
{
error_code: 'InvalidRequest',
message: `Instance ${instance.name} has both IPv4 and IPv6 ephemeral IPs; ipVersion is required to detach one`,
},
{ status: 400 }
throw invalidRequest(
`Instance ${instance.name} has both IPv4 and IPv6 ephemeral IPs; ipVersion is required to detach one`
)
}

Expand Down
3 changes: 3 additions & 0 deletions mock-api/msw/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export const NotImplemented = () => {
throw json({ error_code: 'NotImplemented' }, { status: 501 })
}

export const invalidRequest = (message: string) =>
json({ error_code: 'InvalidRequest', message }, { status: 400 })

export const internalError = (message: string) =>
json({ error_code: 'InternalError', message }, { status: 500 })

Expand Down
Loading