serverValidator.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var blueprintUtils = require('utils/blueprint');
  20. App.ServerValidatorMixin = Em.Mixin.create({
  21. /**
  22. * defines if we use validation and recommendation on wizards
  23. * depend on this flag some properties will be taken from different places
  24. * @type {boolean}
  25. */
  26. isWizard: function() {
  27. return this.get('wizardController') && ['addServiceController', 'installerController'].contains(this.get('wizardController.name'));
  28. }.property('wizardController.name'),
  29. /**
  30. * recommendation configs loaded from server
  31. * (used only during install)
  32. * @type {Object}
  33. */
  34. recommendationsConfigs: null,
  35. /**
  36. * Collection of all config validation errors
  37. *
  38. * @type {Object[]}
  39. */
  40. configErrorList: [],
  41. /**
  42. * Map with allowed error types
  43. *
  44. * @type {Object}
  45. */
  46. errorTypes: {
  47. ERROR: 'ERROR',
  48. WARN: 'WARN',
  49. GENERAL: 'GENERAL'
  50. },
  51. /**
  52. * by default loads data from model otherwise must be overridden as computed property
  53. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  54. * (shouldn't contain configurations filed)
  55. * @type {Object}
  56. */
  57. hostNames: function() {
  58. return this.get('isWizard')
  59. ? Object.keys(this.get('content.hosts'))
  60. : App.get('allHostNames');
  61. }.property('isWizard', 'content.hosts', 'App.allHostNames'),
  62. /**
  63. * by default loads data from model otherwise must be overridden as computed property
  64. * @type {Array} - of strings (serviceNames)
  65. */
  66. serviceNames: function() {
  67. // When editing a service we validate only that service's configs.
  68. // However, we should pass the IDs of services installed, or else,
  69. // default value calculations will alter.
  70. return this.get('isWizard') ? this.get('allSelectedServiceNames') : App.Service.find().mapProperty('serviceName');
  71. }.property('isWizard', 'allSelectedServiceNames'),
  72. /**
  73. * by default loads data from model otherwise must be overridden as computed property
  74. * can be used for service|host configs pages
  75. * @type {Array} of strings (hostNames)
  76. */
  77. hostGroups: function() {
  78. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
  79. }.property('content.recommendationsHostGroups', 'App.allHostNames', 'App.componentToBeAdded', 'App.componentToBeDeleted'),
  80. /**
  81. * controller that is child of this mixin has to contain stepConfigs
  82. * @type {Array}
  83. */
  84. stepConfigs: null,
  85. serverSideValidation: function () {
  86. var deferred = $.Deferred(),
  87. self = this,
  88. primary = function() { deferred.resolve(); },
  89. secondary = function() { deferred.reject('invalid_configs'); };
  90. this.set('configErrorList', []);
  91. this.runServerSideValidation().done(function() {
  92. if (self.get('configErrorList.length')) {
  93. App.showConfigValidationPopup(self.get('configErrorList'), primary, secondary);
  94. } else {
  95. deferred.resolve();
  96. }
  97. }).fail(function() {
  98. App.showConfigValidationFailedPopup(primary, secondary);
  99. });
  100. return deferred.promise();
  101. },
  102. /**
  103. * @method serverSideValidation
  104. * send request to validate configs
  105. * @returns {*}
  106. */
  107. runServerSideValidation: function () {
  108. var self = this;
  109. var recommendations = this.get('hostGroups');
  110. var stepConfigs = this.get('stepConfigs');
  111. var dfd = $.Deferred();
  112. this.getBlueprintConfigurations().done(function(blueprintConfigurations) {
  113. recommendations.blueprint.configurations = blueprintConfigurations;
  114. App.ajax.send({
  115. name: 'config.validations',
  116. sender: self,
  117. data: {
  118. stackVersionUrl: App.get('stackVersionURL'),
  119. hosts: self.get('hostNames'),
  120. services: self.get('serviceNames'),
  121. validate: 'configurations',
  122. recommendations: recommendations
  123. },
  124. success: 'validationSuccess',
  125. error: 'validationError'
  126. }).done(dfd.resolve).fail(dfd.reject);
  127. });
  128. return dfd.promise();
  129. },
  130. /**
  131. * Return JSON for blueprint configurations
  132. * @returns {*}
  133. */
  134. getBlueprintConfigurations: function () {
  135. var dfd = $.Deferred();
  136. var stepConfigs = this.get('stepConfigs');
  137. // check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
  138. if (!stepConfigs.findProperty('serviceName', 'MISC')) {
  139. App.config.getClusterEnvConfigs().done(function(clusterEnvConfigs){
  140. stepConfigs = stepConfigs.concat(Em.Object.create({
  141. serviceName: 'MISC',
  142. configs: clusterEnvConfigs
  143. }));
  144. dfd.resolve(blueprintUtils.buildConfigsJSON(stepConfigs));
  145. });
  146. } else {
  147. dfd.resolve(blueprintUtils.buildConfigsJSON(stepConfigs));
  148. }
  149. return dfd.promise();
  150. },
  151. /**
  152. * Creates config validation error object
  153. *
  154. * @param type - error type, see <code>errorTypes<code>
  155. * @param property - config property object
  156. * @param messages - array of messages
  157. * @returns {{type: String, isError: boolean, isWarn: boolean, isGeneral: boolean, messages: Array}}
  158. */
  159. createErrorMessage: function (type, property, messages) {
  160. var errorTypes = this.get('errorTypes');
  161. var error = {
  162. type: type,
  163. isError: type === errorTypes.ERROR,
  164. isWarn: type === errorTypes.WARN,
  165. isGeneral: type === errorTypes.GENERAL,
  166. messages: Em.makeArray(messages)
  167. };
  168. Em.assert('Unknown config error type ' + type, error.isError || error.isWarn || error.isGeneral);
  169. if (property) {
  170. error.serviceName = App.StackService.find(Em.get(property, 'serviceName')).get('displayName');
  171. error.propertyName = Em.get(property, 'name');
  172. error.filename = Em.get(property, 'filename');
  173. error.value = Em.get(property, 'value');
  174. error.description = Em.get(property, 'description');
  175. }
  176. return error;
  177. },
  178. /**
  179. * Parse data from server to
  180. * <code>configErrorsMap<code> and
  181. * <code>generalErrors<code>
  182. *
  183. * @param data
  184. * @returns {{configErrorsMap: {}, generalErrors: Array}}
  185. */
  186. parseValidation: function(data) {
  187. var configErrorsMap = {}, generalErrors = [];
  188. data.resources.forEach(function(r) {
  189. r.items.forEach(function(item){
  190. if (item.type == "configuration") {
  191. var configId = (item['config-name'] && item['config-type']) && App.config.configId(item['config-name'], item['config-type']);
  192. if (configId) {
  193. if (configErrorsMap[configId]) {
  194. configErrorsMap[configId].messages.push(item.message);
  195. } else {
  196. configErrorsMap[configId] = {
  197. type: item.level,
  198. messages: [item.message]
  199. }
  200. }
  201. } else {
  202. generalErrors.push({
  203. type: this.get('errorTypes').GENERAL,
  204. messages: [item.message]
  205. });
  206. }
  207. }
  208. }, this);
  209. }, this);
  210. return {
  211. configErrorsMap: configErrorsMap,
  212. generalErrors: generalErrors
  213. }
  214. },
  215. /**
  216. * Generates list of all config errors that should be displayed in popup
  217. *
  218. * @param configErrorsMap
  219. * @param generalErrors
  220. * @returns {Array}
  221. */
  222. collectAllIssues: function(configErrorsMap, generalErrors) {
  223. var errorTypes = this.get('errorTypes');
  224. var configErrorList = [];
  225. this.get('stepConfigs').forEach(function(service) {
  226. service.get('configs').forEach(function(property) {
  227. if (property.get('isVisible') && !property.get('hiddenBySection')) {
  228. var serverIssue = configErrorsMap[property.get('id')];
  229. if (serverIssue) {
  230. configErrorList.push(this.createErrorMessage(serverIssue.type, property, serverIssue.messages));
  231. } else if (property.get('warnMessage')) {
  232. configErrorList.push(this.createErrorMessage(errorTypes.WARN, property, [property.get('warnMessage')]));
  233. }
  234. }
  235. }, this);
  236. }, this);
  237. generalErrors.forEach(function(serverIssue) {
  238. configErrorList.push(this.createErrorMessage(errorTypes.GENERAL, null, serverIssue.messages));
  239. }, this);
  240. return configErrorList;
  241. },
  242. /**
  243. * @method validationSuccess
  244. * success callback after getting response from server
  245. * go through the step configs and set warn and error messages
  246. * @param data
  247. */
  248. validationSuccess: function(data) {
  249. var parsed = this.parseValidation(data);
  250. this.set('configErrorList', this.collectAllIssues(parsed.configErrorsMap, parsed.generalErrors));
  251. },
  252. validationError: Em.K
  253. });