| 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 |
1
1
1
1
1
1
41
41
41
40
33
33
33
33
33
33
33
40
6
6
6
6
6
6
6
6
24
18
18
12
6
6
6
6
6
24
24
12
12
6
16
16
| 'use strict';
;require.register("mixins/common/serverValidator", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
var blueprintUtils = require('utils/blueprint');
/**
* @typedef {object} ConfigsValidationRequestData
* @property {string} stackVersionUrl stack version url
* @property {string[]} hosts host names
* @property {string[]} services service names
* @property {string} validate validation type e.g. 'configurations'
* @property {object} recommendations blueprint object
*/
/**
* @typedef {object} ConfigsValidationOptions
* @property {string[]} hosts host names
* @property {string[]} services service names
* @property {object} blueprint service configurations blueprint
*/
App.ServerValidatorMixin = Em.Mixin.create({
/**
* defines if we use validation and recommendation on wizards
* depend on this flag some properties will be taken from different places
* @type {boolean}
*/
isWizard: function () {
return this.get('wizardController') && ['addServiceController', 'installerController'].contains(this.get('wizardController.name'));
}.property('wizardController.name'),
/**
* recommendation configs loaded from server
* (used only during install)
* @type {Object}
*/
recommendationsConfigs: null,
/**
* Collection of all config validation errors
*
* @type {Object[]}
*/
configErrorList: Em.Object.create({
issues: [],
criticalIssues: []
}),
/**
* Ajax request object to validation API
* @type {$.ajax}
*/
validationRequest: null,
/**
* Timer id returned by setTimeout for calling function to send validation request
*/
requestTime: 0,
/**
* Map with allowed error types
*
* @type {Object}
*/
errorTypes: {
CRITICAL_ERROR: 'NOT_APPLICABLE',
ERROR: 'ERROR',
WARN: 'WARN',
GENERAL: 'GENERAL'
},
/**
* by default loads data from model otherwise must be overridden as computed property
* refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
* (shouldn't contain configurations filed)
* @type {Object}
*/
hostNames: function () {
return this.get('isWizard') ? Object.keys(this.get('content.hosts')) : App.get('allHostNames');
}.property('isWizard', 'content.hosts', 'App.allHostNames'),
/**
* by default loads data from model otherwise must be overridden as computed property
* @type {Array} - of strings (serviceNames)
*/
serviceNames: function () {
// When editing a service we validate only that service's configs.
// However, we should pass the IDs of services installed, or else,
// default value calculations will alter.
return this.get('isWizard') ? this.get('allSelectedServiceNames') : App.Service.find().mapProperty('serviceName');
}.property('isWizard', 'allSelectedServiceNames'),
/**
* by default loads data from model otherwise must be overridden as computed property
* can be used for service|host configs pages
* @type {Array} of strings (hostNames)
*/
hostGroups: function () {
return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
}.property('content.recommendationsHostGroups', 'App.allHostNames', 'App.componentToBeAdded', 'App.componentToBeDeleted'),
/**
* controller that is child of this mixin has to contain stepConfigs
* @type {Array}
*/
stepConfigs: null,
serverSideValidation: function serverSideValidation() {
var deferred = $.Deferred(),
self = this,
primary = function primary() {
deferred.resolve();
},
secondary = function secondary() {
deferred.reject('invalid_configs');
};
this.set('configErrorList', Em.Object.create({
issues: [],
criticalIssues: []
}));
this.runServerSideValidation().done(function () {
if (self.get('configErrorList.issues.length') || self.get('configErrorList.criticalIssues.length')) {
App.showConfigValidationPopup(self.get('configErrorList'), primary, secondary, self);
} else {
deferred.resolve();
}
}).fail(function () {
App.showConfigValidationFailedPopup(primary, secondary);
});
return deferred.promise();
},
/**
* @method serverSideValidation
* send request to validate configs
* @returns {*}
*/
runServerSideValidation: function runServerSideValidation() {
var self = this;
var recommendations = this.get('hostGroups');
var stepConfigs = this.get('stepConfigs');
var dfd = $.Deferred();
this.getBlueprintConfigurations(this.get('stepConfigs')).done(function (blueprintConfigurations) {
recommendations.blueprint.configurations = blueprintConfigurations;
var request = self.validateSelectedConfigs({
hosts: self.get('hostNames'),
services: self.get('serviceNames'),
blueprint: recommendations
});
self.set('validationRequest', request);
request.done(dfd.resolve).fail(dfd.reject);
});
return dfd.promise();
},
/**
* Perform service config validation
* @param validateSelectedConfigs
* @param {ConfigsValidationOptions} options
* @returns {$.Deferred}
*/
validateSelectedConfigs: function validateSelectedConfigs(options) {
var opts = $.extend({
services: [],
hosts: [],
blueprint: null
}, options || {});
return this.getServiceConfigsValidationRequest(this.getServiceConfigsValidationParams(opts));
},
/**
* @method getServiceConfigsValidationRequest
* @param {ConfigsValidationRequestData} validationData
* @returns {$.Deferred}
*/
getServiceConfigsValidationRequest: function getServiceConfigsValidationRequest(validationData) {
return App.ajax.send({
name: 'config.validations',
sender: this,
data: validationData,
success: 'validationSuccess'
});
},
/**
* @method getServiceConfigsValidationParams
* @param {ConfigsValidationOptions} options
* @returns {ConfigsValidationRequestData}
*/
getServiceConfigsValidationParams: function getServiceConfigsValidationParams(options) {
return {
stackVersionUrl: App.get('stackVersionURL'),
hosts: options.hosts,
services: options.services,
validate: 'configurations',
recommendations: options.blueprint
};
},
/**
* Return JSON for blueprint configurations
* @param {App.ServiceConfigs[]} serviceConfigs
* @returns {*}
*/
getBlueprintConfigurations: function getBlueprintConfigurations(serviceConfigs) {
var dfd = $.Deferred();
// check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
if (!serviceConfigs.findProperty('serviceName', 'MISC')) {
App.config.getConfigsByTypes([{ site: 'cluster-env', serviceName: 'MISC' }]).done(function (configs) {
dfd.resolve(blueprintUtils.buildConfigsJSON(serviceConfigs.concat(configs)));
});
} else {
dfd.resolve(blueprintUtils.buildConfigsJSON(serviceConfigs));
}
return dfd.promise();
},
/**
* Creates config validation error object
*
* @param type - error type, see <code>errorTypes<code>
* @param property - config property object
* @param messages - array of messages
* @returns {{type: String, isError: boolean, isWarn: boolean, isGeneral: boolean, messages: Array}}
*/
createErrorMessage: function createErrorMessage(type, property, messages) {
var errorTypes = this.get('errorTypes');
var error = {
type: type,
isCriticalError: type === errorTypes.CRITICAL_ERROR,
isError: type === errorTypes.ERROR,
isWarn: type === errorTypes.WARN,
isGeneral: type === errorTypes.GENERAL,
cssClass: this.get('isInstallWizard') ? '' : type === errorTypes.ERROR ? 'error' : 'warning',
messages: Em.makeArray(messages)
};
Em.assert('Unknown config error type ' + type, error.isError || error.isWarn || error.isGeneral || error.isCriticalError);
if (property) {
var value = Em.get(property, 'value');
error.id = Em.get(property, 'id');
error.serviceName = Em.get(property, 'serviceDisplayName') || App.StackService.find(Em.get(property, 'serviceName')).get('displayName');
error.propertyName = Em.get(property, 'name');
error.filename = Em.get(property, 'filename');
error.value = value && Em.get(property, 'displayType') === 'password' ? new Array(value.length + 1).join('*') : value;
error.description = Em.get(property, 'description');
}
return error;
},
/**
* Parse data from server to
* <code>configErrorsMap<code> and
* <code>generalErrors<code>
*
* @param data
* @returns {{configErrorsMap: {}, generalErrors: Array}}
*/
parseValidation: function parseValidation(data) {
var configErrorsMap = {},
generalErrors = [];
data.resources.forEach(function (r) {
r.items.forEach(function (item) {
if (item.type == "configuration") {
var configId = item['config-name'] && item['config-type'] && App.config.configId(item['config-name'], item['config-type']);
if (configId) {
if (configErrorsMap[configId]) {
configErrorsMap[configId].messages.push(item.message);
} else {
configErrorsMap[configId] = {
type: item.level,
messages: [item.message],
name: item['config-name'],
filename: item['config-type']
};
}
} else {
generalErrors.push({
type: this.get('errorTypes').GENERAL,
messages: [item.message]
});
}
}
}, this);
}, this);
return {
configErrorsMap: configErrorsMap,
generalErrors: generalErrors
};
},
/**
* Generates list of all config errors that should be displayed in popup
*
* @param configErrorsMap
* @param generalErrors
* @returns {Array}
*/
collectAllIssues: function collectAllIssues(configErrorsMap, generalErrors) {
var errorTypes = this.get('errorTypes');
var configErrorList = {};
configErrorList[errorTypes.WARN] = [];
configErrorList[errorTypes.ERROR] = [];
configErrorList[errorTypes.CRITICAL_ERROR] = [];
configErrorList[errorTypes.GENERAL] = [];
this.get('stepConfigs').forEach(function (service) {
service.get('configs').forEach(function (property) {
if (property.get('isVisible') && !property.get('hiddenBySection')) {
var serverIssue = configErrorsMap[property.get('id')];
if (serverIssue) {
configErrorList[serverIssue.type].push(this.createErrorMessage(serverIssue.type, property, serverIssue.messages));
} else Eif (property.get('warnMessage')) {
configErrorList[errorTypes.WARN].push(this.createErrorMessage(errorTypes.WARN, property, [property.get('warnMessage')]));
}
}
}, this);
}, this);
generalErrors.forEach(function (serverIssue) {
configErrorList[errorTypes.GENERAL].push(this.createErrorMessage(errorTypes.GENERAL, null, serverIssue.messages));
}, this);
Em.keys(configErrorsMap).forEach(function (id) {
var serverIssue = configErrorsMap[id];
if (!configErrorList[serverIssue.type].someProperty('id', id)) {
var filename = Em.get(serverIssue, 'filename'),
service = App.config.get('serviceByConfigTypeMap')[filename],
property = {
id: id,
name: Em.get(serverIssue, 'name'),
filename: App.config.getOriginalFileName(filename),
serviceDisplayName: service && Em.get(service, 'displayName')
};
configErrorList[serverIssue.type].push(this.createErrorMessage(serverIssue.type, property, serverIssue.messages));
}
}, this);
return Em.Object.create({
criticalIssues: configErrorList[errorTypes.CRITICAL_ERROR],
issues: configErrorList[errorTypes.ERROR].concat(configErrorList[errorTypes.WARN], configErrorList[errorTypes.GENERAL])
});
},
/**
* @method validationSuccess
* success callback after getting response from server
* go through the step configs and set warn and error messages
* @param data
*/
validationSuccess: function validationSuccess(data) {
var parsed = this.parseValidation(data);
this.set('configErrorList', this.collectAllIssues(parsed.configErrorsMap, parsed.generalErrors));
},
valueObserver: function () {
var self = this;
Iif (this.get('isInstallWizard') && this.get('currentTabName') === 'all-configurations') {
if (this.get('requestTimer')) clearTimeout(this.get('requestTimer'));
self.set('requestTimer', setTimeout(function () {
if (self.get('validationRequest')) {
self.get('validationRequest').abort();
}
if (self.get('recommendationsInProgress')) {
self.valueObserver();
} else {
self.runServerSideValidation().done(function () {
self.set('validationRequest', null);
self.set('requestTimer', 0);
});
}
}, 500));
}
}.observes('selectedService.configs.@each.value')
});
}); |