Skip to content

Commit 9361c3e

Browse files
authored
feat(api7): get plugin metadata in one request (#151)
1 parent 9674289 commit 9361c3e

File tree

4 files changed

+112
-58
lines changed

4 files changed

+112
-58
lines changed

libs/backend-api7/src/fetcher.ts

+60-36
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import * as ADCSDK from '@api7/adc-sdk';
22
import { Axios } from 'axios';
33
import { ListrTask } from 'listr2';
44
import { isEmpty } from 'lodash';
5+
import { SemVer, gte as semVerGTE } from 'semver';
56

67
import { ToADC } from './transformer';
78
import * as typing from './typing';
89
import { buildReqAndRespDebugOutput } from './utils';
910

1011
type FetchTask = ListrTask<{
12+
api7Version: SemVer;
1113
gatewayGroupId: string;
1214
remote: ADCSDK.Configuration;
1315
}>;
@@ -133,42 +135,11 @@ export class Fetcher {
133135
title: 'Fetch plugin metadata',
134136
skip: this.isSkip([ADCSDK.ResourceType.PLUGIN_METADATA]),
135137
task: async (ctx, task) => {
136-
const resp = await this.client.get<Array<string>>(
137-
'/apisix/admin/plugins/list',
138-
{
139-
params: { has_metadata: true },
140-
},
141-
);
142-
task.output = buildReqAndRespDebugOutput(
143-
resp,
144-
'Get plugins that contain plugin metadata',
145-
);
146-
147-
const plugins = resp.data;
148-
const getMetadataConfig = plugins.map<
149-
Promise<[string, typing.PluginMetadata]>
150-
>(async (pluginName) => {
151-
try {
152-
const resp = await this.client.get<{
153-
value: typing.PluginMetadata;
154-
}>(`/apisix/admin/plugin_metadata/${pluginName}`, {
155-
params: { gateway_group_id: ctx.gatewayGroupId },
156-
});
157-
task.output = buildReqAndRespDebugOutput(
158-
resp,
159-
`Get plugin metadata for "${pluginName}"`,
160-
);
161-
return [pluginName, resp?.data?.value];
162-
} catch (err) {
163-
return [pluginName, null];
164-
}
165-
});
166-
const metadataObj = Object.fromEntries(
167-
(await Promise.all(getMetadataConfig)).filter((item) => item[1]),
168-
);
169-
170-
ctx.remote.plugin_metadata =
171-
this.toADC.transformPluginMetadatas(metadataObj);
138+
if (semVerGTE(ctx.api7Version, '3.2.14')) {
139+
return this.listMetadatasGTE03021400(ctx, task);
140+
} else {
141+
return this.listMetadatasLT03021400(ctx, task);
142+
}
172143
},
173144
};
174145
}
@@ -208,6 +179,59 @@ export class Fetcher {
208179
};
209180
}
210181

