Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x | import { z } from 'zod';
import { createDrain } from '../../clever-client/drains.js';
import { defineArgument } from '../../lib/define-argument.js';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { DRAIN_TYPE_CLI_CODES, DRAIN_TYPES, resolveDrainResource } from '../../models/drain.js';
import { sendToApi } from '../../models/send-to-api.js';
import { addonIdOrRealIdOption, aliasOption, appIdOrNameOption } from '../global.options.js';
export const drainCreateCommand = defineCommand({
description: 'Create a drain',
since: '0.9.0',
options: {
username: defineOption({
name: 'username',
schema: z.string().optional(),
description: 'Basic auth username (for elasticsearch or raw-http)',
aliases: ['u'],
placeholder: 'username',
}),
password: defineOption({
name: 'password',
schema: z.string().optional(),
description: 'Basic auth password (for elasticsearch or raw-http)',
aliases: ['p'],
placeholder: 'password',
}),
apiKey: defineOption({
name: 'api-key',
schema: z.string().optional(),
description: 'API key (for newrelic)',
aliases: ['k'],
placeholder: 'api-key',
}),
sourceToken: defineOption({
name: 'source-token',
schema: z.string().optional(),
description: 'Source token (for betterstack)',
aliases: ['t'],
placeholder: 'source-token',
}),
indexPrefix: defineOption({
name: 'index-prefix',
schema: z.string().optional(),
description: 'Optional index prefix (for elasticsearch), `logstash` value is used if not set',
aliases: ['i'],
placeholder: 'index-prefix',
}),
rfc5424StructuredDataParameters: defineOption({
name: 'sd-params',
schema: z.string().optional(),
description: 'RFC5424 structured data parameters (for ovh-tcp), e.g.: `X-OVH-TOKEN=\\\"REDACTED\\\"`',
aliases: ['s'],
placeholder: 'sd-params',
}),
alias: aliasOption,
appIdOrName: appIdOrNameOption,
addonIdOrRealId: addonIdOrRealIdOption,
},
args: [
defineArgument({
schema: z.enum(DRAIN_TYPE_CLI_CODES),
description: 'Drain type',
placeholder: 'drain-type',
}),
defineArgument({
schema: z.string(),
description: 'Drain URL',
placeholder: 'drain-url',
}),
],
async handler(options, drainTypeCliCode, url) {
const { alias, appIdOrName, addonIdOrRealId } = options;
const { username, password, apiKey, sourceToken, indexPrefix, rfc5424StructuredDataParameters } = options;
const drainType = Object.values(DRAIN_TYPES).find((drainType) => drainType.cliCode === drainTypeCliCode);
const { ownerId, resourceId } = await resolveDrainResource(alias, appIdOrName, addonIdOrRealId);
const body = {
kind: 'LOG',
recipient: {
type: drainType.apiCode,
url,
},
};
if (drainTypeCliCode === DRAIN_TYPES.ELASTICSEARCH.cliCode) {
if (!indexPrefix) {
throw new Error(
`${DRAIN_TYPES.ELASTICSEARCH.cliCode} drains require an index prefix (--index-prefix) to be set`,
);
}
if (!url.endsWith('/_bulk')) {
throw new Error(`${DRAIN_TYPES.ELASTICSEARCH.cliCode} drain URL must end with '/_bulk'`);
}
body.recipient.index = indexPrefix;
}
if (drainTypeCliCode === DRAIN_TYPES.ELASTICSEARCH.cliCode || drainTypeCliCode === DRAIN_TYPES.RAW_HTTP.cliCode) {
if (username) {
body.recipient.username = username;
}
if (password) {
body.recipient.password = password;
}
}
if (drainTypeCliCode === DRAIN_TYPES.NEWRELIC.cliCode) {
if (!apiKey) {
throw new Error(`${DRAIN_TYPES.NEWRELIC.cliCode} drains require an API key (--api-key) to be set`);
}
body.recipient.apiKey = apiKey;
}
if (drainTypeCliCode === DRAIN_TYPES.BETTERSTACK.cliCode) {
if (!sourceToken) {
throw new Error(`${DRAIN_TYPES.BETTERSTACK.cliCode} drains require a source token (--source-token) to be set`);
}
body.recipient.sourceToken = sourceToken;
}
if (
drainTypeCliCode === DRAIN_TYPES.OVH_TCP.cliCode ||
drainTypeCliCode === DRAIN_TYPES.SYSLOG_TCP.cliCode ||
drainTypeCliCode === DRAIN_TYPES.SYSLOG_UDP.cliCode
) {
if (rfc5424StructuredDataParameters) {
body.recipient.rfc5424StructuredDataParameters = rfc5424StructuredDataParameters;
}
}
const drain = await createDrain({ ownerId, resourceId, body }).then(sendToApi);
Logger.printSuccess(`Drain ${styleText(['bold', 'green'], drain.id)} has been successfully created and enabled!`);
},
});
|