This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
forked from cloudfoundry/overview-broker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_broker.js
242 lines (223 loc) · 8.08 KB
/
service_broker.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var fs = require('fs'),
cfenv = require('cfenv'),
sha256 = require('sha256'),
validate = require ('jsonschema').validate,
Logger = require('./logger');
class ServiceBroker {
constructor() {
this.logger = new Logger();
let serviceName = process.env.SERVICE_NAME || 'overview-service';
this.catalog = {
services: [
{
name: serviceName,
description: process.env.SERVICE_DESCRIPTION || 'Provides an overview of any service instances and bindings that have been created by a platform.',
id: sha256(serviceName).substring(0, 32),
tags: [ 'overview-broker' ],
bindable: true,
plan_updateable: true,
bindings_retrievable: true,
instances_retrievable: true,
metadata: { shareable: true },
plans: this.generatePlansForService(serviceName),
}
]
};
// Expose a syslog drain service if requested
if (process.env.SYSLOG_DRAIN_URL) {
this.catalog.services.push({
name: serviceName + '-syslog-drain',
description: 'Provides an example syslog drain service.',
id: sha256(serviceName + '-syslog-drain').substring(0, 32),
tags: [ 'overview-broker' ],
requires: [ 'syslog_drain' ],
bindable: true,
bindings_retrievable: true,
instances_retrievable: true,
plan_updateable: true,
plans: this.generatePlansForService(serviceName + '-syslog-drain'),
metadata: { shareable: true }
});
}
// Expose a volume mount service if requested
if (process.env.EXPOSE_VOLUME_MOUNT_SERVICE) {
this.catalog.services.push({
name: serviceName + '-volume-mount',
description: 'Provides an example volume mount service.',
id: sha256(serviceName + '-volume-mount').substring(0, 32),
tags: [ 'overview-broker' ],
requires: [ 'volume_mount' ],
bindable: true,
bindings_retrievable: true,
instances_retrievable: true,
plan_updateable: true,
plans: this.generatePlansForService(serviceName + '-volume-mount'),
metadata: { shareable: true }
});
};
this.dashboardUrl = `${cfenv.getAppEnv().url}/dashboard`;
logger.debug(`Service broker created. (${this.catalog.services.length} service${this.catalog.services.length == 1 ? '' : 's'} exposed)`);
}
getCatalog() {
return this.catalog;
}
setCatalog(data) {
try {
var catalogData = JSON.parse(data);
this.catalog = catalogData;
return null;
}
catch (e) {
return e.toString();
}
}
getDashboardUrl() {
return this.dashboardUrl;
}
getService(serviceId) {
return this.catalog.services.find(function(service) {
return service.id == serviceId;
});
}
getPlanForService(serviceId, planId) {
var service = this.getService(serviceId);
if (!service) {
return null;
}
return service.plans.find(function(plan) {
return plan.id == planId;
});
}
// getServiceInstanceExtensionAPIs(serviceId) {
// return [
// {
// discovery_url: '/logs',
// server_url: `${cfenv.getAppEnv().url}/v2/service_instances/${serviceId}`,
// adheres_to: 'http://broker.sapi.life/logs'
// },
// {
// discovery_url: '/health',
// server_url: `${cfenv.getAppEnv().url}/v2/service_instances/${serviceId}`,
// adheres_to: 'http://broker.sapi.life/health'
// },
// {
// discovery_url: '/info',
// server_url: `${cfenv.getAppEnv().url}/v2/service_instances/${serviceId}`,
// adheres_to: 'http://broker.sapi.life/info'
// }
// ]
// };
validateParameters(schema, parameters) {
var result = validate(parameters, schema);
if (!result.valid) {
return result.errors.toString();
}
else {
return null;
}
}
generatePlansForService(serviceName) {
var plans = [];
// Add a small plan
plans.push({
name: 'small',
description: 'A small instance of the service.',
free: true
});
// Add a large plan with a schema
var largePlanSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
additionalProperties: false,
type: 'object',
properties: {
rainbow: {
type: 'boolean',
default: false,
description: 'Follow the rainbow'
},
name: {
type: 'string',
minLength: 1,
maxLength: 30,
default: 'This is a default string',
description: 'The name of the broker'
},
color: {
type: 'string',
enum: [ 'red', 'amber', 'green' ],
default: 'green',
description: 'Your favourite color'
},
config: {
type: 'object',
properties: {
url: {
type: 'string'
},
port: {
type: 'integer'
}
}
}
}
};
plans.push({
name: 'large',
description: 'A large instance of the service.',
free: true,
schemas: {
service_instance: {
create: {
parameters: largePlanSchema
},
update: {
parameters: largePlanSchema
}
},
service_binding: {
create: {
parameters: largePlanSchema
}
}
}
});
// Load example schemas if requested and generate a plan for each
if (process.env.ENABLE_EXAMPLE_SCHEMAS) {
var exampleSchemas = fs.readdirSync('example_schemas');
for (var i = 0; i < exampleSchemas.length; i++) {
var name = exampleSchemas[i].split('.json')[0];
var schema = require(`./example_schemas/${name}`);
plans.push({
name: name,
description: name.replace(/-/g, ' '),
free: true,
schemas: {
service_instance: {
create: {
parameters: schema
},
update: {
parameters: schema
}
},
service_binding: {
create: {
parameters: schema
}
}
}
});
}
}
// Add an id to each plan
plans.forEach(function(plan) {
plan.id = sha256(`${serviceName}-${plan.name}`).substring(0, 32);
if (parseInt(process.env.MAXIMUM_POLLING_DURATION_IN_SECONDS)) {
plan.maximum_polling_duration = parseInt(process.env.MAXIMUM_POLLING_DURATION_IN_SECONDS);
}
});
// All plans generated
return plans;
}
}
module.exports = ServiceBroker;