/
index.js
153 lines (120 loc) · 4.11 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
/* jshint camelcase: false */
/* jshint newcap: false */
'use strict';
var HttpStatus = require('http-status-codes');
var common = require('../../common/');
var Config = require('./service');
var log = common.getLogger(Config.name);
var cmdPoll = require('./cmd-poll');
var cmdProvision = require('./cmd-provision');
var cmdUpdate = require('./cmd-update');
var cmdDeprovision = require('./cmd-deprovision');
var cmdBind = require('./cmd-bind');
var redisClient = require('./client');
var Reply = require('../../common/reply');
// Space Scoping if enabled needs unique names in multi-tenant environments
Config = common.fixNamesIfSpaceScopingEnabled(Config);
var Handlers = {};
Handlers.fixParameters = function(parameters) {
parameters = common.fixParametersWithDefaults('DEFAULT_PARAMETERS_AZURE_REDISCACHE', parameters);
if (process.env['ALLOW_TO_GENERATE_NAMES_AND_PASSWORDS_FOR_THE_MISSING'] === 'true') {
if (!parameters.cacheName) parameters.cacheName = common.generateName();
}
return parameters;
};
Handlers.catalog = function (params, next) {
var availableEnvironments = ['AzureCloud', 'AzureGermanCloud', 'AzureChinaCloud', 'AzureUSGovernment'];
if (availableEnvironments.indexOf(params.azure.environment) === -1) {
return next(null);
}
var reply = Config;
next(null, reply);
};
Handlers.generateAzureInstanceId = function(params) {
return Config.name + '-' + params.parameters['cacheName'];
};
Handlers.provision = function (params, next) {
log.debug('Redis Cache/index/provision/params: %j', params);
var cp = new cmdProvision(params);
if (!cp.allValidatorsSucceed()) {
return common.handleServiceErrorEx(HttpStatus.BAD_REQUEST, 'Parameter validation failed. Did you supply a redis cache parameters file?', next);
}
redisClient.initialize(params.azure);
cp.provision(redisClient, function (err, result) {
if (err) {
common.handleServiceError(err, next);
} else {
next(null, Reply(HttpStatus.ACCEPTED), result);
}
});
};
Handlers.update = function (params, next) {
log.debug('Redis Cache/index/update/params: %j', params);
var cu = new cmdUpdate(params);
redisClient.initialize(params.azure);
cu.update(redisClient, function (err, reply, updatedInstance) {
if (err) {
return common.handleServiceError(err, next);
}
log.debug('Redis Cache/index/update/reply: %j', reply);
next(null, reply, updatedInstance);
});
};
Handlers.poll = function (params, next) {
log.debug('Redis Cache/index/poll/params: %j', params);
var lastOperation = params.last_operation;
var cp = new cmdPoll(params);
redisClient.initialize(params.azure);
cp.poll(redisClient, function (err, reply, result) {
if (err) {
common.handleServiceError(err, function(error) {
next(error, lastOperation);
});
} else {
next(null, lastOperation, reply, result);
}
});
};
Handlers.deprovision = function (params, next) {
log.debug('Redis Cache/index/deprovision/params: %j', params);
var cp = new cmdDeprovision(params);
redisClient.initialize(params.azure);
cp.deprovision(redisClient, function (err, result) {
if (err) {
common.handleServiceError(err, next);
} else {
next(null, Reply(HttpStatus.ACCEPTED), result);
}
});
};
Handlers.bind = function (params, next) {
log.debug('Redis Cache/index/bind/params: %j', params);
var cp = new cmdBind(params);
redisClient.initialize(params.azure);
cp.bind(redisClient, function(err, credentials) {
if (err) {
common.handleServiceError(err, next);
} else {
// contents of reply.value winds up in VCAP_SERVICES
var reply = {
statusCode: HttpStatus.CREATED,
code: HttpStatus.getStatusText(HttpStatus.CREATED),
value: {
credentials: credentials
}
};
next(null, reply, {});
}
});
};
Handlers.unbind = function (params, next) {
log.debug('Redis Cache/index/unbind/params: %j', params);
var reply = {
statusCode: HttpStatus.OK,
code: HttpStatus.getStatusText(HttpStatus.OK),
value: {},
};
var result = {};
next(null, reply, result);
};
module.exports = Handlers;