Skip to content

Commit

Permalink
Merge pull request #38 from andreafspeziale/develop
Browse files Browse the repository at this point in the history
Stable release
  • Loading branch information
andreafspeziale authored Dec 14, 2023
2 parents 461dd34 + 9dc933f commit 54e34b6
Show file tree
Hide file tree
Showing 22 changed files with 423 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
20.8.1
20.10.0

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.8.1-alpine AS base
FROM node:20.10.0-alpine AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Like the `docker-compose` file there are also some recipes included in the repos

- `npx @andreafspeziale/os-cli i cr -i books -f ./recipes/create-index.json` or `os-cli i cr -i books -f ./recipes/create-index.json`
- `npx @andreafspeziale/os-cli d cr -i books -f ./recipes/create-documents.json` or `os-cli d cr -i books -f ./recipes/create-documents.json`
- `npx @andreafspeziale/os-cli d qr -i books -f ./recipes/query.json` or `os-cli d qr -i books -f ./recipes/query.json`
- `npx @andreafspeziale/os-cli d q -i books -f ./recipes/query.json` or `os-cli d q -i books -f ./recipes/query.json`

### Configuration
> For proxy mode check [aws-sigv4-proxy](https://github.com/awslabs/aws-sigv4-proxy)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
"engines": {
"node": ">=18.16.1"
},
"packageManager": "pnpm@8.9.2",
"packageManager": "pnpm@8.12.1",
"bin": {
"os-cli": "dist/os-cli.js"
},
"scripts": {
"prepare": "husky install",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"format": "prettier --write \"src/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/os-cli",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint": "eslint \"src/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand All @@ -51,7 +51,7 @@
"@nestjs/core": "^10.0.0",
"@opensearch-project/opensearch": "^2.3.0",
"nest-commander": "^3.11.0",
"reflect-metadata": "^0.1.13",
"reflect-metadata": "^0.2.0",
"winston": "^3.10.0",
"zod": "^3.21.4",
"zod-validation-error": "^1.3.1"
Expand Down
38 changes: 19 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions recipes/alias-filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"term": {
"date": "19970726"
}
}
21 changes: 21 additions & 0 deletions src/aliases/aliases.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Module } from '@nestjs/common';
import {
AliasCommand,
CreateAliasCommand,
GetAliasCommand,
ListAliasCommand,
RemoveAliasCommand,
} from './commands';
import { AliasesService } from './aliases.service';

@Module({
providers: [
AliasesService,
AliasCommand,
GetAliasCommand,
CreateAliasCommand,
ListAliasCommand,
RemoveAliasCommand,
],
})
export class AliasesModule {}
46 changes: 46 additions & 0 deletions src/aliases/aliases.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Client } from '@opensearch-project/opensearch';
import { Injectable } from '@nestjs/common';
import { LoggerService } from '../logger';
import { InjectOSClient } from '../os';

@Injectable()
export class AliasesService {
constructor(
private readonly logger: LoggerService,
@InjectOSClient() private readonly osClient: Client,
) {
this.logger.setContext(AliasesService.name);
}

async get(alias: string): Promise<Record<string, unknown>> {
return (await this.osClient.cat.aliases({ name: alias, format: 'json' }))
.body;
}

async create(
alias: string,
index: string,
isWriteIndex: boolean,
filter: Record<string, unknown>,
): Promise<Record<string, unknown>> {
return (
await this.osClient.indices.putAlias({
name: alias,
index,
body: {
is_write_index: isWriteIndex,
filter,
},
})
).body;
}

async list(): Promise<Record<string, unknown>> {
return (await this.osClient.cat.aliases({ format: 'json' })).body;
}

async remove(alias: string, index: string): Promise<Record<string, unknown>> {
return (await this.osClient.indices.deleteAlias({ name: alias, index }))
.body;
}
}
26 changes: 26 additions & 0 deletions src/aliases/commands/aliases.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command, CommandRunner } from 'nest-commander';
import { GetAliasCommand } from './get.command';
import { CreateAliasCommand } from './create.command';
import { ListAliasCommand } from './list.command';
import { RemoveAliasCommand } from './remove.command';

@Command({
name: 'aliases',
description: 'opensearch aliases related commands',
aliases: ['a'],
subCommands: [
GetAliasCommand,
CreateAliasCommand,
ListAliasCommand,
RemoveAliasCommand,
],
})
export class AliasCommand extends CommandRunner {
constructor() {
super();
}

async run(): Promise<void> {
process.stdout.write(this.command.helpInformation());
}
}
119 changes: 119 additions & 0 deletions src/aliases/commands/create.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { z } from 'zod';
import { SubCommand, CommandRunner, Option } from 'nest-commander';
import { LoggerService } from '../../logger';
import { AliasesService } from '../aliases.service';
import {
validateAndParseOrExit,
validateFileOrExit,
ValidJsonPayloadFromString,
} from '../../common';

@SubCommand({
name: 'create',
description: 'create alias or add index to it',
aliases: ['cr'],
})
export class CreateAliasCommand extends CommandRunner {
constructor(
private readonly logger: LoggerService,
private readonly aliasesService: AliasesService,
) {
super();
this.logger.setContext(CreateAliasCommand.name);
}

@Option({
flags: '-a, --alias, <string>',
description: 'alias name',
required: true,
})
parseAlias(val: string): string {
return val;
}

@Option({
flags: '-i, --index, <string>',
description: 'index name',
required: true,
})
parseIndex(val: string): string {
return val;
}

@Option({
flags: '-wr, --write-index, <boolean>',
description: 'is write index',
required: false,
defaultValue: false,
})
parseisWriteIndex(val: string): boolean {
return (
validateAndParseOrExit(val, z.enum(['true', 'false']), this.logger) ===
'true'
);
}

@Option({
flags: '-p, --payload, [string]',
description: 'inline alias custom filter JSON payload',
})
parsePayload(val: string): Record<string, unknown> {
return validateAndParseOrExit(
val,
ValidJsonPayloadFromString.pipe(z.object({}).passthrough()),
this.logger,
);
}

@Option({
flags: '-f, --file, [string]',
description: 'alias custom filter JSON payload file path',
})
parseFile(val: string): Record<string, unknown> {
return validateAndParseOrExit(
validateFileOrExit(val, this.logger),
ValidJsonPayloadFromString.pipe(z.object({}).passthrough()),
this.logger,
);
}

async run(
passedParam: string[],
options: {
alias: string;
index: string;
writeIndex: boolean;
payload?: Record<string, unknown>;
file?: Record<string, unknown>;
},
): Promise<void> {
this.logger.debug('Running command...', {
fn: this.run.name,
passedParam,
options,
});

try {
const res = await this.aliasesService.create(
options.alias,
options.index,
options.writeIndex,
options.payload || options.file || {},
);

this.logger.log('Alias successfully created', {
fn: this.run.name,
res,
});
} catch (error) {
this.logger.error('Error while creating alias', {
fn: this.run.name,
alias: options.alias,
index: options.index,
name: error.name,
body: error.meta.body,
statusCode: error.meta.statusCode,
});
}
}
}
Loading

0 comments on commit 54e34b6

Please sign in to comment.