Skip to content

Commit

Permalink
feat: add resource type filter (api7#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored May 15, 2024
1 parent 0ffe9c8 commit e136297
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 9 deletions.
29 changes: 27 additions & 2 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BackendCommand } from './helper';
import { BackendOptions } from './typing';
import {
fillLabels,
filterResourceType,
loadBackend,
mergeKVConfigurations,
toConfiguration,
Expand All @@ -32,6 +33,8 @@ export interface TaskContext {
export const LoadLocalConfigurationTask = (
files: Array<string>,
labelSelector?: BackendOptions['labelSelector'],
includeResourceType?: Array<ADCSDK.ResourceType>,
excludeResourceType?: Array<ADCSDK.ResourceType>,
): ListrTask<{
local: ADCSDK.Configuration;
}> => ({
Expand Down Expand Up @@ -84,6 +87,18 @@ export const LoadLocalConfigurationTask = (
ctx.local = toConfiguration(localKVConfiguration);
},
},
{
title: 'Filter configuration resource type',
enabled: () =>
includeResourceType?.length > 0 || excludeResourceType?.length > 0,
task: () => {
ctx.local = filterResourceType(
ctx.local,
includeResourceType,
excludeResourceType,
);
},
},
{
title: 'Filter configuration',
enabled: !!labelSelector && Object.keys(labelSelector).length > 0,
Expand Down Expand Up @@ -147,8 +162,18 @@ export const DiffCommand = new BackendCommand<DiffOptions>(

const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
[
LoadLocalConfigurationTask(opts.file, opts.labelSelector),
LoadRemoteConfigurationTask(backend, opts.labelSelector),
LoadLocalConfigurationTask(
opts.file,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
LoadRemoteConfigurationTask(
backend,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
DiffResourceTask(true, true),
{
title: 'Write detail diff result to file',
Expand Down
27 changes: 23 additions & 4 deletions apps/cli/src/command/dump.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@ import { SignaleRenderer } from '../utils/listr';
import { TaskContext } from './diff.command';
import { BackendCommand } from './helper';
import type { BackendOptions } from './typing';
import { filterConfiguration, loadBackend } from './utils';
import { filterConfiguration, filterResourceType, loadBackend } from './utils';

type DumpOptions = BackendOptions & {
output: string;
};

export const LoadRemoteConfigurationTask = (
backend: ADCSDK.Backend,
labelSelector: BackendOptions['labelSelector'],
labelSelector?: BackendOptions['labelSelector'],
includeResourceType?: Array<ADCSDK.ResourceType>,
excludeResourceType?: Array<ADCSDK.ResourceType>,
): ListrTask => ({
title: 'Load remote configuration',
task: async (ctx, task) => {
return task.newListr([
{
title: 'Fetch all configuration',
task: async (ctx, task) => await backend.dump(),
task: async () => await backend.dump(),
},
{
title: 'Filter configuration resource type',
enabled: () =>
includeResourceType?.length > 0 || excludeResourceType?.length > 0,
task: () => {
ctx.remote = filterResourceType(
ctx.remote,
includeResourceType,
excludeResourceType,
);
},
},
{
title: 'Filter remote configuration',
Expand Down Expand Up @@ -51,7 +65,12 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
const backend = loadBackend(opts.backend, opts);
const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
[
LoadRemoteConfigurationTask(backend, opts.labelSelector),
LoadRemoteConfigurationTask(
backend,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
{
title: 'Write to dump file',
task: async (ctx, task) => {
Expand Down
31 changes: 30 additions & 1 deletion apps/cli/src/command/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ADCSDK from '@api7/adc-sdk';
import chalk from 'chalk';
import { Command, Option } from 'commander';
import { Command, InvalidArgumentError, Option } from 'commander';
import { has } from 'lodash';
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
Expand Down Expand Up @@ -78,6 +79,18 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
return path;
};

const parseResourceTypeFilter = (
cv: ADCSDK.ResourceType,
pv: Array<string> = [],
) => {
const resourceTypes = Object.values(ADCSDK.ResourceType);
if (!resourceTypes.includes(cv))
throw new InvalidArgumentError(
`Allowed choices are ${resourceTypes.join(', ')}.`,
);
return pv.concat(cv);
};

this.addOption(
new Option('--backend <backend>', 'Type of backend to connect')
.env('ADC_BACKEND')
Expand Down Expand Up @@ -113,6 +126,22 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
Object.assign(previous, qs.parse(val, { delimiter: ',' })),
),
)
.addOption(
new Option(
'--include-resource-type <string>',
'Filter for resource types, contains only the specified type',
)
.conflicts('excludeResourceType')
.argParser(parseResourceTypeFilter),
)
.addOption(
new Option(
'--exclude-resource-type <string>',
'Filter for resource types, not contains only the specified type',
)
.conflicts('includeResourceType')
.argParser(parseResourceTypeFilter),
)
.addOption(
new Option(
'--timeout <duration>',
Expand Down
14 changes: 12 additions & 2 deletions apps/cli/src/command/sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ export const SyncCommand = new BackendCommand<SyncOption>(

const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
[
LoadLocalConfigurationTask(opts.file, opts.labelSelector),
LoadRemoteConfigurationTask(backend, opts.labelSelector),
LoadLocalConfigurationTask(
opts.file,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
LoadRemoteConfigurationTask(
backend,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
DiffResourceTask(false, false),
{
title: 'Sync configuration',
Expand Down
2 changes: 2 additions & 0 deletions apps/cli/src/command/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type BackendOptions = {
gatewayGroup: string;

labelSelector?: Record<string, string>;
includeResourceType?: Array<ADCSDK.ResourceType>;
excludeResourceType?: Array<ADCSDK.ResourceType>;
} & GlobalOptions;

export interface KVConfiguration {
Expand Down
16 changes: 16 additions & 0 deletions apps/cli/src/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ export const fillLabels = (
return configuration;
};

export const filterResourceType = (
config: ADCSDK.Configuration,
includes: Array<string>,
excludes: Array<string>,
) => {
const key = (item) =>
item !== ADCSDK.ResourceType.PLUGIN_METADATA ? item.slice(0, -1) : item;
return Object.fromEntries(
Object.entries(config).filter(([resourceType]) => {
return includes
? includes.includes(key(resourceType))
: !excludes.includes(key(resourceType));
}),
);
};

export const configurePluralize = () => {
pluralize.addIrregularRule('route', 'routes');
pluralize.addIrregularRule('service', 'services');
Expand Down

0 comments on commit e136297

Please # to comment.