manage_config_groups_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 validator = require('utils/validator');
  20. var hostsManagement = require('utils/hosts');
  21. var numberUtils = require('utils/number_utils');
  22. App.ManageConfigGroupsController = Em.Controller.extend(App.ConfigOverridable, {
  23. name: 'manageConfigGroupsController',
  24. /**
  25. * Determines if needed data is already loaded
  26. * Loading chain starts at <code>loadHosts</code> and is complete on the <code>loadConfigGroups</code> (if user on
  27. * the Installer) or on the <code>_onLoadConfigGroupsSuccess</code> (otherwise)
  28. * @type {boolean}
  29. */
  30. isLoaded: false,
  31. /**
  32. * Determines if user currently is on the Cluster Installer
  33. * @type {boolean}
  34. */
  35. isInstaller: false,
  36. /**
  37. * Determines if user currently is on the Add Service Wizard
  38. * @type {boolean}
  39. */
  40. isAddService: false,
  41. /**
  42. * Current service name
  43. * @type {string}
  44. */
  45. serviceName: null,
  46. /**
  47. * @type {App.ConfigGroup[]}
  48. */
  49. configGroups: [],
  50. /**
  51. * @type {App.ConfigGroup[]}
  52. */
  53. originalConfigGroups: [],
  54. /**
  55. * @type {App.ConfigGroup}
  56. */
  57. selectedConfigGroup: null,
  58. /**
  59. * @type {string[]}
  60. */
  61. selectedHosts: [],
  62. /**
  63. * List of all hosts in the cluster
  64. * @type {{
  65. * id: string,
  66. * ip: string,
  67. * osType: string,
  68. * osArch: string,
  69. * hostName: string,
  70. * publicHostName: string,
  71. * cpu: number,
  72. * memory: number,
  73. * diskTotal: string,
  74. * diskFree: string,
  75. * disksMounted: number,
  76. * hostComponents: {
  77. * componentName: string,
  78. * displayName: string
  79. * }[]
  80. * }[]}
  81. */
  82. clusterHosts: [],
  83. /**
  84. * trigger <code>selectDefaultGroup</code> after group delete
  85. * @type {null}
  86. */
  87. groupDeleteTrigger: null,
  88. /**
  89. * List of available service components for <code>serviceName</code>
  90. * @type {{componentName: string, displayName: string, selected: boolean}[]}
  91. */
  92. componentsForFilter: function () {
  93. return App.StackServiceComponent.find().filterProperty('serviceName', this.get('serviceName')).map(function (component) {
  94. return Em.Object.create({
  95. componentName: component.get('componentName'),
  96. displayName: App.format.role(component.get('componentName')),
  97. selected: false
  98. });
  99. });
  100. }.property('serviceName'),
  101. /**
  102. * Determines when host may be deleted from config group
  103. * @type {boolean}
  104. */
  105. isDeleteHostsDisabled: function () {
  106. var selectedConfigGroup = this.get('selectedConfigGroup');
  107. if (selectedConfigGroup) {
  108. return selectedConfigGroup.get('isDefault') || this.get('selectedHosts').length === 0;
  109. }
  110. return true;
  111. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.length', 'selectedHosts.length'),
  112. /**
  113. * Map with modified/deleted/created config groups
  114. * @type {{
  115. * toClearHosts: App.ConfigGroup[],
  116. * toDelete: App.ConfigGroup[],
  117. * toSetHosts: App.ConfigGroup[],
  118. * toCreate: App.ConfigGroup[]
  119. * }}
  120. */
  121. hostsModifiedConfigGroups: {},
  122. /**
  123. * Check when some config group was changed and updates <code>hostsModifiedConfigGroups</code> once
  124. * @method hostsModifiedConfigGroupsObs
  125. */
  126. hostsModifiedConfigGroupsObs: function() {
  127. Em.run.once(this, this.hostsModifiedConfigGroupsObsOnce);
  128. }.observes('selectedConfigGroup.hosts.@each', 'selectedConfigGroup.hosts.length', 'selectedConfigGroup.description', 'configGroups', 'isLoaded'),
  129. /**
  130. * Update <code>hostsModifiedConfigGroups</code>-value
  131. * Called once in the <code>hostsModifiedConfigGroupsObs</code>
  132. * @method hostsModifiedConfigGroupsObsOnce
  133. * @returns {boolean}
  134. */
  135. hostsModifiedConfigGroupsObsOnce: function() {
  136. if (!this.get('isLoaded')) {
  137. return false;
  138. }
  139. var groupsToClearHosts = [];
  140. var groupsToDelete = [];
  141. var groupsToSetHosts = [];
  142. var groupsToCreate = [];
  143. var groups = this.get('configGroups');
  144. var originalGroups = [];
  145. var originalGroupsMap = {};
  146. this.get('originalConfigGroups').forEach(function(item){
  147. if (!item.is_default) {
  148. originalGroupsMap[item.id] = item;
  149. originalGroups.push(item);
  150. }
  151. }, this);
  152. groups.forEach(function (groupRecord) {
  153. if (!groupRecord.get('isDefault')) {
  154. var originalGroup = originalGroupsMap[groupRecord.get('id')];
  155. if (originalGroup) {
  156. if (!(JSON.stringify(groupRecord.get('hosts').slice().sort()) === JSON.stringify(originalGroup.hosts.sort()))) {
  157. groupsToClearHosts.push(groupRecord);
  158. if (groupRecord.get('hosts').length) {
  159. groupsToSetHosts.push(groupRecord);
  160. }
  161. // should update name or description
  162. } else if (groupRecord.get('description') !== originalGroup.description || groupRecord.get('name') !== originalGroup.name) {
  163. groupsToSetHosts.push(groupRecord);
  164. }
  165. delete originalGroupsMap[groupRecord.get('id')];
  166. } else {
  167. groupsToCreate.push({
  168. id: groupRecord.get('id'),
  169. config_group_id: groupRecord.get('configGroupId'),
  170. name: groupRecord.get('name'),
  171. description: groupRecord.get('description'),
  172. hosts: groupRecord.get('hosts').slice(0),
  173. service_id: groupRecord.get('serviceName'),
  174. desired_configs: groupRecord.get('desiredConfigs'),
  175. properties: groupRecord.get('properties')
  176. });
  177. }
  178. }
  179. });
  180. //groups to delete
  181. for (var id in originalGroupsMap) {
  182. groupsToDelete.push(App.ServiceConfigGroup.find(id));
  183. }
  184. this.set('hostsModifiedConfigGroups', {
  185. toClearHosts: groupsToClearHosts,
  186. toDelete: groupsToDelete,
  187. toSetHosts: groupsToSetHosts,
  188. toCreate: groupsToCreate,
  189. initialGroups: originalGroups
  190. });
  191. },
  192. /**
  193. * Determines if some changes were done with config groups
  194. * @use hostsModifiedConfigGroups
  195. * @type {boolean}
  196. */
  197. isHostsModified: function () {
  198. if (!this.get('isLoaded')) {
  199. return false;
  200. }
  201. var ignoreKeys = ['initialGroups'];
  202. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  203. return Em.keys(modifiedGroups).map(function (key) {
  204. return ignoreKeys.contains(key) ? 0 : Em.get(modifiedGroups[key], 'length');
  205. }).reduce(Em.sum, 0) > 0;
  206. }.property('hostsModifiedConfigGroups'),
  207. /**
  208. * Resort config groups according to order:
  209. * default group first, other - last
  210. * @method resortConfigGroup
  211. */
  212. resortConfigGroup: function() {
  213. var configGroups = Em.copy(this.get('configGroups'));
  214. if(configGroups.length < 2) return;
  215. var defaultConfigGroup = configGroups.findProperty('isDefault');
  216. configGroups.removeObject(defaultConfigGroup);
  217. var sorted = [defaultConfigGroup].concat(configGroups.sortProperty('name'));
  218. this.removeObserver('configGroups.@each.name', this, 'resortConfigGroup');
  219. this.set('configGroups', sorted);
  220. this.addObserver('configGroups.@each.name', this, 'resortConfigGroup');
  221. }.observes('configGroups.@each.name'),
  222. /**
  223. * Load hosts from server or
  224. * get them from installerController if user on the install wizard
  225. * get them from isAddServiceController if user on the add service wizard
  226. * @method loadHosts
  227. */
  228. loadHosts: function() {
  229. this.set('isLoaded', false);
  230. if (this.get('isInstaller')) {
  231. var allHosts = this.get('isAddService') ? App.router.get('addServiceController').get('allHosts') : App.router.get('installerController').get('allHosts');
  232. this.set('clusterHosts', allHosts);
  233. this.loadConfigGroups(this.get('serviceName'));
  234. }
  235. else {
  236. this.loadHostsFromServer();
  237. this.loadConfigGroups(this.get('serviceName'));
  238. }
  239. },
  240. /**
  241. * Request all hosts directly from server
  242. * @method loadHostsFromServer
  243. * @return {$.ajax}
  244. */
  245. loadHostsFromServer: function() {
  246. return App.ajax.send({
  247. name: 'hosts.config_groups',
  248. sender: this,
  249. data: {},
  250. success: '_loadHostsFromServerSuccessCallback',
  251. error: '_loadHostsFromServerErrorCallback'
  252. });
  253. },
  254. /**
  255. * Success-callback for <code>loadHostsFromServer</code>
  256. * Parse hosts response and wrap them into Ember.Object
  257. * @param {object} data
  258. * @method _loadHostsFromServerSuccessCallback
  259. * @private
  260. */
  261. _loadHostsFromServerSuccessCallback: function (data) {
  262. var wrappedHosts = [];
  263. data.items.forEach(function (host) {
  264. var hostComponents = [];
  265. var diskInfo = host.Hosts.disk_info.filter(function(item) {
  266. return /^ext|^ntfs|^fat|^xfs/i.test(item.type);
  267. });
  268. if (diskInfo.length) {
  269. diskInfo = diskInfo.reduce(function(a, b) {
  270. return {
  271. available: parseInt(a.available) + parseInt(b.available),
  272. size: parseInt(a.size) + parseInt(b.size)
  273. };
  274. });
  275. }
  276. host.host_components.forEach(function (hostComponent) {
  277. hostComponents.push(Em.Object.create({
  278. componentName: hostComponent.HostRoles.component_name,
  279. displayName: App.format.role(hostComponent.HostRoles.component_name)
  280. }));
  281. }, this);
  282. wrappedHosts.pushObject(Em.Object.create({
  283. id: host.Hosts.host_name,
  284. ip: host.Hosts.ip,
  285. osType: host.Hosts.os_type,
  286. osArch: host.Hosts.os_arch,
  287. hostName: host.Hosts.host_name,
  288. publicHostName: host.Hosts.public_host_name,
  289. cpu: host.Hosts.cpu_count,
  290. memory: host.Hosts.total_mem,
  291. diskTotal: numberUtils.bytesToSize(diskInfo.size, 0, undefined, 1024),
  292. diskFree: numberUtils.bytesToSize(diskInfo.available, 0, undefined, 1024),
  293. disksMounted: host.Hosts.disk_info.length,
  294. hostComponents: hostComponents
  295. }
  296. ));
  297. }, this);
  298. this.set('clusterHosts', wrappedHosts);
  299. },
  300. /**
  301. * Error-callback for <code>loadHostsFromServer</code>
  302. * @method _loadHostsFromServerErrorCallback
  303. * @private
  304. */
  305. _loadHostsFromServerErrorCallback: function () {
  306. this.set('clusterHosts', []);
  307. },
  308. /**
  309. * Load config groups from server if user is on the already installed cluster
  310. * If not - use loaded data form wizardStep7Controller
  311. * @param {string} serviceName
  312. * @method loadConfigGroups
  313. */
  314. loadConfigGroups: function (serviceName) {
  315. if (this.get('isInstaller')) {
  316. var configGroups = App.router.get('wizardStep7Controller.selectedService.configGroups').slice(0);
  317. var originalConfigGroups = this.generateOriginalConfigGroups(configGroups);
  318. this.setProperties({
  319. configGroups: configGroups,
  320. originalConfigGroups: originalConfigGroups,
  321. isLoaded: true
  322. });
  323. }
  324. else {
  325. this.set('serviceName', serviceName);
  326. App.ajax.send({
  327. name: 'service.load_config_groups',
  328. data: {
  329. serviceName: serviceName
  330. },
  331. sender: this,
  332. success: '_onLoadConfigGroupsSuccess'
  333. });
  334. }
  335. },
  336. /**
  337. * Success-callback for <code>loadConfigGroups</code>
  338. * @param {object} data
  339. * @private
  340. * @method _onLoadConfigGroupsSuccess
  341. */
  342. _onLoadConfigGroupsSuccess: function (data) {
  343. var serviceName = this.get('serviceName');
  344. App.configGroupsMapper.map(data, false, [serviceName]);
  345. var configGroups = App.ServiceConfigGroup.find().filterProperty('serviceName', serviceName);
  346. var rawConfigGroups = this.generateOriginalConfigGroups(configGroups);
  347. var groupToTypeToTagMap = {};
  348. rawConfigGroups.forEach(function (item) {
  349. if (Array.isArray(item.desired_configs)) {
  350. item.desired_configs.forEach(function (config) {
  351. if (!groupToTypeToTagMap[item.name]) {
  352. groupToTypeToTagMap[item.name] = {};
  353. }
  354. groupToTypeToTagMap[item.name][config.type] = config.tag;
  355. });
  356. }
  357. });
  358. this.set('configGroups', configGroups);
  359. this.set('originalConfigGroups', rawConfigGroups);
  360. this.loadProperties(groupToTypeToTagMap);
  361. this.set('isLoaded', true);
  362. },
  363. /**
  364. *
  365. * @param {Array} configGroups
  366. * @returns {Array}
  367. */
  368. generateOriginalConfigGroups: function(configGroups) {
  369. return configGroups.map(function (item) {
  370. return {
  371. id: item.get('id'),
  372. config_group_id: item.get('configGroupId'),
  373. name: item.get('name'),
  374. service_name: item.get('serviceName'),
  375. description: item.get('description'),
  376. hosts: item.get('hosts').slice(0),
  377. service_id: item.get('serviceName'),
  378. desired_configs: item.get('desiredConfigs'),
  379. is_default: item.get('isDefault'),
  380. child_config_groups: item.get('childConfigGroups') ? item.get('childConfigGroups').mapProperty('id') : [],
  381. parent_config_group_id: item.get('parentConfigGroup.id'),
  382. properties: item.get('properties')
  383. };
  384. });
  385. },
  386. /**
  387. *
  388. * @param {object} groupToTypeToTagMap
  389. * @method loadProperties
  390. */
  391. loadProperties: function (groupToTypeToTagMap) {
  392. var typeTagToGroupMap = {};
  393. var urlParams = [];
  394. for (var group in groupToTypeToTagMap) {
  395. var overrideTypeTags = groupToTypeToTagMap[group];
  396. for (var type in overrideTypeTags) {
  397. var tag = overrideTypeTags[type];
  398. typeTagToGroupMap[type + "///" + tag] = group;
  399. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  400. }
  401. }
  402. var params = urlParams.join('|');
  403. if (urlParams.length) {
  404. App.ajax.send({
  405. name: 'config.host_overrides',
  406. sender: this,
  407. data: {
  408. params: params,
  409. typeTagToGroupMap: typeTagToGroupMap
  410. },
  411. success: '_onLoadPropertiesSuccess'
  412. });
  413. }
  414. },
  415. /**
  416. * Success-callback for <code>loadProperties</code>
  417. * @param {object} data
  418. * @param {object} opt
  419. * @param {object} params
  420. * @private
  421. * @method _onLoadPropertiesSuccess
  422. */
  423. _onLoadPropertiesSuccess: function (data, opt, params) {
  424. data.items.forEach(function (configs) {
  425. var typeTagConfigs = [];
  426. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  427. for (var config in configs.properties) {
  428. typeTagConfigs.push({
  429. name: config,
  430. value: configs.properties[config]
  431. });
  432. }
  433. this.get('configGroups').findProperty('name', group).set('properties', typeTagConfigs);
  434. }, this);
  435. },
  436. /**
  437. * Show popup with properties overridden in the selected config group
  438. * @method showProperties
  439. */
  440. showProperties: function () {
  441. var properies = this.get('selectedConfigGroup.propertiesList').htmlSafe();
  442. if (properies) {
  443. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  444. }
  445. },
  446. /**
  447. * Show popup with hosts to add to the selected config group
  448. * @returns {boolean}
  449. * @method addHosts
  450. */
  451. addHosts: function () {
  452. if (this.get('selectedConfigGroup.isAddHostsDisabled')) {
  453. return false;
  454. }
  455. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  456. var popupDescription = {
  457. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  458. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message').format(this.get('selectedConfigGroup.displayName'))
  459. };
  460. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, this.get('componentsForFilter'), this.addHostsCallback.bind(this), popupDescription);
  461. },
  462. /**
  463. * Remove selected hosts from default group (<code>selectedConfigGroup.parentConfigGroup</code>) and add them to the <code>selectedConfigGroup</code>
  464. * @param {string[]} selectedHosts
  465. * @method addHostsCallback
  466. */
  467. addHostsCallback: function (selectedHosts) {
  468. if (selectedHosts) {
  469. var group = this.get('selectedConfigGroup');
  470. var parentGroupHosts = group.get('parentConfigGroup.hosts');
  471. var newHostsForParentGroup = parentGroupHosts.filter(function(hostName) {
  472. return !selectedHosts.contains(hostName);
  473. });
  474. group.get('hosts').pushObjects(selectedHosts);
  475. group.set('parentConfigGroup.hosts', newHostsForParentGroup);
  476. }
  477. },
  478. /**
  479. * Delete hosts from <code>selectedConfigGroup</code> and move them to the Default group (<code>selectedConfigGroup.parentConfigGroup</code>)
  480. * @method deleteHosts
  481. */
  482. deleteHosts: function () {
  483. if (this.get('isDeleteHostsDisabled')) {
  484. return;
  485. }
  486. var hosts = this.get('selectedHosts').slice();
  487. var newHosts = [];
  488. this.get('selectedConfigGroup.parentConfigGroup.hosts').pushObjects(hosts);
  489. this.get('selectedConfigGroup.hosts').forEach(function(host) {
  490. if (!hosts.contains(host)) {
  491. newHosts.pushObject(host);
  492. }
  493. });
  494. this.set('selectedConfigGroup.hosts', newHosts);
  495. this.set('selectedHosts', []);
  496. },
  497. /**
  498. * show popup for confirmation delete config group
  499. * @method confirmDelete
  500. */
  501. confirmDelete: function () {
  502. var self = this;
  503. App.showConfirmationPopup(function() {
  504. self.deleteConfigGroup();
  505. });
  506. },
  507. /**
  508. * delete selected config group (stored in the <code>selectedConfigGroup</code>)
  509. * then select default config group
  510. * @method deleteConfigGroup
  511. */
  512. deleteConfigGroup: function () {
  513. var selectedConfigGroup = this.get('selectedConfigGroup');
  514. if (this.get('isDeleteGroupDisabled')) {
  515. return;
  516. }
  517. //move hosts of group to default group (available hosts)
  518. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  519. this.deleteHosts();
  520. this.get('configGroups').removeObject(selectedConfigGroup);
  521. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  522. this.propertyDidChange('groupDeleteTrigger');
  523. },
  524. /**
  525. * rename new config group (not allowed for default group)
  526. * @method renameConfigGroup
  527. */
  528. renameConfigGroup: function () {
  529. if(this.get('selectedConfigGroup.isDefault')) {
  530. return;
  531. }
  532. var self = this;
  533. var renameGroupPopup = App.ModalPopup.show({
  534. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  535. bodyClass: Em.View.extend({
  536. templateName: require('templates/main/service/new_config_group')
  537. }),
  538. configGroupName: self.get('selectedConfigGroup.name'),
  539. configGroupDesc: self.get('selectedConfigGroup.description'),
  540. warningMessage: null,
  541. isDescriptionDirty: false,
  542. validate: function () {
  543. var warningMessage = '';
  544. var originalGroup = self.get('selectedConfigGroup');
  545. var groupName = this.get('configGroupName').trim();
  546. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  547. this.set('isDescriptionDirty', true);
  548. }
  549. if (originalGroup.get('name').trim() === groupName) {
  550. if (this.get('isDescriptionDirty')) {
  551. warningMessage = '';
  552. } else {
  553. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  554. }
  555. } else {
  556. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  557. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  558. }
  559. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  560. warningMessage = Em.I18n.t("form.validator.configGroupName");
  561. }
  562. }
  563. this.set('warningMessage', warningMessage);
  564. }.observes('configGroupName', 'configGroupDesc'),
  565. disablePrimary: function () {
  566. return !(this.get('configGroupName').trim().length > 0 && (this.get('warningMessage') !== null && !this.get('warningMessage')));
  567. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  568. onPrimary: function () {
  569. self.set('selectedConfigGroup.name', this.get('configGroupName'));
  570. self.set('selectedConfigGroup.description', this.get('configGroupDesc'));
  571. this.hide();
  572. }
  573. });
  574. this.set('renameGroupPopup', renameGroupPopup);
  575. },
  576. /**
  577. * add new config group (or copy existing)
  578. * @param {boolean} duplicated true - copy <code>selectedConfigGroup</code>, false - create a new one
  579. * @method addConfigGroup
  580. */
  581. addConfigGroup: function (duplicated) {
  582. duplicated = (duplicated === true);
  583. var self = this;
  584. var addGroupPopup = App.ModalPopup.show({
  585. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  586. bodyClass: Em.View.extend({
  587. templateName: require('templates/main/service/new_config_group')
  588. }),
  589. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  590. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  591. warningMessage: '',
  592. didInsertElement: function(){
  593. this.validate();
  594. this.$('input').focus();
  595. this.fitZIndex();
  596. },
  597. validate: function () {
  598. var warningMessage = '';
  599. var groupName = this.get('configGroupName').trim();
  600. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  601. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  602. }
  603. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  604. warningMessage = Em.I18n.t("form.validator.configGroupName");
  605. }
  606. this.set('warningMessage', warningMessage);
  607. }.observes('configGroupName'),
  608. disablePrimary: function () {
  609. return !(this.get('configGroupName').trim().length > 0 && !this.get('warningMessage'));
  610. }.property('warningMessage', 'configGroupName'),
  611. onPrimary: function () {
  612. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault'),
  613. properties = [], serviceName = self.get('serviceName'),
  614. groupName = this.get('configGroupName').trim(),
  615. newGroupId = App.ServiceConfigGroup.groupId(serviceName, groupName);
  616. App.store.load(App.ServiceConfigGroup, {
  617. id: newGroupId,
  618. name: this.get('configGroupName').trim(),
  619. description: this.get('configGroupDesc'),
  620. isDefault: false,
  621. parent_config_group_id: App.ServiceConfigGroup.getParentConfigGroupId(serviceName),
  622. service_id: serviceName,
  623. service_name: serviceName,
  624. hosts: [],
  625. desiredConfigs: [],
  626. properties: []
  627. });
  628. App.store.commit();
  629. var childConfigGroups = defaultConfigGroup.get('childConfigGroups').mapProperty('id');
  630. childConfigGroups.push(newGroupId);
  631. App.store.load(App.ServiceConfigGroup, App.configGroupsMapper.generateDefaultGroup(self.get('serviceName'), defaultConfigGroup.get('hosts'), childConfigGroups));
  632. App.store.commit();
  633. if (duplicated) {
  634. self.get('selectedConfigGroup.properties').forEach(function (item) {
  635. var property = App.ServiceConfigProperty.create($.extend(false, {}, item));
  636. property.set('group', App.ServiceConfigGroup.find(newGroupId));
  637. properties.push(property);
  638. });
  639. App.ServiceConfigGroup.find(newGroupId).setProperties({
  640. 'properties': properties,
  641. 'desiredConfigs': self.get('selectedConfigGroup.desiredConfigs')
  642. });
  643. }
  644. self.get('configGroups').pushObject(App.ServiceConfigGroup.find(newGroupId));
  645. this.hide();
  646. }
  647. });
  648. this.set('addGroupPopup', addGroupPopup);
  649. },
  650. /**
  651. * Duplicate existing config group
  652. * @method duplicateConfigGroup
  653. */
  654. duplicateConfigGroup: function() {
  655. this.addConfigGroup(true);
  656. },
  657. /**
  658. * Show popup with config groups
  659. * User may edit/create/delete them
  660. * @param {Em.Controller} controller
  661. * @param {App.Service} service
  662. * @returns {App.ModalPopup}
  663. * @method manageConfigurationGroups
  664. */
  665. manageConfigurationGroups: function (controller, service) {
  666. var configsController = this;
  667. var serviceData = (controller && controller.get('selectedService')) || service;
  668. var serviceName = serviceData.get('serviceName');
  669. var displayName = serviceData.get('displayName');
  670. this.setProperties({
  671. isInstaller: !!controller,
  672. serviceName: serviceName
  673. });
  674. if (controller) {
  675. configsController.set('isAddService', controller.get('content.controllerName') == 'addServiceController');
  676. }
  677. return App.ModalPopup.show({
  678. header: Em.I18n.t('services.service.config_groups_popup.header').format(displayName),
  679. bodyClass: App.MainServiceManageConfigGroupView.extend({
  680. serviceName: serviceName,
  681. displayName: displayName,
  682. controller: configsController
  683. }),
  684. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  685. primary: Em.I18n.t('common.save'),
  686. subViewController: configsController,
  687. /**
  688. * handle onPrimary action particularly in wizard
  689. * @param {Em.Controller} controller
  690. * @param {object} modifiedConfigGroups
  691. */
  692. onPrimaryWizard: function (controller, modifiedConfigGroups) {
  693. controller.set('selectedService.configGroups', configsController.get('configGroups'));
  694. controller.selectedServiceObserver();
  695. if (controller.get('name') == "wizardStep7Controller") {
  696. if (controller.get('selectedService.selected') === false && modifiedConfigGroups.toDelete.length > 0) {
  697. controller.setGroupsToDelete(modifiedConfigGroups.toDelete);
  698. }
  699. configsController.persistConfigGroups();
  700. this.updateConfigGroupOnServicePage();
  701. }
  702. this.hide();
  703. },
  704. onClose: function () {
  705. //<code>_super</code> has to be called before <code>resetGroupChanges</code>
  706. var originalGroups = this.get('subViewController.originalConfigGroups').slice(0);
  707. this._super();
  708. this.resetGroupChanges(originalGroups);
  709. },
  710. onSecondary: function () {
  711. this.onClose();
  712. },
  713. /**
  714. * reset group changes made by user
  715. * @param {Array} originalGroups
  716. */
  717. resetGroupChanges: function (originalGroups) {
  718. if (this.get('subViewController.isHostsModified')) {
  719. App.ServiceConfigGroup.find().clear();
  720. App.store.commit();
  721. App.store.loadMany(App.ServiceConfigGroup, originalGroups);
  722. App.store.commit();
  723. }
  724. },
  725. /**
  726. * run requests which delete config group and clear its hosts
  727. * @param {Function} finishFunction
  728. * @param {object} modifiedConfigGroups
  729. */
  730. runClearCGQueue: function (finishFunction, modifiedConfigGroups) {
  731. var counter = 0;
  732. var dfd = $.Deferred();
  733. var doneFunction = function (xhr, text, errorThrown) {
  734. counter--;
  735. if (counter === 0) dfd.resolve();
  736. finishFunction(xhr, text, errorThrown);
  737. };
  738. modifiedConfigGroups.toClearHosts.forEach(function (cg) {
  739. counter++;
  740. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction)
  741. }, this);
  742. modifiedConfigGroups.toDelete.forEach(function (cg) {
  743. counter++;
  744. configsController.deleteConfigurationGroup(cg, doneFunction, doneFunction);
  745. }, this);
  746. if (counter === 0) dfd.resolve();
  747. return dfd.promise();
  748. },
  749. /**
  750. * run requests which change properties of config group
  751. * @param {Function} finishFunction
  752. * @param {object} modifiedConfigGroups
  753. */
  754. runModifyCGQueue: function (finishFunction, modifiedConfigGroups) {
  755. var counter = 0;
  756. var dfd = $.Deferred();
  757. var doneFunction = function (xhr, text, errorThrown) {
  758. counter--;
  759. if (counter === 0) dfd.resolve();
  760. finishFunction(xhr, text, errorThrown);
  761. };
  762. modifiedConfigGroups.toSetHosts.forEach(function (cg) {
  763. counter++;
  764. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction);
  765. }, this);
  766. if (counter === 0) dfd.resolve();
  767. return dfd.promise();
  768. },
  769. /**
  770. * run requests which create new config group
  771. * @param {Function} finishFunction
  772. * @param {object} modifiedConfigGroups
  773. */
  774. runCreateCGQueue: function (finishFunction, modifiedConfigGroups) {
  775. var counter = 0;
  776. var dfd = $.Deferred();
  777. var doneFunction = function (xhr, text, errorThrown) {
  778. counter--;
  779. if (counter === 0) dfd.resolve();
  780. finishFunction(xhr, text, errorThrown);
  781. };
  782. modifiedConfigGroups.toCreate.forEach(function (cg) {
  783. counter++;
  784. configsController.postNewConfigurationGroup(cg, doneFunction);
  785. }, this);
  786. if (counter === 0) dfd.resolve();
  787. return dfd.promise();
  788. },
  789. onPrimary: function () {
  790. var modifiedConfigGroups = configsController.get('hostsModifiedConfigGroups');
  791. var errors = [];
  792. var self = this;
  793. var finishFunction = function (xhr, text, errorThrown) {
  794. if (xhr && errorThrown) {
  795. var error = xhr.status + "(" + errorThrown + ") ";
  796. try {
  797. var json = $.parseJSON(xhr.responseText);
  798. error += json.message;
  799. } catch (err) {
  800. }
  801. errors.push(error);
  802. }
  803. };
  804. // Save modified config-groups
  805. if (controller) {
  806. //called only in Wizard
  807. return this.onPrimaryWizard(controller, modifiedConfigGroups);
  808. }
  809. this.runClearCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  810. self.runModifyCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  811. self.runCreateCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  812. if (errors.length > 0) {
  813. self.get('subViewController').set('errorMessage', errors.join(". "));
  814. } else {
  815. if (!self.get('isAddService') && !self.get('isInstaller') && !modifiedConfigGroups.toCreate.everyProperty('properties.length', 0)) {
  816. //update service config versions only if it is service configs page and at least one newly created group had properties
  817. App.router.get('mainServiceInfoConfigsController').loadServiceConfigVersions().done(function () {
  818. self.updateConfigGroupOnServicePage();
  819. self.hide();
  820. });
  821. } else {
  822. self.updateConfigGroupOnServicePage();
  823. self.hide();
  824. }
  825. }
  826. });
  827. });
  828. });
  829. },
  830. updateConfigGroupOnServicePage: function () {
  831. var selectedConfigGroup = configsController.get('selectedConfigGroup');
  832. var managedConfigGroups = configsController.get('configGroups').slice(0);
  833. if (!controller) {
  834. controller = App.router.get('mainServiceInfoConfigsController');
  835. //controller.set('configGroups', managedConfigGroups);
  836. controller.loadConfigGroups([controller.get('content.serviceName')]);
  837. } else {
  838. controller.set('selectedService.configGroups', managedConfigGroups);
  839. }
  840. var selectEventObject = {};
  841. //check whether selectedConfigGroup exists
  842. if (selectedConfigGroup && controller.get('configGroups').someProperty('name', selectedConfigGroup.get('name'))) {
  843. selectEventObject.context = selectedConfigGroup;
  844. } else {
  845. selectEventObject.context = managedConfigGroups.findProperty('isDefault', true);
  846. }
  847. controller.selectConfigGroup(selectEventObject);
  848. },
  849. updateButtons: function () {
  850. var modified = this.get('subViewController.isHostsModified');
  851. this.set('disablePrimary', !modified);
  852. }.observes('subViewController.isHostsModified'),
  853. didInsertElement: function () {
  854. this.fitZIndex();
  855. }
  856. });
  857. }
  858. });