mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 11:40:06 +00:00
Merge pull request #66 from sasjs/create-context
feat(context): add the ability to create and delete a compute context
This commit is contained in:
@@ -193,6 +193,107 @@ export class SASViyaApiClient {
|
|||||||
return createdSession
|
return createdSession
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a compute context on the given server.
|
||||||
|
* @param contextName - the name of the context to be created.
|
||||||
|
* @param launchContextName - the name of the launcher context used by the compute service.
|
||||||
|
* @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 authorizedUsers - an optional list of authorized user IDs.
|
||||||
|
* @param accessToken - an access token for an authorized user.
|
||||||
|
*/
|
||||||
|
public async createContext(
|
||||||
|
contextName: string,
|
||||||
|
launchContextName: string,
|
||||||
|
sharedAccountId: string,
|
||||||
|
autoExecLines: string[],
|
||||||
|
authorizedUsers: string[],
|
||||||
|
accessToken?: string
|
||||||
|
) {
|
||||||
|
if (!contextName) {
|
||||||
|
throw new Error('Missing context name.')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!launchContextName) {
|
||||||
|
throw new Error('Missing launch 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 requestBody: any = {
|
||||||
|
name: contextName,
|
||||||
|
launchContext: {
|
||||||
|
contextName: launchContextName
|
||||||
|
},
|
||||||
|
attributes: {
|
||||||
|
reuseServerProcesses: true,
|
||||||
|
runServerAs: sharedAccountId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authorizedUsers && authorizedUsers.length) {
|
||||||
|
requestBody['authorizedUsers'] = authorizedUsers
|
||||||
|
} else {
|
||||||
|
requestBody['authorizeAllAuthenticatedUsers'] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (autoExecLines) {
|
||||||
|
requestBody.environment = { autoExecLines }
|
||||||
|
}
|
||||||
|
|
||||||
|
const createContextRequest: RequestInit = {
|
||||||
|
method: 'POST',
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify(requestBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
const { result: context } = await this.request<Context>(
|
||||||
|
`${this.serverUrl}/compute/contexts`,
|
||||||
|
createContextRequest
|
||||||
|
)
|
||||||
|
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a compute context on the given server.
|
||||||
|
* @param contextId - the ID of the context to be deleted.
|
||||||
|
* @param accessToken - an access token for an authorized user.
|
||||||
|
*/
|
||||||
|
public async deleteContext(contextId: string, accessToken?: string) {
|
||||||
|
if (!contextId) {
|
||||||
|
throw new Error('Invalid context ID.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const headers: any = {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accessToken) {
|
||||||
|
headers.Authorization = `Bearer ${accessToken}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteContextRequest: RequestInit = {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.request<Context>(
|
||||||
|
`${this.serverUrl}/compute/contexts/${contextId}`,
|
||||||
|
deleteContextRequest
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes code on the current SAS Viya server.
|
* Executes code on the current SAS Viya server.
|
||||||
* @param fileName - a name for the file being submitted for execution.
|
* @param fileName - a name for the file being submitted for execution.
|
||||||
|
|||||||
42
src/SASjs.ts
42
src/SASjs.ts
@@ -107,6 +107,48 @@ export default class SASjs {
|
|||||||
return await this.sasViyaApiClient!.getExecutableContexts(accessToken)
|
return await this.sasViyaApiClient!.getExecutableContexts(accessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a compute context on the given server.
|
||||||
|
* @param contextName - the name of the context to be created.
|
||||||
|
* @param launchContextName - the name of the launcher context used by the compute service.
|
||||||
|
* @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 authorizedUsers - an optional list of authorized user IDs.
|
||||||
|
* @param accessToken - an access token for an authorized user.
|
||||||
|
*/
|
||||||
|
public async createContext(
|
||||||
|
contextName: string,
|
||||||
|
launchContextName: string,
|
||||||
|
sharedAccountId: string,
|
||||||
|
autoExecLines: string[],
|
||||||
|
authorizedUsers: 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,
|
||||||
|
launchContextName,
|
||||||
|
sharedAccountId,
|
||||||
|
autoExecLines,
|
||||||
|
authorizedUsers,
|
||||||
|
accessToken
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a compute context on the given server.
|
||||||
|
* @param contextId - the ID of the context to be deleted.
|
||||||
|
* @param accessToken - an access token for an authorized user.
|
||||||
|
*/
|
||||||
|
public async deleteContext(contextId: 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!.deleteContext(contextId, accessToken)
|
||||||
|
}
|
||||||
|
|
||||||
public async createSession(contextName: string, accessToken: string) {
|
public async createSession(contextName: string, accessToken: string) {
|
||||||
if (this.sasjsConfig.serverType !== ServerType.SASViya) {
|
if (this.sasjsConfig.serverType !== ServerType.SASViya) {
|
||||||
throw new Error('This operation is only supported on SAS Viya servers.')
|
throw new Error('This operation is only supported on SAS Viya servers.')
|
||||||
|
|||||||
Reference in New Issue
Block a user