182+
// Get plugin metadata on API7 3.2.14.0 and up
183+
private listMetadatasGTE03021400: FetchTask['task'] = async (ctx, task) => {
184+
const resp = await this.client.get<{
185+
value: ADCSDK.Plugins;
186+
}>('/apisix/admin/plugin_metadata', {
187+
params: { gateway_group_id: ctx.gatewayGroupId },
188+
});
189+
task.output = buildReqAndRespDebugOutput(resp, 'Get plugin metadata');
190+
ctx.remote.plugin_metadata = this.toADC.transformPluginMetadatas(
191+
resp.data.value,
192+
);
193+
};
194+
195+
// Get plugin metadata below API7 3.2.14.0
196+
private listMetadatasLT03021400: FetchTask['task'] = async (ctx, task) => {
197+
const resp = await this.client.get<Array<string>>(
198+
'/apisix/admin/plugins/list',
199+
{
200+
params: { has_metadata: true },
201+
},
202+
);
203+
task.output = buildReqAndRespDebugOutput(
204+
resp,
205+
'Get plugins that contain plugin metadata',
206+
);
207+
208+
const plugins = resp.data;
209+
const getMetadataConfig = plugins.map<
210+
Promise<[string, typing.PluginMetadata]>
211+
>(async (pluginName) => {
212+
try {
213+
const resp = await this.client.get<{
214+
value: typing.PluginMetadata;
215+
}>(`/apisix/admin/plugin_metadata/${pluginName}`, {
216+
params: { gateway_group_id: ctx.gatewayGroupId },
217+
});
218+
task.output = buildReqAndRespDebugOutput(
219+
resp,
220+
`Get plugin metadata for "${pluginName}"`,
221+
);
222+
return [pluginName, resp?.data?.value];
223+
} catch (err) {
224+
return [pluginName, null];
225+
}
226+
});
227+
const metadataObj = Object.fromEntries(
228+
(await Promise.all(getMetadataConfig)).filter((item) => item[1]),
229+
);
230+
231+
ctx.remote.plugin_metadata =
232+
this.toADC.transformPluginMetadatas(metadataObj);
233+
};
234+
211235
private attachLabelSelector(
212236
params: Record<string, string>,
213237
): Record<string, string> {

libs/backend-api7/src/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Listr, ListrTask } from 'listr2';
55
import { isEmpty, isNil } from 'lodash';
66
import { readFileSync } from 'node:fs';
77
import { AgentOptions, Agent as httpsAgent } from 'node:https';
8+
import semver, { SemVer } from 'semver';
89

910
import { Fetcher } from './fetcher';
1011
import { OperateContext, Operator } from './operator';
@@ -17,6 +18,7 @@ export class BackendAPI7 implements ADCSDK.Backend {
1718
private readonly gatewayGroup: string;
1819
private static logScope = ['API7'];
1920

21+
private version: SemVer;
2022
private gatewayGroupId: string;
2123
private defaultValue: ADCSDK.DefaultValue;
2224

@@ -177,11 +179,30 @@ export class BackendAPI7 implements ADCSDK.Backend {
177179
};
178180
}
179181

182+
private getAPI7VersionTask(): ListrTask {
183+
return {
184+
enabled: (ctx) => !ctx.api7Version,
185+
task: async (ctx, task) => {
186+
if (this.version) {
187+
ctx.api7Version = this.version;
188+
return;
189+
}
190+
191+
const resp = await this.client.get<{ value: string }>('/api/version');
192+
task.output = buildReqAndRespDebugOutput(resp, `Get API7 version`);
193+
ctx.api7Version = this.version = semver.coerce(
194+
resp?.data.value || '0.0.0',
195+
);
196+
},
197+
};
198+
}
199+
180200
public async dump(): Promise<Listr<{ remote: ADCSDK.Configuration }>> {
181201
const fetcher = new Fetcher(this.client, this.opts);
182202

183203
return new Listr<{ remote: ADCSDK.Configuration }>(
184204
[
205+
this.getAPI7VersionTask(),
185206
this.getResourceDefaultValueTask(),
186207
this.getGatewayGroupIdTask(this.gatewayGroup),
187208
...fetcher.allTask(),
@@ -197,6 +218,7 @@ export class BackendAPI7 implements ADCSDK.Backend {
197218
const operator = new Operator(this.client, this.gatewayGroup);
198219
return new Listr<OperateContext>(
199220
[
221+
this.getAPI7VersionTask(),
200222
this.getGatewayGroupIdTask(this.gatewayGroup),
201223
this.syncPreprocessEventsTask(),
202224
{

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@types/node": "18.16.9",
2828
"@types/pluralize": "^0.0.33",
2929
"@types/qs": "^6.9.15",
30+
"@types/semver": "^7.5.8",
3031
"@types/signale": "^1.4.7",
3132
"@typescript-eslint/eslint-plugin": "^6.21.0",
3233
"@typescript-eslint/parser": "^6.21.0",
@@ -61,6 +62,7 @@
6162
"pluralize": "^8.0.0",
6263
"reflect-metadata": "^0.1.14",
6364
"rxjs": "^7.8.1",
65+
"semver": "^7.6.3",
6466
"signale": "^1.4.0",
6567
"slugify": "^1.6.6",
6668
"tslib": "^2.6.2",

0 commit comments

Comments
 (0)