|
| 1 | +// `insta services` — manage a project's opt-in services (postgres | storage | compute). |
| 2 | +import { ApiClient, requireProject } from '../api.js' |
| 3 | +import { info, printJson, handleApproval } from '../util.js' |
| 4 | + |
| 5 | +export const SERVICE_TYPES = ['postgres', 'storage', 'compute'] as const |
| 6 | +export type ServiceType = (typeof SERVICE_TYPES)[number] |
| 7 | + |
| 8 | +// ---- pure, unit-tested helpers (throw plain Errors; the CLI guard turns them into clean output) ---- |
| 9 | + |
| 10 | +// Validate a service-type argument against the allowed set for a command. |
| 11 | +export function assertType(type: string, allowed: readonly string[] = SERVICE_TYPES): asserts type is ServiceType { |
| 12 | + if (!allowed.includes(type)) throw new Error(`type must be ${allowed.join('|')}`) |
| 13 | +} |
| 14 | + |
| 15 | +// Parse a positive-integer machine count. |
| 16 | +export function parseCount(raw: string): number { |
| 17 | + const n = Number(raw) |
| 18 | + if (!Number.isInteger(n) || n < 1) throw new Error(`count must be a positive integer, got: ${raw}`) |
| 19 | + return n |
| 20 | +} |
| 21 | + |
| 22 | +// Resolve a service id from a `services list` result by (type, name). |
| 23 | +export function resolveServiceId(services: Array<{ id: string; type: string; name: string }>, type: string, name: string): string { |
| 24 | + const svc = services.find((s) => s.type === type && s.name === name) |
| 25 | + if (!svc) throw new Error(`service not found: ${type} ${name}`) |
| 26 | + return svc.id |
| 27 | +} |
| 28 | + |
| 29 | +// ---- commands ---- |
| 30 | + |
| 31 | +export async function servicesAdd(type: string, name: string): Promise<void> { |
| 32 | + assertType(type) |
| 33 | + const api = await ApiClient.load() |
| 34 | + const p = await requireProject() |
| 35 | + const res = await api.rawRequest('POST', `/projects/${p.projectId}/services`, { type, name }) |
| 36 | + if (handleApproval(res)) return |
| 37 | + const svc = res.body.service |
| 38 | + info(`added ${type} service ${name} (${svc.id})${svc.domain ? ` — ${svc.domain}` : ''}`) |
| 39 | +} |
| 40 | + |
| 41 | +export async function servicesList(opts: { json?: boolean }): Promise<void> { |
| 42 | + const api = await ApiClient.load() |
| 43 | + const p = await requireProject() |
| 44 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 45 | + if (opts.json) return printJson(services) |
| 46 | + if (!services.length) return info('(no services — add one with `insta services add <postgres|storage|compute> <name>`)') |
| 47 | + for (const s of services) { |
| 48 | + const extra = s.type === 'compute' ? ` x${s.machine_count}` : '' |
| 49 | + info(`${s.type}/${s.name} [${s.status}]${extra}${s.domain ? ` ${s.domain}` : ''} ${s.id}`) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export async function servicesRemove(type: string, name: string): Promise<void> { |
| 54 | + assertType(type) |
| 55 | + const api = await ApiClient.load() |
| 56 | + const p = await requireProject() |
| 57 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 58 | + const id = resolveServiceId(services, type, name) |
| 59 | + const res = await api.rawRequest('DELETE', `/projects/${p.projectId}/services/${id}`) |
| 60 | + if (handleApproval(res)) return |
| 61 | + info(`removed ${type} service ${name}`) |
| 62 | +} |
| 63 | + |
| 64 | +// insta services scale compute <name> <number> [region] |
| 65 | +export async function servicesScale(type: string, name: string, number: string, region: string | undefined, _opts: { json?: boolean }): Promise<void> { |
| 66 | + assertType(type, ['compute']) |
| 67 | + const machineCount = parseCount(number) |
| 68 | + const api = await ApiClient.load() |
| 69 | + const p = await requireProject() |
| 70 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 71 | + const id = resolveServiceId(services, type, name) |
| 72 | + const res = await api.rawRequest('POST', `/projects/${p.projectId}/services/${id}/scale`, { machineCount, region }) |
| 73 | + if (handleApproval(res)) return |
| 74 | + if (_opts.json) return printJson(res.body.service) |
| 75 | + info(`scaled compute ${name} to ${machineCount} machine(s)${region ? ` in ${region}` : ''}`) |
| 76 | +} |
| 77 | + |
| 78 | +// insta services upgrade <compute|postgres> <name> <new-spec> |
| 79 | +export async function servicesUpgrade(type: string, name: string, spec: string, _opts: { json?: boolean }): Promise<void> { |
| 80 | + assertType(type, ['compute', 'postgres']) |
| 81 | + const api = await ApiClient.load() |
| 82 | + const p = await requireProject() |
| 83 | + const { services } = await api.request('GET', `/projects/${p.projectId}/services`) |
| 84 | + const id = resolveServiceId(services, type, name) |
| 85 | + const res = await api.rawRequest('POST', `/projects/${p.projectId}/services/${id}/upgrade`, { spec }) |
| 86 | + if (handleApproval(res)) return |
| 87 | + if (_opts.json) return printJson(res.body.service) |
| 88 | + info(`upgraded ${type} ${name} to ${spec}`) |
| 89 | +} |
0 commit comments