From 9d0c3410a5218c212f5e68bbb9e505c33e68533b Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 29 Dec 2020 11:11:26 +0300 Subject: [PATCH] feat(context-edit): restricted editing system compute contexts --- src/ContextManager.ts | 25 ++++++++++++++++++++----- src/SASViyaApiClient.ts | 11 +++++++++-- src/SASjs.ts | 15 ++++++++++++--- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/ContextManager.ts b/src/ContextManager.ts index c6b6753..cf0b603 100644 --- a/src/ContextManager.ts +++ b/src/ContextManager.ts @@ -9,6 +9,17 @@ import { SASViyaApiClient } from './SASViyaApiClient' import { prefixMessage } from '@sasjs/utils/error' export class ContextManager { + public defaultComputeContexts = [ + 'CAS Formats service compute context', + 'SAS Model Manager compute context', + 'SAS Studio compute context', + 'SAS Visual Forecasting compute context', + 'Data Mining compute context', + 'SAS Job Execution compute context' + ] + + private csrfToken: CsrfToken | null = null + constructor( private serverUrl: string, private setCsrfToken: (csrfToken: CsrfToken) => void @@ -16,8 +27,6 @@ export class ContextManager { if (serverUrl) isUrl(serverUrl) // ? } - private csrfToken: CsrfToken | null = null - public async getComputeContexts(accessToken?: string) { const headers: any = { 'Content-Type': 'application/json' @@ -206,9 +215,7 @@ export class ContextManager { return context } - // TODO: Check if trying to edit one of default SAS contexts, reject with the error if so - // TODO: rename to editComputeContext - public async editContext( + public async editComputeContext( contextName: string, editedContext: EditContextInput, accessToken?: string @@ -217,6 +224,14 @@ export class ContextManager { throw new Error('Invalid context name.') } + if (this.defaultComputeContexts.includes(contextName)) { + throw new Error( + `Editing default SAS compute contexts is not allowed.\nDefault contexts:${this.defaultComputeContexts.map( + (context, i) => `\n${i + 1}. ${context}` + )}` + ) + } + const headers: any = { 'Content-Type': 'application/json' } diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 82ae332..23fa8cc 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -104,6 +104,13 @@ export class SASViyaApiClient { return await this.contextManager.getComputeContexts(accessToken) } + /** + * Returns default(system) compute contexts. + */ + public getDefaultComputeContexts() { + return this.contextManager.defaultComputeContexts + } + /** * Returns all available launcher contexts on this server. * @param accessToken - an access token for an authorized user. @@ -220,12 +227,12 @@ export class SASViyaApiClient { * @param editedContext - an object with the properties to be updated. * @param accessToken - an access token for an authorized user. */ - public async editContext( + public async editComputeContext( contextName: string, editedContext: EditContextInput, accessToken?: string ) { - return await this.contextManager.editContext( + return await this.contextManager.editComputeContext( contextName, editedContext, accessToken diff --git a/src/SASjs.ts b/src/SASjs.ts index 7c8318f..94408fe 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -116,6 +116,15 @@ export default class SASjs { return await this.sasViyaApiClient!.getLauncherContexts(accessToken) } + /** + * Gets default(system) launcher contexts. + */ + public getDefaultComputeContexts() { + this.isMethodSupported('getDefaultComputeContexts', ServerType.SASViya) + + return this.sasViyaApiClient!.getDefaultComputeContexts() + } + /** * Gets executable compute contexts. * @param accessToken - an access token for an authorized user. @@ -184,14 +193,14 @@ export default class SASjs { * @param editedContext - an object with the properties to be updated. * @param accessToken - an access token for an authorized user. */ - public async editContext( + public async editComputeContext( contextName: string, editedContext: EditContextInput, accessToken?: string ) { - this.isMethodSupported('editContext', ServerType.SASViya) + this.isMethodSupported('editComputeContext', ServerType.SASViya) - return await this.sasViyaApiClient!.editContext( + return await this.sasViyaApiClient!.editComputeContext( contextName, editedContext, accessToken