Skip to content

Commit 03e350b

Browse files
authored
CNI: Support defining on-prem connections inline (#389)
1 parent 9c8de02 commit 03e350b

File tree

4 files changed

+165
-4
lines changed

4 files changed

+165
-4
lines changed

packages/spectral/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@prismatic-io/spectral",
3-
"version": "10.13.0",
3+
"version": "10.14.0",
44
"description": "Utility library for building Prismatic connectors and code-native integrations",
55
"keywords": ["prismatic"],
66
"main": "dist/index.js",

packages/spectral/src/serverTypes/convertIntegration.test.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "@jest/globals";
22
import { configPage, configVar, flow } from "..";
3-
import { convertConfigPages, convertFlow } from "./convertIntegration";
3+
import { convertConfigPages, convertFlow, convertConfigVar } from "./convertIntegration";
4+
import type { ConfigVar } from "../types";
45

56
describe("convertFlow with polling triggers", () => {
67
const baseFlowInput = {
@@ -146,3 +147,132 @@ describe("convertConfigPages", () => {
146147
expect(result[0].elements).toHaveLength(0);
147148
});
148149
});
150+
151+
describe("convertConfigVar", () => {
152+
const referenceKey = "test-component";
153+
const componentRegistry = {};
154+
155+
describe("onPremConnectionConfig validation", () => {
156+
it("should return onPremiseConnectionConfig when connection has onPremControlled inputs and config set", () => {
157+
const connectionConfigVar = {
158+
stableKey: "test-connection",
159+
dataType: "connection",
160+
onPremConnectionConfig: "allowed",
161+
inputs: {
162+
host: {
163+
label: "Host",
164+
type: "string",
165+
onPremControlled: true,
166+
},
167+
port: {
168+
label: "Port",
169+
type: "string",
170+
onPremControlled: true,
171+
},
172+
},
173+
} as ConfigVar;
174+
175+
const result = convertConfigVar(
176+
"TestConnection",
177+
connectionConfigVar,
178+
referenceKey,
179+
componentRegistry,
180+
);
181+
expect("onPremiseConnectionConfig" in result && result.onPremiseConnectionConfig).toBe(
182+
"allowed",
183+
);
184+
});
185+
186+
it("should throw when connection has onPremControlled inputs but no onPremConnectionConfig", () => {
187+
const connectionConfigVar = {
188+
stableKey: "test-connection",
189+
dataType: "connection",
190+
inputs: {
191+
host: {
192+
label: "Host",
193+
type: "string",
194+
onPremControlled: true,
195+
},
196+
port: {
197+
label: "Port",
198+
type: "string",
199+
onPremControlled: true,
200+
},
201+
},
202+
} as ConfigVar;
203+
204+
expect(() =>
205+
convertConfigVar("TestConnection", connectionConfigVar, referenceKey, componentRegistry),
206+
).toThrow(
207+
"Connection test-connection has onPremControlled inputs but no onPremConnectionConfig value set",
208+
);
209+
});
210+
211+
it("should default to 'disallowed' when connection has no onPremControlled inputs and no config", () => {
212+
const connectionConfigVar = {
213+
stableKey: "test-connection",
214+
dataType: "connection",
215+
inputs: {
216+
apiKey: {
217+
label: "API Key",
218+
type: "string",
219+
},
220+
},
221+
} as ConfigVar;
222+
223+
const result = convertConfigVar(
224+
"TestConnection",
225+
connectionConfigVar,
226+
referenceKey,
227+
componentRegistry,
228+
);
229+
expect("onPremiseConnectionConfig" in result && result.onPremiseConnectionConfig).toBe(
230+
"disallowed",
231+
);
232+
});
233+
234+
it("should allow 'disallowed' config when connection has no onPremControlled inputs", () => {
235+
const connectionConfigVar = {
236+
stableKey: "test-connection",
237+
dataType: "connection",
238+
onPremConnectionConfig: "disallowed",
239+
inputs: {
240+
apiKey: {
241+
label: "API Key",
242+
type: "string",
243+
},
244+
},
245+
} as ConfigVar;
246+
247+
const result = convertConfigVar(
248+
"TestConnection",
249+
connectionConfigVar,
250+
referenceKey,
251+
componentRegistry,
252+
);
253+
expect("onPremiseConnectionConfig" in result && result.onPremiseConnectionConfig).toBe(
254+
"disallowed",
255+
);
256+
});
257+
258+
it("should throw when connection has defined config but no onPremControlled inputs", () => {
259+
const connectionConfigVar = {
260+
stableKey: "test-connection",
261+
dataType: "connection",
262+
onPremConnectionConfig: "allowed",
263+
inputs: {
264+
apiKey: {
265+
label: "API Key",
266+
type: "string",
267+
},
268+
},
269+
} as ConfigVar;
270+
271+
expect(() =>
272+
convertConfigVar("TestConnection", connectionConfigVar, referenceKey, componentRegistry),
273+
).toThrow(
274+
"Connection test-connection has onPremConnectionConfig set but no onPremControlled inputs",
275+
);
276+
});
277+
});
278+
});

packages/spectral/src/serverTypes/convertIntegration.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
PollingTriggerType,
4040
StandardTriggerType,
4141
TriggerPerformFunction,
42+
OnPremiseConnectionConfigTypeEnum,
4243
} from "../types";
4344
import {
4445
Component as ServerComponent,
@@ -751,6 +752,34 @@ export const convertInputValue = (value: unknown, collectionType: CollectionType
751752
}));
752753
};
753754

