apiconnect-cli-publish-bluemix
Version:
Plugin for IBM API Connect Developer Toolkit
151 lines (130 loc) • 4.18 kB
JavaScript
/********************************************************* {COPYRIGHT-TOP} ***
* Licensed Materials - Property of IBM
* 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
*
* (C) Copyright IBM Corporation 2016, 2018
*
* All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
********************************************************** {COPYRIGHT-END} **/
// Node module: apiconnect-cli-publish-bluemix
;
var fs = require('fs');
var rp = require('request-promise');
var bluemixRuntime = module.exports;
/** ---------------- Deploy the runtime to BlueMix and get the route returned -------------------- */
bluemixRuntime.createApp = function(options) {
/** =================== file stream to read in Zip ====================== */
var applicationStream = fs.createReadStream(options.zipFilePath);
var fileName = options.appName + '_runtime';
/** =================== Make the request to the RTM ====================== */
var opt = {
url: [ getAppUrl(options.deployer), options.appName ].join('/'),
qs: {},
headers: {
authorization: getAuthorization(options.encryptedToken),
'content-type': 'multipart/form-data',
},
method: 'PUT',
json: true,
formData: {
projectType: options.projectType,
org: options.bmOrgName,
space: options.bmSpaceName,
providerAccount: options.providerAccount,
application: {
value: applicationStream,
options: {
filename: fileName,
contentType: 'application/zip',
},
},
},
};
/** =================== Set the flag for UI ====================== */
if (options.ui) {
opt.qs.guid = true;
}
/** =================== Set the flag for Versioning ============== */
if (!(options.inplaceUpdate)) {
opt.qs.inplaceUpdate = false;
if (options.newName !== null) {
opt.formData.newAppName = options.newName;
}
opt.formData.removeOldRoute = 'true';
}
/** =================== Send request to RTM ====================== */
return rp(opt).then(function(body) {
return body;
}).catch(function(error) {
return error.error;
});
};
bluemixRuntime.getBluemixAPIHost = function(url, token) {
var options = {
url: [ getInfoUrl(url), 'bluemixAPIHost' ].join('/'),
headers: {
authorization: getAuthorization(token),
},
json: true,
method: 'GET',
};
return rp(options).then(function(body) {
if (body.BLUEMIX_API_HOST) {
return body.BLUEMIX_API_HOST;
} else {
return bluemixRuntime.getRegion(url, token).then(function(region) {
return bluemixRuntime.getBluemixAPIHostByRegion(url, region, token);
});
}
});
};
bluemixRuntime.getBluemixAPIHostByRegion = function(url, region, token) {
var options = {
url: [ getInfoUrl(url), 'bluemixAPIHost' ].join('/'),
headers: {
authorization: getAuthorization(token),
},
qs: {
q: { bmInstance: region },
},
json: true,
method: 'GET',
};
return rp(options).then(function(body) {
return body[region];
});
};
bluemixRuntime.getRegion = function(url, token) {
var options = {
url: [ getInfoUrl(url), 'region' ].join('/'),
headers: {
authorization: getAuthorization(token),
},
json: true,
method: 'GET',
};
return rp(options).then(function(body) {
return body.region;
});
};
bluemixRuntime.getBaseUrl = function(host) {
var baseUrl;
if (host.indexOf('apiconnect.ibmcloud.com') === -1 && host.indexOf('apim.ibmcloud.com') === -1
&& host.indexOf('apiconnect.cloud.ibm.com') === -1 && host.indexOf('apiconnect.test.cloud.ibm.com') === -1) {
baseUrl = 'https://deployer-service-apiconnect' + host.substr(host.indexOf('.'), host.length);
} else {
baseUrl = 'https://deployer.service.' + host.replace(/apimanager./ig, '');
}
return baseUrl;
};
function getAuthorization(token) {
return [ 'bearer', token ].join(' ');
}
function getInfoUrl(url) {
return [ url, 'v1', 'info' ].join('/');
}
function getAppUrl(url) {
return [ url, 'v1', 'cf-app' ].join('/');
}