Skip to content

Commit

Permalink
Merge pull request #56 from barracksiot/BO-1555
Browse files Browse the repository at this point in the history
Bo 1555 - Add get and update hook commands
  • Loading branch information
remiriv committed Jul 26, 2017
2 parents 5178651 + 13c439a commit 63736eb
Show file tree
Hide file tree
Showing 14 changed files with 698 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lint": "jshint src/**/**.js && jshint tests/**/**.js",
"test": "npm run lint && npm run coverage && npm run check-coverage",
"coverage": "DEBUG=true istanbul cover ./node_modules/mocha/bin/_mocha ./tests/ --recursive -- --recursive",
"check-coverage": "istanbul check-coverage --statement 95 --branch 95 --function 95",
"check-coverage": "istanbul check-coverage --statement 98 --branch 95 --function 95",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},
"bin": {
Expand Down
2 changes: 2 additions & 0 deletions src/bin/barracks-hook
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const pjson = require('../../package.json');
const barracks = program
.version(pjson.version)
.command('create', 'Manage hooks creation')
.command('get', 'Display detailed information of a hook')
.command('update', 'Update a hook')
.command('ls', 'List the hooks')
.command('rm', 'Delete hook');

Expand Down
4 changes: 4 additions & 0 deletions src/bin/barracks-hook-get
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

const GetHookCommand = require('../commands/hook/GetHookCommand');
new GetHookCommand().render();
4 changes: 4 additions & 0 deletions src/bin/barracks-hook-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

const UpdateHook = require('../commands/hook/UpdateHookCommand');
new UpdateHook().render();
4 changes: 3 additions & 1 deletion src/clients/BarracksClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ function mergeUpdateClient(barracksClient) {
function mergeHookClient(barracksClient) {
const hookClient = new HookClient();
barracksClient.createHook = hookClient.createHook.bind(hookClient);
barracksClient.deleteHook = hookClient.deleteHook.bind(hookClient);
barracksClient.getHook = hookClient.getHook.bind(hookClient);
barracksClient.getHooks = hookClient.getHooks.bind(hookClient);
barracksClient.updateHook = hookClient.updateHook.bind(hookClient);
barracksClient.deleteHook = hookClient.deleteHook.bind(hookClient);
}

function mergeSDKProxy(barracksClient) {
Expand Down
3 changes: 1 addition & 2 deletions src/clients/FilterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ class FilterClient {

updateFilter(token, filter) {
return new Promise((resolve, reject) => {
const endpointKey = 'updateFilter';
this.httpClient.sendEndpointRequest(
endpoints[endpointKey],
endpoints.updateFilter,
{
headers: {
'x-auth-token': token
Expand Down
1 change: 0 additions & 1 deletion src/clients/HTTPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class HTTPClient {
pageableStream.lastPage();
}
}

}

module.exports = HTTPClient;
52 changes: 51 additions & 1 deletion src/clients/HookClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ const endpoints = {
method: 'POST',
path: '/api/dispatcher/hooks'
},
getHook: {
method: 'GET',
path: '/api/dispatcher/hooks/:hook'
},
getHooks: {
method: 'GET',
path: '/api/dispatcher/hooks'
},
updateHook: {
method: 'PUT',
path: '/api/dispatcher/hooks/:hook'
},
deleteHook: {
method: 'DELETE',
path: '/api/dispatcher/hooks/:hook'
Expand Down Expand Up @@ -46,6 +54,27 @@ class HookClient {
});
}

getHook(token, name) {
return new Promise((resolve, reject) => {
logger.debug(`Getting hook ${name}`);
this.httpClient.sendEndpointRequest(
endpoints.getHook,
{
headers: {
'x-auth-token': token
},
pathVariables: {
hook: name
}
}
).then(response => {
resolve(response.body);
}).catch(errResponse => {
reject(errResponse.message);
});
});
}

getHooks(token) {
return new Promise(resolve => {
logger.debug('Getting hooks');
Expand All @@ -59,11 +88,32 @@ class HookClient {
'x-auth-token': token
}
},
'hookEntities'
'hooks'
);
});
}

updateHook(token, name, hook) {
return new Promise((resolve, reject) => {
this.httpClient.sendEndpointRequest(
endpoints.updateHook,
{
headers: {
'x-auth-token': token
},
pathVariables: {
hook: name
},
body: hook
}
).then(response => {
resolve(response.body);
}).catch(err => {
reject(err.message);
});
});
}

deleteHook(token, hook) {
return new Promise((resolve, reject) => {
this.httpClient.sendEndpointRequest(
Expand Down
20 changes: 20 additions & 0 deletions src/commands/hook/GetHookCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const BarracksCommand = require('../BarracksCommand');

class GetHookCommand extends BarracksCommand {

configureCommand(program) {
return program.arguments('<hook-name>');
}

validateCommand(program) {
return !!(program.args && program.args.length === 1);
}

execute(program) {
return this.getAuthenticationToken().then(token => {
return this.barracks.getHook(token, program.args[0]);
});
}
}

module.exports = GetHookCommand;
41 changes: 41 additions & 0 deletions src/commands/hook/UpdateHookCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const BarracksCommand = require('../BarracksCommand');

function buildNewHook(currentHook, program) {
return Object.assign({}, currentHook, {
name: program.newName || currentHook.name,
url: program.url || currentHook.url
});
}

class UpdateHookCommand extends BarracksCommand {

configureCommand(program) {
return program
.option('--name [hookName]', 'The unique name of the webhook')
.option('--newName [newHookName]', '(Optionnal) A new unique name for the webhook')
.option('--url [newUrl]', '(Optionnal) A new URL for this webhook');
}

validateCommand(program) {
return !!(
program.name && program.name !== true &&
(
(program.newName && program.newName !== true) ||
(program.url && program.url !== true)
)
);
}

execute(program) {
let token;
return this.getAuthenticationToken().then(authToken => {
token = authToken;
return this.barracks.getHook(token, program.name);
}).then(hook => {
const newHook = buildNewHook(hook, program);
return this.barracks.updateHook(token, program.name, newHook);
});
}
}

module.exports = UpdateHookCommand;
3 changes: 3 additions & 0 deletions tests/clients/BarracksClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ describe('Barracks', () => {

// Then
expect(barracksClient).to.have.property('createHook').and.to.be.a('function');
expect(barracksClient).to.have.property('getHook').and.to.be.a('function');
expect(barracksClient).to.have.property('getHooks').and.to.be.a('function');
expect(barracksClient).to.have.property('updateHook').and.to.be.a('function');
expect(barracksClient).to.have.property('deleteHook').and.to.be.a('function');
});

Expand Down
Loading

0 comments on commit 63736eb

Please sign in to comment.