diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 990c2bc..73c9c5c 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -8,7 +8,14 @@ import { } from './utils' import * as NodeFormData from 'form-data' import * as path from 'path' -import { Job, Session, Context, Folder, CsrfToken } from './types' +import { + Job, + Session, + Context, + Folder, + CsrfToken, + EditContextInput +} from './types' import { JobDefinition } from './types/JobDefinition' import { formatDataForRequest } from './utils/formatDataForRequest' import { SessionManager } from './SessionManager' @@ -267,6 +274,53 @@ export class SASViyaApiClient { return context } + /** + * Updates a compute context on the given server. + * @param contextId - the ID of the context to be deleted. + * @param editedContext - an object with the properties to be updated. + * @param accessToken - an access token for an authorized user. + */ + public async editContext( + contextId: string, + updatedContext: EditContextInput, + accessToken?: string + ) { + if (!contextId) { + throw new Error('Invalid context ID.') + } + + const headers: any = { + 'Content-Type': 'application/json' + } + + if (accessToken) { + headers.Authorization = `Bearer ${accessToken}` + } + + const { result: context, etag } = await this.request( + `${this.serverUrl}/compute/contexts/${contextId}`, + { + headers + } + ) + + // An If-Match header with the value of the last ETag for the context + // is required to be able to update it + // https://developer.sas.com/apis/rest/Compute/#update-a-context-definition + headers['If-Match'] = etag + + const updateContextRequest: RequestInit = { + method: 'PUT', + headers, + body: JSON.stringify({ ...context, ...updatedContext }) + } + + return await this.request( + `${this.serverUrl}/compute/contexts/${contextId}`, + updateContextRequest + ) + } + /** * Deletes a compute context on the given server. * @param contextId - the ID of the context to be deleted. diff --git a/src/SASjs.ts b/src/SASjs.ts index 4b3bbcd..578ff96 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -29,7 +29,8 @@ import { SASjsWaitingRequest, ServerType, CsrfToken, - UploadFile + UploadFile, + EditContextInput } from './types' import { SASViyaApiClient } from './SASViyaApiClient' import { SAS9ApiClient } from './SAS9ApiClient' @@ -137,6 +138,27 @@ export default class SASjs { ) } + /** + * Updates a compute context on the given server. + * @param contextId - the ID of the context to be deleted. + * @param editedContext - an object with the properties to be updated. + * @param accessToken - an access token for an authorized user. + */ + public async editContext( + contextId: string, + editedContext: EditContextInput, + accessToken?: string + ) { + if (this.sasjsConfig.serverType !== ServerType.SASViya) { + throw new Error('This operation is only supported on SAS Viya servers.') + } + return await this.sasViyaApiClient!.editContext( + contextId, + editedContext, + accessToken + ) + } + /** * Deletes a compute context on the given server. * @param contextId - the ID of the context to be deleted. diff --git a/src/types/Context.ts b/src/types/Context.ts index 41be8d7..baa1da0 100644 --- a/src/types/Context.ts +++ b/src/types/Context.ts @@ -4,3 +4,12 @@ export interface Context { createdBy: string version: number } + +export interface EditContextInput { + name?: string + description?: string + launchContext?: { name: string } + environment?: { options?: string[]; autoExecLines?: string[] } + authorizedUsers?: string[] + authorizeAllAuthenticatedUsers?: boolean +}