Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

feat: refactor to use bluemix object to specify services #372

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 160 additions & 68 deletions app/index.js

Large diffs are not rendered by default.

168 changes: 92 additions & 76 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ function sanitizeCredentialsAndFillInDefaults (serviceType, service) {
password: '',
secured: false
}
if (service.credentials.url) {
var parsedURL = url.parse(service.credentials.url)
if (service.url) {
var parsedURL = url.parse(service.url)
if (parsedURL.host) defaults.host = parsedURL.hostname
if (parsedURL.port) defaults.port = parsedURL.port
if (parsedURL.auth) {
Expand All @@ -393,109 +393,112 @@ function sanitizeCredentialsAndFillInDefaults (serviceType, service) {
defaults.secured = (parsedURL.protocol === 'https')
}
}
if (service.credentials.host) defaults.url.hostname = service.credentials.host
if (service.credentials.port) defaults.url.port = service.credentials.port
if (service.credentials.secured) defaults.url.protocol = 'https'
if (service.credentials.username || service.credentials.password) {
if (service.host) defaults.url.hostname = service.host
if (service.port) defaults.url.port = service.port
if (service.secured) defaults.url.protocol = 'https'
if (service.username || service.password) {
defaults.url.auth = [
service.credentials.username,
service.credentials.password
service.username,
service.password
].join(':')
}
return {
host: service.credentials.host || defaults.host,
url: service.credentials.url || url.format(defaults.url),
username: service.credentials.username || defaults.username,
password: service.credentials.password || defaults.password,
secured: service.credentials.secured || defaults.secured,
port: service.credentials.port || defaults.port
host: service.host || defaults.host,
url: service.url || url.format(defaults.url),
username: service.username || defaults.username,
password: service.password || defaults.password,
secured: service.secured || defaults.secured,
port: service.port || defaults.port
}
case 'redis':
var defaultRedisURI = { protocol: 'redis', auth: ':', hostname: 'localhost', port: 6397, slashes: true }
if (service.credentials.host) defaultRedisURI.hostname = service.credentials.host
if (service.credentials.port) defaultRedisURI.port = service.credentials.port
if (service.credentials.password) defaultRedisURI.auth = `:${service.credentials.password}`
if (service.host) defaultRedisURI.hostname = service.host
if (service.port) defaultRedisURI.port = service.port
if (service.password) defaultRedisURI.auth = `:${service.password}`
return {
uri: service.credentials.uri || url.format(defaultRedisURI)
uri: service.uri || url.format(defaultRedisURI)
}
case 'mongodb':
var defaultMongoURI = { protocol: 'mongodb', hostname: 'localhost', port: 27017, slashes: true }
if (service.credentials.host) defaultMongoURI.hostname = service.credentials.host
if (service.credentials.port) defaultMongoURI.port = service.credentials.port
if (service.credentials.password) defaultMongoURI.auth = `:${service.credentials.password}`
if (service.credentials.database) defaultMongoURI.pathname = service.credentials.database
if (service.host) defaultMongoURI.hostname = service.host
if (service.port) defaultMongoURI.port = service.port
if (service.password) defaultMongoURI.auth = `:${service.password}`
if (service.database) defaultMongoURI.pathname = service.database
return {
uri: service.credentials.uri || url.format(defaultMongoURI)
uri: service.uri || url.format(defaultMongoURI)
}
case 'postgresql':
var defaultPostgresURI = { protocol: 'postgres', hostname: 'localhost', port: 5432, slashes: true }
if (service.credentials.host) defaultPostgresURI.hostname = service.credentials.host
if (service.credentials.port) defaultPostgresURI.port = service.credentials.port
if (service.credentials.username || service.credentials.password) {
if (service.host) defaultPostgresURI.hostname = service.host
if (service.port) defaultPostgresURI.port = service.port
if (service.username || service.password) {
defaultPostgresURI.auth = [
service.credentials.username,
service.credentials.password
service.username,
service.password
].join(':')
}
if (service.credentials.database) defaultPostgresURI.pathname = service.credentials.database
if (service.database) defaultPostgresURI.pathname = service.database
return {
uri: service.credentials.uri || url.format(defaultPostgresURI)
uri: service.uri || url.format(defaultPostgresURI)
}
case 'objectstorage':
case 'objectStorage':
var defaultAuthURL = 'https://identity.open.softlayer.com'
return {
auth_url: service.credentials.auth_url || defaultAuthURL,
project: service.credentials.project || '',
projectId: service.credentials.projectId || '',
region: service.credentials.region || '',
userId: service.credentials.userId || '',
username: service.credentials.username || '',
password: service.credentials.password || '',
domainId: service.credentials.domainId || '',
domainName: service.credentials.domainName || '',
role: service.credentials.role || ''
auth_url: service.auth_url || defaultAuthURL,
project: service.project || '',
projectId: service.projectId || '',
region: service.region || '',
userId: service.userId || '',
username: service.username || '',
password: service.password || '',
domainId: service.domainId || '',
domainName: service.domainName || '',
role: service.role || ''
}
case 'appid':
case 'auth':
return {
clientId: service.credentials.clientId || '',
oauthServerUrl: service.credentials.oauthServerUrl || '',
profilesUrl: service.credentials.profilesUrl || '',
secret: service.credentials.secret || '',
tenantId: service.credentials.tenantId || ''
clientId: service.clientId || '',
oauthServerUrl: service.oauthServerUrl || '',
profilesUrl: service.profilesUrl || '',
secret: service.secret || '',
tenantId: service.tenantId || ''
}
case 'watsonconversation':
case 'conversation':
var defaultConversationURL = 'https://gateway.watsonplatform.net/conversation/api'
return {
url: service.credentials.url || defaultConversationURL,
username: service.credentials.username || '',
password: service.credentials.password || ''
url: service.url || defaultConversationURL,
username: service.username || '',
password: service.password || ''
}
case 'alertnotification':
case 'alertNotification':
return {
url: service.credentials.url || '',
name: service.credentials.name || '',
password: service.credentials.password || ''
url: service.url || '',
name: service.name || '',
password: service.password || ''
}
case 'pushnotifications':
case 'push':
return {
appGuid: service.credentials.appGuid || '',
url: service.credentials.url || '',
admin_url: service.credentials.admin_url || '',
appSecret: service.credentials.appSecret || '',
clientSecret: service.credentials.clientSecret || ''
appGuid: service.appGuid || '',
url: service.url || '',
admin_url: service.admin_url || '',
appSecret: service.appSecret || '',
clientSecret: service.clientSecret || ''
}
case 'autoscaling':
return {}
}
};

exports.sanitizeServiceAndFillInDefaults = function (serviceType, service) {
return {
name: service.name,
label: service.label || exports.getBluemixServiceLabel(serviceType),
plan: service.plan || exports.getBluemixDefaultPlan(serviceType),
credentials: sanitizeCredentialsAndFillInDefaults(serviceType, service)
var credentials = sanitizeCredentialsAndFillInDefaults(serviceType, service)
var serviceInfo = {
serviceInfo: {
name: service.serviceInfo.name,
label: service.serviceInfo.label || exports.getBluemixServiceLabel(serviceType),
plan: service.serviceInfo.plan || exports.getBluemixDefaultPlan(serviceType)
}
}
return Object.assign(credentials, serviceInfo)
}

exports.getBluemixServiceLabel = function (serviceType) {
Expand All @@ -504,11 +507,11 @@ exports.getBluemixServiceLabel = function (serviceType) {
case 'redis': return 'compose-for-redis'
case 'mongodb': return 'compose-for-mongodb'
case 'postgresql': return 'compose-for-postgresql'
case 'objectstorage': return 'Object-Storage'
case 'appid': return 'AppID'
case 'watsonconversation': return 'conversation'
case 'alertnotification': return 'AlertNotification'
case 'pushnotifications': return 'imfpush'
case 'objectStorage': return 'Object-Storage'
case 'auth': return 'AppID'
case 'conversation': return 'conversation'
case 'alertNotification': return 'AlertNotification'
case 'push': return 'imfpush'
case 'autoscaling': return 'Auto-Scaling'
default: return serviceType
}
Expand All @@ -520,11 +523,11 @@ exports.getBluemixDefaultPlan = function (serviceType) {
case 'redis': return 'Standard'
case 'mongodb': return 'Standard'
case 'postgresql': return 'Standard'
case 'objectstorage': return 'Free'
case 'appid': return 'Graduated tier'
case 'watsonconversation': return 'free'
case 'alertnotification': return 'authorizedusers'
case 'pushnotifications': return 'lite'
case 'objectStorage': return 'Free'
case 'auth': return 'Graduated tier'
case 'conversation': return 'free'
case 'alertNotification': return 'authorizedusers'
case 'push': return 'lite'
case 'autoscaling': return 'free'
default: return 'Lite'
}
Expand Down Expand Up @@ -573,3 +576,16 @@ exports.resourceNameFromPath = function (thepath) {
var resource = thepath.match(/^\/*([^/]+)/)[1]
return resource.charAt(0).toUpperCase() + resource.slice(1)
}

exports.isThisServiceAnArray = function (serviceType) {
return (serviceType === 'cloudant' || serviceType === 'objectStorage')
}

exports.validateServiceFields = function (serviceType, service) {
if (!service.serviceInfo.name) {
throw new Error(chalk.red(`Service name is missing in spec for bluemix.${serviceType}`))
}
if (typeof (service.serviceInfo.name) !== 'string') {
throw new Error(chalk.red(`Ensure Type of Service name in spec for bluemix.${serviceType}`))
}
}
Loading