This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
217 lines (182 loc) · 5.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/** @module BrokerV2 */
'use strict';
var Db = require('../db');
var Events = require('events');
var Handlers = require('./api-handlers');
var Restify = require('restify');
var RestifyMiddleware = require('../../common/restify-middleware');
var Util = require('util');
var async = require('async');
var _ = require('underscore');
var common = require('../../common');
var log = common.getLogger(common.LOG_CONSTANTS.BROKER);
/**
* The Broker starts a restify broker that talks to the CF cloud controller
* API, primarily for updating the service catalog and service plans.
* @constructor
* @param {Object} opts - Broker options.
*/
var BrokerV2 = function(opts) {
if (!(this instanceof BrokerV2)) {
return new BrokerV2(opts);
}
Events.EventEmitter.call(this);
this.opts = opts;
this.db = new Db(opts.database);
this.restServer = this.createRestServer();
return this;
};
Util.inherits(BrokerV2, Events.EventEmitter);
/**
* Will shutdown the server gracefully.
*
* @this {BrokerV2}
*/
BrokerV2.prototype.stop = function() {
var broker = this;
if (broker.restServer) {
broker.restServer.close();
}
log.info('Broker has been shutdown.');
};
/**
* This is the main entry point to start the broker, after the constructor
* has been instantiated.
*
* @this {BrokerV2}
*/
BrokerV2.prototype.start = function () {
var broker = this;
var server = broker.restServer;
this.db.createSchema(function (err) {
if (err) {
throw new Error(Util.format('Failed to create schema - error: %j', err));
}
// Fix credentials in the database that have been changed in the Server Pool
broker.updateBrokerSQLCrededentials(broker.opts.accountPool.sqldb, function(err){
if (err) {
log.error('Failed to updade SQL credentials in broker db: %j', err);
}
});
server.listen(broker.opts.port, function (error) {
if (error) {
throw new Error(Util.format('Broker REST server failed to listen on port %s : %j', broker.opts.port, error));
}
log.info('%s broker is listening on port %s', server.name, broker.opts.port);
broker.initPeriodicJobs();
log.info('Broker now started with PID: ' + process.pid);
});
});
};
BrokerV2.prototype.updateBrokerSQLCrededentials = function(sqldbpool, callback) {
var broker = this;
var sqldbServiceId = require('../../services/azuresqldb/service.json').id;
async.waterfall([
function(callback){
broker.db.getServiceInstances(callback);
},
function (instances, callback) {
var updateFunctions = [];
_.each(instances, function (instance) {
// Only update instances that are of type sqldb and that are part of the sqldbpool
if (instance['service_id'] === sqldbServiceId && instance['provisioning_result'] && sqldbpool[instance['provisioning_result'].sqlServerName]) {
var currentPassword = instance['provisioning_result'].administratorLoginPassword;
var newPassword = sqldbpool[instance['provisioning_result'].sqlServerName].administratorLoginPassword;
// Only consider update if the password changed
if (newPassword !== currentPassword) {
log.info('Should update instance: %s', instance.azureInstanceId);
updateFunctions.push(async.apply(broker.db.updateSQLPassword.bind(broker.db) , instance, newPassword));
}
}
});
callback(null, updateFunctions);
},
function(updateFunctions, callback) {
async.parallel(updateFunctions,
function(err, result)
{
// result is an array of the results
callback(err);
});
}
],
function(err, result){
callback(err);
});
};
/**
* Creates the REST API server that the cloud controller will use
* to communicate {un}provisioning/{un}binding requests.
*
* See also, Restify on NPM.
*
* @this {BrokerV2}
*/
BrokerV2.prototype.createRestServer = function() {
var broker = this;
var versionNS = '/v' + broker.opts.semver.major;
var server = Restify.createServer({
name: broker.opts.name,
version: broker.opts.apiVersion
});
server.use(RestifyMiddleware.validateAPIVersion(broker.opts.semver));
server.use(Restify.authorizationParser());
server.use(Handlers.authenticate(broker.opts.serviceBroker.credentials));
server.use(Restify.acceptParser(server.acceptable));
server.use(Restify.queryParser());
server.use(Restify.bodyParser());
server.use(RestifyMiddleware.requestLogger({
prefix: 'HTTP Request'
}));
// Catalog
server.get(versionNS + '/catalog',
function(req, res, next) {
Handlers.handleCatalogRequest(broker, req, res, next);
}
);
// Provision
server.put(versionNS + '/service_instances/:instance_id',
function(req, res, next) {
Handlers.handleProvisionRequest(broker, req, res, next);
}
);
// Poll
server.get(versionNS + '/service_instances/:instance_id/last_operation',
function(
req, res, next) {
Handlers.handlePollRequest(broker, req, res, next);
}
);
// Deprovision
server.del(versionNS + '/service_instances/:instance_id',
function(req, res, next) {
Handlers.handleDeProvisionRequest(broker, req, res, next);
}
);
// Bind
server.put(versionNS + '/service_instances/:instance_id/service_bindings/:binding_id',
function(req, res, next) {
Handlers.handleBindRequest(broker, req, res, next);
}
);
// Unbind
server.del(versionNS + '/service_instances/:instance_id/service_bindings/:binding_id',
function(req, res, next) {
Handlers.handleUnbindRequest(broker, req, res, next);
}
);
// Update
server.patch(versionNS + '/service_instances/:instance_id',
function(req, res, next) {
Handlers.handleUpdateRequest(broker, req, res, next);
}
);
return server;
};
/**
* Sets up background jobs to run at predefined intervals.
*
* @this {BrokerV2}
*/
BrokerV2.prototype.initPeriodicJobs = function() {};
module.exports = BrokerV2;