mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 08:30:07 +00:00
refactor(contextManager): used helper methods
This commit is contained in:
@@ -11,11 +11,22 @@ import { prefixMessage } from '@sasjs/utils/error'
|
|||||||
export class ContextManager {
|
export class ContextManager {
|
||||||
public defaultComputeContexts = [
|
public defaultComputeContexts = [
|
||||||
'CAS Formats service compute context',
|
'CAS Formats service compute context',
|
||||||
|
'Data Mining compute context',
|
||||||
|
'Import 9 service compute context',
|
||||||
|
'SAS Job Execution compute context',
|
||||||
'SAS Model Manager compute context',
|
'SAS Model Manager compute context',
|
||||||
'SAS Studio compute context',
|
'SAS Studio compute context',
|
||||||
'SAS Visual Forecasting compute context',
|
'SAS Visual Forecasting compute context'
|
||||||
'Data Mining compute context',
|
]
|
||||||
'SAS Job Execution compute context'
|
public defaultLauncherContexts = [
|
||||||
|
'CAS Formats service launcher context',
|
||||||
|
'Data Mining launcher context',
|
||||||
|
'Import 9 service launcher context',
|
||||||
|
'Job Flow Execution launcher context',
|
||||||
|
'SAS Job Execution launcher context',
|
||||||
|
'SAS Model Manager launcher context',
|
||||||
|
'SAS Studio launcher context',
|
||||||
|
'SAS Visual Forecasting launcher context'
|
||||||
]
|
]
|
||||||
|
|
||||||
private csrfToken: CsrfToken | null = null
|
private csrfToken: CsrfToken | null = null
|
||||||
@@ -24,7 +35,7 @@ export class ContextManager {
|
|||||||
private serverUrl: string,
|
private serverUrl: string,
|
||||||
private setCsrfToken: (csrfToken: CsrfToken) => void
|
private setCsrfToken: (csrfToken: CsrfToken) => void
|
||||||
) {
|
) {
|
||||||
if (serverUrl) isUrl(serverUrl) // ?
|
if (serverUrl) isUrl(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getComputeContexts(accessToken?: string) {
|
public async getComputeContexts(accessToken?: string) {
|
||||||
@@ -89,9 +100,13 @@ export class ContextManager {
|
|||||||
accessToken?: string,
|
accessToken?: string,
|
||||||
authorizedUsers?: string[]
|
authorizedUsers?: string[]
|
||||||
) {
|
) {
|
||||||
if (!contextName) {
|
this.validateContextName(contextName)
|
||||||
throw new Error('Context name is required.')
|
|
||||||
}
|
this.isDefaultContext(
|
||||||
|
contextName,
|
||||||
|
this.defaultComputeContexts,
|
||||||
|
`Compute context '${contextName}' already exists.`
|
||||||
|
)
|
||||||
|
|
||||||
const existingComputeContexts = await this.getComputeContexts(accessToken)
|
const existingComputeContexts = await this.getComputeContexts(accessToken)
|
||||||
|
|
||||||
@@ -102,27 +117,31 @@ export class ContextManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (launchContextName) {
|
if (launchContextName) {
|
||||||
const launcherContexts = await this.getLauncherContexts(accessToken)
|
if (!this.defaultLauncherContexts.includes(launchContextName)) {
|
||||||
|
const launcherContexts = await this.getLauncherContexts(accessToken)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!launcherContexts.find((context) => context.name === launchContextName)
|
!launcherContexts.find(
|
||||||
) {
|
(context) => context.name === launchContextName
|
||||||
const description = `The launcher context for ${launchContextName}`
|
)
|
||||||
const launchType = 'direct'
|
) {
|
||||||
|
const description = `The launcher context for ${launchContextName}`
|
||||||
|
const launchType = 'direct'
|
||||||
|
|
||||||
const newLauncherContext = await this.createLauncherContext(
|
const newLauncherContext = await this.createLauncherContext(
|
||||||
launchContextName,
|
launchContextName,
|
||||||
description,
|
description,
|
||||||
launchType,
|
launchType,
|
||||||
accessToken
|
accessToken
|
||||||
).catch((err) => {
|
).catch((err) => {
|
||||||
throw new Error(`Error while creating launcher context. ${err}`)
|
throw new Error(`Error while creating launcher context. ${err}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (newLauncherContext && newLauncherContext.name) {
|
if (newLauncherContext && newLauncherContext.name) {
|
||||||
launchContextName = newLauncherContext.name
|
launchContextName = newLauncherContext.name
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Error while creating launcher context.')
|
throw new Error('Error while creating launcher context.')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,7 +193,6 @@ export class ContextManager {
|
|||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check if context already exist, reject with the error if so
|
|
||||||
public async createLauncherContext(
|
public async createLauncherContext(
|
||||||
contextName: string,
|
contextName: string,
|
||||||
description: string,
|
description: string,
|
||||||
@@ -185,6 +203,20 @@ export class ContextManager {
|
|||||||
throw new Error('Context name is required.')
|
throw new Error('Context name is required.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.isDefaultContext(
|
||||||
|
contextName,
|
||||||
|
this.defaultLauncherContexts,
|
||||||
|
`Launcher context '${contextName}' already exists.`
|
||||||
|
)
|
||||||
|
|
||||||
|
const existingLauncherContexts = await this.getLauncherContexts(accessToken)
|
||||||
|
|
||||||
|
if (
|
||||||
|
existingLauncherContexts.find((context) => context.name === contextName)
|
||||||
|
) {
|
||||||
|
throw new Error(`Launcher context '${contextName}' already exists.`)
|
||||||
|
}
|
||||||
|
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
@@ -220,17 +252,14 @@ export class ContextManager {
|
|||||||
editedContext: EditContextInput,
|
editedContext: EditContextInput,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
if (!contextName) {
|
this.validateContextName(contextName)
|
||||||
throw new Error('Invalid context name.')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.defaultComputeContexts.includes(contextName)) {
|
this.isDefaultContext(
|
||||||
throw new Error(
|
contextName,
|
||||||
`Editing default SAS compute contexts is not allowed.\nDefault contexts:${this.defaultComputeContexts.map(
|
this.defaultComputeContexts,
|
||||||
(context, i) => `\n${i + 1}. ${context}`
|
'Editing default SAS compute contexts is not allowed.',
|
||||||
)}`
|
true
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -357,7 +386,7 @@ export class ContextManager {
|
|||||||
`${this.serverUrl}/compute/contexts?limit=10000`,
|
`${this.serverUrl}/compute/contexts?limit=10000`,
|
||||||
{ headers }
|
{ headers }
|
||||||
).catch((err) => {
|
).catch((err) => {
|
||||||
throw err // fixme
|
throw prefixMessage(err, 'Error while fetching compute contexts.')
|
||||||
})
|
})
|
||||||
|
|
||||||
const contextsList = contexts.items || []
|
const contextsList = contexts.items || []
|
||||||
@@ -415,20 +444,15 @@ export class ContextManager {
|
|||||||
return executableContexts
|
return executableContexts
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check if trying to delete one of default SAS contexts, reject with the error if so
|
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
||||||
// TODO: rename to deleteComputeContext
|
this.validateContextName(contextName)
|
||||||
public async deleteContext(contextName: string, accessToken?: string) {
|
|
||||||
if (!contextName) {
|
|
||||||
throw new Error('Invalid context name.')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.defaultComputeContexts.includes(contextName)) {
|
this.isDefaultContext(
|
||||||
throw new Error(
|
contextName,
|
||||||
`Deleting default SAS compute contexts is not allowed.\nDefault contexts:${this.defaultComputeContexts.map(
|
this.defaultComputeContexts,
|
||||||
(context, i) => `\n${i + 1}. ${context}`
|
'Deleting default SAS compute contexts is not allowed.',
|
||||||
)}`
|
true
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -482,4 +506,26 @@ export class ContextManager {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private validateContextName(name: string) {
|
||||||
|
if (!name) throw new Error('Context name is required.')
|
||||||
|
}
|
||||||
|
|
||||||
|
public isDefaultContext(
|
||||||
|
context: string,
|
||||||
|
defaultContexts: string[] = this.defaultComputeContexts,
|
||||||
|
errorMessage = '',
|
||||||
|
listDefaults = false
|
||||||
|
) {
|
||||||
|
if (defaultContexts.includes(context)) {
|
||||||
|
throw new Error(
|
||||||
|
`${errorMessage}${
|
||||||
|
listDefaults
|
||||||
|
? '\nDefault contexts:' +
|
||||||
|
defaultContexts.map((context, i) => `\n${i + 1}. ${context}`)
|
||||||
|
: ''
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user