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
65 changes: 37 additions & 28 deletions src/clients/cc-api/commands/log-drain/create-log-drain-command.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
/**
* @import { CreateLogDrainCommandInput, CreateLogDrainCommandOutput } from './create-log-drain-command.types.js';
* @import { LogDrainTarget } from './log-drain.types.js';
* @import { LogDrainTarget, LogDrainKind } from './log-drain.types.js';
*/
import { post } from '../../../../lib/request/request-params-builder.js';
import { safeUrl } from '../../../../lib/utils.js';
import { CcApiCompositeCommand, CcApiSimpleCommand } from '../../lib/cc-api-command.js';
import { GetLogDrainCommand } from './get-log-drain-command.js';
import { waitForLogDrainEnabled } from './log-drain-utils.js';

/**
*
* @extends {CcApiCompositeCommand<CreateLogDrainCommandInput, CreateLogDrainCommandOutput>}
* @endpoint [POST] /v2/logs/:XXX/drains
* @endpoint [GET] /v2/logs/:XXX/drains/:XXX
* @endpoint [POST] /v4/drains/organisations/:XXX/applications/:XXX/drains
* @endpoint [GET] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX
* @group LogDrain
* @version 2
* @version 4
*/
export class CreateLogDrainCommand extends CcApiCompositeCommand {
/** @type {CcApiCompositeCommand<CreateLogDrainCommandInput, CreateLogDrainCommandOutput>['compose']} */
async compose(params, composer) {
const created = await composer.send(new CreateLogDrainInnerCommand(params));

return composer.send(
new GetLogDrainCommand(
'applicationId' in params
? { applicationId: params.applicationId, drainId: created.id }
: { addonId: params.addonId, drainId: created.id },
),
);
return waitForLogDrainEnabled(composer, params.ownerId, params.applicationId, created.id);
}
}

