-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from andreafspeziale/develop
Stable release
- Loading branch information
Showing
22 changed files
with
423 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
20.8.1 | ||
20.10.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"term": { | ||
"date": "19970726" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.