Skip to content

Commit 8e91465

Browse files
authored
feat: Add internal options to engine create (#89)
1 parent b9153b9 commit 8e91465

File tree

2 files changed

+106
-15
lines changed

2 files changed

+106
-15
lines changed

src/service/engine/index.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class EngineService {
2222
auto_stop: "AUTO_STOP"
2323
};
2424

25+
private INTERNAL_OPTIONS: Record<string, string> = {
26+
FB_INTERNAL_OPTIONS_ENGINE_VERSION: "VERSION"
27+
};
28+
2529
constructor(context: ResourceManagerContext) {
2630
this.context = context;
2731
}
@@ -128,6 +132,18 @@ export class EngineService {
128132
}
129133
}
130134

135+
private getInternalOptions() {
136+
const internalOptions: Record<string, string> = {};
137+
for (const [env, optionName] of Object.entries(this.INTERNAL_OPTIONS)) {
138+
const optionValue = process.env[env];
139+
if (optionValue) {
140+
internalOptions[optionName] = optionValue;
141+
console.log(`Setting internal option ${optionName} to ${optionValue}`);
142+
}
143+
}
144+
return internalOptions;
145+
}
146+
131147
async create(
132148
name: string,
133149
options: CreateEngineOptions = {}
@@ -149,20 +165,34 @@ export class EngineService {
149165
? this.CREATE_PARAMETER_NAMES_V2
150166
: this.CREATE_PARAMETER_NAMES;
151167

152-
if (Object.values(createOptions).some(v => v !== undefined)) {
168+
const internalOptions = this.getInternalOptions();
169+
170+
// Exlude options that are not set or not allowed for the account version
171+
const filteredCreateOptions = Object.fromEntries(
172+
Object.entries(createOptions).filter(
173+
([key, value]) => value !== undefined && key in createParameterNames
174+
)
175+
);
176+
177+
if (Object.keys(filteredCreateOptions).length > 0 || internalOptions) {
153178
query += " WITH ";
154-
for (const [key, value] of Object.entries(createOptions)) {
155-
if (key in createParameterNames) {
156-
if (key == "spec" && accountVersion >= 2) {
157-
// spec value is provided raw without quotes for accounts v2
158-
query += `${createParameterNames[key]} = ${value} `;
159-
} else {
160-
query += `${createParameterNames[key]} = ?`;
161-
queryParameters.push(value);
162-
}
163-
}
179+
}
180+
181+
for (const [key, value] of Object.entries(filteredCreateOptions)) {
182+
if (key == "spec" && accountVersion >= 2) {
183+
// spec value is provided raw without quotes for accounts v2
184+
query += `${createParameterNames[key]} = ${value} `;
185+
} else {
186+
query += `${createParameterNames[key]} = ? `;
187+
queryParameters.push(value);
164188
}
165189
}
190+
191+
for (const [key, value] of Object.entries(internalOptions)) {
192+
query += `${key} = ? `;
193+
queryParameters.push(value);
194+
}
195+
166196
await this.context.connection.execute(query, {
167197
parameters: queryParameters
168198
});

test/unit/v2/engine.test.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Firebolt, QueryFormatter } from "../../../src";
66
import { ResourceManager } from "../../../src/service";
77
import { ConnectionError, DeprecationError } from "../../../src/common/errors";
88
import { NodeHttpClient } from "../../../src/http/node";
9-
import { Authenticator} from "../../../src/auth";
9+
import { Authenticator } from "../../../src/auth";
1010

1111
const apiEndpoint = "api.fake.firebolt.io";
1212

@@ -225,7 +225,7 @@ describe("engine service", () => {
225225
client_id: "id",
226226
client_secret: "secret"
227227
}
228-
}
228+
};
229229
const firebolt = Firebolt({ apiEndpoint });
230230
const connection = await firebolt.connect(connectionOptions);
231231
// Also test diffrent way of instantiating a resource manager
@@ -367,9 +367,70 @@ describe("engine service", () => {
367367
try {
368368
await engine.delete();
369369
expect(false).toBeTruthy();
370-
}
371-
catch (e) {
370+
} catch (e) {
372371
expect(true).toBeTruthy();
373372
}
374373
});
374+
it("create engine with options", async () => {
375+
server.use(
376+
rest.post(
377+
`https://some_system_engine.com/${QUERY_URL}`,
378+
async (req, res, ctx) => {
379+
const requestBody = await req.text();
380+
if (requestBody.includes("CREATE ENGINE")) {
381+
expect(requestBody).toContain(`ENGINE_TYPE = 'GENERAL_PURPOSE'`);
382+
expect(requestBody).not.toContain(`REGION`);
383+
}
384+
return res(ctx.json(selectEngineResponse));
385+
}
386+
)
387+
);
388+
const firebolt = Firebolt({ apiEndpoint });
389+
await firebolt.connect({
390+
account: "my_account",
391+
auth: {
392+
client_id: "id",
393+
client_secret: "secret"
394+
}
395+
});
396+
const resourceManager = firebolt.resourceManager;
397+
const engine = await resourceManager.engine.create("some_engine", {
398+
region: undefined,
399+
engine_type: "GENERAL_PURPOSE"
400+
});
401+
expect(engine).toBeTruthy();
402+
expect(engine.endpoint).toEqual("https://some_engine.com");
403+
});
404+
it("create engine with environment variable", async () => {
405+
const engine_version = "20.1.1";
406+
server.use(
407+
rest.post(
408+
`https://some_system_engine.com/${QUERY_URL}`,
409+
async (req, res, ctx) => {
410+
const requestBody = await req.text();
411+
if (requestBody.includes("CREATE ENGINE")) {
412+
expect(requestBody).toContain(`VERSION = '${engine_version}'`);
413+
}
414+
return res(ctx.json(selectEngineResponse));
415+
}
416+
)
417+
);
418+
const firebolt = Firebolt({ apiEndpoint });
419+
await firebolt.connect({
420+
account: "my_account",
421+
auth: {
422+
client_id: "id",
423+
client_secret: "secret"
424+
}
425+
});
426+
const resourceManager = firebolt.resourceManager;
427+
428+
process.env.FB_INTERNAL_OPTIONS_ENGINE_VERSION = engine_version;
429+
430+
const engine = await resourceManager.engine.create("some_engine");
431+
expect(engine).toBeTruthy();
432+
expect(engine.endpoint).toEqual("https://some_engine.com");
433+
434+
delete process.env.ENGINE_NAME;
435+
});
375436
});

0 commit comments

Comments
 (0)