/**
*
* @extends {CcApiSimpleCommand<CreateLogDrainCommandInput, {id: string}>}
* @endpoint [POST] /v2/logs/:XXX/drains
* @endpoint [POST] /v4/drains/organisations/:XXX/applications/:XXX/drains
* @group LogDrain
* @version 2
* @version 4
*/
class CreateLogDrainInnerCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<CreateLogDrainCommandInput, {id: string}>['toRequestParams']} */
toRequestParams(params) {
const resourceId = 'applicationId' in params ? params.applicationId : params.addonId;

return post(safeUrl`/v2/logs/${resourceId}/drains`, this.#getBody(params.target));
return post(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains`,
this.#getBody(params.target, params.kind),
);
}

/** @type {CcApiSimpleCommand<CreateLogDrainCommandInput, {id: string}>['transformCommandOutput']} */
Expand All @@ -53,34 +47,49 @@ class CreateLogDrainInnerCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
addonId: 'ADDON_ID',
ownerId: true,
};
}

/**
* @param {LogDrainTarget} drain
* @param {LogDrainKind} kind
*/
#getBody(drain) {
#getBody(drain, kind) {
/** @type {any} */
const body = {
url: drain.url,
drainType: drain.type,
kind,
recipient: {
type: drain.type,
url: drain.url,
},
};

if (drain.type === 'HTTP' || drain.type === 'ElasticSearch') {
// RAW_HTTP and ELASTICSEARCH: credentials
if (drain.type === 'RAW_HTTP' || drain.type === 'ELASTICSEARCH') {
if (drain.credentials != null) {
body.credentials = drain.credentials;
body.recipient.username = drain.credentials.username;
body.recipient.password = drain.credentials.password;
}
}

if (drain.type === 'ElasticSearch') {
// ELASTICSEARCH: index (renamed from indexPrefix)
if (drain.type === 'ELASTICSEARCH') {
if (drain.indexPrefix != null) {
body.indexPrefix = drain.indexPrefix;
body.recipient.index = drain.indexPrefix;
}
}

if (drain.type === 'NewRelicHTTP') {
body.apiKey = drain.apiKey;
// NEWRELIC: apiKey
if (drain.type === 'NEWRELIC') {
body.recipient.apiKey = drain.apiKey;
}

// Syslog: RFC 5424 structured data parameters
if (drain.type === 'SYSLOG_TCP' || drain.type === 'SYSLOG_UDP') {
if (drain.structuredDataParameters != null) {
body.recipient.rfc5424StructuredDataParameters = drain.structuredDataParameters;
}
}

return body;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { LogDrain, LogDrainTarget } from './log-drain.types.js';
import { ApplicationId } from '../../types/cc-api.types.js';
import type { LogDrain, LogDrainKind, LogDrainTarget } from './log-drain.types.js';

export type CreateLogDrainCommandInput = ({ applicationId: string } | { addonId: string }) & {
export type CreateLogDrainCommandInput = ApplicationId & {
kind: LogDrainKind;
target: LogDrainTarget;
};

Expand Down
15 changes: 10 additions & 5 deletions src/clients/cc-api/commands/log-drain/delete-log-drain-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ import { CcApiSimpleCommand } from '../../lib/cc-api-command.js';
/**
*
* @extends {CcApiSimpleCommand<DeleteLogDrainCommandInput, void>}
* @endpoint [DELETE] /v2/logs/:XXX/drains/:XXX
* @endpoint [DELETE] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX
* @group LogDrain
* @version 2
* @version 4
*/
export class DeleteLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<DeleteLogDrainCommandInput, void>['toRequestParams']} */
toRequestParams(params) {
const resourceId = 'applicationId' in params ? params.applicationId : params.addonId;
return delete_(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains/${params.drainId}`,
);
}

return delete_(safeUrl`/v2/logs/${resourceId}/drains/${params.drainId}`);
/** @type {CcApiSimpleCommand<DeleteLogDrainCommandInput, void>['transformCommandOutput']} */
transformCommandOutput() {
return null;
}

/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
addonId: 'ADDON_ID',
ownerId: true,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type DeleteLogDrainCommandInput = ({ applicationId: string } | { addonId: string }) & {
import { ApplicationId } from '../../types/cc-api.types.js';

export type DeleteLogDrainCommandInput = ApplicationId & {
drainId: string;
};
51 changes: 51 additions & 0 deletions src/clients/cc-api/commands/log-drain/disable-log-drain-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @import { DisableLogDrainCommandInput, DisableLogDrainCommandOutput } from './disable-log-drain-command.types.js';
*/
import { put } from '../../../../lib/request/request-params-builder.js';
import { safeUrl } from '../../../../lib/utils.js';
import { CcApiCompositeCommand, CcApiSimpleCommand } from '../../lib/cc-api-command.js';
import { waitForLogDrainDisabled } from './log-drain-utils.js';

/**
*
* @extends {CcApiCompositeCommand<DisableLogDrainCommandInput, DisableLogDrainCommandOutput>}
* @endpoint [PUT] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX/disable
* @endpoint [GET] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX
* @group LogDrain
* @version 4
*/
export class DisableLogDrainCommand extends CcApiCompositeCommand {
/** @type {CcApiCompositeCommand<DisableLogDrainCommandInput, DisableLogDrainCommandOutput>['compose']} */
async compose(params, composer) {
await composer.send(new InnerDisableLogDrainCommand(params));
return waitForLogDrainDisabled(composer, params.ownerId, params.applicationId, params.drainId);
}
}

/**
*
* @extends {CcApiSimpleCommand<DisableLogDrainCommandInput, void>}
* @endpoint [PUT] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX/disable
* @group LogDrain
* @version 4
*/
class InnerDisableLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<DisableLogDrainCommandInput, void>['toRequestParams']} */
toRequestParams(params) {
return put(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains/${params.drainId}/disable`,
);
}

/** @type {CcApiSimpleCommand<DisableLogDrainCommandInput, void>['transformCommandOutput']} */
transformCommandOutput() {
return null;
}

/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
ownerId: true,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ApplicationId } from '../../types/cc-api.types.js';
import type { LogDrain } from './log-drain.types.js';

export type DisableLogDrainCommandInput = ApplicationId & {
drainId: string;
};

export type DisableLogDrainCommandOutput = LogDrain;
51 changes: 51 additions & 0 deletions src/clients/cc-api/commands/log-drain/enable-log-drain-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @import { EnableLogDrainCommandInput, EnableLogDrainCommandOutput } from './enable-log-drain-command.types.js';
*/
import { put } from '../../../../lib/request/request-params-builder.js';
import { safeUrl } from '../../../../lib/utils.js';
import { CcApiCompositeCommand, CcApiSimpleCommand } from '../../lib/cc-api-command.js';
import { waitForLogDrainEnabled } from './log-drain-utils.js';

/**
*
* @extends {CcApiCompositeCommand<EnableLogDrainCommandInput, EnableLogDrainCommandOutput>}
* @endpoint [PUT] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX/enable
* @endpoint [GET] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX
* @group LogDrain
* @version 4
*/
export class EnableLogDrainCommand extends CcApiCompositeCommand {
/** @type {CcApiCompositeCommand<EnableLogDrainCommandInput, EnableLogDrainCommandOutput>['compose']} */
async compose(params, composer) {
await composer.send(new InnerEnableLogDrainCommand(params));
return waitForLogDrainEnabled(composer, params.ownerId, params.applicationId, params.drainId);
}
}

/**
*
* @extends {CcApiSimpleCommand<EnableLogDrainCommandInput, void>}
* @endpoint [PUT] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX/enable
* @group LogDrain
* @version 4
*/
class InnerEnableLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<EnableLogDrainCommandInput, void>['toRequestParams']} */
toRequestParams(params) {
return put(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains/${params.drainId}/enable`,
);
}

/** @type {CcApiSimpleCommand<EnableLogDrainCommandInput, void>['transformCommandOutput']} */
transformCommandOutput() {
return null;
}

/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
ownerId: true,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApplicationId } from '../../types/cc-api.types.js';
import type { LogDrain } from './log-drain.types.js';

