1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

Compare commits

...

60 Commits

Author SHA1 Message Date
Krishna Acondy
85a530ae5a Merge pull request #231 from sasjs/insecure-connection
feat: enable insecure connection for accessToken
2021-01-29 12:10:41 +00:00
Saad Jutt
1fc6db114d chore: docs updated 2021-01-27 15:57:21 +05:00
Saad Jutt
8d203b8df4 chore: annotation added 2021-01-27 15:47:15 +05:00
Saad Jutt
39924ff078 chore: added 'https' 2021-01-26 18:45:21 +05:00
Saad Jutt
de25f106ec feat: enable insecure connection for accessToken 2021-01-26 17:17:46 +05:00
Yury Shkoda
c0b82c5125 Merge pull request #211 from sasjs/deps-fix
chore(deps): rolled back isomorphic-fetch
2021-01-13 15:38:27 +03:00
Yury Shkoda
1c1b5baefe chore(deps): rolled back isomorphic-fetch 2021-01-13 15:22:07 +03:00
Yury Shkoda
8b17aeaea2 Merge pull request #121 from sasjs/dependabot/npm_and_yarn/isomorphic-fetch-3.0.0
chore(deps): bump isomorphic-fetch from 2.2.1 to 3.0.0
2021-01-13 12:36:28 +03:00
Yury Shkoda
cb0d03c965 Merge branch 'master' into dependabot/npm_and_yarn/isomorphic-fetch-3.0.0 2021-01-13 12:34:40 +03:00
Yury Shkoda
9e77f3d64e Merge pull request #191 from sasjs/dependabot/npm_and_yarn/typedoc-0.19.2
chore(deps-dev): bump typedoc from 0.17.8 to 0.19.2
2021-01-13 12:34:03 +03:00
Yury Shkoda
25f61815dc Merge branch 'master' into dependabot/npm_and_yarn/typedoc-0.19.2 2021-01-13 12:30:51 +03:00
dependabot-preview[bot]
3a2252e69c chore(deps-dev): bump typedoc from 0.17.8 to 0.19.2
Bumps [typedoc](https://github.com/TypeStrong/TypeDoc) from 0.17.8 to 0.19.2.
- [Release notes](https://github.com/TypeStrong/TypeDoc/releases)
- [Commits](https://github.com/TypeStrong/TypeDoc/compare/0.17.8...v0.19.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:30:42 +00:00
dependabot-preview[bot]
8a08980e6a chore(deps): bump isomorphic-fetch from 2.2.1 to 3.0.0
Bumps [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) from 2.2.1 to 3.0.0.
- [Release notes](https://github.com/matthew-andrews/isomorphic-fetch/releases)
- [Commits](https://github.com/matthew-andrews/isomorphic-fetch/compare/v2.2.1...v3.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:30:40 +00:00
Yury Shkoda
d0f31771ad Merge pull request #194 from sasjs/dependabot/npm_and_yarn/semantic-release-17.3.1
chore(deps-dev): bump semantic-release from 17.3.0 to 17.3.1
2021-01-13 12:30:22 +03:00
Yury Shkoda
e9e2c9372d Merge branch 'master' into dependabot/npm_and_yarn/semantic-release-17.3.1 2021-01-13 12:28:50 +03:00
Yury Shkoda
70c4a095a0 Merge pull request #199 from sasjs/dependabot/npm_and_yarn/webpack-cli-4.3.1
chore(deps-dev): bump webpack-cli from 4.2.0 to 4.3.1
2021-01-13 12:28:29 +03:00
Yury Shkoda
82e2fc4445 Merge branch 'master' into dependabot/npm_and_yarn/webpack-cli-4.3.1 2021-01-13 12:26:22 +03:00
dependabot-preview[bot]
6661d81fdf chore(deps-dev): bump semantic-release from 17.3.0 to 17.3.1
Bumps [semantic-release](https://github.com/semantic-release/semantic-release) from 17.3.0 to 17.3.1.
- [Release notes](https://github.com/semantic-release/semantic-release/releases)
- [Commits](https://github.com/semantic-release/semantic-release/compare/v17.3.0...v17.3.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:26:20 +00:00
dependabot-preview[bot]
e76abaafa8 chore(deps-dev): bump webpack-cli from 4.2.0 to 4.3.1
Bumps [webpack-cli](https://github.com/webpack/webpack-cli) from 4.2.0 to 4.3.1.
- [Release notes](https://github.com/webpack/webpack-cli/releases)
- [Changelog](https://github.com/webpack/webpack-cli/blob/master/CHANGELOG.md)
- [Commits](https://github.com/webpack/webpack-cli/compare/webpack-cli@4.2.0...webpack-cli@4.3.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:26:13 +00:00
Yury Shkoda
fbfc1c05d6 Merge pull request #200 from sasjs/dependabot/npm_and_yarn/typedoc-plugin-external-module-name-4.0.6
chore(deps-dev): bump typedoc-plugin-external-module-name from 4.0.5 to 4.0.6
2021-01-13 12:25:47 +03:00
Yury Shkoda
839c211c64 Merge branch 'master' into dependabot/npm_and_yarn/typedoc-plugin-external-module-name-4.0.6 2021-01-13 12:24:19 +03:00
Yury Shkoda
f3ff82143a Merge pull request #204 from sasjs/dependabot/npm_and_yarn/ts-loader-8.0.14
chore(deps-dev): bump ts-loader from 8.0.12 to 8.0.14
2021-01-13 12:24:02 +03:00
dependabot-preview[bot]
0dd0abae87 chore(deps-dev): bump typedoc-plugin-external-module-name
Bumps [typedoc-plugin-external-module-name](https://github.com/christopherthielen/typedoc-plugin-external-module-name) from 4.0.5 to 4.0.6.
- [Release notes](https://github.com/christopherthielen/typedoc-plugin-external-module-name/releases)
- [Changelog](https://github.com/christopherthielen/typedoc-plugin-external-module-name/blob/master/CHANGELOG.md)
- [Commits](https://github.com/christopherthielen/typedoc-plugin-external-module-name/compare/4.0.5...4.0.6)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:23:15 +00:00
Yury Shkoda
13781c993e Merge branch 'master' into dependabot/npm_and_yarn/ts-loader-8.0.14 2021-01-13 12:21:25 +03:00
Yury Shkoda
7616cacbec Merge pull request #205 from sasjs/dependabot/npm_and_yarn/types/jest-26.0.20
chore(deps-dev): bump @types/jest from 26.0.19 to 26.0.20
2021-01-13 12:21:01 +03:00
dependabot-preview[bot]
cab7d3c012 chore(deps-dev): bump ts-loader from 8.0.12 to 8.0.14
Bumps [ts-loader](https://github.com/TypeStrong/ts-loader) from 8.0.12 to 8.0.14.
- [Release notes](https://github.com/TypeStrong/ts-loader/releases)
- [Changelog](https://github.com/TypeStrong/ts-loader/blob/master/CHANGELOG.md)
- [Commits](https://github.com/TypeStrong/ts-loader/compare/v8.0.12...v8.0.14)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:20:57 +00:00
Yury Shkoda
dfce676fdf Merge branch 'master' into dependabot/npm_and_yarn/types/jest-26.0.20 2021-01-13 12:18:53 +03:00
dependabot-preview[bot]
1890cab623 chore(deps-dev): bump @types/jest from 26.0.19 to 26.0.20
Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 26.0.19 to 26.0.20.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 09:18:44 +00:00
Yury Shkoda
4307d8fe43 Merge pull request #207 from sasjs/dependabot/npm_and_yarn/webpack-5.13.0
chore(deps-dev): bump webpack from 4.44.2 to 5.13.0
2021-01-13 12:18:17 +03:00
Yury Shkoda
8df6fdbee6 Merge branch 'master' into dependabot/npm_and_yarn/webpack-5.13.0 2021-01-13 12:16:49 +03:00
Yury Shkoda
ac5c2a3088 Merge pull request #210 from sasjs/dependabot/npm_and_yarn/sasjs/utils-2.0.2
chore(deps): bump @sasjs/utils from 1.5.0 to 2.0.2
2021-01-13 12:16:18 +03:00
dependabot-preview[bot]
0212b677ae chore(deps): bump @sasjs/utils from 1.5.0 to 2.0.2
Bumps [@sasjs/utils](https://github.com/sasjs/utils) from 1.5.0 to 2.0.2.
- [Release notes](https://github.com/sasjs/utils/releases)
- [Commits](https://github.com/sasjs/utils/compare/v1.5.0...v2.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-13 08:03:31 +00:00
Yury Shkoda
1a0d62d8f3 Merge pull request #209 from sasjs/job-status-fix
feat(*): improved session and job logging
2021-01-12 17:53:25 +03:00
Yury Shkoda
8f4d1c7aea chore(*): improved session and job state logging 2021-01-12 17:26:57 +03:00
dependabot-preview[bot]
2a4735c6f2 chore(deps-dev): bump webpack from 4.44.2 to 5.13.0
Bumps [webpack](https://github.com/webpack/webpack) from 4.44.2 to 5.13.0.
- [Release notes](https://github.com/webpack/webpack/releases)
- [Commits](https://github.com/webpack/webpack/compare/v4.44.2...v5.13.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-01-12 08:21:08 +00:00
Yury Shkoda
5a2ee88cbc fix(job-status): print job status only if it changes 2021-01-11 13:52:38 +03:00
Allan Bowe
b23f199334 Merge pull request #203 from sasjs/relative-service-paths
fix(relative-paths): process relative and absolute paths the same way
2021-01-05 21:02:45 +01:00
Krishna Acondy
ed5dabee9f fix(relative-paths): process relative and absolute paths the same way 2021-01-05 19:37:40 +00:00
Allan Bowe
0c88c5a522 Merge pull request #201 from sasjs/services-subfolder
fix(deploy-service-pack): deploy services into 'services' subfolder
2021-01-05 18:23:52 +01:00
Krishna Acondy
640e7015c8 fix(*): deploy services into 'services' subfolder 2021-01-05 17:19:05 +00:00
Yury Shkoda
2fd306f435 Merge pull request #195 from sasjs/v2-bump
test(coverage): enabled gathering coverage
2020-12-30 15:31:01 +03:00
Yury Shkoda
e3f779dbd1 test(coverage): enabled gathering coverage
BREAKING CHANGE: SASjs Adapter 2.0 - Electric Boogaloo
2020-12-30 15:04:59 +03:00
Yury Shkoda
1064f11663 Merge pull request #177 from sasjs/cli-issue-182
feat(context): moved context related logic to ContextManager
2020-12-30 13:24:34 +03:00
Yury Shkoda
46abc54cb0 chore(default-contexts): made default contexts private 2020-12-30 13:13:28 +03:00
Yury Shkoda
2c808a937a docs(*): updated docs 2020-12-30 11:43:10 +03:00
Yury Shkoda
52cf9a420f style(*): fixed styling 2020-12-30 11:42:31 +03:00
Yury Shkoda
2d29be45f5 test(contextManager): added unit tests 2020-12-30 11:38:56 +03:00
Yury Shkoda
a44222c3ba refactor(contextManager): used helper methods 2020-12-30 11:38:10 +03:00
Yury Shkoda
efc82101c1 chore(context-delete): renamed method 2020-12-30 11:36:10 +03:00
Yury Shkoda
09ce2fb6be chore(context-delete): renamed method 2020-12-30 11:35:41 +03:00
Yury Shkoda
a383388e54 feat(context-delete): restricted system compute context deletion 2020-12-29 11:42:14 +03:00
Yury Shkoda
362078b12c docs(context): updated docs 2020-12-29 11:12:33 +03:00
Yury Shkoda
9d0c3410a5 feat(context-edit): restricted editing system compute contexts 2020-12-29 11:11:26 +03:00
Yury Shkoda
dfb9c28f3a feat(createComputeContext): added throw an error if context already exists 2020-12-29 10:00:49 +03:00
Yury Shkoda
8d155283dd fix(context): fixed executeScript method 2020-12-24 13:54:19 +03:00
Yury Shkoda
d991ead86a Merge branch 'master' into cli-issue-182 2020-12-23 15:11:19 +03:00
Yury Shkoda
6b3a0cdb13 wip(context): created ContextManager 2020-12-21 14:51:01 +03:00
Yury Shkoda
8c98a26160 chore(SessionManager): removed unnecessary comment 2020-12-11 08:50:44 +03:00
Yury Shkoda
bcd9310f26 feat(context): add public method createLauncherContext 2020-12-09 16:51:07 +03:00
Yury Shkoda
57e9b67207 feat(context): added create launcher context method 2020-12-09 16:41:29 +03:00
53 changed files with 11844 additions and 2633 deletions

4
.gitignore vendored
View File

@@ -1,4 +1,6 @@
node_modules
build
.env
.env
/coverage

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2473
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,7 @@
"publish:lib": "npm run build && cd build && npm publish",
"lint:fix": "npx prettier --write 'src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}'",
"lint": "npx prettier --check 'src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}'",
"test": "jest",
"test": "jest --coverage",
"prepublishOnly": "cp -r ./build/* . && rm -rf ./build",
"postpublish": "git clean -fd",
"semantic-release": "semantic-release",
@@ -37,30 +37,31 @@
"license": "ISC",
"devDependencies": {
"@types/isomorphic-fetch": "0.0.35",
"@types/jest": "^26.0.15",
"@types/jest": "^26.0.20",
"cp": "^0.2.0",
"dotenv": "^8.2.0",
"jest": "^25.5.4",
"path": "^0.12.7",
"rimraf": "^3.0.2",
"semantic-release": "^17.3.0",
"semantic-release": "^17.3.1",
"terser-webpack-plugin": "^4.2.3",
"ts-jest": "^25.5.1",
"ts-loader": "^8.0.11",
"ts-loader": "^8.0.14",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typedoc": "^0.17.8",
"typedoc": "^0.19.2",
"typedoc-neo-theme": "^1.0.10",
"typedoc-plugin-external-module-name": "^4.0.3",
"typedoc-plugin-external-module-name": "^4.0.6",
"typescript": "^3.9.7",
"webpack": "^4.44.2",
"webpack-cli": "^4.2.0"
"webpack": "^5.13.0",
"webpack-cli": "^4.3.1"
},
"main": "index.js",
"dependencies": {
"@sasjs/utils": "^1.5.0",
"@sasjs/utils": "^2.0.2",
"es6-promise": "^4.2.8",
"form-data": "^3.0.0",
"https": "^1.0.0",
"isomorphic-fetch": "^2.2.1"
}
}

538
src/ContextManager.ts Normal file
View File

@@ -0,0 +1,538 @@
import {
Context,
CsrfToken,
EditContextInput,
ContextAllAttributes
} from './types'
import { makeRequest, isUrl } from './utils'
import { SASViyaApiClient } from './SASViyaApiClient'
import { prefixMessage } from '@sasjs/utils/error'
export class ContextManager {
private defaultComputeContexts = [
'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 Studio compute context',
'SAS Visual Forecasting compute context'
]
private 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
get getDefaultComputeContexts() {
return this.defaultComputeContexts
}
get getDefaultLauncherContexts() {
return this.defaultLauncherContexts
}
constructor(
private serverUrl: string,
private setCsrfToken: (csrfToken: CsrfToken) => void
) {
if (serverUrl) isUrl(serverUrl)
}
public async getComputeContexts(accessToken?: string) {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?limit=10000`,
{ headers }
).catch((err) => {
throw prefixMessage(err, 'Error while getting compute contexts. ')
})
const contextsList = contexts && contexts.items ? contexts.items : []
return contextsList.map((context: any) => ({
createdBy: context.createdBy,
id: context.id,
name: context.name,
version: context.version,
attributes: {}
}))
}
public async getLauncherContexts(accessToken?: string) {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/launcher/contexts?limit=10000`,
{ headers }
).catch((err) => {
throw prefixMessage(err, 'Error while getting launcher contexts. ')
})
const contextsList = contexts && contexts.items ? contexts.items : []
return contextsList.map((context: any) => ({
createdBy: context.createdBy,
id: context.id,
name: context.name,
version: context.version,
attributes: {}
}))
}
public async createComputeContext(
contextName: string,
launchContextName: string,
sharedAccountId: string,
autoExecLines: string[],
accessToken?: string,
authorizedUsers?: string[]
) {
this.validateContextName(contextName)
this.isDefaultContext(
contextName,
this.defaultComputeContexts,
`Compute context '${contextName}' already exists.`
)
const existingComputeContexts = await this.getComputeContexts(accessToken)
if (
existingComputeContexts.find((context) => context.name === contextName)
) {
throw new Error(`Compute context '${contextName}' already exists.`)
}
if (launchContextName) {
if (!this.defaultLauncherContexts.includes(launchContextName)) {
const launcherContexts = await this.getLauncherContexts(accessToken)
if (
!launcherContexts.find(
(context) => context.name === launchContextName
)
) {
const description = `The launcher context for ${launchContextName}`
const launchType = 'direct'
const newLauncherContext = await this.createLauncherContext(
launchContextName,
description,
launchType,
accessToken
).catch((err) => {
throw new Error(`Error while creating launcher context. ${err}`)
})
if (newLauncherContext && newLauncherContext.name) {
launchContextName = newLauncherContext.name
} else {
throw new Error('Error while creating launcher context.')
}
}
}
}
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
let attributes = { reuseServerProcesses: true } as object
if (sharedAccountId)
attributes = { ...attributes, runServerAs: sharedAccountId }
const requestBody: any = {
name: contextName,
launchContext: {
contextName: launchContextName || ''
},
attributes
}
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
).catch((err) => {
throw prefixMessage(err, 'Error while creating compute context. ')
})
return context
}
public async createLauncherContext(
contextName: string,
description: string,
launchType = 'direct',
accessToken?: string
) {
if (!contextName) {
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 = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const requestBody: any = {
name: contextName,
description: description,
launchType
}
const createContextRequest: RequestInit = {
method: 'POST',
headers,
body: JSON.stringify(requestBody)
}
const { result: context } = await this.request<Context>(
`${this.serverUrl}/launcher/contexts`,
createContextRequest
).catch((err) => {
throw prefixMessage(err, 'Error while creating launcher context. ')
})
return context
}
public async editComputeContext(
contextName: string,
editedContext: EditContextInput,
accessToken?: string
) {
this.validateContextName(contextName)
this.isDefaultContext(
contextName,
this.defaultComputeContexts,
'Editing default SAS compute contexts is not allowed.',
true
)
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
let originalContext
originalContext = await this.getComputeContextByName(
contextName,
accessToken
)
// Try to find context by id, when context name has been changed.
if (!originalContext) {
originalContext = await this.getComputeContextById(
editedContext.id!,
accessToken
)
}
const { result: context, etag } = await this.request<Context>(
`${this.serverUrl}/compute/contexts/${originalContext.id}`,
{
headers
}
).catch((err) => {
if (err && err.status === 404) {
throw new Error(
`The context '${contextName}' was not found on this server.`
)
}
throw err
})
// 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,
...editedContext,
attributes: { ...context.attributes, ...editedContext.attributes }
})
}
return await this.request<Context>(
`${this.serverUrl}/compute/contexts/${context.id}`,
updateContextRequest
)
}
public async getComputeContextByName(
contextName: string,
accessToken?: string
): Promise<Context> {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
{ headers }
).catch((err) => {
throw prefixMessage(err, 'Error while getting compute context by name. ')
})
if (!contexts || !(contexts.items && contexts.items.length)) {
throw new Error(
`The context '${contextName}' was not found at '${this.serverUrl}'.`
)
}
return contexts.items[0]
}
public async getComputeContextById(
contextId: string,
accessToken?: string
): Promise<ContextAllAttributes> {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: context } = await this.request<ContextAllAttributes>(
`${this.serverUrl}/compute/contexts/${contextId}`,
{ headers }
).catch((err) => {
throw prefixMessage(err, 'Error while getting compute context by id. ')
})
return context
}
public async getExecutableContexts(
executeScript: Function,
accessToken?: string
) {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?limit=10000`,
{ headers }
).catch((err) => {
throw prefixMessage(err, 'Error while fetching compute contexts.')
})
const contextsList = contexts.items || []
const executableContexts: any[] = []
const promises = contextsList.map((context: any) => {
const linesOfCode = ['%put &=sysuserid;']
return () =>
executeScript(
`test-${context.name}`,
linesOfCode,
context.name,
accessToken,
null,
false,
true,
true
).catch((err: any) => err)
})
let results: any[] = []
for (const promise of promises) results.push(await promise())
results.forEach((result: any, index: number) => {
if (result && result.log) {
try {
const resultParsed = result.log
let sysUserId = ''
const sysUserIdLog = resultParsed
.split('\n')
.find((line: string) => line.startsWith('SYSUSERID='))
if (sysUserIdLog) {
sysUserId = sysUserIdLog.replace('SYSUSERID=', '')
executableContexts.push({
createdBy: contextsList[index].createdBy,
id: contextsList[index].id,
name: contextsList[index].name,
version: contextsList[index].version,
attributes: {
sysUserId
}
})
}
} catch (error) {
throw error
}
}
})
return executableContexts
}
public async deleteComputeContext(contextName: string, accessToken?: string) {
this.validateContextName(contextName)
this.isDefaultContext(
contextName,
this.defaultComputeContexts,
'Deleting default SAS compute contexts is not allowed.',
true
)
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const context = await this.getComputeContextByName(contextName, accessToken)
const deleteContextRequest: RequestInit = {
method: 'DELETE',
headers
}
return await this.request<Context>(
`${this.serverUrl}/compute/contexts/${context.id}`,
deleteContextRequest
)
}
// TODO: implement editLauncherContext method
// TODO: implement deleteLauncherContext method
private async request<T>(
url: string,
options: RequestInit,
contentType: 'text' | 'json' = 'json'
) {
if (this.csrfToken) {
options.headers = {
...options.headers,
[this.csrfToken.headerName]: this.csrfToken.value
}
}
return await makeRequest<T>(
url,
options,
(token) => {
this.csrfToken = token
this.setCsrfToken(token)
},
contentType
).catch((err) => {
throw prefixMessage(
err,
'Error while making request in Context Manager. '
)
})
}
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}`)
: ''
}`
)
}
}
}

View File

@@ -21,6 +21,7 @@ import {
} from './types'
import { formatDataForRequest } from './utils/formatDataForRequest'
import { SessionManager } from './SessionManager'
import { ContextManager } from './ContextManager'
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
import { Logger, LogLevel } from '@sasjs/utils/logger'
@@ -46,6 +47,7 @@ export class SASViyaApiClient {
this.contextName,
this.setCsrfToken
)
private contextManager = new ContextManager(this.serverUrl, this.setCsrfToken)
private folderMap = new Map<string, Job[]>()
public get debug() {
@@ -98,29 +100,23 @@ export class SASViyaApiClient {
* Returns all available compute contexts on this server.
* @param accessToken - an access token for an authorized user.
*/
public async getAllContexts(accessToken?: string) {
const headers: any = {
'Content-Type': 'application/json'
}
public async getComputeContexts(accessToken?: string) {
return await this.contextManager.getComputeContexts(accessToken)
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
/**
* Returns default(system) compute contexts.
*/
public getDefaultComputeContexts() {
return this.contextManager.getDefaultComputeContexts
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?limit=10000`,
{ headers }
)
const contextsList = contexts && contexts.items ? contexts.items : []
return contextsList.map((context: any) => ({
createdBy: context.createdBy,
id: context.id,
name: context.name,
version: context.version,
attributes: {}
}))
/**
* Returns all available launcher contexts on this server.
* @param accessToken - an access token for an authorized user.
*/
public async getLauncherContexts(accessToken?: string) {
return await this.contextManager.getLauncherContexts(accessToken)
}
/**
@@ -128,74 +124,12 @@ export class SASViyaApiClient {
* @param accessToken - an access token for an authorized user.
*/
public async getExecutableContexts(accessToken?: string) {
const headers: any = {
'Content-Type': 'application/json'
}
const bindedExecuteScript = this.executeScript.bind(this)
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?limit=10000`,
{ headers }
).catch((err) => {
throw err
})
const contextsList = contexts.items || []
const executableContexts: any[] = []
const promises = contextsList.map((context: any) => {
const linesOfCode = ['%put &=sysuserid;']
return () =>
this.executeScript(
`test-${context.name}`,
linesOfCode,
context.name,
accessToken,
null,
false,
true,
true
).catch((err) => err)
})
let results: any[] = []
for (const promise of promises) results.push(await promise())
results.forEach((result: any, index: number) => {
if (result && result.log) {
try {
const resultParsed = result.log
let sysUserId = ''
const sysUserIdLog = resultParsed
.split('\n')
.find((line: string) => line.startsWith('SYSUSERID='))
if (sysUserIdLog) {
sysUserId = sysUserIdLog.replace('SYSUSERID=', '')
executableContexts.push({
createdBy: contextsList[index].createdBy,
id: contextsList[index].id,
name: contextsList[index].name,
version: contextsList[index].version,
attributes: {
sysUserId
}
})
}
} catch (error) {
throw error
}
}
})
return executableContexts
return await this.contextManager.getExecutableContexts(
bindedExecuteScript,
accessToken
)
}
/**
@@ -248,7 +182,7 @@ export class SASViyaApiClient {
* @param accessToken - an access token for an authorized user.
* @param authorizedUsers - an optional list of authorized user IDs.
*/
public async createContext(
public async createComputeContext(
contextName: string,
launchContextName: string,
sharedAccountId: string,
@@ -256,59 +190,35 @@ export class SASViyaApiClient {
accessToken?: string,
authorizedUsers?: string[]
) {
if (!contextName) {
throw new Error('Context name is required.')
}
if (!launchContextName) {
throw new Error('Launch context name is required.')
}
if (!sharedAccountId) {
throw new Error('Shared account ID is required.')
}
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 await this.contextManager.createComputeContext(
contextName,
launchContextName,
sharedAccountId,
autoExecLines,
accessToken,
authorizedUsers
)
}
return context
/**
* Creates a launcher context on the given server.
* @param contextName - the name of the context to be created.
* @param description - the description of the context to be created.
* @param launchType - launch type of the context to be created.
* @param accessToken - an access token for an authorized user.
*/
public async createLauncherContext(
contextName: string,
description: string,
launchType = 'direct',
accessToken?: string
) {
return await this.contextManager.createLauncherContext(
contextName,
description,
launchType,
accessToken
)
}
/**
@@ -317,75 +227,15 @@ 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
) {
if (!contextName) {
throw new Error('Invalid context name.')
}
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
let originalContext
originalContext = await this.getComputeContextByName(
return await this.contextManager.editComputeContext(
contextName,
editedContext,
accessToken
).catch((err) => {
throw err
})
// Try to find context by id, when context name has been changed.
if (!originalContext) {
originalContext = await this.getComputeContextById(
editedContext.id!,
accessToken
).catch((err) => {
throw err
})
}
const { result: context, etag } = await this.request<Context>(
`${this.serverUrl}/compute/contexts/${originalContext.id}`,
{
headers
}
).catch((err) => {
if (err && err.status === 404) {
throw new Error(
`The context '${contextName}' was not found on this server.`
)
}
throw err
})
// 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,
...editedContext,
attributes: { ...context.attributes, ...editedContext.attributes }
})
}
return await this.request<Context>(
`${this.serverUrl}/compute/contexts/${context.id}`,
updateContextRequest
)
}
@@ -394,29 +244,10 @@ export class SASViyaApiClient {
* @param contextName - the name of the context to be deleted.
* @param accessToken - an access token for an authorized user.
*/
public async deleteContext(contextName: string, accessToken?: string) {
if (!contextName) {
throw new Error('Invalid context name.')
}
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const context = await this.getComputeContextByName(contextName, accessToken)
const deleteContextRequest: RequestInit = {
method: 'DELETE',
headers
}
return await this.request<Context>(
`${this.serverUrl}/compute/contexts/${context.id}`,
deleteContextRequest
public async deleteComputeContext(contextName: string, accessToken?: string) {
return await this.contextManager.deleteComputeContext(
contextName,
accessToken
)
}
@@ -870,11 +701,13 @@ export class SASViyaApiClient {
* @param clientId - the client ID to authenticate with.
* @param clientSecret - the client secret to authenticate with.
* @param authCode - the auth code received from the server.
* @param insecure - this boolean tells adapter to ignore SSL errors. [Not Recommended]
*/
public async getAccessToken(
clientId: string,
clientSecret: string,
authCode: string
authCode: string,
insecure: boolean = false
) {
const url = this.serverUrl + '/SASLogon/oauth/token'
let token
@@ -898,12 +731,23 @@ export class SASViyaApiClient {
formData.append('code', authCode)
}
let moreOptions = {}
if (insecure) {
const https = require('https')
moreOptions = {
agent: new https.Agent({
rejectUnauthorized: false
})
}
}
const authResponse = await fetch(url, {
method: 'POST',
credentials: 'include',
headers,
body: formData as any,
referrerPolicy: 'same-origin'
referrerPolicy: 'same-origin',
...moreOptions
}).then((res) => res.json())
return authResponse
@@ -1002,23 +846,20 @@ export class SASViyaApiClient {
)
}
if (isRelativePath(sasJob)) {
const folderName = sasJob.split('/')[0]
await this.populateFolderMap(
`${this.rootFolderName}/${folderName}`,
accessToken
)
const folderPathParts = sasJob.split('/')
const jobName = folderPathParts.pop()
const folderPath = folderPathParts.join('/')
const fullFolderPath = isRelativePath(sasJob)
? `${this.rootFolderName}/${folderPath}`
: folderPath
if (!this.folderMap.get(`${this.rootFolderName}/${folderName}`)) {
throw new Error(
`The folder '${folderName}' was not found at '${this.serverUrl}/${this.rootFolderName}'`
)
}
} else {
const folderPathParts = sasJob.split('/')
folderPathParts.pop()
const folderPath = folderPathParts.join('/')
await this.populateFolderMap(folderPath, accessToken)
await this.populateFolderMap(fullFolderPath, accessToken)
const jobFolder = this.folderMap.get(fullFolderPath)
if (!jobFolder) {
throw new Error(
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'`
)
}
const headers: any = { 'Content-Type': 'application/json' }
@@ -1026,21 +867,7 @@ export class SASViyaApiClient {
headers.Authorization = `Bearer ${accessToken}`
}
let jobToExecute
if (isRelativePath(sasJob)) {
const folderName = sasJob.split('/')[0]
const jobName = sasJob.split('/')[1]
const jobFolder = this.folderMap.get(
`${this.rootFolderName}/${folderName}`
)
jobToExecute = jobFolder?.find((item) => item.name === jobName)
} else {
const folderPathParts = sasJob.split('/')
const jobName = folderPathParts.pop()
const folderPath = folderPathParts.join('/')
const jobFolder = this.folderMap.get(folderPath)
jobToExecute = jobFolder?.find((item) => item.name === jobName)
}
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
if (!jobToExecute) {
throw new Error(`Job was not found.`)
@@ -1106,52 +933,28 @@ export class SASViyaApiClient {
)
}
if (isRelativePath(sasJob)) {
const folderName = sasJob.split('/')[0]
await this.populateFolderMap(
`${this.rootFolderName}/${folderName}`,
accessToken
)
const folderPathParts = sasJob.split('/')
const jobName = folderPathParts.pop()
const folderPath = folderPathParts.join('/')
const fullFolderPath = isRelativePath(sasJob)
? `${this.rootFolderName}/${folderPath}`
: folderPath
await this.populateFolderMap(fullFolderPath, accessToken)
if (!this.folderMap.get(`${this.rootFolderName}/${folderName}`)) {
throw new Error(
`The folder '${folderName}' was not found at '${this.serverUrl}/${this.rootFolderName}'.`
)
}
} else {
const folderPathParts = sasJob.split('/')
folderPathParts.pop()
const folderPath = folderPathParts.join('/')
await this.populateFolderMap(folderPath, accessToken)
if (!this.folderMap.get(folderPath)) {
throw new Error(
`The folder '${folderPath}' was not found at '${this.serverUrl}'.`
)
}
const jobFolder = this.folderMap.get(fullFolderPath)
if (!jobFolder) {
throw new Error(
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'.`
)
}
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
let files: any[] = []
if (data && Object.keys(data).length) {
files = await this.uploadTables(data, accessToken)
}
let jobToExecute: Job | undefined
let jobName: string | undefined
let jobPath: string | undefined
if (isRelativePath(sasJob)) {
const folderName = sasJob.split('/')[0]
jobName = sasJob.split('/')[1]
jobPath = `${this.rootFolderName}/${folderName}`
const jobFolder = this.folderMap.get(jobPath)
jobToExecute = jobFolder?.find((item) => item.name === jobName)
} else {
const folderPathParts = sasJob.split('/')
jobName = folderPathParts.pop()
jobPath = folderPathParts.join('/')
const jobFolder = this.folderMap.get(jobPath)
jobToExecute = jobFolder?.find((item) => item.name === jobName)
}
if (!jobToExecute) {
throw new Error(`Job was not found.`)
}
@@ -1176,7 +979,7 @@ export class SASViyaApiClient {
const jobArguments: { [key: string]: any } = {
_contextName: contextName,
_program: `${jobPath}/${jobName}`,
_program: `${fullFolderPath}/${jobName}`,
_webin_file_count: files.length,
_OMITJSONLISTING: true,
_OMITJSONLOG: true,
@@ -1317,6 +1120,8 @@ export class SASViyaApiClient {
}
return new Promise(async (resolve, _) => {
let printedState = ''
const interval = setInterval(async () => {
if (
postedJobState === 'running' ||
@@ -1324,9 +1129,6 @@ export class SASViyaApiClient {
postedJobState === 'pending'
) {
if (stateLink) {
if (this.debug) {
console.log('Polling job status... \n')
}
const { result: jobState } = await this.request<string>(
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
{
@@ -1336,10 +1138,16 @@ export class SASViyaApiClient {
)
postedJobState = jobState.trim()
if (this.debug) {
console.log(`Current state: ${postedJobState}\n`)
if (this.debug && printedState !== postedJobState) {
console.log('Polling job status...')
console.log(`Current job state: ${postedJobState}`)
printedState = postedJobState
}
pollCount++
if (pollCount >= MAX_POLL_COUNT) {
resolve(postedJobState)
}
@@ -1438,26 +1246,10 @@ export class SASViyaApiClient {
contextName: string,
accessToken?: string
): Promise<Context> {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
{ headers }
return await this.contextManager.getComputeContextByName(
contextName,
accessToken
)
if (!contexts || !(contexts.items && contexts.items.length)) {
throw new Error(
`The context '${contextName}' was not found at '${this.serverUrl}'.`
)
}
return contexts.items[0]
}
/**
@@ -1469,22 +1261,10 @@ export class SASViyaApiClient {
contextId: string,
accessToken?: string
): Promise<ContextAllAttributes> {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: context } = await this.request<ContextAllAttributes>(
`${this.serverUrl}/compute/contexts/${contextId}`,
{ headers }
).catch((err) => {
throw err
})
return context
return await this.contextManager.getComputeContextById(
contextId,
accessToken
)
}
/**

View File

@@ -96,12 +96,39 @@ export default class SASjs {
)
}
public async getAllContexts(accessToken: string) {
this.isMethodSupported('getAllContexts', ServerType.SASViya)
/**
* Gets compute contexts.
* @param accessToken - an access token for an authorized user.
*/
public async getComputeContexts(accessToken: string) {
this.isMethodSupported('getComputeContexts', ServerType.SASViya)
return await this.sasViyaApiClient!.getAllContexts(accessToken)
return await this.sasViyaApiClient!.getComputeContexts(accessToken)
}
/**
* Gets launcher contexts.
* @param accessToken - an access token for an authorized user.
*/
public async getLauncherContexts(accessToken: string) {
this.isMethodSupported('getLauncherContexts', ServerType.SASViya)
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.
*/
public async getExecutableContexts(accessToken: string) {
this.isMethodSupported('getExecutableContexts', ServerType.SASViya)
@@ -117,7 +144,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user.
* @param authorizedUsers - an optional list of authorized user IDs.
*/
public async createContext(
public async createComputeContext(
contextName: string,
launchContextName: string,
sharedAccountId: string,
@@ -125,9 +152,9 @@ export default class SASjs {
accessToken: string,
authorizedUsers?: string[]
) {
this.isMethodSupported('createContext', ServerType.SASViya)
this.isMethodSupported('createComputeContext', ServerType.SASViya)
return await this.sasViyaApiClient!.createContext(
return await this.sasViyaApiClient!.createComputeContext(
contextName,
launchContextName,
sharedAccountId,
@@ -137,20 +164,43 @@ export default class SASjs {
)
}
/**
* Creates a launcher context on the given server.
* @param contextName - the name of the context to be created.
* @param description - the description of the context to be created.
* @param launchType - launch type of the context to be created.
* @param accessToken - an access token for an authorized user.
*/
public async createLauncherContext(
contextName: string,
description: string,
launchType: string,
accessToken: string
) {
this.isMethodSupported('createLauncherContext', ServerType.SASViya)
return await this.sasViyaApiClient!.createLauncherContext(
contextName,
description,
launchType,
accessToken
)
}
/**
* Updates a compute context on the given server.
* @param contextName - the original name 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(
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
@@ -162,10 +212,13 @@ export default class SASjs {
* @param contextName - the name of the context to be deleted.
* @param accessToken - an access token for an authorized user.
*/
public async deleteContext(contextName: string, accessToken?: string) {
this.isMethodSupported('deleteContext', ServerType.SASViya)
public async deleteComputeContext(contextName: string, accessToken?: string) {
this.isMethodSupported('deleteComputeContext', ServerType.SASViya)
return await this.sasViyaApiClient!.deleteContext(contextName, accessToken)
return await this.sasViyaApiClient!.deleteComputeContext(
contextName,
accessToken
)
}
/**
@@ -333,17 +386,26 @@ export default class SASjs {
return await this.sasViyaApiClient!.getAuthCode(clientId)
}
/**
* Exchanges the auth code for an access token for the given client.
* @param clientId - the client ID to authenticate with.
* @param clientSecret - the client secret to authenticate with.
* @param authCode - the auth code received from the server.
* @param insecure - this boolean tells adapter to ignore SSL errors. [Not Recommended]
*/
public async getAccessToken(
clientId: string,
clientSecret: string,
authCode: string
authCode: string,
insecure: boolean = false
) {
this.isMethodSupported('getAccessToken', ServerType.SASViya)
return await this.sasViyaApiClient!.getAccessToken(
clientId,
clientSecret,
authCode
authCode,
insecure
)
}
@@ -694,10 +756,7 @@ export default class SASjs {
)
}
const members =
serviceJson.members[0].name === 'services'
? serviceJson.members[0].members
: serviceJson.members
const members = serviceJson.members
await this.createFoldersAndServices(
appLoc,

View File

@@ -23,6 +23,10 @@ export class SessionManager {
private currentContext: Context | null = null
private csrfToken: CsrfToken | null = null
private _debug: boolean = false
private printedSessionState = {
printed: false,
state: ''
}
public get debug() {
return this._debug
@@ -175,8 +179,10 @@ export class SessionManager {
sessionState === ''
) {
if (stateLink) {
if (this.debug) {
console.log('Polling session status... \n') // ?
if (this.debug && !this.printedSessionState.printed) {
console.log('Polling session status...')
this.printedSessionState.printed = true
}
const { result: state } = await this.requestSessionStatus<string>(
@@ -191,8 +197,11 @@ export class SessionManager {
sessionState = state.trim()
if (this.debug) {
console.log(`Current state is '${sessionState}'\n`)
if (this.debug && this.printedSessionState.state !== sessionState) {
console.log(`Current session state is '${sessionState}'`)
this.printedSessionState.state = sessionState
this.printedSessionState.printed = false
}
// There is an internal error present in SAS Viya 3.5

View File

@@ -0,0 +1,585 @@
import { ContextManager } from '../ContextManager'
describe('ContextManager', () => {
let originalFetch: any
let fetchCallNumber = 0
const fakeGlobalFetch = (fakeResponses: object[]) => {
;(global as any).fetch = jest.fn().mockImplementation(() => {
const fakeResponse = fakeResponses[fetchCallNumber]
if (
fetchCallNumber !== fakeResponses.length &&
fakeResponses.length > 1
) {
if (fetchCallNumber + 1 === fakeResponses.length) fetchCallNumber = 0
else fetchCallNumber += 1
} else {
fetchCallNumber = 0
}
return Promise.resolve({
ok: true,
headers: { get: () => '' },
json: () => Promise.resolve(fakeResponse)
})
})
}
const contextManager = new ContextManager(
process.env.SERVER_URL as string,
() => {}
)
const defaultComputeContexts = contextManager.getDefaultComputeContexts
const defaultLauncherContexts = contextManager.getDefaultLauncherContexts
const getRandomDefaultComputeContext = () =>
defaultComputeContexts[
Math.floor(Math.random() * defaultComputeContexts.length)
]
const getRandomDefaultLauncherContext = () =>
defaultLauncherContexts[
Math.floor(Math.random() * defaultLauncherContexts.length)
]
beforeAll(() => {
originalFetch = (global as any).fetch
})
afterEach(() => {
;(global as any).fetch = originalFetch
})
describe('getComputeContexts', () => {
it('should fetch compute contexts', async () => {
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Fake Compute Context',
attributes: {}
}
const sampleResponse = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponse])
await expect(contextManager.getComputeContexts()).resolves.toEqual([
sampleComputeContext
])
})
})
describe('getLauncherContexts', () => {
it('should fetch launcher contexts', async () => {
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Fake Launcher Context',
attributes: {}
}
const sampleResponse = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponse])
await expect(contextManager.getLauncherContexts()).resolves.toEqual([
sampleComputeContext
])
})
})
describe('createComputeContext', () => {
it('should throw an error if context name was not provided', async () => {
await expect(
contextManager.createComputeContext(
'',
'Test Launcher Context',
'fakeAccountId',
[]
)
).rejects.toEqual(new Error('Context name is required.'))
})
it('should throw an error when attempt to create context with reserved name', async () => {
const contextName = getRandomDefaultComputeContext()
await expect(
contextManager.createComputeContext(
contextName,
'Test Launcher Context',
'fakeAccountId',
[]
)
).rejects.toEqual(
new Error(`Compute context '${contextName}' already exists.`)
)
})
it('should throw an error if context already exists', async () => {
const contextName = 'Existing Compute Context'
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponse = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponse])
await expect(
contextManager.createComputeContext(
contextName,
'Test Launcher Context',
'fakeAccountId',
[]
)
).rejects.toEqual(
new Error(`Compute context '${contextName}' already exists.`)
)
})
it('should create compute context without launcher context', async () => {
const contextName = 'New Compute Context'
const sampleExistingComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Existing Compute Context',
attributes: {}
}
const sampleNewComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponseExistingComputeContexts = {
items: [sampleExistingComputeContext]
}
const sampleResponseCreatedComputeContext = {
items: [sampleNewComputeContext]
}
fakeGlobalFetch([
sampleResponseExistingComputeContexts,
sampleResponseCreatedComputeContext
])
await expect(
contextManager.createComputeContext(
contextName,
'',
'fakeAccountId',
[]
)
).resolves.toEqual({
items: [
{
attributes: {},
createdBy: 'fake creator',
id: 'fakeId',
name: contextName,
version: 2
}
]
})
})
it('should create compute context with default launcher context', async () => {
const contextName = 'New Compute Context'
const sampleExistingComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Existing Compute Context',
attributes: {}
}
const sampleNewComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponseExistingComputeContexts = {
items: [sampleExistingComputeContext]
}
const sampleResponseCreatedComputeContext = {
items: [sampleNewComputeContext]
}
fakeGlobalFetch([
sampleResponseExistingComputeContexts,
sampleResponseCreatedComputeContext
])
await expect(
contextManager.createComputeContext(
contextName,
getRandomDefaultLauncherContext(),
'fakeAccountId',
[]
)
).resolves.toEqual({
items: [
{
attributes: {},
createdBy: 'fake creator',
id: 'fakeId',
name: contextName,
version: 2
}
]
})
})
it('should create compute context with not existing launcher context', async () => {
const computeContextName = 'New Compute Context'
const launcherContextName = 'New Launcher Context'
const sampleExistingComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Existing Compute Context',
attributes: {}
}
const sampleNewComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: computeContextName,
attributes: {}
}
const sampleNewLauncherContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: launcherContextName,
attributes: {}
}
const sampleResponseExistingComputeContexts = {
items: [sampleExistingComputeContext]
}
const sampleResponseCreatedLauncherContext = {
items: [sampleNewLauncherContext]
}
const sampleResponseCreatedComputeContext = {
items: [sampleNewComputeContext]
}
fakeGlobalFetch([
sampleResponseExistingComputeContexts,
sampleResponseCreatedLauncherContext,
sampleResponseCreatedComputeContext
])
await expect(
contextManager.createComputeContext(
computeContextName,
launcherContextName,
'fakeAccountId',
[]
)
).resolves.toEqual({
items: [
{
attributes: {},
createdBy: 'fake creator',
id: 'fakeId',
name: computeContextName,
version: 2
}
]
})
})
})
describe('createLauncherContext', () => {
it('should throw an error if context name was not provided', async () => {
await expect(
contextManager.createLauncherContext('', 'Test Description')
).rejects.toEqual(new Error('Context name is required.'))
})
it('should throw an error when attempt to create context with reserved name', async () => {
const contextName = getRandomDefaultLauncherContext()
await expect(
contextManager.createLauncherContext(contextName, 'Test Description')
).rejects.toEqual(
new Error(`Launcher context '${contextName}' already exists.`)
)
})
it('should throw an error if context already exists', async () => {
const contextName = 'Existing Launcher Context'
const sampleLauncherContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponse = {
items: [sampleLauncherContext]
}
fakeGlobalFetch([sampleResponse])
await expect(
contextManager.createLauncherContext(contextName, 'Test Description')
).rejects.toEqual(
new Error(`Launcher context '${contextName}' already exists.`)
)
})
it('should create launcher context', async () => {
const contextName = 'New Launcher Context'
const sampleExistingLauncherContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Existing Launcher Context',
attributes: {}
}
const sampleNewLauncherContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponseExistingLauncherContext = {
items: [sampleExistingLauncherContext]
}
const sampleResponseCreatedLauncherContext = {
items: [sampleNewLauncherContext]
}
fakeGlobalFetch([
sampleResponseExistingLauncherContext,
sampleResponseCreatedLauncherContext
])
await expect(
contextManager.createLauncherContext(contextName, 'Test Description')
).resolves.toEqual({
items: [
{
attributes: {},
createdBy: 'fake creator',
id: 'fakeId',
name: contextName,
version: 2
}
]
})
})
})
describe('editComputeContext', () => {
const editedContext = {
name: 'updated name',
description: 'updated description',
id: 'someId'
}
it('should throw an error if context name was not provided', async () => {
await expect(
contextManager.editComputeContext('', editedContext)
).rejects.toEqual(new Error('Context name is required.'))
})
it('should throw an error when attempt to edit context with reserved name', async () => {
const contextName = getRandomDefaultComputeContext()
let editError: Error = { name: '', message: '' }
try {
contextManager.isDefaultContext(
contextName,
defaultComputeContexts,
'Editing default SAS compute contexts is not allowed.',
true
)
} catch (error) {
editError = error
}
await expect(
contextManager.editComputeContext(contextName, editedContext)
).rejects.toEqual(editError)
})
it('should edit context if founded by name', async () => {
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: editedContext.name,
attributes: {}
}
const sampleResponseGetComputeContextByName = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponseGetComputeContextByName])
const expectedResponse = {
etag: '',
result: sampleResponseGetComputeContextByName
}
await expect(
contextManager.editComputeContext(editedContext.name, editedContext)
).resolves.toEqual(expectedResponse)
})
})
describe('getExecutableContexts', () => {
it('should return executable contexts', async () => {
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Executable Compute Context',
attributes: {}
}
const sampleResponse = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponse])
const user = 'testUser'
const fakedExecuteScript = async () => {
return Promise.resolve({ log: `SYSUSERID=${user}` })
}
const expectedResponse = [
{
...sampleComputeContext,
attributes: { sysUserId: user }
}
]
await expect(
contextManager.getExecutableContexts(fakedExecuteScript)
).resolves.toEqual(expectedResponse)
})
it('should not return executable contexts', async () => {
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: 'Not Executable Compute Context',
attributes: {}
}
const sampleResponse = {
items: [sampleComputeContext]
}
fakeGlobalFetch([sampleResponse])
const fakedExecuteScript = async () => {
return Promise.resolve({ log: '' })
}
await expect(
contextManager.getExecutableContexts(fakedExecuteScript)
).resolves.toEqual([])
})
})
describe('deleteComputeContext', () => {
it('should throw an error if context name was not provided', async () => {
await expect(contextManager.deleteComputeContext('')).rejects.toEqual(
new Error('Context name is required.')
)
})
it('should throw an error when attempt to delete context with reserved name', async () => {
const contextName = getRandomDefaultComputeContext()
let deleteError: Error = { name: '', message: '' }
try {
contextManager.isDefaultContext(
contextName,
defaultComputeContexts,
'Deleting default SAS compute contexts is not allowed.',
true
)
} catch (error) {
deleteError = error
}
await expect(
contextManager.deleteComputeContext(contextName)
).rejects.toEqual(deleteError)
})
it('should delete context', async () => {
const contextName = 'Compute Context To Delete'
const sampleComputeContext = {
createdBy: 'fake creator',
id: 'fakeId',
version: 2,
name: contextName,
attributes: {}
}
const sampleResponseGetComputeContextByName = {
items: [sampleComputeContext]
}
const sampleResponseDeletedContext = {
items: [sampleComputeContext]
}
fakeGlobalFetch([
sampleResponseGetComputeContextByName,
sampleResponseDeletedContext
])
const expectedResponse = {
etag: '',
result: sampleResponseDeletedContext
}
await expect(
contextManager.deleteComputeContext(contextName)
).resolves.toEqual(expectedResponse)
})
})
})

View File

@@ -26,7 +26,8 @@ const browserConfig = {
]
},
resolve: {
extensions: ['.ts', '.js']
extensions: ['.ts', '.js'],
fallback: { https: false }
},
output: {
filename: 'index.js',