Skip to content

Commit

Permalink
DEVX-1244: App Builder experience for Configurable Logging (#254)
Browse files Browse the repository at this point in the history
- adapt generic LogForwarding API

Co-authored-by: Jesse MacFadyen <purplecabbage@gmail.com>
  • Loading branch information
buskamuza and purplecabbage authored Jan 7, 2022
1 parent 90f227e commit 79b3602
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 106 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,13 @@ OPTIONS
--version Show version
ALIASES
$ aio runtime:lf
$ aio rt:log-forwarding
$ aio rt:lf
$ aio runtime:ns:log-forwarding
$ aio runtime:ns:lf
$ aio runtime:namespace:lf
$ aio rt:namespace:log-forwarding
$ aio rt:namespace:lf
$ aio rt:ns:log-forwarding
$ aio rt:ns:lf
```

_See code: [src/commands/runtime/namespace/log-forwarding/index.js](https://github.com/adobe/aio-cli-plugin-runtime/blob/5.1.0/src/commands/runtime/namespace/log-forwarding/index.js)_
Expand Down
84 changes: 14 additions & 70 deletions src/commands/runtime/namespace/log-forwarding/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,26 @@ const RuntimeBaseCommand = require('../../../../RuntimeBaseCommand')

class SetCommand extends RuntimeBaseCommand {
async run () {
const responses = await inquirer.prompt([{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: [{ name: 'Adobe I/O Runtime', value: 'adobe_io_runtime' }, { name: 'Azure Log Analytics', value: 'azure_log_analytics' }, { name: 'Splunk HEC', value: 'splunk_hec' }]
}])
const type = responses.type
if (this['set_' + type] === undefined) {
throw new Error(`Unsupported destination type: '${type}'`)
}
const ow = await this.wsk()
const logForwarding = ow.logForwarding
const destination = await this.promptDestination(logForwarding.getSupportedDestinations())
const settings = await inquirer.prompt(logForwarding.getDestinationSettings(destination))
try {
await this['set_' + type](ow.logForwarding)
this.log(`Log forwarding was set to ${type} for this namespace`)
await logForwarding.setDestination(destination, settings)
this.log(`Log forwarding was set to ${destination} for this namespace`)
} catch (e) {
this.handleError('failed to update log forwarding configuration', e)
this.handleError('Failed to update log forwarding configuration', e)
}
}

// eslint-disable-next-line camelcase
async set_adobe_io_runtime (logForwarding) {
await logForwarding.setAdobeIoRuntime()
}

// eslint-disable-next-line camelcase
async set_azure_log_analytics (logForwarding) {
const responses = await inquirer.prompt([
{
name: 'customer_id',
message: 'customer ID'
},
{
name: 'shared_key',
message: 'shared key',
type: 'password'
},
{
name: 'log_type',
message: 'log type'
}
])

await logForwarding.setAzureLogAnalytics(
responses.customer_id,
responses.shared_key,
responses.log_type
)
}

// eslint-disable-next-line camelcase
async set_splunk_hec (logForwarding) {
const responses = await inquirer.prompt([
{
name: 'host',
message: 'host'
},
{
name: 'port',
message: 'port'
},
{
name: 'index',
message: 'index'
},
{
name: 'hec_token',
message: 'hec_token',
type: 'password'
}
])
await logForwarding.setSplunkHec(
responses.host,
responses.port,
responses.index,
responses.hec_token
)
async promptDestination (supportedDestinations) {
const responses = await inquirer.prompt([{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: supportedDestinations
}])
return responses.type
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AdobeIoRuntimeCommand extends RuntimeBaseCommand {
async run () {
const ow = await this.wsk()
try {
await ow.logForwarding.setAdobeIoRuntime()
await ow.logForwarding.setDestination('adobe_io_runtime', {})
this.log(`Log forwarding was set to adobe_io_runtime for this namespace`)
} catch (e) {
this.handleError('failed to update log forwarding configuration', e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class AzureLogAnalyticsCommand extends RuntimeBaseCommand {
const { flags } = this.parse(AzureLogAnalyticsCommand)
const ow = await this.wsk()
try {
await ow.logForwarding.setAzureLogAnalytics(
flags['customer-id'],
flags['shared-key'],
flags['log-type']
)
await ow.logForwarding.setDestination('azure_log_analytics', {
customer_id: flags['customer-id'],
shared_key: flags['shared-key'],
log_type: flags['log-type']
})
this.log(`Log forwarding was set to azure_log_analytics for this namespace`)
} catch (e) {
this.handleError('failed to update log forwarding configuration', e)
Expand Down
12 changes: 6 additions & 6 deletions src/commands/runtime/namespace/log-forwarding/set/splunk-hec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class SplunkHecCommand extends RuntimeBaseCommand {
const { flags } = this.parse(SplunkHecCommand)
const ow = await this.wsk()
try {
await ow.logForwarding.setSplunkHec(
flags.host,
flags.port,
flags.index,
flags['hec-token']
)
await ow.logForwarding.setDestination('splunk_hec', {
host: flags.host,
port: flags.port,
index: flags.index,
hec_token: flags['hec-token']
})
this.log(`Log forwarding was set to splunk_hec for this namespace`)
} catch (e) {
this.handleError('failed to update log forwarding configuration', e)
Expand Down
27 changes: 14 additions & 13 deletions test/commands/runtime/namespace/log-forwarding/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const { stdout } = require('stdout-stderr')
jest.mock('inquirer')

const dataFixtures = [
['adobe_io_runtime', 'setAdobeIoRuntime', undefined],
['azure_log_analytics', 'setAzureLogAnalytics', {
['adobe_io_runtime', undefined],
['azure_log_analytics', {
customer_id: 'customer1',
shared_key: 'key1',
log_type: 'mylog'
}],
['splunk_hec', 'setSplunkHec', {
['splunk_hec', {
host: 'host1',
port: 'port1',
index: 'index1',
Expand All @@ -40,28 +40,29 @@ beforeEach(async () => {
prompt = jest.fn()
inquirer.prompt = prompt
rtLib = await RuntimeLib.init({ apihost: 'fakehost', api_key: 'fakekey' })
rtLib.logForwarding.getSupportedDestinations = jest.fn().mockReturnValue([{ value: 'destination', name: 'Destination' }])
rtLib.logForwarding.getDestinationSettings = jest.fn().mockReturnValue({ key: 'value' })
})

test('choices contain all supported log destinations', () => {
return new Promise(resolve => {
prompt.mockResolvedValueOnce({ type: 'something' })
return command.run()
.catch((e) => {
expect(e.message).toMatch("Unsupported destination type: 'something'")
expect(prompt).toHaveBeenNthCalledWith(1, [{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: [{ name: 'Adobe I/O Runtime', value: 'adobe_io_runtime' }, { name: 'Azure Log Analytics', value: 'azure_log_analytics' }, { name: 'Splunk HEC', value: 'splunk_hec' }]
choices: [{ name: 'Destination', value: 'destination' }]
}])
resolve()
})
})
})

test.each(dataFixtures)('set log forwarding settings to %s (interactive)', async (destination, fnName, input) => {
test.each(dataFixtures)('set log forwarding settings to %s (interactive)', async (destination, input) => {
return new Promise(resolve => {
mockSelectedDestination(destination, fnName, setCall)
mockSelectedDestination(destination, setCall)
if (input !== undefined) {
mockDestinationConfig(input)
}
Expand All @@ -70,24 +71,24 @@ test.each(dataFixtures)('set log forwarding settings to %s (interactive)', async
expect(stdout.output).toMatch(`Log forwarding was set to ${destination} for this namespace`)
expect(setCall).toBeCalledTimes(1)
if (input !== undefined) {
expect(setCall).toHaveBeenCalledWith(...Object.values(input))
expect(setCall).toHaveBeenCalledWith(destination, input)
}
resolve()
})
})
})

test.each(dataFixtures)('failed to set log forwarding settings to %s (interactive)', async (destination, fnName, input) => {
mockSelectedDestination(destination, fnName, jest.fn().mockRejectedValue(new Error(`mocked error for ${destination}`)))
test.each(dataFixtures)('failed to set log forwarding settings to %s (interactive)', async (destination, input) => {
mockSelectedDestination(destination, jest.fn().mockRejectedValue(new Error(`mocked error for ${destination}`)))
if (input !== undefined) {
mockDestinationConfig(input)
}
await expect(command.run()).rejects.toThrow(`failed to update log forwarding configuration: mocked error for ${destination}`)
await expect(command.run()).rejects.toThrow(`Failed to update log forwarding configuration: mocked error for ${destination}`)
})

function mockSelectedDestination (dstName, fnName, fnCallback) {
function mockSelectedDestination (dstName, fnCallback) {
prompt.mockResolvedValueOnce({ type: dstName })
rtLib.logForwarding[fnName] = fnCallback
rtLib.logForwarding.setDestination = fnCallback
}

function mockDestinationConfig (config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ beforeEach(async () => {
test('set log forwarding settings to adobe_io_runtime', () => {
return new Promise(resolve => {
const setCall = jest.fn()
rtLib.logForwarding.setAdobeIoRuntime = setCall
rtLib.logForwarding.setDestination = setCall
return command.run()
.then(() => {
expect(stdout.output).toMatch(/Log forwarding was set to adobe_io_runtime for this namespace/)
expect(setCall).toBeCalledTimes(1)
expect(setCall).toBeCalledWith('adobe_io_runtime', {})
resolve()
})
})
})

test('failed to set log forwarding settings to adobe_io_runtime', async () => {
rtLib.logForwarding.setAdobeIoRuntime = jest.fn().mockRejectedValue(new Error('mocked error'))
rtLib.logForwarding.setDestination = jest.fn().mockRejectedValue(new Error('mocked error'))
await expect(command.run()).rejects.toThrow(`failed to update log forwarding configuration: mocked error`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ beforeEach(async () => {
test('set log forwarding settings to azure_log_analytics', () => {
return new Promise(resolve => {
const setCall = jest.fn()
rtLib.logForwarding.setAzureLogAnalytics = setCall
rtLib.logForwarding.setDestination = setCall
command.argv = ['--customer-id', 'customer1', '--shared-key', 'key1', '--log-type', 'mylog']
return command.run()
.then(() => {
expect(stdout.output).toMatch(/Log forwarding was set to azure_log_analytics for this namespace/)
expect(setCall).toBeCalledTimes(1)
expect(setCall).toHaveBeenCalledWith('customer1', 'key1', 'mylog')
expect(setCall).toHaveBeenCalledWith('azure_log_analytics', { customer_id: 'customer1', shared_key: 'key1', log_type: 'mylog' })
resolve()
})
})
})

test('failed to set log forwarding settings to azure_log_analytics', async () => {
rtLib.logForwarding.setAzureLogAnalytics = jest.fn().mockRejectedValue(new Error('mocked error'))
rtLib.logForwarding.setDestination = jest.fn().mockRejectedValue(new Error('mocked error'))
command.argv = ['--customer-id', 'customer1', '--shared-key', 'key1', '--log-type', 'mylog']
await expect(command.run()).rejects.toThrow(`failed to update log forwarding configuration: mocked error`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ beforeEach(async () => {
test('set log forwarding settings to splunk_hec', () => {
return new Promise(resolve => {
const setCall = jest.fn()
rtLib.logForwarding.setSplunkHec = setCall
rtLib.logForwarding.setDestination = setCall
command.argv = ['--host', 'host1', '--port', 'port1', '--index', 'index1', '--hec-token', 'token1']
return command.run()
.then(() => {
expect(stdout.output).toMatch(/Log forwarding was set to splunk_hec for this namespace/)
expect(setCall).toBeCalledTimes(1)
expect(setCall).toHaveBeenCalledWith('host1', 'port1', 'index1', 'token1')
expect(setCall).toHaveBeenCalledWith('splunk_hec', { host: 'host1', port: 'port1', index: 'index1', hec_token: 'token1' })
resolve()
})
})
})

test('failed to set log forwarding settings to splunk_hec', async () => {
rtLib.logForwarding.setSplunkHec = jest.fn().mockRejectedValue(new Error('mocked error'))
rtLib.logForwarding.setDestination = jest.fn().mockRejectedValue(new Error('mocked error'))
command.argv = ['--host', 'host1', '--port', 'port1', '--index', 'index1', '--hec-token', 'token1']
await expect(command.run()).rejects.toThrow(`failed to update log forwarding configuration: mocked error`)
})

0 comments on commit 79b3602

Please sign in to comment.