Skip to content

Commit

Permalink
Merge pull request #151 from sasjs/issue-140
Browse files Browse the repository at this point in the history
test(sasjs-commands): covered 'servicepack' and 'context' commands
  • Loading branch information
YuryShkoda authored Oct 14, 2020
2 parents dab704a + 4c6d55e commit 45e4538
Show file tree
Hide file tree
Showing 13 changed files with 1,111 additions and 530 deletions.
1,305 changes: 869 additions & 436 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"access": "public"
},
"dependencies": {
"@sasjs/adapter": "^1.15.2",
"@sasjs/adapter": "^1.15.4",
"@sasjs/core": "^1.5.5",
"base64-img": "^1.0.4",
"btoa": "^1.2.1",
Expand Down
24 changes: 11 additions & 13 deletions src/sasjs-context/create.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import SASjs from '@sasjs/adapter/node'
import { displayResult } from '../utils/displayResult'
import { getAccessToken } from '../utils/config-utils'

/**
* Creates compute context using provided config.
* @param {object} config - context configuration.
* @param {object} target - SAS server configuration.
* @param {object} sasjs - configuration object of SAS adapter.
* @param {string} accessToken - an access token for an authorized user.
*/
export async function create(config, target) {
const sasjs = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType
})

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})

export async function create(config, sasjs, accessToken) {
const { name } = config
const launchName = config.launchContext && config.launchContext.contextName
const autoExecLines = config.environment && config.environment.autoExecLines
const sharedAccountId = config.attributes && config.attributes.runServerAs

let result

const createdContext = await sasjs
.createContext(
name,
Expand All @@ -32,13 +24,19 @@ export async function create(config, target) {
)
.catch((err) => {
displayResult(err, 'An error has occurred when processing context.', null)

result = err
})

if (createdContext) {
result = true

displayResult(
null,
null,
`Context '${name}' with id '${createdContext.id}' successfully created!`
)
}

return result
}
23 changes: 10 additions & 13 deletions src/sasjs-context/edit.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import SASjs from '@sasjs/adapter/node'
import { displayResult } from '../utils/displayResult'
import { getAccessToken } from '../utils/config-utils'

/**
* Edits existing compute context.
* @param {string} configName - name of the config to edit.
* @param {object} config - context configuration.
* @param {object} target - SAS server configuration.
* @param {object} sasjs - configuration object of SAS adapter.
* @param {string} accessToken - an access token for an authorized user.
*/
export async function edit(configName, config, target) {
const sasjs = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType
})

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})

export async function edit(configName, config, sasjs, accessToken) {
const name = configName || config.name

delete config.id

let result

const editedContext = await sasjs
.editContext(name, config, accessToken)
.catch((err) => {
result = err

displayResult(err, 'An error has occurred when processing context.', null)
})

if (editedContext) {
result = true
const editedContextName = editedContext.result.name || ''

displayResult(
Expand All @@ -37,4 +32,6 @@ export async function edit(configName, config, target) {
`Context '${editedContextName}' successfully updated!`
)
}

return result
}
32 changes: 15 additions & 17 deletions src/sasjs-context/export.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import SASjs from '@sasjs/adapter/node'
import { displayResult } from '../utils/displayResult'
import { getAccessToken } from '../utils/config-utils'
import path from 'path'
import { createFile } from '../utils/file-utils'
import { createFile, sanitizeFileName } from '../utils/file-utils'

/**
* Export compute context to json file in current folder.
* @param {string} contextName - name of the context to export.
* @param {object} target - SAS server configuration.
* @param {object} sasjs - configuration object of SAS adapter.
* @param {string} accessToken - an access token for an authorized user.
*/
export async function exportContext(contextName, target) {
const sasjs = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType
})

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})

export async function exportContext(contextName, sasjs, accessToken) {
const context = await sasjs
.getComputeContextByName(contextName, accessToken)
.catch((err) => {
displayResult(err, '', null)
})

let result

if (context && context.id) {
const contextAllAttributes = await sasjs
.getComputeContextById(context.id, accessToken)
Expand All @@ -33,20 +25,24 @@ export async function exportContext(contextName, target) {
if (contextAllAttributes) {
delete contextAllAttributes.links

result = true

let output

try {
output = JSON.stringify(contextAllAttributes, null, 2)
} catch (error) {
displayResult(null, null, 'Context has bad format.')

return
return false
}

const outputFileName = contextName.replace(/[^a-z0-9]/gi, '_') + '.json'
const outputFileName = sanitizeFileName(contextName) + '.json'
const outputPath = path.join(process.cwd(), outputFileName)

await createFile(outputPath, output)
await createFile(outputPath, output).catch((err) => {
result = err
})

displayResult(
null,
Expand All @@ -55,4 +51,6 @@ export async function exportContext(contextName, target) {
)
}
}

return result
}
32 changes: 26 additions & 6 deletions src/sasjs-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { remove } from './remove'
import { list } from './list'
import { exportContext } from './export'
import { fileExists, readFile } from '../utils/file-utils'
import { getBuildTarget } from '../utils/config-utils'
import { getBuildTarget, getAccessToken } from '../utils/config-utils'
import { displayResult } from '../utils/displayResult'
import SASjs from '@sasjs/adapter/node'

export async function processContext(commandLine) {
const command = commandLine[1]
Expand Down Expand Up @@ -110,6 +112,18 @@ export async function processContext(commandLine) {
return contextName
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
appLoc: target.appLoc,
serverType: target.serverType
})

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})

