From e3edace882821db8cfcc3376f5e3ad7ab7b54e66 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Sun, 6 Sep 2020 21:01:04 +0100 Subject: [PATCH] feat(create-context): add the ability to create a compute context --- src/SASViyaApiClient.ts | 52 +++++++++++++++++++++++++++++++++++++++++ src/SASjs.ts | 7 ++++++ 2 files changed, 59 insertions(+) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index b560076..4b2e62a 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -189,6 +189,58 @@ export class SASViyaApiClient { return createdSession } + /** + * Creates a compute context on the given server. + * @param contextName - the name of the context to create a session on. + * @param sharedAccountId - the ID of the account to run the servers for this context as. + * @param autoExecLines - the lines of code to execute during session initialization. + * @param accessToken - an access token for an authorized user. + */ + public async createContext( + contextName: string, + sharedAccountId: string, + autoExecLines: string, + accessToken?: string + ) { + if (!contextName) { + throw new Error('Missing context name.') + } + + if (!sharedAccountId) { + throw new Error('Missing shared account ID.') + } + + const headers: any = { + 'Content-Type': 'application/json' + } + + if (accessToken) { + headers.Authorization = `Bearer ${accessToken}` + } + + const createContextRequest: RequestInit = { + method: 'POST', + headers, + body: JSON.stringify({ + name: contextName, + environment: { + autoExecLines: autoExecLines || '' + }, + attributes: { + reuseServerProcesses: true, + runServerAs: sharedAccountId + } + }) + } + + const { result: context } = await this.request( + `${this.serverUrl}/compute/contexts`, + createContextRequest + ) + + return context + } + /** * Executes code on the current SAS Viya server. * @param fileName - a name for the file being submitted for execution. diff --git a/src/SASjs.ts b/src/SASjs.ts index 02eecc6..82d56d7 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -107,6 +107,13 @@ export default class SASjs { return await this.sasViyaApiClient!.getExecutableContexts(accessToken) } + public async createContext(contextName: string, sharedAccountId: string, autoExecLines: string, accessToken: string) { + if (this.sasjsConfig.serverType !== ServerType.SASViya) { + throw new Error('This operation is only supported on SAS Viya servers.') + } + return await this.sasViyaApiClient!.createContext(contextName, sharedAccountId, autoExecLines, accessToken) + } + public async createSession(contextName: string, accessToken: string) { if (this.sasjsConfig.serverType !== ServerType.SASViya) { throw new Error('This operation is only supported on SAS Viya servers.')