755+
const validateOnPremConnectionConfig = (
756+
connection: ConfigVar,
757+
): OnPremiseConnectionConfigTypeEnum => {
758+
if (isConnectionDefinitionConfigVar(connection)) {
759+
const hasOnPremControlledInputs = Object.values(connection.inputs).some((value) => {
760+
return "onPremControlled" in value && value.onPremControlled;
761+
});
762+
763+
const { onPremConnectionConfig: config } = connection;
764+
765+
if (hasOnPremControlledInputs && !config) {
766+
throw new Error(
767+
`Connection ${connection.stableKey} has onPremControlled inputs but no onPremConnectionConfig value set. Please set an onPremConnectionConfig value for the connection.`,
768+
);
769+
}
770+
771+
if (!hasOnPremControlledInputs && config && config !== "disallowed") {
772+
throw new Error(
773+
`Connection ${connection.stableKey} has onPremConnectionConfig set but no onPremControlled inputs. The connection will not be valid without onPremControlled inputs (host, port).`,
774+
);
775+
}
776+
777+
return hasOnPremControlledInputs && config ? config : "disallowed";
778+
}
779+
780+
return "disallowed";
781+
};
782+
754783
/** Converts a Config Var into the structure necessary for YAML generation. */
755784
export const convertConfigVar = (
756785
key: string,
@@ -781,6 +810,7 @@ export const convertConfigVar = (
781810
description,
782811
key,
783812
dataType: "connection",
813+
onPremiseConnectionConfig: validateOnPremConnectionConfig(configVar),
784814
connection: {
785815
key: camelCase(key),
786816
component: codeNativeIntegrationComponentReference(referenceKey),

packages/spectral/src/types/ConfigVars.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,15 @@ type BaseConnectionConfigVar = BaseConfigVar & {
296296
dataType: "connection";
297297
};
298298

299+
export type OnPremiseConnectionConfigTypeEnum = "allowed" | "disallowed" | "required";
300+
299301
type ConnectionDefinitionConfigVar =
300302
ConnectionDefinition extends infer TConnectionDefinitionType extends ConnectionDefinition
301303
? TConnectionDefinitionType extends infer TConnectionDefinition extends ConnectionDefinition
302304
? BaseConnectionConfigVar &
303305
Omit<TConnectionDefinition, "inputs" | "display" | "key"> & {
304306
icons?: TConnectionDefinition["display"]["icons"];
307+
onPremConnectionConfig?: OnPremiseConnectionConfigTypeEnum;
305308
inputs: {
306309
[Key in keyof TConnectionDefinition["inputs"]]: TConnectionDefinition["inputs"][Key] &
307310
ConfigVarInputVisibility;
@@ -310,8 +313,6 @@ type ConnectionDefinitionConfigVar =
310313
: never
311314
: never;
312315

313-
type OnPremiseConnectionConfigTypeEnum = "allowed" | "disallowed" | "required";
314-
315316
type ConnectionReferenceConfigVar = ComponentRegistryConnection extends infer TConnectionReference
316317
? TConnectionReference extends ComponentRegistryConnection
317318
? BaseConnectionConfigVar & {

0 commit comments

Comments
 (0)