Files
now-deployment/node_modules/now/dist/util/scale/patch-deployment-scale.ts
2019-10-17 12:36:15 +09:00

56 lines
1.4 KiB
TypeScript

import chalk from 'chalk';
import wait from '../output/wait';
import joinWords from '../output/join-words';
import * as Errors from '../errors-ts';
import { Output } from '../output';
import Client from '../client';
type ScaleArgs = {
min: number;
max: number | 'auto';
};
export default async function patchDeploymentScale(
output: Output,
client: Client,
deploymentId: string,
scaleArgs: ScaleArgs,
url: string
) {
const cancelWait = wait(
`Setting scale rules for ${joinWords(
Object.keys(scaleArgs).map(dc => `${chalk.bold(dc)}`)
)}`
);
try {
await client.fetch(
`/v3/now/deployments/${encodeURIComponent(deploymentId)}/instances`,
{
method: 'PATCH',
body: scaleArgs
}
);
cancelWait();
} catch (error) {
cancelWait();
if (error.code === 'forbidden_min_instances') {
return new Errors.ForbiddenScaleMinInstances(url, error.max);
}
if (error.code === 'forbidden_max_instances') {
return new Errors.ForbiddenScaleMaxInstances(url, error.max);
}
if (error.code === 'wrong_min_max_relation') {
return new Errors.InvalidScaleMinMaxRelation(url);
}
if (error.code === 'not_supported_min_scale_slots') {
return new Errors.NotSupportedMinScaleSlots(url);
}
if (error.code === 'deployment_type_unsupported') {
return new Errors.DeploymentTypeUnsupported();
}
throw error;
}
}