Skip to content

Commit

Permalink
Moved and using aio-lib-runtime for value-add functionality in feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Himavanth committed Jun 4, 2020
1 parent 15c132e commit b939cc5
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 125 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@adobe/aio-lib-core-config": "^1.2.4",
"@adobe/aio-lib-runtime": "0.0.1",
"@oclif/command": "^1.5.6",
"@oclif/config": "^1.9.0",
"@oclif/errors": "^1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions src/RuntimeBaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { propertiesFile, PropertyEnv, PropertyDefault } = require('./properties')
const createDebug = require('debug')
const debug = createDebug('aio-cli-plugin-runtime')
const http = require('http')
const OpenWhisk = require('openwhisk')
const runtimeLib = require('@adobe/aio-lib-runtime')
const config = require('@adobe/aio-lib-core-config')

class RuntimeBaseCommand extends Command {
Expand Down Expand Up @@ -66,7 +66,7 @@ class RuntimeBaseCommand extends Command {
if (!options) {
options = await this.getOptions()
}
return OpenWhisk(options)
return runtimeLib.init(options)
}

async init () {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/runtime/deploy/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DeployReport extends RuntimeBaseCommand {
if (packages[key]['triggers']) {
Object.keys(packages[key]['triggers']).forEach((trigger) => {
const objTrigger = { name: trigger }
if (packages[key]['triggers'][trigger]['feed']) {
if (packages[key]['triggers'][trigger]['feed']) {
objTrigger.feed = packages[key]['triggers'][trigger]['feed']
}
const packageInputs = packages[key]['triggers'][trigger]['inputs'] || {}
Expand Down
16 changes: 4 additions & 12 deletions src/commands/runtime/trigger/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile, createKeyValueObjectFromFlag } = require('../../../runtime-helpers')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile } = require('../../../runtime-helpers')
const { flags } = require('@oclif/command')

class TriggerCreate extends RuntimeBaseCommand {
Expand All @@ -35,9 +35,6 @@ class TriggerCreate extends RuntimeBaseCommand {
} else if (flags['annotation-file']) {
annotationParams = createKeyValueArrayFromFile(flags['annotation-file'])
}
if (flags.feed) {
annotationParams.push({ key: 'feed', value: flags.feed })
}

// triggerParams.parameters is expected to be passed as an array of key value pairs
// For example : [{key : 'Your key 1' , value: 'Your value 1'}, {key : 'Your key 2' , value: 'Your value 2'} ]
Expand All @@ -48,6 +45,9 @@ class TriggerCreate extends RuntimeBaseCommand {
if (Object.keys(annotationParams).length) {
triggerParams.annotations = annotationParams
}
if (flags.feed) {
triggerParams.feed = flags.feed
}

const options = {
name: args.triggerName,
Expand All @@ -56,14 +56,6 @@ class TriggerCreate extends RuntimeBaseCommand {

const ow = await this.wsk()
await ow.triggers.create(options)
if (flags.feed) {
try {
await ow.feeds.create({ name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFromFlag(flags.param) })
} catch (err) {
await ow.triggers.delete(options)
this.handleError('failed to create the feed, deleted trigger', err)
}
}
} catch (err) {
this.handleError('failed to create the trigger', err)
}
Expand Down
8 changes: 0 additions & 8 deletions src/commands/runtime/trigger/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ class TriggerDelete extends RuntimeBaseCommand {
try {
const ow = await this.wsk()
const obj = { namespace, name }
const retTrigger = await ow.triggers.get(obj)
if (retTrigger.annotations) {
for (const annotation of retTrigger.annotations) {
if (annotation.key === 'feed') {
await ow.feeds.delete({ name: annotation.value, trigger: name })
}
}
}

await ow.triggers.delete(obj)
} catch (err) {
Expand Down
9 changes: 1 addition & 8 deletions src/commands/runtime/trigger/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile, createKeyValueObjectFromFlag } = require('../../../runtime-helpers')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile } = require('../../../runtime-helpers')
const { flags } = require('@oclif/command')

class TriggerUpdate extends RuntimeBaseCommand {
Expand Down Expand Up @@ -53,9 +53,6 @@ class TriggerUpdate extends RuntimeBaseCommand {

const ow = await this.wsk()
await ow.triggers.update(options)
if (flags.feed) {
await ow.feeds.update({ name: flags.feed, trigger: args.triggerName, params: createKeyValueObjectFromFlag(flags.param) })
}
} catch (err) {
this.handleError('failed to update the trigger', err)
}
Expand Down Expand Up @@ -89,10 +86,6 @@ TriggerUpdate.flags = {
'annotation-file': flags.string({
char: 'A',
description: 'FILE containing annotation values in JSON format' // help description for flag
}),
feed: flags.string({
char: 'f',
description: 'trigger feed ACTION_NAME'
})
}

Expand Down
43 changes: 0 additions & 43 deletions src/runtime-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,6 @@ function createKeyValueObjectFromFlag (flag) {
}
}

/**
* @description returns key value pairs in an object from the key value array supplied. Used to create parameters object
* @returns An object of key value pairs in this format : {Your key1 : 'Your Value 1' , Your key2: 'Your value 2'}
*/
function createKeyValueObjectFromArray (inputsArray) {
const tempObj = {}
inputsArray.forEach((input) => {
if (input.key && input.value) {
try {
// assume it is JSON, there is only 1 way to find out
tempObj[input.key] = JSON.parse(input.value)
} catch (ex) {
// hmm ... not json, treat as string
tempObj[input.key] = input.value
}
} else {
throw (new Error('Please provide correct input array with key and value params in each array item'))
}
})
return tempObj
}

/**
* @description parses a string and returns the namespace and entity name for a package.
* @returns An object { namespace: string, name: string }
Expand Down Expand Up @@ -722,8 +700,6 @@ function processPackage (packages, deploymentPackages, deploymentTriggers, param
objTrigger.trigger.annotations = createKeyValueInput(packages[key]['triggers'][triggerName]['annotations'])
}
if (packages[key]['triggers'][triggerName]['feed']) {
objTrigger.trigger.annotations = objTrigger.trigger.annotations || []
objTrigger.trigger.annotations.push({ key: 'feed', value: packages[key]['triggers'][triggerName]['feed'] })
objTrigger.trigger.feed = packages[key]['triggers'][triggerName]['feed']
}
ruleTrigger.push(triggerName)
Expand Down Expand Up @@ -869,16 +845,6 @@ async function deployPackage (entities, ow, logger) {
logger(`Info: Deploying trigger [${trigger.name}]...`)
await ow.triggers.update(trigger)
logger(`Info: trigger [${trigger.name}] has been successfully deployed.\n`)
if (trigger.trigger.feed) {
try {
// ow.feeds.update on a non-existent feed throws 502 (Bad Gateway) --> "Response Missing Error Message."
await ow.feeds.create({ name: trigger.trigger.feed, trigger: trigger.name, params: createKeyValueObjectFromArray(trigger.trigger.parameters) })
} catch (err) {
await ow.triggers.delete({ name: trigger.name })
logger(`Info: feed for trigger [${trigger.name}] could not be deployed.\n`)
logger(`Info: Deleted trigger`)
}
}
}
for (const rule of entities.rules) {
logger(`Info: Deploying rule [${rule.name}]...`)
Expand All @@ -897,14 +863,6 @@ async function undeployPackage (entities, ow, logger) {
}
for (const trigger of entities.triggers) {
logger(`Info: Undeploying trigger [${trigger.name}]...`)
const retTrigger = await ow.triggers.get({ name: trigger.name })
if (retTrigger.annotations) {
for (const annotation of retTrigger.annotations) {
if (annotation.key === 'feed') {
await ow.feeds.delete({ name: annotation.value, trigger: trigger.name })
}
}
}
await ow.triggers.delete({ name: trigger.name })
logger(`Info: trigger [${trigger.name}] has been successfully undeployed.\n`)
}
Expand Down Expand Up @@ -1134,7 +1092,6 @@ module.exports = {
createKeyValueArrayFromObject,
createKeyValueArrayFromFile,
createKeyValueArrayFromFlag,
createKeyValueObjectFromArray,
createKeyValueObjectFromFlag,
createKeyValueObjectFromFile,
parsePathPattern,
Expand Down
16 changes: 2 additions & 14 deletions test/commands/runtime/deploy/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const owPackage = 'packages.update'
const owAction = 'actions.update'
const owAPI = 'routes.create'
const owTriggers = 'triggers.update'
const owFeeds = 'feeds.create'
const owRules = 'rules.update'

test('exports', async () => {
Expand Down Expand Up @@ -632,23 +631,12 @@ describe('instance methods', () => {
})

test('deploys trigger with feed in manifest file', () => {
const cmd = ow.mockResolved(owFeeds, '')
command.argv = ['-m', '/deploy/manifest_dep_Triggers_feeds.yaml']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalled()
expect(stdout.output).toMatch('')
})
})

test('deploys trigger with feed in manifest file - error', () => {
const cmd = ow.mockRejected(owFeeds, new Error('an error'))
const cmdDelete = ow.mockResolved('triggers.delete', '')
const cmd = ow.mockResolved(owTriggers, '')
command.argv = ['-m', '/deploy/manifest_dep_Triggers_feeds.yaml']
return command.run()
.then(() => {
expect(cmd).toHaveBeenCalled()
expect(cmdDelete).toHaveBeenCalled()
expect(cmd).toHaveBeenCalledWith(expect.objectContaining({ trigger: expect.objectContaining({ feed: '/whisk.system/alarms/alarm' }) }))
expect(stdout.output).toMatch('')
})
})
Expand Down
2 changes: 2 additions & 0 deletions test/commands/runtime/trigger/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('instance methods', () => {
value: '/whisk.system/alarms/alarm'
}
],
feed: '/whisk.system/alarms/alarm',
parameters: [
{
key: 'cron',
Expand Down Expand Up @@ -174,6 +175,7 @@ describe('instance methods', () => {
value: '/whisk.system/alarms/alarm'
}
],
feed: '/whisk.system/alarms/alarm',
parameters: [
{
key: 'cron',
Expand Down
26 changes: 0 additions & 26 deletions test/commands/runtime/trigger/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,6 @@ describe('instance methods', () => {
})
})

test('update a simple trigger, use feed flag', () => {
const cmd = ow.mockResolved(owAction, '')
const cmdfeed = ow.mockResolved('feeds.update', '')
command.argv = ['trigger1', '--feed', '/whisk.system/alarms/alarm', '--param', 'cron', '* * * * *']
return command.run()
.then(() => {
expect(cmdfeed).toHaveBeenCalledWith({ name: '/whisk.system/alarms/alarm',
trigger: 'trigger1',
params: {
cron: '* * * * *'
}
})
expect(cmd).toHaveBeenCalledWith({ name: 'trigger1',
trigger: {
parameters: [
{
key: 'cron',
value: '* * * * *'
}
]
}
})
expect(stdout.output).toMatch('')
})
})

test('update a simple trigger, use param flag', () => {
const cmd = ow.mockResolved(owAction, '')
command.argv = ['trigger1', '--param', 'a', 'b', '--param', 'c', 'd']
Expand Down
11 changes: 0 additions & 11 deletions test/runtime-helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ describe('createKeyValueObjectFromFlag', () => {
})
})

describe('createKeyValueObjectFromArray', () => {
test('fail when array item does not have key or value', () => {
const func = () => TheHelper.createKeyValueObjectFromArray([{}])
expect(func).toThrow(new Error('Please provide correct input array with key and value params in each array item'))
})
test('array of key:value (string) pairs', () => {
const res = TheHelper.createKeyValueObjectFromArray([{ key: 'key1', value: 'val2' }])
expect(res).toMatchObject({ key1: 'val2' })
})
})

describe('createKeyValueArrayFromFile', () => {
test('array of key:value pairs', () => {
const res = TheHelper.createKeyValueArrayFromFile('/file.json')
Expand Down

0 comments on commit b939cc5

Please sign in to comment.