Skip to content

Commit

Permalink
Merge pull request #215 in KSD/cli-appsec from feature/SECKSD-7888 to…
Browse files Browse the repository at this point in the history
… develop

* commit 'e54afa0c1dad79091eb86d63e0e9fafb588f6f73':
  SECKSD-7888 - Incorporating code review comments
  SECKSD-7888 - Remove host/api changes for modify match target.
  • Loading branch information
Otto Merge committed Feb 2, 2021
2 parents 414bdf3 + e54afa0 commit a3c0505
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion bin/commands/apimatchtarget.modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let out = require('./lib/out');
let MatchTarget = require('../../src/matchtarget').matchTarget;
let logger = require('../../src/constants').logger('modify-api-match-target');

const SUB_CPMMANDS = ['add-api'];
const SUB_CPMMANDS = ['add-api', 'remove-api'];
class ModifyAPIMatchTargetCommand {
constructor() {
this.flags = 'modify-api-match-target';
Expand Down Expand Up @@ -55,6 +55,8 @@ class ModifyAPIMatchTargetCommand {
switch (options.subcommand) {
case 'add-api':
return new MatchTarget(options).addApi();
case 'remove-api':
return new MatchTarget(options).removeApi();
default:
return null;
}
Expand Down
8 changes: 6 additions & 2 deletions bin/commands/matchtarget.modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let out = require('./lib/out');
let MatchTarget = require('../../src/matchtarget').matchTarget;
let logger = require('../../src/constants').logger('modify-match-target');

const SUB_CPMMANDS = ['add-hostname'];
const SUB_CPMMANDS = ['add-hostname', 'remove-hostname'];
class ModifyMatchTargetCommand {
constructor() {
this.flags = 'modify-match-target';
Expand Down Expand Up @@ -42,7 +42,9 @@ class ModifyMatchTargetCommand {

run(options) {
logger.debug(JSON.stringify(options));
options.hostnames = [options.hostname];
if (options.hostname) {
options.hostnames = [options.hostname];
}
out.print({
promise: this._getOperation(options),
args: options,
Expand All @@ -56,6 +58,8 @@ class ModifyMatchTargetCommand {
switch (options.subcommand) {
case 'add-hostname':
return new MatchTarget(options).addHostnames();
case 'remove-hostname':
return new MatchTarget(options).removeHostname();
default:
return null;
}
Expand Down
54 changes: 54 additions & 0 deletions src/matchtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ class MatchTarget {
});
}

removeHostname() {
return this._version
.readResource(URIs.MATCH_TARGET, [this._options['match-target']])
.then(matchTarget => {
matchTarget.hostnames = matchTarget.hostnames || [];
let hostExists = false;
for (let i = 0; i < matchTarget.hostnames.length; i++) {
if (matchTarget.hostnames[i] === this._options.hostname) {
matchTarget.hostnames.splice(i, 1);
hostExists = true;
break;
}
}
if (!hostExists) {
throw 'The specified hostname not present in the match target';
}

delete matchTarget.validations;
logger.debug('Updated match target: %s', JSON.stringify(matchTarget));
return this._version.updateResource(
URIs.MATCH_TARGET,
[this._options['match-target']],
matchTarget
);
});
}

addApi() {
return this._version
.readResource(URIs.MATCH_TARGET, [this._options['match-target']])
Expand All @@ -102,6 +129,7 @@ class MatchTarget {
for (let i = 0; i < matchTarget.apis.length; i++) {
if (matchTarget.apis[i].id == this._options.api) {
apiExists = true;
break;
}
}
if (!apiExists) {
Expand All @@ -117,6 +145,32 @@ class MatchTarget {
});
}

removeApi() {
return this._version
.readResource(URIs.MATCH_TARGET, [this._options['match-target']])
.then(matchTarget => {
matchTarget.apis = matchTarget.apis || [];
let apiExists = false;
for (let i = 0; i < matchTarget.apis.length; i++) {
if (matchTarget.apis[i].id == this._options.api) {
matchTarget.apis.splice(i, 1);
apiExists = true;
break;
}
}
if (!apiExists) {
throw 'The specified api not present in the match target';
}
delete matchTarget.validations;
logger.debug('Updated match target: %s', JSON.stringify(matchTarget));
return this._version.updateResource(
URIs.MATCH_TARGET,
[this._options['match-target']],
matchTarget
);
});
}

_updateOrder(targetIdsInOrder) {
let targetSequence = [];
for (let i = 0; i < targetIdsInOrder.length; i++) {
Expand Down

0 comments on commit a3c0505

Please sign in to comment.