Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add monitor sync command #358

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 56 additions & 0 deletions workflow-cli/src/commands/opensearch-sync-monitors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'reflect-metadata';
import { Command } from '@oclif/core';
import AwsService from '../services/aws.service';
import { bindBroker, bindVault, vsContainer } from '../inversify.config';
import {
accessId,
accessKey,
accountNumber,
arn,
brokerApiUrl,
brokerToken,
domainName,
help,
hostname,
region,
vaultAddr,
vaultToken,
dryRun,
} from '../flags';
import OpenSearchController from '../controller/opensearch.controller';
import { TYPES } from '../inversify.types';

export default class OpenSearchSync extends Command {
static description = 'Sync OpenSearch settings';

static examples = ['<%= config.bin %> <%= command.id %>'];

static flags = {
...hostname,
...domainName,
...region,
...accessId,
...accessKey,
...accountNumber,
...arn,
...brokerApiUrl,
...brokerToken,
...vaultAddr,
...vaultToken,
...help,
...dryRun,
};

public async run(): Promise<void> {
const { flags } = await this.parse(OpenSearchSync);

await AwsService.assumeIdentity(flags);

bindBroker(flags['broker-api-url'], flags['broker-token']);
bindVault(flags['vault-addr'], flags['vault-token']);

await vsContainer
.get<OpenSearchController>(TYPES.OpenSearchController)
.syncMonitors(flags);
}
}
4 changes: 3 additions & 1 deletion workflow-cli/src/controller/opensearch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
)
).data.data,
);
}

// console.log('======= Monitor Sync');
public async syncMonitors(flags: any) {

Check failure on line 49 in workflow-cli/src/controller/opensearch.controller.ts

View workflow job for this annotation

GitHub Actions / test

'flags' is defined but never used

Check failure on line 49 in workflow-cli/src/controller/opensearch.controller.ts

View workflow job for this annotation

GitHub Actions / test

'flags' is defined but never used
console.log('======= Monitor Sync');
// await this.monitorService.sync(flags);
}
}
4 changes: 2 additions & 2 deletions workflow-cli/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const vaultToken = {

export const dryRun = {
dryRun: Flags.boolean({
description: 'Disable deletion of messages',
env: 'AWS_SQS_DRY_RUN',
description: 'Enables dry run',
env: 'DRY_RUN',
default: false,
}),
};
Expand Down
73 changes: 40 additions & 33 deletions workflow-cli/src/services/opensearch-monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BrokerApi } from '../broker/broker.api';
import { GraphServerInstallInstanceDto } from '../broker/dto/graph-server-installs-rest.dto';
import { inject, injectable } from 'inversify';
import { TYPES } from '../inversify.types';
import { dryRun } from '../flags';

const ID_MAX_LENGTH = 20;

Expand Down Expand Up @@ -160,16 +161,18 @@ export default class OpenSearchMonitorService extends AwsService {
// return;
for (const removeHit of removeHits) {
console.log(`Remove monitor: ${removeHit._source.name}`);
// DELETE _plugins/_alerting/monitors/<monitor_id>
await this.executeSignedHttpRequest({
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: `/_plugins/_alerting/monitors/${removeHit._id}`,
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
if (!dryRun) {
// DELETE _plugins/_alerting/monitors/<monitor_id>
await this.executeSignedHttpRequest({
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: `/_plugins/_alerting/monitors/${removeHit._id}`,
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
}
}

for (const monitor of monitors) {
Expand All @@ -196,34 +199,38 @@ export default class OpenSearchMonitorService extends AwsService {
// Add
// POST _plugins/_alerting/monitors
console.log(`Add monitor: ${monitor.name}`);
await this.executeSignedHttpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: '/_plugins/_alerting/monitors',
body: JSON.stringify(monitor),
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
if (!dryRun) {
await this.executeSignedHttpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: '/_plugins/_alerting/monitors',
body: JSON.stringify(monitor),
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
}
} else {
// Update
// PUT _plugins/_alerting/monitors/<monitor_id>
console.log(`Update monitor: ${monitor.name}`);
if (!body.hits.hits[0]._source.enabled) {
// Do not re-enable
monitor.enabled = false;
if (!dryRun) {
if (!body.hits.hits[0]._source.enabled) {
// Do not re-enable
monitor.enabled = false;
}
await this.executeSignedHttpRequest({
method: 'PUT',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: `/_plugins/_alerting/monitors/${body.hits.hits[0]._id}`,
body: JSON.stringify(monitor),
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
}
await this.executeSignedHttpRequest({
method: 'PUT',
headers: {
'Content-Type': 'application/json',
host: settings.hostname,
},
hostname: settings.hostname,
path: `/_plugins/_alerting/monitors/${body.hits.hits[0]._id}`,
body: JSON.stringify(monitor),
}).then((res) => this.waitAndReturnResponseBody(res, [404]));
}
}
}
Expand Down
Loading