Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat. Support for --copy flag on create action. #255

Merged
merged 10 commits into from
Jan 26, 2022
53 changes: 40 additions & 13 deletions src/commands/runtime/action/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ActionCreate extends RuntimeBaseCommand {
let annotationParams
const webAction = flags.web ? flags.web.toLowerCase() : false
const webSecure = flags['web-secure'] ? flags['web-secure'].toLowerCase() : false
const method = this.isUpdate() ? 'update' : 'create'

try {
// sanity check web secure must have web truthy
Expand All @@ -41,7 +42,7 @@ class ActionCreate extends RuntimeBaseCommand {
throw (new Error(ActionCreate.errorMessages.sequenceWithDocker))
} else if (flags.docker && flags.kind) {
throw (new Error(ActionCreate.errorMessages.kindWithDocker))
} else if (!args.actionPath && !flags.sequence && !flags.docker && !this.isUpdate()) {
} else if (!flags.copy && !args.actionPath && !flags.sequence && !flags.docker && !this.isUpdate()) {
throw (new Error(ActionCreate.errorMessages.missingKind))
}

Expand Down Expand Up @@ -94,6 +95,9 @@ class ActionCreate extends RuntimeBaseCommand {
} else {
exec = createComponentsfromSequence(sequenceAction)
}
} else if (flags.copy) {
const ow = await this.wsk()
return await this.syncAction(ow, name, null, flags, method)
}

if (flags.docker) {
Expand All @@ -112,7 +116,7 @@ class ActionCreate extends RuntimeBaseCommand {
envParams = createKeyValueArrayFromFile(flags['env-file'])
}

// merge parametes and environemtn variables
// merge parameters and environment variables
if (envParams) {
envParams = envParams.map(e => ({ ...e, init: true }))
if (paramsAction) {
Expand Down Expand Up @@ -178,22 +182,42 @@ class ActionCreate extends RuntimeBaseCommand {
}

const options = { name }
if (exec) options.exec = exec
if (limits) options.limits = limits
if (paramsAction) options.parameters = paramsAction
if (annotationParams) options.annotations = annotationParams

const ow = await this.wsk()
const method = this.isUpdate() ? 'update' : 'create'
const result = await ow.actions[method]({ name, action: options })
if (flags.json) {
this.logJSON('', result)
if (exec) {
options.exec = exec
}
if (limits) {
options.limits = limits
}
if (paramsAction) {
options.parameters = paramsAction
}
if (annotationParams) {
options.annotations = annotationParams
}
const ow = await this.wsk()
await this.syncAction(ow, name, options, flags, method)
} catch (err) {
const method = this.isUpdate() ? 'update' : 'create'
this.handleError(`failed to ${method} the action`, err)
}
}

/** @private
* @param {OpenwhiskClient} ow
* @param {string} actionName
* @param {object} actionData
* @param {object} flags
* @param {'update' | 'create'} method
* */
async syncAction (ow, actionName, actionData, flags, method) {
let action = actionData
if (flags.copy) {
action = await ow.actions.get(flags.copy)
purplecabbage marked this conversation as resolved.
Show resolved Hide resolved
}
const result = await ow.actions[method]({ name: actionName, action })
if (flags.json) {
purplecabbage marked this conversation as resolved.
Show resolved Hide resolved
this.logJSON('', result)
}
}
}

ActionCreate.args = [
Expand All @@ -213,6 +237,9 @@ ActionCreate.flags = {
description: 'parameter values in KEY VALUE format', // help description for flag
multiple: true // allow setting this flag multiple times
}),
copy: flags.string({
description: 'copy an existing action' // help description for flag
}),
env: flags.string({
char: 'e',
description: 'environment values in KEY VALUE format', // help description for flag
Expand Down
17 changes: 17 additions & 0 deletions test/commands/runtime/action/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const RuntimeBaseCommand = require('../../../../src/RuntimeBaseCommand.js')
const RuntimeLib = require('@adobe/aio-lib-runtime')
const rtUtils = RuntimeLib.utils
const rtAction = 'actions.create'
const rtActionGet = 'actions.get'

test('exports', async () => {
expect(typeof TheCommand).toEqual('function')
Expand Down Expand Up @@ -47,6 +48,7 @@ test('flags', async () => {
expect(TheCommand.flags['annotation-file'].char).toBe('A')
expect(typeof TheCommand.flags['annotation-file']).toBe('object')
expect(typeof TheCommand.flags.sequence).toBe('object')
expect(typeof TheCommand.flags.copy).toBe('object')
})

test('args', async () => {
Expand Down Expand Up @@ -878,5 +880,20 @@ describe('instance methods', () => {
})
})
})

test('aio runtime:action:create newAction --copy oldAction', () => {
const oldActionJson = require('../../../__fixtures__/action/get.json')
const cmdGet = rtLib.mockResolvedFixture(rtActionGet, 'action/get.json')
const cmdCreate = rtLib.mockResolved(rtAction, { fake: '' })
const name = 'oldAction'
const newAction = 'newAction'
command.argv = [newAction, '--copy', name]
return command.run()
.then(() => {
expect(cmdGet).toHaveBeenCalledWith(name)
expect(cmdCreate).toHaveBeenCalledWith({ name: newAction, action: oldActionJson })
expect(stdout.output).toMatch('')
})
})
})
})