let output

switch (command) {
case commands.create:
config = await getConfig()
Expand All @@ -118,7 +132,7 @@ export async function processContext(commandLine) {

parsedConfig = parseConfig(config)

create(parsedConfig, target)
output = await create(parsedConfig, sasjs, accessToken)

break
case commands.edit: {
Expand All @@ -128,31 +142,37 @@ export async function processContext(commandLine) {

parsedConfig = parseConfig(config)

edit(contextName, parsedConfig, target)
output = await edit(contextName, parsedConfig, sasjs, accessToken)

break
}
case commands.delete: {
const contextName = getContextName()

if (contextName) remove(contextName, target)
if (contextName) {
output = remove(contextName, sasjs, accessToken)
}

break
}
case commands.list:
list(target)
output = list(target, sasjs, accessToken)

break
case commands.export: {
const contextName = getContextName()

if (contextName) exportContext(contextName, target)
if (contextName) {
output = await exportContext(contextName, sasjs, accessToken)
}

break
}
default:
break
}

return output
}

async function validateConfigPath(path) {
Expand Down
22 changes: 9 additions & 13 deletions src/sasjs-context/list.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import SASjs from '@sasjs/adapter/node'
import chalk from 'chalk'
import ora from 'ora'
import { displayResult } from '../utils/displayResult'
import { getAccessToken } from '../utils/config-utils'

/**
* Lists all accessible and inaccessible compute contexts.
* @param {object} target - SAS server configuration.
* @param {object} sasjs - configuration object of SAS adapter.
* @param {string} accessToken - an access token for an authorized user.
*/
export async function list(target) {
export async function list(target, sasjs, accessToken) {
if (target.serverType !== 'SASVIYA') {
throw new Error(
`'context list' command is only supported for SAS Viya build targets.\nPlease check the target name and try again.`
)
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
appLoc: target.appLoc,
serverType: target.serverType
})

const startTime = new Date().getTime()

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})

const spinner = ora(
`Checking the compute contexts on ${chalk.greenBright(
target.serverUrl
Expand All @@ -45,6 +35,8 @@ export async function list(target) {
)
})

let result

if (contexts) {
const accessibleContexts = contexts.map((context) => ({
createdBy: context.createdBy,
Expand Down Expand Up @@ -74,6 +66,8 @@ export async function list(target) {
}))

if (accessibleContexts.length) {
result = accessibleContexts

displayResult(
null,
null,
Expand Down Expand Up @@ -101,4 +95,6 @@ export async function list(target) {
`This operation took ${(endTime - startTime) / 1000} seconds`
)
)

return result ? result : false
}
22 changes: 10 additions & 12 deletions src/sasjs-context/remove.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import SASjs from '@sasjs/adapter/node'
import { displayResult } from '../utils/displayResult'
import { getAccessToken } from '../utils/config-utils'

/**
* Removes compute context.
* @param {string} contextName - name of the context to delete.
* @param {object} target - SAS server configuration.
* @param {object} sasjs - configuration object of SAS adapter.
* @param {string} accessToken - an access token for an authorized user.
*/
export async function remove(contextName, target) {
const sasjs = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType
})

const accessToken = await getAccessToken(target).catch((err) => {
displayResult(err)
})
export async function remove(contextName, sasjs, accessToken) {
let result

const deletedContext = await sasjs
.deleteContext(contextName, accessToken)
.catch((err) => {
result = err

displayResult(
err,
`An error has occurred when deleting context '${contextName}'.`,
Expand All @@ -28,6 +22,10 @@ export async function remove(contextName, target) {
})

if (deletedContext) {
result = true

displayResult(null, null, `Context '${contextName}' has been deleted!`)
}

return result
}
9 changes: 2 additions & 7 deletions src/sasjs-servicepack/deploy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import path from 'path'
import SASjs from '@sasjs/adapter/node'
import chalk from 'chalk'
import {
readFile,
folderExists,
createFile,
createFolder
} from '../utils/file-utils'
import { readFile } from '../utils/file-utils'
import { displayResult } from '../utils/displayResult'
import {
getAccessToken,
Expand All @@ -28,7 +23,7 @@ export async function servicePackDeploy(
throw new Error('Provided data file must be valid json.')
}

const { target, isLocal } = await findTargetInConfiguration(targetName, true)
const { target } = await findTargetInConfiguration(targetName, true)

if (!target.serverType === 'SASVIYA') {
console.log(
Expand Down
Loading

0 comments on commit 45e4538

Please sign in to comment.