manage_config_groups_controller.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 hostsManagement = require('utils/hosts');
  20. var componentHelper = require('utils/component');
  21. App.ManageConfigGroupsController = Em.Controller.extend({
  22. name: 'manageConfigGroupsController',
  23. isLoaded: false,
  24. serviceName: null,
  25. configGroups: [],
  26. selectedConfigGroup: null,
  27. selectedHosts: [],
  28. loadedHostsToGroupMap: {},
  29. resortConfigGroup: function() {
  30. var configGroups = Ember.copy(this.get('configGroups'));
  31. if(configGroups.length < 2){
  32. return;
  33. }
  34. var defaultConfigGroup = configGroups.findProperty('isDefault');
  35. configGroups.removeObject(defaultConfigGroup);
  36. var sorted = configGroups.sort(function(configGroupA, configGroupB){
  37. return String(configGroupA.get('name')) >= String(configGroupB.get('name'));
  38. });
  39. sorted = [defaultConfigGroup].concat(sorted);
  40. this.removeObserver('configGroups.@each.name', this, 'resortConfigGroup');
  41. this.set('configGroups', sorted);
  42. this.addObserver('configGroups.@each.name', this, 'resortConfigGroup');
  43. }.observes('configGroups.@each.name'),
  44. loadConfigGroups: function (serviceName) {
  45. this.set('serviceName', serviceName);
  46. App.ajax.send({
  47. name: 'service.load_config_groups',
  48. data: {
  49. serviceName: serviceName
  50. },
  51. sender: this,
  52. success: 'onLoadConfigGroupsSuccess',
  53. error: 'onLoadConfigGroupsError'
  54. });
  55. },
  56. onLoadConfigGroupsSuccess: function (data) {
  57. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  58. var usedHosts = [];
  59. var unusedHosts = [];
  60. var serviceName = this.get('serviceName');
  61. var defaultConfigGroup = App.ConfigGroup.create({
  62. name: App.Service.DisplayNames[serviceName] + " Default",
  63. description: "Default cluster level " + this.get('serviceName') + " configuration",
  64. isDefault: true,
  65. parentConfigGroup: null,
  66. service: this.get('content'),
  67. configSiteTags: [],
  68. serviceName: serviceName
  69. });
  70. if (data && data.items) {
  71. var groupToTypeToTagMap = {};
  72. var configGroups = [];
  73. data.items.forEach(function (configGroup) {
  74. configGroup = configGroup.ConfigGroup;
  75. var hostNames = configGroup.hosts.mapProperty('host_name');
  76. loadedHostsToGroupMap[configGroup.group_name] = hostNames.slice();
  77. var newConfigGroup = App.ConfigGroup.create({
  78. id: configGroup.id,
  79. name: configGroup.group_name,
  80. description: configGroup.description,
  81. isDefault: false,
  82. parentConfigGroup: defaultConfigGroup,
  83. service: App.Service.find().findProperty('serviceName', configGroup.tag),
  84. hosts: hostNames,
  85. configSiteTags: [],
  86. properties: [],
  87. apiResponse: configGroup
  88. });
  89. usedHosts = usedHosts.concat(newConfigGroup.get('hosts'));
  90. configGroups.push(newConfigGroup);
  91. var newConfigGroupSiteTags = newConfigGroup.get('configSiteTags');
  92. configGroup.desired_configs.forEach(function (config) {
  93. newConfigGroupSiteTags.push(App.ConfigSiteTag.create({
  94. site: config.type,
  95. tag: config.tag
  96. }));
  97. if (!groupToTypeToTagMap[configGroup.group_name]) {
  98. groupToTypeToTagMap[configGroup.group_name] = {}
  99. }
  100. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  101. });
  102. }, this);
  103. unusedHosts = App.Host.find().mapProperty('hostName');
  104. usedHosts.uniq().forEach(function (host) {
  105. unusedHosts = unusedHosts.without(host);
  106. }, this);
  107. defaultConfigGroup.set('childConfigGroups', configGroups);
  108. defaultConfigGroup.set('hosts', unusedHosts);
  109. this.set('configGroups', [defaultConfigGroup].concat(configGroups));
  110. this.loadProperties(groupToTypeToTagMap);
  111. this.set('isLoaded', true);
  112. }
  113. },
  114. onLoadConfigGroupsError: function () {
  115. console.error('Unable to load config groups for service.');
  116. },
  117. loadProperties: function (groupToTypeToTagMap) {
  118. var typeTagToGroupMap = {};
  119. var urlParams = [];
  120. for (var group in groupToTypeToTagMap) {
  121. var overrideTypeTags = groupToTypeToTagMap[group];
  122. for (var type in overrideTypeTags) {
  123. var tag = overrideTypeTags[type];
  124. typeTagToGroupMap[type + "///" + tag] = group;
  125. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  126. }
  127. }
  128. var params = urlParams.join('|');
  129. if (urlParams.length) {
  130. App.ajax.send({
  131. name: 'config.host_overrides',
  132. sender: this,
  133. data: {
  134. params: params,
  135. typeTagToGroupMap: typeTagToGroupMap
  136. },
  137. success: 'onLoadPropertiesSuccess'
  138. });
  139. }
  140. },
  141. onLoadPropertiesSuccess: function (data, opt, params) {
  142. data.items.forEach(function (configs) {
  143. var typeTagConfigs = [];
  144. App.config.loadedConfigurationsCache[configs.type + "_" + configs.tag] = configs.properties;
  145. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  146. for (var config in configs.properties) {
  147. typeTagConfigs.push({
  148. name: config,
  149. value: configs.properties[config]
  150. });
  151. }
  152. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  153. }, this);
  154. },
  155. showProperties: function () {
  156. var properies = this.get('selectedConfigGroup.propertiesList').htmlSafe();
  157. if (properies) {
  158. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  159. }
  160. },
  161. /**
  162. * add hosts to group
  163. * @return {Array}
  164. */
  165. componentsForFilter: function() {
  166. var components = componentHelper.getInstalledComponents().filterProperty('serviceName', this.get('serviceName'));
  167. return components.map(function(component) {
  168. return Em.Object.create({
  169. displayName: component.displayName,
  170. componentName: component.id,
  171. selected: false
  172. });
  173. });
  174. }.property('serviceName'),
  175. addHosts: function () {
  176. if (this.get('selectedConfigGroup.isAddHostsDisabled')){
  177. return false;
  178. }
  179. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  180. var popupDescription = {
  181. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  182. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message').format(App.Service.DisplayNames[this.get('serviceName')])
  183. };
  184. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, this.get('componentsForFilter'), this.addHostsCallback.bind(this), popupDescription);
  185. },
  186. /**
  187. * add hosts callback
  188. */
  189. addHostsCallback: function (selectedHosts) {
  190. var group = this.get('selectedConfigGroup');
  191. if (selectedHosts) {
  192. var defaultHosts = group.get('parentConfigGroup.hosts');
  193. var configGroupHosts = group.get('hosts');
  194. selectedHosts.forEach(function (hostName) {
  195. configGroupHosts.pushObject(hostName);
  196. defaultHosts.removeObject(hostName);
  197. });
  198. }
  199. },
  200. /**
  201. * delete hosts from group
  202. */
  203. deleteHosts: function () {
  204. if (this.get('isDeleteHostsDisabled')) {
  205. return false;
  206. }
  207. var groupHosts = this.get('selectedConfigGroup.hosts');
  208. var defaultGroupHosts = this.get('selectedConfigGroup.parentConfigGroup.hosts');
  209. this.get('selectedHosts').slice().forEach(function (hostName) {
  210. defaultGroupHosts.pushObject(hostName);
  211. groupHosts.removeObject(hostName);
  212. });
  213. this.set('selectedHosts', []);
  214. },
  215. isDeleteHostsDisabled: function () {
  216. var selectedConfigGroup = this.get('selectedConfigGroup');
  217. if (selectedConfigGroup) {
  218. if (selectedConfigGroup.isDefault || this.get('selectedHosts').length === 0) {
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. }
  224. return true;
  225. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.length', 'selectedHosts.length'),
  226. /**
  227. * confirm delete config group
  228. */
  229. confirmDelete : function () {
  230. var self = this;
  231. App.showConfirmationPopup(function() {
  232. self.deleteConfigGroup();
  233. });
  234. },
  235. /**
  236. * delete selected config group
  237. */
  238. deleteConfigGroup: function () {
  239. var selectedConfigGroup = this.get('selectedConfigGroup');
  240. if (this.get('isDeleteGroupDisabled')) {
  241. return;
  242. }
  243. App.ajax.send({
  244. name: 'config_groups.delete_config_group',
  245. sender: this,
  246. data: {
  247. id: selectedConfigGroup.get('id')
  248. }
  249. });
  250. //move hosts of group to default group (available hosts)
  251. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  252. this.deleteHosts();
  253. this.get('configGroups').removeObject(selectedConfigGroup);
  254. delete this.get('loadedHostsToGroupMap')[selectedConfigGroup.get('name')];
  255. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  256. },
  257. /**
  258. * rename new config group
  259. */
  260. renameConfigGroup: function () {
  261. if(this.get('selectedConfigGroup.isDefault')) {
  262. return;
  263. }
  264. var content = this;
  265. var self = this;
  266. this.renameGroupPopup = App.ModalPopup.show({
  267. primary: Em.I18n.t('ok'),
  268. secondary: Em.I18n.t('common.cancel'),
  269. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  270. bodyClass: Ember.View.extend({
  271. templateName: require('templates/main/service/new_config_group')
  272. }),
  273. configGroupName: "",
  274. configGroupDesc: "",
  275. content: content,
  276. isDescriptionDirty: false,
  277. validate: function () {
  278. var warningMessage = '';
  279. var originalGroup = self.get('selectedConfigGroup');
  280. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  281. this.set('isDescriptionDirty', true);
  282. }
  283. if (originalGroup.get('name') === this.get('configGroupName')) {
  284. if (this.get('isDescriptionDirty')) {
  285. warningMessage = '';
  286. } else {
  287. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  288. }
  289. } else {
  290. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  291. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  292. }
  293. }
  294. this.set('warningMessage', warningMessage);
  295. }.observes('configGroupName', 'configGroupDesc'),
  296. enablePrimary: function () {
  297. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  298. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  299. onPrimary: function () {
  300. if (!this.get('enablePrimary')) {
  301. return false;
  302. }
  303. var copyHsots = this.get('content.loadedHostsToGroupMap')[this.get('content.selectedConfigGroup.name')];
  304. delete this.get('content.loadedHostsToGroupMap')[this.get('content.selectedConfigGroup.name')];
  305. this.get('content.loadedHostsToGroupMap')[this.get('configGroupName')] = copyHsots;
  306. this.get('content.selectedConfigGroup').set('name', this.get('configGroupName'));
  307. this.get('content.selectedConfigGroup').set('description', this.get('configGroupDesc'));
  308. this.get('content.selectedConfigGroup.apiResponse').group_name = this.get('configGroupName');
  309. this.get('content.selectedConfigGroup.apiResponse').description = this.get('configGroupDesc');
  310. var configGroup = {
  311. ConfigGroup: this.get('content.selectedConfigGroup.apiResponse')
  312. };
  313. App.ajax.send({
  314. name: 'config_groups.update_config_group',
  315. sender: this,
  316. data: {
  317. id: this.get('content.selectedConfigGroup.id'),
  318. configGroup: configGroup
  319. }
  320. });
  321. this.hide();
  322. }
  323. });
  324. this.get('renameGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name'));
  325. this.get('renameGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description'));
  326. },
  327. /**
  328. * add new config group
  329. */
  330. addConfigGroup: function (isDuplicated) {
  331. isDuplicated = isDuplicated === true ? true : false;
  332. var content = this;
  333. var self = this;
  334. this.addGroupPopup = App.ModalPopup.show({
  335. primary: Em.I18n.t('ok'),
  336. secondary: Em.I18n.t('common.cancel'),
  337. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  338. bodyClass: Ember.View.extend({
  339. templateName: require('templates/main/service/new_config_group')
  340. }),
  341. configGroupName: "",
  342. configGroupDesc: "",
  343. content: content,
  344. warningMessage: '',
  345. validate: function () {
  346. var warningMessage = '';
  347. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName').trim())) {
  348. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  349. }
  350. this.set('warningMessage', warningMessage);
  351. }.observes('configGroupName'),
  352. enablePrimary: function () {
  353. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  354. }.property('warningMessage', 'configGroupName'),
  355. onPrimary: function () {
  356. if (!this.get('enablePrimary')) {
  357. return false;
  358. }
  359. this.get('content').set('configGroupName', this.get('configGroupName').trim());
  360. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  361. var desiredConfig = [];
  362. if (isDuplicated) {
  363. this.get('content.selectedConfigGroup.apiResponse.desired_configs').forEach(function(desired_config){
  364. var properties = {};
  365. this.get('content.selectedConfigGroup.properties').forEach(function(property){
  366. properties[property.name] = property.value;
  367. });
  368. desiredConfig.push({
  369. tag: 'version' + (new Date).getTime(),
  370. type: desired_config.type,
  371. properties : properties
  372. })
  373. }, this);
  374. }
  375. self.createNewConfigurationGroup(this.get('configGroupName').trim(),this.get('content.serviceName'),this.get('configGroupDesc'), desiredConfig, this.get('content'));
  376. }
  377. });
  378. },
  379. createNewConfigurationGroup: function(configGroupName, serviceName, configGroupDesc, desiredConfigs, sender) {
  380. App.ajax.send({
  381. name: 'config_groups.create',
  382. sender: sender,
  383. data: {
  384. 'group_name': configGroupName,
  385. 'service_id': serviceName,
  386. 'description': configGroupDesc,
  387. 'desired_configs': desiredConfigs
  388. },
  389. success: 'onAddNewConfigGroup',
  390. error: 'onAddNewConfigGroupError'
  391. });
  392. },
  393. /**
  394. * On successful api resonse for creating new config group
  395. */
  396. onAddNewConfigGroup: function (data,response) {
  397. var defaultConfigGroup = this.get('configGroups').findProperty('isDefault');
  398. var desiredConfigs = jQuery.parseJSON(response.data)[0].ConfigGroup.desired_configs;
  399. var properties = [];
  400. var configSiteTags = [];
  401. if (desiredConfigs && desiredConfigs.length > 0) {
  402. desiredConfigs.forEach(function(configs){
  403. var configSiteTag = App.ConfigSiteTag.create({
  404. site: configs.type,
  405. tag: configs.tag
  406. });
  407. configSiteTags.push(configSiteTag);
  408. });
  409. properties = this.get('selectedConfigGroup.properties');
  410. }
  411. var newConfigGroupData = App.ConfigGroup.create({
  412. id: data.resources[0].ConfigGroup.id,
  413. name: this.get('configGroupName'),
  414. description: this.get('configGroupDesc'),
  415. isDefault: false,
  416. parentConfigGroup: defaultConfigGroup,
  417. service: App.Service.find().findProperty('serviceName', this.get('serviceName')),
  418. hosts: [],
  419. configSiteTags: configSiteTags,
  420. properties: properties
  421. });
  422. this.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  423. defaultConfigGroup.get('childConfigGroups').push(newConfigGroupData);
  424. this.get('configGroups').pushObject(newConfigGroupData);
  425. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  426. this.addGroupPopup.hide();
  427. },
  428. onAddNewConfigGroupError: function() {
  429. console.warn('Can\'t add configuration group');
  430. },
  431. /**
  432. * update config group apiResponse property
  433. */
  434. updateConfigGroup: function (id) {
  435. App.ajax.send({
  436. name: 'config_groups.get_config_group_by_id',
  437. sender: this,
  438. data: {
  439. 'id': id
  440. },
  441. success: 'successLoadingConfigGroup'
  442. });
  443. },
  444. successLoadingConfigGroup: function (data) {
  445. if(data.ConfigGroup) {
  446. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  447. confGroup.set('apiResponse', data.ConfigGroup);
  448. }
  449. },
  450. /**
  451. * duplicate config group
  452. */
  453. duplicateConfigGroup: function() {
  454. if(this.get('selectedConfigGroup.isDefault')) {
  455. return;
  456. }
  457. this.addConfigGroup(true);
  458. this.get('addGroupPopup').set('header',Em.I18n.t('services.service.config_groups.duplicate_config_group_popup.header'));
  459. this.get('addGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name') + ' Copy');
  460. this.get('addGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description') + ' (Copy)');
  461. },
  462. hostsModifiedConfigGroups: function () {
  463. var groupsToClearHosts = [];
  464. var groupsToSetHosts = [];
  465. var groups = this.get('configGroups');
  466. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  467. groups.forEach(function (group) {
  468. if (!group.get('isDefault')) {
  469. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(loadedHostsToGroupMap[group.get('name')].sort()))) {
  470. groupsToClearHosts.push(group);
  471. if (group.get('hosts').length) {
  472. groupsToSetHosts.push(group);
  473. }
  474. }
  475. }
  476. });
  477. return {
  478. toClearHosts: groupsToClearHosts,
  479. toSetHosts: groupsToSetHosts
  480. };
  481. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  482. isHostsModified: function () {
  483. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  484. return !!(modifiedGroups.toClearHosts.length || modifiedGroups.toSetHosts.length);
  485. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  486. });
  487. App.InstallerManageConfigGroupsController = App.ManageConfigGroupsController.extend({
  488. name: 'installerManageConfigGroupsController',
  489. loadConfigGroups: function (serviceName) {
  490. this.set('serviceName', serviceName);
  491. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  492. var configGroups = App.router.get('wizardStep7Controller.selectedService.configGroups');
  493. configGroups.forEach(function (configGroup) {
  494. if (!configGroup.get('isDefault')) {
  495. loadedHostsToGroupMap[configGroup.name] = configGroup.hosts.slice();
  496. }
  497. });
  498. this.set('configGroups', configGroups);
  499. this.set('isLoaded', true);
  500. },
  501. /**
  502. * delete selected config group
  503. */
  504. deleteConfigGroup: function () {
  505. var selectedConfigGroup = this.get('selectedConfigGroup');
  506. if (this.get('isDeleteGroupDisabled')) {
  507. return;
  508. }
  509. //move hosts of group to default group (available hosts)
  510. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  511. this.deleteHosts();
  512. this.get('configGroups').removeObject(selectedConfigGroup);
  513. delete this.get('loadedHostsToGroupMap')[selectedConfigGroup.get('name')];
  514. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  515. },
  516. /**
  517. * rename new config group
  518. */
  519. renameConfigGroup: function () {
  520. if(this.get('selectedConfigGroup.isDefault')) {
  521. return;
  522. }
  523. var self = this;
  524. this.renameGroupPopup = App.ModalPopup.show({
  525. primary: Em.I18n.t('ok'),
  526. secondary: Em.I18n.t('common.cancel'),
  527. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  528. bodyClass: Ember.View.extend({
  529. templateName: require('templates/main/service/new_config_group')
  530. }),
  531. configGroupName: self.get('selectedConfigGroup.name'),
  532. configGroupDesc: self.get('selectedConfigGroup.description'),
  533. warningMessage: '',
  534. isDescriptionDirty: false,
  535. validate: function () {
  536. var warningMessage = '';
  537. var originalGroup = self.get('selectedConfigGroup');
  538. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  539. this.set('isDescriptionDirty', true);
  540. }
  541. if (originalGroup.get('name').trim() === this.get('configGroupName').trim()) {
  542. if (this.get('isDescriptionDirty')) {
  543. warningMessage = '';
  544. } else {
  545. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  546. }
  547. } else {
  548. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  549. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  550. }
  551. }
  552. this.set('warningMessage', warningMessage);
  553. }.observes('configGroupName', 'configGroupDesc'),
  554. enablePrimary: function () {
  555. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  556. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  557. onPrimary: function () {
  558. if (!this.get('enablePrimary')) {
  559. return false;
  560. }
  561. var copyHsots = self.get('loadedHostsToGroupMap')[self.get('selectedConfigGroup.name')];
  562. delete self.get('loadedHostsToGroupMap')[self.get('selectedConfigGroup.name')];
  563. self.get('loadedHostsToGroupMap')[this.get('configGroupName')] = copyHsots;
  564. self.set('selectedConfigGroup.name', this.get('configGroupName'));
  565. self.set('selectedConfigGroup.description', this.get('configGroupDesc'));
  566. this.hide();
  567. }
  568. });
  569. this.get('renameGroupPopup').validate();
  570. },
  571. /**
  572. * add new config group
  573. */
  574. addConfigGroup: function (duplicated) {
  575. duplicated = (duplicated === true);
  576. var self = this;
  577. this.addGroupPopup = App.ModalPopup.show({
  578. primary: Em.I18n.t('ok'),
  579. secondary: Em.I18n.t('common.cancel'),
  580. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  581. bodyClass: Ember.View.extend({
  582. templateName: require('templates/main/service/new_config_group')
  583. }),
  584. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  585. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  586. warningMessage: '',
  587. validate: function () {
  588. var warningMessage = '';
  589. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName').trim())) {
  590. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  591. }
  592. this.set('warningMessage', warningMessage);
  593. }.observes('configGroupName'),
  594. enablePrimary: function () {
  595. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  596. }.property('warningMessage', 'configGroupName'),
  597. onPrimary: function () {
  598. if (!this.get('enablePrimary')) {
  599. return false;
  600. }
  601. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault');
  602. var properties = [];
  603. var newConfigGroupData = App.ConfigGroup.create({
  604. id: null,
  605. name: this.get('configGroupName').trim(),
  606. description: this.get('configGroupDesc'),
  607. isDefault: false,
  608. parentConfigGroup: defaultConfigGroup,
  609. service: Em.Object.create({id: self.get('serviceName')}),
  610. hosts: [],
  611. configSiteTags: [],
  612. properties: []
  613. });
  614. if (duplicated) {
  615. self.get('selectedConfigGroup.properties').forEach(function(property) {
  616. var property = $.extend(false, {},property);
  617. property.set('group', newConfigGroupData);
  618. properties.push(App.ServiceConfigProperty.create(property));
  619. });
  620. newConfigGroupData.set('properties', properties);
  621. } else {
  622. newConfigGroupData.set('properties', []);
  623. }
  624. self.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  625. self.get('configGroups').pushObject(newConfigGroupData);
  626. defaultConfigGroup.get('childConfigGroups').pushObject(newConfigGroupData);
  627. this.hide();
  628. }
  629. });
  630. },
  631. duplicateConfigGroup: function() {
  632. this.addConfigGroup(true);
  633. }
  634. })