Skip to content

Commit 01eae10

Browse files
committed
f
Signed-off-by: Jarvis <jarvis@api7.ai>
1 parent f07f059 commit 01eae10

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

apps/cli/src/tasks/validate.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ export const ValidateTask = (): ListrTask<{
2929
}
3030
for (const e of result.errors) {
3131
const parts: string[] = [e.resource_type];
32-
if (e.resource_id) parts.push(`id="${e.resource_id}"`);
33-
if (e.index !== undefined) parts.push(`index=${e.index}`);
32+
if (e.resource_name) {
33+
parts.push(`name="${e.resource_name}"`);
34+
} else {
35+
if (e.resource_id) parts.push(`id="${e.resource_id}"`);
36+
if (e.index !== undefined) parts.push(`index=${e.index}`);
37+
}
3438
lines.push(` - [${parts.join(', ')}]: ${e.error}`);
3539
}
3640
const error = new Error(

libs/backend-api7/src/validator.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Validator extends ADCSDK.backend.BackendEventSource {
3535
public async validate(
3636
config: ADCSDK.Configuration,
3737
): Promise<ADCSDK.BackendValidateResult> {
38-
const body = this.buildRequestBody(config);
38+
const { body, nameIndex } = this.buildRequestBody(config);
3939

4040
try {
4141
const resp = await this.client.post(
@@ -58,35 +58,50 @@ export class Validator extends ADCSDK.backend.BackendEventSource {
5858
},
5959
});
6060
const data = error.response.data;
61+
const errors: ADCSDK.BackendValidationError[] = (data?.errors ?? []).map(
62+
(e: ADCSDK.BackendValidationError) => {
63+
const name = nameIndex[e.resource_type]?.[e.index];
64+
return name ? { ...e, resource_name: name } : e;
65+
},
66+
);
6167
return {
6268
success: false,
6369
errorMessage: data?.error_msg,
64-
errors: data?.errors ?? [],
70+
errors,
6571
};
6672
}
6773
throw error;
6874
}
6975
}
7076

71-
private buildRequestBody(config: ADCSDK.Configuration): ValidateRequestBody {
77+
private buildRequestBody(config: ADCSDK.Configuration): {
78+
body: ValidateRequestBody;
79+
nameIndex: Record<string, string[]>;
80+
} {
7281
const body: ValidateRequestBody = {};
82+
const nameIndex: Record<string, string[]> = {};
7383

7484
if (config.services?.length) {
7585
const services: Array<typing.Service> = [];
7686
const routes: Array<typing.Route> = [];
7787
const streamRoutes: Array<typing.StreamRoute> = [];
88+
const serviceNames: string[] = [];
89+
const routeNames: string[] = [];
90+
const streamRouteNames: string[] = [];
7891

7992
for (const service of config.services) {
8093
const serviceId =
8194
service.id ?? ADCSDK.utils.generateId(service.name);
8295
const svc = { ...service, id: serviceId };
8396
const transformed = this.fromADC.transformService(svc);
8497
services.push(transformed);
98+
serviceNames.push(service.name);
8599

86100
for (const route of service.routes ?? []) {
87101
const routeId = route.id ?? ADCSDK.utils.generateId(route.name);
88102
const r = { ...route, id: routeId };
89103
routes.push(this.fromADC.transformRoute(r, serviceId));
104+
routeNames.push(route.name);
90105
}
91106

92107
for (const streamRoute of service.stream_routes ?? []) {
@@ -96,31 +111,42 @@ export class Validator extends ADCSDK.backend.BackendEventSource {
96111
streamRoutes.push(
97112
this.fromADC.transformStreamRoute(sr, serviceId),
98113
);
114+
streamRouteNames.push(streamRoute.name);
99115
}
100116
}
101117

102118
body.services = services;
103-
if (routes.length) body.routes = routes;
104-
if (streamRoutes.length) body.stream_routes = streamRoutes;
119+
nameIndex.services = serviceNames;
120+
if (routes.length) {
121+
body.routes = routes;
122+
nameIndex.routes = routeNames;
123+
}
124+
if (streamRoutes.length) {
125+
body.stream_routes = streamRoutes;
126+
nameIndex.stream_routes = streamRouteNames;
127+
}
105128
}
106129

107130
if (config.consumers?.length) {
108131
body.consumers = config.consumers.map((c) =>
109132
this.fromADC.transformConsumer(c),
110133
);
134+
nameIndex.consumers = config.consumers.map((c) => c.username);
111135
}
112136

113137
if (config.ssls?.length) {
114138
body.ssls = config.ssls.map((ssl) => {
115139
const sslId = ssl.id ?? ADCSDK.utils.generateId(ssl.snis?.[0] ?? '');
116140
return this.fromADC.transformSSL({ ...ssl, id: sslId });
117141
});
142+
nameIndex.ssls = config.ssls.map((ssl) => ssl.snis?.[0] ?? '');
118143
}
119144

120145
if (config.global_rules && Object.keys(config.global_rules).length) {
121146
body.global_rules = this.fromADC.transformGlobalRule(
122147
config.global_rules as Record<string, ADCSDK.GlobalRule>,
123148
);
149+
nameIndex.global_rules = Object.keys(config.global_rules);
124150
}
125151

126152
if (
@@ -133,6 +159,7 @@ export class Validator extends ADCSDK.backend.BackendEventSource {
133159
...ADCSDK.utils.recursiveOmitUndefined(config),
134160
}),
135161
);
162+
nameIndex.plugin_metadata = Object.keys(config.plugin_metadata);
136163
}
137164

138165
if (config.consumer_groups?.length) {
@@ -146,8 +173,9 @@ export class Validator extends ADCSDK.backend.BackendEventSource {
146173
plugins: cg.plugins,
147174
}) as unknown as Record<string, unknown>;
148175
});
176+
nameIndex.consumer_groups = config.consumer_groups.map((cg) => cg.name);
149177
}
150178

151-
return body;
179+
return { body, nameIndex };
152180
}
153181
}

libs/sdk/src/backend/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface BackendSyncResult {
7070
export interface BackendValidationError {
7171
resource_type: string;
7272
resource_id?: string;
73+
resource_name?: string;
7374
index: number;
7475
error: string;
7576
}

0 commit comments

Comments
 (0)