Skip to content

Commit

Permalink
add more cache commands: get, list clear
Browse files Browse the repository at this point in the history
also tests for same
  • Loading branch information
DinoChiesa committed May 6, 2022
1 parent e63b160 commit cb8ca59
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 183 deletions.
36 changes: 36 additions & 0 deletions lib/commands/clearCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const util = require('util'),
defaults = require('../defaults'),
command_utils = require('./command-utils');

var descriptor = defaults.defaultDescriptor({
environment: {
name: 'Environment',
shortOption: 'e',
required: true,
prompt: true
},
cache: {
name: 'Cache Resource',
shortOption: 'z',
required: true
}
});

module.exports.descriptor = descriptor;

module.exports.run = function(opts, cb) {
if (opts.debug) {
console.log('clearCache: %j', opts);
}
var uri = util.format('%s/v1/o/%s/e/%s/caches/%s/entries?action=clear',
opts.baseuri, opts.organization, opts.environment, opts.cache);
let requestOptions = {
uri,
method: 'POST',
json: true,
body: '',
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json' }
};
command_utils.run('clearCache', opts, requestOptions, cb);
};
44 changes: 30 additions & 14 deletions lib/commands/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,57 +102,73 @@ const Commands = {
description: 'Delete undeployed Proxy or Node.js Application',
load: () => require('./delete')
},
createcache: {
createCache: {
description: 'Create a new Cache Resource in the environment',
load: () => require('./createcache')
load: () => require('./createCache')
},
deletecache: {
getCache: {
description: 'Get a KVM map',
load: () => require('./getCache')
},
deleteCache: {
description: 'Deletes a Cache Resource in the environment',
load: () => require('./deletecache')
load: () => require('./deleteCache')
},
listCaches: {
description: 'Lists Cache Resources in the environment',
load: () => require('./listCaches')
},
clearCache: {
description: 'Clears the entries in a named Cache Resource in the environment',
load: () => require('./clearCache')
},
createProduct: {
description: 'Create a new API Product',
load: () => require('./createProduct.js')
load: () => require('./createProduct')
},
deleteProduct: {
description: 'Delete a API Product',
load: () => require('./deleteProduct.js')
load: () => require('./deleteProduct')
},
createDeveloper: {
description: 'Create a new Developer',
load: () => require('./createDeveloper.js')
load: () => require('./createDeveloper')
},
deleteDeveloper: {
description: 'Delete a Developer',
load: () => require('./deleteDeveloper.js')
load: () => require('./deleteDeveloper')
},
createApp: {
description: 'Create a new App',
load: () => require('./createApp.js')
load: () => require('./createApp')
},
createAppKey: {
description: 'Create a new App',
load: () => require('./createAppKey')
},
deleteApp: {
description: 'Delete a App',
load: () => require('./deleteApp.js')
load: () => require('./deleteApp')
},
deploySharedflow: {
description: 'Deploy SharedFlow',
load: () => require('./deploySharedflow.js')
load: () => require('./deploySharedflow')
},
undeploySharedflow: {
description: "Undeploy SharedFlow",
load: () => require('./undeploySharedflow.js')
load: () => require('./undeploySharedflow')
},
fetchSharedflow: {
description: "Download SharedFlow bundle",
load: () => require('./fetchSharedflow.js')
load: () => require('./fetchSharedflow')
},
listSharedflowDeployments: {
description: "List SharedFlow deployments",
load: () => require('./listSharedflowDeployments')
},
deleteSharedflow: {
description: "Delete undeployed SharedFlow",
load: () => require('./deleteSharedflow.js')
load: () => require('./deleteSharedflow')
},
deployExistingRevision: {
description: "Deploy an existing revision to an environment",
Expand Down
148 changes: 148 additions & 0 deletions lib/commands/createAppKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const util = require('util'),
defaults = require('../defaults'),
options = require('../options');

var descriptor = defaults.defaultDescriptor({
developerId: {
name: 'Developer Id',
required: true
},
appName: {
name: 'App Name',
required: true
},
key: {
name: 'Client Key',
required: true
},
secret: {
name: 'Client secret',
required: true
},
apiProducts : {
name: 'API Products',
required: true
}

});

module.exports.descriptor = descriptor;

module.exports.run = function(opts, cb) {
options.validateSync(opts, descriptor);
if (opts.debug) {
console.log('createAppKey: %j', opts);
}
var request = defaults.defaultRequest(opts);
createAppKey(opts, request, function(err, results) {
if (err) {
cb(err);
}
else {
if (opts.debug) {
console.log('results: %j', results);
}
associateKeySecret(opts, request, function(err, results) {
if (err) {
cb(err);
} else {
if (opts.debug) {
console.log('results: %j', results);
}
cb(undefined, results);
}
});
}
});
};

function createAppKey(opts,request,done) {
var payload = {
consumerKey : opts.key,
consumerSecret : opts.secret
};

var uri = util.format('%s/v1/o/%s/developers/%s/apps/%s/keys/create',
opts.baseuri, opts.organization, opts.developerId, opts.appName);
request({
uri: uri,
method:'POST',
body: payload,
json:true
},function(err,res,body){
var jsonBody = body;
if(err){
if (opts.debug) {
console.log('Error occured %s', err);
}
done(err);
}else if (res.statusCode === 201) {
if (opts.verbose) {
console.log('Create KeySecret successful');
}
if (opts.debug) {
console.log('%s', body);
}
done(undefined, jsonBody);
}else {
if (opts.verbose) {
console.error('Create KeySecret result: %j', body);
}
var errMsg;
if (jsonBody && (jsonBody.message)) {
errMsg = jsonBody.message;
} else {
errMsg = util.format('Create KeySecret failed with status code %d', res.statusCode);
}
done(new Error(errMsg));
}
});
}

function associateKeySecret(opts,request,done) {
var prods = opts.apiProducts;
if (typeof prods === 'string') {
prods = [prods];
}

var payload = {
apiProducts : prods
};

var uri = util.format('%s/v1/o/%s/developers/%s/apps/%s/keys/%s',
opts.baseuri, opts.organization, opts.developerId, opts.appName, opts.key);
request({
uri: uri,
method:'POST',
body: payload,
json:true
},function(err,res,body){
var jsonBody = body;
if(err){
if (opts.debug) {
console.log('Error occured %s', err);
}
done(err);
}
else if (res.statusCode === 200) {
if (opts.verbose) {
console.log('Associate KeySecret successful');
}
if (opts.debug) {
console.log('%s', body);
}
done(undefined, jsonBody);
}else {
if (opts.verbose) {
console.error('Associate KeySecret result: %j', body);
}
var errMsg;
if (jsonBody && (jsonBody.message)) {
errMsg = jsonBody.message;
} else {
errMsg = util.format('Associate KeySecret failed with status code %d', res.statusCode);
}
done(new Error(errMsg));
}
});
}
File renamed without changes.
Loading

0 comments on commit cb8ca59

Please sign in to comment.