manage_config_groups_controller.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 serviceComponents = require('data/service_components');
  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 = serviceComponents.filterProperty('service_name',this.get('serviceName'));
  167. return components.map(function(component) {
  168. return Em.Object.create({
  169. displayName: component.display_name,
  170. componentName: component.component_name,
  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. content: content,
  275. validate: function () {
  276. var warningMessage = '';
  277. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  278. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  279. }
  280. this.set('warningMessage', warningMessage);
  281. }.observes('configGroupName'),
  282. enablePrimary: function () {
  283. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  284. }.property('warningMessage', 'configGroupName'),
  285. onPrimary: function () {
  286. if (!this.get('enablePrimary')) {
  287. return false;
  288. }
  289. var copyHsots = this.get('content.loadedHostsToGroupMap')[this.get('content.selectedConfigGroup.name')];
  290. delete this.get('content.loadedHostsToGroupMap')[this.get('content.selectedConfigGroup.name')];
  291. this.get('content.loadedHostsToGroupMap')[this.get('configGroupName')] = copyHsots;
  292. this.get('content.selectedConfigGroup').set('name', this.get('configGroupName'));
  293. this.get('content.selectedConfigGroup').set('description', this.get('configGroupDesc'));
  294. this.get('content.selectedConfigGroup.apiResponse').group_name = this.get('configGroupName');
  295. this.get('content.selectedConfigGroup.apiResponse').description = this.get('configGroupDesc');
  296. var configGroup = {
  297. ConfigGroup: this.get('content.selectedConfigGroup.apiResponse')
  298. };
  299. App.ajax.send({
  300. name: 'config_groups.update_config_group',
  301. sender: this,
  302. data: {
  303. id: this.get('content.selectedConfigGroup.id'),
  304. configGroup: configGroup
  305. }
  306. });
  307. this.hide();
  308. }
  309. });
  310. this.get('renameGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name'));
  311. this.get('renameGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description'));
  312. },
  313. /**
  314. * add new config group
  315. */
  316. addConfigGroup: function (isDuplicated) {
  317. isDuplicated = isDuplicated === true ? true : false;
  318. var content = this;
  319. var self = this;
  320. this.addGroupPopup = App.ModalPopup.show({
  321. primary: Em.I18n.t('ok'),
  322. secondary: Em.I18n.t('common.cancel'),
  323. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  324. bodyClass: Ember.View.extend({
  325. templateName: require('templates/main/service/new_config_group')
  326. }),
  327. configGroupName: "",
  328. configGroupDesc: "",
  329. content: content,
  330. warningMessage: '',
  331. validate: function () {
  332. var warningMessage = '';
  333. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName').trim())) {
  334. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  335. }
  336. this.set('warningMessage', warningMessage);
  337. }.observes('configGroupName'),
  338. enablePrimary: function () {
  339. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  340. }.property('warningMessage', 'configGroupName'),
  341. onPrimary: function () {
  342. if (!this.get('enablePrimary')) {
  343. return false;
  344. }
  345. this.get('content').set('configGroupName', this.get('configGroupName').trim());
  346. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  347. var desiredConfig = [];
  348. if (isDuplicated) {
  349. this.get('content.selectedConfigGroup.apiResponse.desired_configs').forEach(function(desired_config){
  350. var properties = {};
  351. this.get('content.selectedConfigGroup.properties').forEach(function(property){
  352. properties[property.name] = property.value;
  353. });
  354. desiredConfig.push({
  355. tag: 'version' + (new Date).getTime(),
  356. type: desired_config.type,
  357. properties : properties
  358. })
  359. }, this);
  360. }
  361. self.createNewConfigurationGroup(this.get('configGroupName').trim(),this.get('content.serviceName'),this.get('configGroupDesc'), desiredConfig, this.get('content'));
  362. }
  363. });
  364. },
  365. createNewConfigurationGroup: function(configGroupName, serviceName, configGroupDesc, desiredConfigs, sender) {
  366. App.ajax.send({
  367. name: 'config_groups.create',
  368. sender: sender,
  369. data: {
  370. 'group_name': configGroupName,
  371. 'service_id': serviceName,
  372. 'description': configGroupDesc,
  373. 'desired_configs': desiredConfigs
  374. },
  375. success: 'onAddNewConfigGroup',
  376. error: 'onAddNewConfigGroupError'
  377. });
  378. },
  379. /**
  380. * On successful api resonse for creating new config group
  381. */
  382. onAddNewConfigGroup: function (data,response) {
  383. var defaultConfigGroup = this.get('configGroups').findProperty('isDefault');
  384. var desiredConfigs = jQuery.parseJSON(response.data)[0].ConfigGroup.desired_configs;
  385. var properties = [];
  386. var configSiteTags = [];
  387. if (desiredConfigs && desiredConfigs.length > 0) {
  388. desiredConfigs.forEach(function(configs){
  389. var configSiteTag = App.ConfigSiteTag.create({
  390. site: configs.type,
  391. tag: configs.tag
  392. });
  393. configSiteTags.push(configSiteTag);
  394. });
  395. properties = this.get('selectedConfigGroup.properties');
  396. }
  397. var newConfigGroupData = App.ConfigGroup.create({
  398. id: data.resources[0].ConfigGroup.id,
  399. name: this.get('configGroupName'),
  400. description: this.get('configGroupDesc'),
  401. isDefault: false,
  402. parentConfigGroup: defaultConfigGroup,
  403. service: App.Service.find().findProperty('serviceName', this.get('serviceName')),
  404. hosts: [],
  405. configSiteTags: configSiteTags,
  406. properties: properties
  407. });
  408. this.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  409. defaultConfigGroup.get('childConfigGroups').push(newConfigGroupData);
  410. this.get('configGroups').pushObject(newConfigGroupData);
  411. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  412. this.addGroupPopup.hide();
  413. },
  414. onAddNewConfigGroupError: function() {
  415. console.warn('Can\'t add configuration group');
  416. },
  417. /**
  418. * update config group apiResponse property
  419. */
  420. updateConfigGroup: function (id) {
  421. App.ajax.send({
  422. name: 'config_groups.get_config_group_by_id',
  423. sender: this,
  424. data: {
  425. 'id': id
  426. },
  427. success: 'successLoadingConfigGroup'
  428. });
  429. },
  430. successLoadingConfigGroup: function (data) {
  431. if(data.ConfigGroup) {
  432. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  433. confGroup.set('apiResponse', data.ConfigGroup);
  434. }
  435. },
  436. /**
  437. * duplicate config group
  438. */
  439. duplicateConfigGroup: function() {
  440. if(this.get('selectedConfigGroup.isDefault')) {
  441. return;
  442. }
  443. this.addConfigGroup(true);
  444. this.get('addGroupPopup').set('header',Em.I18n.t('services.service.config_groups.duplicate_config_group_popup.header'));
  445. this.get('addGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name') + ' Copy');
  446. this.get('addGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description') + ' (Copy)');
  447. },
  448. hostsModifiedConfigGroups: function () {
  449. var groupsToClearHosts = [];
  450. var groupsToSetHosts = [];
  451. var groups = this.get('configGroups');
  452. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  453. groups.forEach(function (group) {
  454. if (!group.get('isDefault')) {
  455. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(loadedHostsToGroupMap[group.get('name')].sort()))) {
  456. groupsToClearHosts.push(group);
  457. if (group.get('hosts').length) {
  458. groupsToSetHosts.push(group);
  459. }
  460. }
  461. }
  462. });
  463. return {
  464. toClearHosts: groupsToClearHosts,
  465. toSetHosts: groupsToSetHosts
  466. };
  467. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  468. isHostsModified: function () {
  469. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  470. return !!(modifiedGroups.toClearHosts.length || modifiedGroups.toSetHosts.length);
  471. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  472. });
  473. App.InstallerManageConfigGroupsController = App.ManageConfigGroupsController.extend({
  474. name: 'installerManageConfigGroupsController',
  475. loadConfigGroups: function (serviceName) {
  476. this.set('serviceName', serviceName);
  477. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  478. var configGroups = App.router.get('wizardStep7Controller.selectedService.configGroups');
  479. configGroups.forEach(function (configGroup) {
  480. if (!configGroup.get('isDefault')) {
  481. loadedHostsToGroupMap[configGroup.name] = configGroup.hosts.slice();
  482. }
  483. });
  484. this.set('configGroups', configGroups);
  485. this.set('isLoaded', true);
  486. },
  487. /**
  488. * delete selected config group
  489. */
  490. deleteConfigGroup: function () {
  491. var selectedConfigGroup = this.get('selectedConfigGroup');
  492. if (this.get('isDeleteGroupDisabled')) {
  493. return;
  494. }
  495. //move hosts of group to default group (available hosts)
  496. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  497. this.deleteHosts();
  498. this.get('configGroups').removeObject(selectedConfigGroup);
  499. delete this.get('loadedHostsToGroupMap')[selectedConfigGroup.get('name')];
  500. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  501. },
  502. /**
  503. * rename new config group
  504. */
  505. renameConfigGroup: function () {
  506. if(this.get('selectedConfigGroup.isDefault')) {
  507. return;
  508. }
  509. var self = this;
  510. App.ModalPopup.show({
  511. primary: Em.I18n.t('ok'),
  512. secondary: Em.I18n.t('common.cancel'),
  513. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  514. bodyClass: Ember.View.extend({
  515. templateName: require('templates/main/service/new_config_group')
  516. }),
  517. configGroupName: self.get('selectedConfigGroup.name'),
  518. configGroupDesc: self.get('selectedConfigGroup.description'),
  519. warningMessage: '',
  520. validate: function () {
  521. var warningMessage = '';
  522. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  523. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  524. }
  525. this.set('warningMessage', warningMessage);
  526. }.observes('configGroupName'),
  527. enablePrimary: function () {
  528. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  529. }.property('warningMessage', 'configGroupName'),
  530. onPrimary: function () {
  531. if (!this.get('enablePrimary')) {
  532. return false;
  533. }
  534. var copyHsots = self.get('loadedHostsToGroupMap')[self.get('selectedConfigGroup.name')];
  535. delete self.get('loadedHostsToGroupMap')[self.get('selectedConfigGroup.name')];
  536. self.get('loadedHostsToGroupMap')[this.get('configGroupName')] = copyHsots;
  537. self.set('selectedConfigGroup.name', this.get('configGroupName'));
  538. self.set('selectedConfigGroup.description', this.get('configGroupDesc'));
  539. this.hide();
  540. }
  541. });
  542. },
  543. /**
  544. * add new config group
  545. */
  546. addConfigGroup: function (duplicated) {
  547. duplicated = (duplicated === true);
  548. var self = this;
  549. this.addGroupPopup = App.ModalPopup.show({
  550. primary: Em.I18n.t('ok'),
  551. secondary: Em.I18n.t('common.cancel'),
  552. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  553. bodyClass: Ember.View.extend({
  554. templateName: require('templates/main/service/new_config_group')
  555. }),
  556. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  557. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  558. warningMessage: '',
  559. validate: function () {
  560. var warningMessage = '';
  561. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  562. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  563. }
  564. this.set('warningMessage', warningMessage);
  565. }.observes('configGroupName'),
  566. enablePrimary: function () {
  567. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  568. }.property('warningMessage', 'configGroupName'),
  569. onPrimary: function () {
  570. if (!this.get('enablePrimary')) {
  571. return false;
  572. }
  573. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault');
  574. var properties = [];
  575. var newConfigGroupData = App.ConfigGroup.create({
  576. id: null,
  577. name: this.get('configGroupName'),
  578. description: this.get('configGroupDesc'),
  579. isDefault: false,
  580. parentConfigGroup: defaultConfigGroup,
  581. service: Em.Object.create({id: self.get('serviceName')}),
  582. hosts: [],
  583. configSiteTags: [],
  584. properties: []
  585. });
  586. if (duplicated) {
  587. self.get('selectedConfigGroup.properties').forEach(function(property) {
  588. var property = $.extend(true, {},property);
  589. property.set('group', newConfigGroupData);
  590. properties.push(App.ServiceConfigProperty.create(property));
  591. });
  592. newConfigGroupData.set('properties', properties);
  593. } else {
  594. newConfigGroupData.set('properties', []);
  595. }
  596. self.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  597. self.get('configGroups').pushObject(newConfigGroupData);
  598. defaultConfigGroup.get('childConfigGroups').pushObject(newConfigGroupData);
  599. this.hide();
  600. }
  601. });
  602. },
  603. duplicateConfigGroup: function() {
  604. this.addConfigGroup(true);
  605. }
  606. })