Skip to content

Commit fed76ee

Browse files
authored
feat(api7): support route vars expr (#219)
1 parent 71c26e2 commit fed76ee

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

.github/workflows/e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
if: contains(github.event.pull_request.labels.*.name, 'test/api7') || github.event_name == 'push'
5454
strategy:
5555
matrix:
56-
version: [3.2.14.6, 3.2.15.2, 3.2.16.2]
56+
version: [3.2.14.6, 3.2.15.2, 3.2.16.7, 3.3.2]
5757
env:
5858
BACKEND_API7_VERSION: ${{ matrix.version }}
5959
BACKEND_API7_DOWNLOAD_URL: https://run.api7.ai/api7-ee/api7-ee-v${{ matrix.version }}.tar.gz

libs/backend-api7/e2e/resources/route.e2e-spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2+
import { gte } from 'semver';
23

34
import { BackendAPI7 } from '../../src';
45
import {
6+
conditionalDescribe,
57
createEvent,
68
deleteEvent,
79
dumpConfiguration,
10+
semverCondition,
811
syncEvents,
912
} from '../support/utils';
1013

@@ -72,4 +75,53 @@ describe('Route E2E', () => {
7275
expect(result.services).toHaveLength(0);
7376
});
7477
});
78+
79+
conditionalDescribe(semverCondition(gte, '3.2.16'))('Vars', () => {
80+
const serviceName = 'test';
81+
const service = {
82+
name: serviceName,
83+
upstream: {
84+
scheme: 'https',
85+
nodes: [
86+
{
87+
host: 'httpbin.org',
88+
port: 443,
89+
weight: 100,
90+
},
91+
],
92+
},
93+
path_prefix: '/test',
94+
strip_path_prefix: true,
95+
} as ADCSDK.Service;
96+
const route1Name = 'route1';
97+
const route1 = {
98+
name: route1Name,
99+
uris: ['/route1'],
100+
vars: [['remote_addr', '==', '1.1.1.1']],
101+
} as ADCSDK.Route;
102+
103+
it('Create resources', async () =>
104+
syncEvents(backend, [
105+
createEvent(ADCSDK.ResourceType.SERVICE, serviceName, service),
106+
createEvent(ADCSDK.ResourceType.ROUTE, route1Name, route1, serviceName),
107+
]));
108+
109+
it('Dump', async () => {
110+
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
111+
expect(result.services).toHaveLength(1);
112+
expect(result.services[0]).toMatchObject(service);
113+
expect(result.services[0].routes).toHaveLength(1);
114+
expect(result.services[0].routes[0]).toMatchObject(route1);
115+
});
116+
117+
it('Delete', async () =>
118+
syncEvents(backend, [
119+
deleteEvent(ADCSDK.ResourceType.SERVICE, serviceName),
120+
]));
121+
122+
it('Dump again (service should not exist)', async () => {
123+
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
124+
expect(result.services).toHaveLength(0);
125+
});
126+
});
75127
});

libs/backend-api7/src/operator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22
import { Axios, AxiosResponse } from 'axios';
33
import { ListrTask } from 'listr2';
4-
import { size } from 'lodash';
4+
import { size, unset } from 'lodash';
55
import { SemVer, gte as semVerGTE } from 'semver';
66

77
import { FromADC } from './transformer';
@@ -41,6 +41,8 @@ export class Operator {
4141
task.output = buildReqAndRespDebugOutput(resp);
4242
} else if (event.resourceType === ADCSDK.ResourceType.ROUTE) {
4343
// Create a route template instead of create route directly
44+
const route = this.fromADC(event);
45+
if (!semVerGTE(ctx.api7Version, '3.2.16')) unset(route, 'vars');
4446
resp = await this.client.put(
4547
`/api/routes/template/${event.resourceId}`,
4648
this.fromADC(event),

libs/backend-api7/src/transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class ToADC {
2727
plugins: route.plugins,
2828
priority: route.priority,
2929
timeout: route.timeout,
30+
vars: route.vars,
3031
});
3132
}
3233

@@ -148,6 +149,7 @@ export class FromADC {
148149
paths: route.uris,
149150
priority: route.priority,
150151
timeout: route.timeout,
152+
vars: route.vars,
151153
});
152154
}
153155

libs/backend-api7/src/typing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
PluginMetadata as ADCPluginMetadata,
33
Upstream as ADCUpstream,
4+
Expr,
45
Labels,
56
Plugins,
67
UpstreamBalancer,
@@ -25,6 +26,7 @@ export interface Route {
2526
// matcher
2627
paths: Array<string>;
2728
methods?: Array<string>;
29+
vars: Expr;
2830

2931
// misc
3032
enable_websocket?: boolean;

libs/sdk/src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export * from './resource';
44
export type Labels = Record<string, string | Array<string>>;
55
export type Plugin = Record<string, unknown>;
66
export type Plugins = Record<string, Plugin>;
7-
export type Expr = Array<string | Array<Expr>>;
7+
export type Expr = Array<unknown>;
88

99
export interface Route {
1010
id?: string;

0 commit comments

Comments
 (0)