Skip to content

Commit

Permalink
fix: name extractor return undefined (api7#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Mar 1, 2024
1 parent 9329579 commit 11ac5fc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 45 deletions.
22 changes: 11 additions & 11 deletions apps/cli-e2e/src/support/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/* eslint-disable */
import _ from "lodash"
import axios from 'axios';
import { stringify, parse } from "qs";
import _ from 'lodash';
import { parse, stringify } from 'qs';

module.exports = async function () {
// Configure axios for tests to use.
// const host = process.env.HOST ?? 'localhost';
// const port = process.env.PORT ?? '3000';
axios.defaults.paramsSerializer = {
encode: parse,
serialize: stringify
}
encode: (params) => parse(params),
serialize: (params) => stringify(params),
};
axios.defaults.baseURL = process.env.API7EE_SERVER_URL;
axios.interceptors.response.use(
(response) => response,
(error) => {
console.log('error: ', error);

if (!error?.request) return
if (!error?.request) return;

const picked = _.pick(error.request, [
'outputData',
Expand All @@ -39,12 +39,12 @@ module.exports = async function () {
'config',
'_header',
'_keepAliveTimeout',
])
error.request = picked
]);
error.request = picked;
if (error.response?.request) {
error.response.request = picked
error.response.request = picked;
}
return error
return error;
},
)
);
};
21 changes: 3 additions & 18 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,15 @@ export const DiffResourceTask = (
ctx.diff.forEach((event) => {
switch (event.type) {
case ADCSDK.EventType.CREATE:
task.output += `create ${
event.resourceType
}: "${ADCSDK.getResourceName(
event.resourceType,
event.newValue,
)}"\n`;
task.output += `create ${event.resourceType}: "${event.resourceName}"\n`;
created++;
break;
case ADCSDK.EventType.DELETE:
task.output += `delete ${
event.resourceType
}: "${ADCSDK.getResourceName(
event.resourceType,
event.oldValue,
)}"\n`;
task.output += `delete ${event.resourceType}: "${event.resourceName}"\n`;
deleted++;
break;
case ADCSDK.EventType.UPDATE:
task.output += `update ${
event.resourceType
}: "${ADCSDK.getResourceName(
event.resourceType,
event.newValue,
)}"\n`;
task.output += `update ${event.resourceType}: "${event.resourceName}"\n`;
updated++;
break;
}
Expand Down
34 changes: 34 additions & 0 deletions apps/cli/src/differ/differv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ export class DifferV2 {
}
}

/**
* Check resource changes for
* - route(name)
* - service(name)
* - upstream(name)
* - ssl(snis.join(','))
* - plugin_configs(name)
* - consumers(username)
* - consumer_groups(name)
* - stream_routes(name)
* @param resourceType
* @param local
* @param remote
* @returns
*/
private diffResource(
resourceType: ADCSDK.EventResourceType,
local: Record<string, ADCSDK.Event['newValue']> = {},
Expand All @@ -200,11 +215,23 @@ export class DifferV2 {

// temporarily stores checked remote resource ids for identifying new resources to added
const checkedId: Array<string> = [];
const nameExtractor = (
resource: ADCSDK.Event['newValue'],
fallback?: string,
) =>
resourceType === ADCSDK.EventResourceType.SSL
? (resource as ADCSDK.SSL).snis.join(',')
: resourceType === ADCSDK.EventResourceType.CONSUMER
? (resource as ADCSDK.Consumer).username
: (resource as { name: string }).name ?? fallback; //TODO optimize typing and fallbacks that should not exist (value source should be unique)

// check each remote resources
remote.forEach((remoteItem) => {
const localId = DifferV2.getADCResourceId(resourceType, remoteItem);
const localItem = local[localId];
const resourceName = !localItem
? nameExtractor(remoteItem)
: nameExtractor(localItem);

// the local resource does not contain a remote resource,
// which means that it should be deleted: send delete event
Expand All @@ -215,6 +242,7 @@ export class DifferV2 {
resourceType,
type: ADCSDK.EventType.DELETE,
resourceId: localId,
resourceName,
oldValue: remoteItem,
});
}
Expand Down Expand Up @@ -255,6 +283,7 @@ export class DifferV2 {
resourceType,
type: ADCSDK.EventType.UPDATE,
resourceId: localId,
resourceName,
oldValue: remoteItem,
newValue: localItem, // use local configuration to avoid turn back the remote old configuration to server-side
diff: detailedDiff(mergedItem, remoteItem),
Expand All @@ -273,6 +302,7 @@ export class DifferV2 {
return result.push({
resourceType,
resourceId: localId,
resourceName: nameExtractor(local[localId], localId),
type: ADCSDK.EventType.CREATE,
newValue: local[localId],
});
Expand Down Expand Up @@ -309,6 +339,7 @@ export class DifferV2 {
resourceType,
type: ADCSDK.EventType.DELETE,
resourceId: localId,
resourceName: remoteItem.name,
oldValue: remoteItem,
subEvents: new DifferV2()
.diff(
Expand Down Expand Up @@ -378,6 +409,7 @@ export class DifferV2 {
? ADCSDK.EventType.UPDATE
: ADCSDK.EventType.ONLY_SUB_EVENTS,
resourceId: localId,
resourceName: localServiceWithoutRoutes.name,
oldValue: remoteServiceWithoutRoutes,
newValue: localServiceWithoutRoutes,
diff: detailedDiff(
Expand Down Expand Up @@ -430,6 +462,7 @@ export class DifferV2 {
? ADCSDK.EventType.UPDATE
: ADCSDK.EventType.ONLY_SUB_EVENTS,
resourceId: localId,
resourceName: localConsumerGroupWithoutConsumers.name,
oldValue: remoteConsumerGroupWithoutConsumers,
newValue: localConsumerGroupWithoutConsumers,
diff: detailedDiff(
Expand All @@ -455,6 +488,7 @@ export class DifferV2 {
return result.push({
resourceType,
resourceId: localId,
resourceName: local[localId].name,
type: ADCSDK.EventType.CREATE,
newValue: local[localId],
subEvents: new DifferV2()
Expand Down
23 changes: 12 additions & 11 deletions libs/sdk/src/core/differ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,61 +40,62 @@ export interface EventDiff {
export type Event = {
type: EventType;
resourceId: string;
resourceName: string;
diff?: EventDiff;

// for nested events
parentId?: string;
subEvents?: Array<Event>;
} & (
| {
| {
resourceType: typeof EventResourceType.ROUTE;
oldValue?: Route;
newValue?: Route;
}
| {
| {
resourceType: EventResourceType.SERVICE;
oldValue?: Service;
newValue?: Service;
}
| {
| {
resourceType: EventResourceType.UPSTREAM;
oldValue?: Upstream;
newValue?: Upstream;
}
| {
| {
resourceType: EventResourceType.SSL;
oldValue?: SSL;
newValue?: SSL;
}
| {
| {
resourceType: EventResourceType.GLOBAL_RULE;
oldValue?: GlobalRule;
newValue?: GlobalRule;
}
| {
| {
resourceType: EventResourceType.PLUGIN_CONFIG;
oldValue?: PluginConfig;
newValue?: PluginConfig;
}
| {
| {
resourceType: EventResourceType.PLUGIN_METADATA;
oldValue?: PluginMetadata;
newValue?: PluginMetadata;
pluginName: string;
}
| {
| {
resourceType: EventResourceType.CONSUMER;
oldValue?: Consumer;
newValue?: Consumer;
}
| {
| {
resourceType: EventResourceType.CONSUMER_GROUP;
oldValue?: ConsumerGroup;
newValue?: ConsumerGroup;
}
| {
| {
resourceType: EventResourceType.STREAM_ROUTE;
oldValue?: StreamRoute;
newValue?: StreamRoute;
}
);
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@readme/openapi-parser": "^2.5.0",
"@types/qs": "^6.9.12",
"axios": "^1.6.5",
"chalk": "^4",
"commander": "^11.1.0",
Expand Down
12 changes: 7 additions & 5 deletions pnpm-lock.yaml

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

0 comments on commit 11ac5fc

Please sign in to comment.