export type EnableLogDrainCommandInput = ApplicationId & {
drainId: string;
};

export type EnableLogDrainCommandOutput = LogDrain;
20 changes: 7 additions & 13 deletions src/clients/cc-api/commands/log-drain/get-log-drain-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,27 @@ import { transformLogDrain } from './log-drain-transform.js';
/**
*
* @extends {CcApiSimpleCommand<GetLogDrainCommandInput, GetLogDrainCommandOutput>}
* @endpoint [GET] /v2/logs/:XXX/drains/:XXX
* @endpoint [GET] /v4/drains/organisations/:XXX/applications/:XXX/drains/:XXX
* @group LogDrain
* @version 2
* @version 4
*/
export class GetLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<GetLogDrainCommandInput, GetLogDrainCommandOutput>['toRequestParams']} */
toRequestParams(params) {
const resourceId = 'applicationId' in params ? params.applicationId : params.addonId;

return get(safeUrl`/v2/logs/${resourceId}/drains/${params.drainId}`);
return get(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains/${params.drainId}`,
);
}

/** @type {CcApiSimpleCommand<GetLogDrainCommandInput, GetLogDrainCommandOutput>['transformCommandOutput']} */
transformCommandOutput(response) {
if (response == null) {
return null;
}
if (response.length === 0) {
return null;
}
return transformLogDrain(response[0]);
return transformLogDrain(response);
}

/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
addonId: 'ADDON_ID',
ownerId: true,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApplicationId } from '../../types/cc-api.types.js';
import type { LogDrain } from './log-drain.types.js';

export type GetLogDrainCommandInput = ({ applicationId: string } | { addonId: string }) & {
export type GetLogDrainCommandInput = ApplicationId & {
drainId: string;
};

Expand Down
19 changes: 12 additions & 7 deletions src/clients/cc-api/commands/log-drain/list-log-drain-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @import { ListLogDrainCommandInput, ListLogDrainCommandOutput } from './list-log-drain-command.types.js';
*/
import { QueryParams } from '../../../../lib/request/query-params.js';
import { get } from '../../../../lib/request/request-params-builder.js';
import { safeUrl, sortBy } from '../../../../lib/utils.js';
import { CcApiSimpleCommand } from '../../lib/cc-api-command.js';
Expand All @@ -9,21 +10,25 @@ import { transformLogDrain } from './log-drain-transform.js';
/**
*
* @extends {CcApiSimpleCommand<ListLogDrainCommandInput, ListLogDrainCommandOutput>}
* @endpoint [GET] /v2/logs/:XXX/drains
* @endpoint [GET] /v4/drains/organisations/:XXX/applications/:XXX/drains
* @group LogDrain
* @version 2
* @version 4
*/
export class ListLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<ListLogDrainCommandInput, ListLogDrainCommandOutput>['toRequestParams']} */
toRequestParams(params) {
const resourceId = 'applicationId' in params ? params.applicationId : params.addonId;

return get(safeUrl`/v2/logs/${resourceId}/drains`);
return get(
safeUrl`/v4/drains/organisations/${params.ownerId}/applications/${params.applicationId}/drains`,
new QueryParams()
.set('status', params.status)
.set('executionStatus', params.executionStatus)
.set('executionStatusNotIn', params.executionStatusNotIn),
);
}

/** @type {CcApiSimpleCommand<ListLogDrainCommandInput, ListLogDrainCommandOutput>['transformCommandOutput']} */
transformCommandOutput(response) {
return sortBy(response.map(transformLogDrain), { key: 'createdAt', order: 'desc' });
return sortBy(response.map(transformLogDrain), { key: 'updatedAt', order: 'desc' });
}

/** @type {CcApiSimpleCommand<?, ?>['getEmptyResponsePolicy']} */
Expand All @@ -34,7 +39,7 @@ export class ListLogDrainCommand extends CcApiSimpleCommand {
/** @type {CcApiSimpleCommand<?, ?>['getIdsToResolve']} */
getIdsToResolve() {
return {
addonId: 'ADDON_ID',
ownerId: true,
};
}
}
Loading
Loading