manage_config_groups_controller.js 31 KB

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