manage_config_groups_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. allConfigGroupsNames: [],
  30. loadConfigGroups: function (serviceName) {
  31. this.set('serviceName', serviceName);
  32. App.ajax.send({
  33. name: 'service.load_config_groups',
  34. sender: this,
  35. success: 'onLoadConfigGroupsSuccess',
  36. error: 'onLoadConfigGroupsError'
  37. });
  38. },
  39. onLoadConfigGroupsSuccess: function (data) {
  40. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  41. var usedHosts = [];
  42. var unusedHosts = [];
  43. var defaultConfigGroup = App.ConfigGroup.create({
  44. name: "Default",
  45. description: "Default cluster level " + this.get('serviceName') + " configuration",
  46. isDefault: true,
  47. parentConfigGroup: null,
  48. service: this.get('content'),
  49. configSiteTags: []
  50. });
  51. if (data && data.items) {
  52. var groupToTypeToTagMap = {};
  53. var configGroups = [];
  54. var serviceName = this.get('serviceName');
  55. var allConfigGroupsNames = [];
  56. data.items.forEach(function (configGroup) {
  57. configGroup = configGroup.ConfigGroup;
  58. allConfigGroupsNames.push(configGroup.group_name);
  59. if (configGroup.tag === serviceName) {
  60. var hostNames = configGroup.hosts.mapProperty('host_name');
  61. loadedHostsToGroupMap[configGroup.group_name] = hostNames.slice();
  62. var newConfigGroup = App.ConfigGroup.create({
  63. id: configGroup.id,
  64. name: configGroup.group_name,
  65. description: configGroup.description,
  66. isDefault: false,
  67. parentConfigGroup: defaultConfigGroup,
  68. service: App.Service.find().findProperty('serviceName', configGroup.tag),
  69. hosts: hostNames,
  70. configSiteTags: [],
  71. properties: [],
  72. apiResponse: configGroup
  73. });
  74. usedHosts = usedHosts.concat(newConfigGroup.get('hosts'));
  75. configGroups.push(newConfigGroup);
  76. var newConfigGroupSiteTags = newConfigGroup.get('configSiteTags');
  77. configGroup.desired_configs.forEach(function (config) {
  78. newConfigGroupSiteTags.push(App.ConfigSiteTag.create({
  79. site: config.type,
  80. tag: config.tag
  81. }));
  82. if (!groupToTypeToTagMap[configGroup.group_name]) {
  83. groupToTypeToTagMap[configGroup.group_name] = {}
  84. }
  85. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  86. });
  87. }
  88. }, this);
  89. this.set('allConfigGroupsNames', allConfigGroupsNames);
  90. unusedHosts = App.Host.find().mapProperty('hostName');
  91. usedHosts.uniq().forEach(function (host) {
  92. unusedHosts = unusedHosts.without(host);
  93. }, this);
  94. defaultConfigGroup.set('childConfigGroups', configGroups);
  95. defaultConfigGroup.set('hosts', unusedHosts);
  96. this.set('configGroups', [defaultConfigGroup].concat(configGroups));
  97. this.loadProperties(groupToTypeToTagMap);
  98. this.set('isLoaded', true);
  99. }
  100. },
  101. onLoadConfigGroupsError: function () {
  102. console.error('Unable to load config groups for service.');
  103. },
  104. loadProperties: function (groupToTypeToTagMap) {
  105. var typeTagToGroupMap = {};
  106. var urlParams = [];
  107. for (var group in groupToTypeToTagMap) {
  108. var overrideTypeTags = groupToTypeToTagMap[group];
  109. for (var type in overrideTypeTags) {
  110. var tag = overrideTypeTags[type];
  111. typeTagToGroupMap[type + "///" + tag] = group;
  112. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  113. }
  114. }
  115. var params = urlParams.join('|');
  116. if (urlParams.length) {
  117. App.ajax.send({
  118. name: 'config.host_overrides',
  119. sender: this,
  120. data: {
  121. params: params,
  122. typeTagToGroupMap: typeTagToGroupMap
  123. },
  124. success: 'onLoadPropertiesSuccess'
  125. });
  126. }
  127. },
  128. onLoadPropertiesSuccess: function (data, opt, params) {
  129. data.items.forEach(function (configs) {
  130. var typeTagConfigs = [];
  131. App.config.loadedConfigurationsCache[configs.type + "_" + configs.tag] = configs.properties;
  132. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  133. for (var config in configs.properties) {
  134. typeTagConfigs.push({
  135. name: config,
  136. value: configs.properties[config]
  137. });
  138. }
  139. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  140. }, this);
  141. },
  142. showProperties: function () {
  143. var properies = this.get('selectedConfigGroup.propertiesList');
  144. if (properies) {
  145. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  146. }
  147. },
  148. /**
  149. * add hosts to group
  150. * @return {Array}
  151. */
  152. componentsForFilter: function() {
  153. var components = serviceComponents.filterProperty('service_name',this.get('serviceName'));
  154. return components.map(function(component) {
  155. return Em.Object.create({
  156. displayName: component.display_name,
  157. componentName: component.component_name,
  158. selected: false
  159. });
  160. });
  161. }.property('serviceName'),
  162. addHosts: function () {
  163. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  164. var popupDescription = {
  165. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  166. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message')
  167. };
  168. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, [], this.addHostsCallback.bind(this), popupDescription);
  169. },
  170. /**
  171. * add hosts callback
  172. */
  173. addHostsCallback: function (selectedHosts) {
  174. var group = this.get('selectedConfigGroup');
  175. if (selectedHosts) {
  176. var defaultHosts = group.get('parentConfigGroup.hosts');
  177. var configGroupHosts = group.get('hosts');
  178. selectedHosts.forEach(function (hostName) {
  179. configGroupHosts.pushObject(hostName);
  180. defaultHosts.removeObject(hostName);
  181. });
  182. }
  183. },
  184. /**
  185. * delete hosts from group
  186. */
  187. deleteHosts: function () {
  188. var groupHosts = this.get('selectedConfigGroup.hosts');
  189. var defaultGroupHosts = this.get('selectedConfigGroup.parentConfigGroup.hosts');
  190. this.get('selectedHosts').slice().forEach(function (hostName) {
  191. defaultGroupHosts.pushObject(hostName);
  192. groupHosts.removeObject(hostName);
  193. });
  194. this.set('selectedHosts', []);
  195. },
  196. /**
  197. * confirm delete config group
  198. */
  199. confirmDelete : function () {
  200. var self = this;
  201. App.showConfirmationPopup(function() {
  202. self.deleteConfigGroup();
  203. });
  204. },
  205. /**
  206. * delete selected config group
  207. */
  208. deleteConfigGroup: function () {
  209. var selectedConfigGroup = this.get('selectedConfigGroup');
  210. if (this.get('isDeleteGroupDisabled')) {
  211. return;
  212. }
  213. App.ajax.send({
  214. name: 'config_groups.delete_config_group',
  215. sender: this,
  216. data: {
  217. id: selectedConfigGroup.get('id')
  218. }
  219. });
  220. //move hosts of group to default group (available hosts)
  221. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  222. this.deleteHosts();
  223. this.get('configGroups').removeObject(selectedConfigGroup);
  224. delete this.get('loadedHostsToGroupMap')[selectedConfigGroup.get('name')];
  225. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  226. },
  227. /**
  228. * rename new config group
  229. */
  230. renameConfigGroup: function () {
  231. if(this.get('selectedConfigGroup.name') == "Default") {
  232. return;
  233. }
  234. var content = this;
  235. this.renameGroupPopup = App.ModalPopup.show({
  236. primary: Em.I18n.t('ok'),
  237. secondary: Em.I18n.t('common.cancel'),
  238. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  239. bodyClass: Ember.View.extend({
  240. templateName: require('templates/main/service/new_config_group')
  241. }),
  242. configGroupName: "",
  243. content: content,
  244. onPrimary: function () {
  245. this.get('content.selectedConfigGroup').set('name', this.get('configGroupName'));
  246. this.get('content.selectedConfigGroup').set('description', this.get('configGroupDesc'));
  247. this.get('content.selectedConfigGroup.apiResponse').group_name = this.get('configGroupName');
  248. this.get('content.selectedConfigGroup.apiResponse').description = this.get('configGroupDesc');
  249. var configGroup = {
  250. ConfigGroup: this.get('content.selectedConfigGroup.apiResponse')
  251. };
  252. App.ajax.send({
  253. name: 'config_groups.update_config_group',
  254. sender: this,
  255. data: {
  256. id: this.get('content.selectedConfigGroup.id'),
  257. configGroup: configGroup
  258. }
  259. });
  260. this.hide();
  261. },
  262. onSecondary: function () {
  263. this.hide();
  264. }
  265. });
  266. this.get('renameGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name'));
  267. this.get('renameGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description'));
  268. },
  269. /**
  270. * add new config group
  271. */
  272. addConfigGroup: function (isDuplicated) {
  273. isDuplicated = isDuplicated === true ? true : false;
  274. var content = this;
  275. var self = this;
  276. this.addGroupPopup = App.ModalPopup.show({
  277. primary: Em.I18n.t('ok'),
  278. secondary: Em.I18n.t('common.cancel'),
  279. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  280. bodyClass: Ember.View.extend({
  281. templateName: require('templates/main/service/new_config_group')
  282. }),
  283. configGroupName: "",
  284. configGroupDesc: "",
  285. content: content,
  286. warningMessage: '',
  287. vaildate: function () {
  288. var warningMessage = '';
  289. if (self.get('allConfigGroupsNames').contains(this.get('configGroupName'))) {
  290. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  291. }
  292. this.set('warningMessage', warningMessage);
  293. }.observes('configGroupName'),
  294. enablePrimary: function () {
  295. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  296. }.property('warningMessage', 'configGroupName'),
  297. onPrimary: function () {
  298. this.get('content').set('configGroupName', this.get('configGroupName'));
  299. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  300. var desiredConfig = [];
  301. if (isDuplicated) {
  302. this.get('content.selectedConfigGroup.apiResponse.desired_configs').forEach(function(desired_config){
  303. var properties = {};
  304. this.get('content.selectedConfigGroup.properties').forEach(function(property){
  305. properties[property.name] = property.value;
  306. });
  307. desiredConfig.push({
  308. tag: 'version' + (new Date).getTime(),
  309. type: desired_config.type,
  310. properties : properties
  311. })
  312. }, this);
  313. }
  314. self.createNewConfigurationGroup(this.get('configGroupName'),this.get('content.serviceName'),this.get('configGroupDesc'), desiredConfig, this.get('content'));
  315. },
  316. onSecondary: function () {
  317. this.hide();
  318. }
  319. });
  320. },
  321. createNewConfigurationGroup: function(configGroupName, serviceName, configGroupDesc, desiredConfigs, sender) {
  322. App.ajax.send({
  323. name: 'config_groups.create',
  324. sender: sender,
  325. data: {
  326. 'group_name': configGroupName,
  327. 'service_id': serviceName,
  328. 'description': configGroupDesc,
  329. 'desired_configs': desiredConfigs
  330. },
  331. success: 'onAddNewConfigGroup',
  332. error: 'onAddNewConfigGroupError'
  333. });
  334. },
  335. /**
  336. * On successful api resonse for creating new config group
  337. */
  338. onAddNewConfigGroup: function (data,response) {
  339. var defaultConfigGroup = this.get('configGroups').findProperty('isDefault');
  340. var desiredConfigs = jQuery.parseJSON(response.data)[0].ConfigGroup.desired_configs;
  341. var properties = [];
  342. var configSiteTags = [];
  343. if (desiredConfigs && desiredConfigs.length > 0) {
  344. desiredConfigs.forEach(function(configs){
  345. var configSiteTag = {
  346. site: configs.type,
  347. tag: configs.tag
  348. }
  349. configSiteTags.push(configSiteTag);
  350. });
  351. properties = this.get('selectedConfigGroup.properties');
  352. }
  353. var newConfigGroupData = App.ConfigGroup.create({
  354. id: data.resources[0].ConfigGroup.id,
  355. name: this.get('configGroupName'),
  356. description: this.get('configGroupDesc'),
  357. isDefault: false,
  358. parentConfigGroup: defaultConfigGroup,
  359. service: App.Service.find().findProperty('serviceName', this.get('serviceName')),
  360. hosts: [],
  361. configSiteTags: configSiteTags,
  362. properties: properties
  363. });
  364. this.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  365. defaultConfigGroup.get('childConfigGroups').push(newConfigGroupData);
  366. this.get('configGroups').pushObject(newConfigGroupData);
  367. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  368. this.addGroupPopup.hide();
  369. },
  370. onAddNewConfigGroupError: function() {
  371. console.warn('Can\'t add configuration group');
  372. },
  373. /**
  374. * update config group apiResponse property
  375. */
  376. updateConfigGroup: function (id) {
  377. App.ajax.send({
  378. name: 'config_groups.get_config_group_by_id',
  379. sender: this,
  380. data: {
  381. 'id': id
  382. },
  383. success: 'successLoadingConfigGroup'
  384. });
  385. },
  386. successLoadingConfigGroup: function (data) {
  387. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  388. confGroup.set('apiResponse', data.ConfigGroup);
  389. },
  390. /**
  391. * duplicate config group
  392. */
  393. duplicateConfigGroup: function() {
  394. this.addConfigGroup(true);
  395. this.get('addGroupPopup').set('header',Em.I18n.t('services.service.config_groups.duplicate_config_group_popup.header'));
  396. this.get('addGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name') + ' Copy');
  397. this.get('addGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description') + ' (Copy)');
  398. },
  399. hostsModifiedConfigGroups: function () {
  400. var groupsToClearHosts = [];
  401. var groupsToSetHosts = [];
  402. var groups = this.get('configGroups');
  403. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  404. groups.forEach(function (group) {
  405. if (!group.get('isDefault')) {
  406. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(loadedHostsToGroupMap[group.get('name')].sort()))) {
  407. groupsToClearHosts.push(group);
  408. if (group.get('hosts').length) {
  409. groupsToSetHosts.push(group);
  410. }
  411. }
  412. }
  413. });
  414. return {
  415. toClearHosts: groupsToClearHosts,
  416. toSetHosts: groupsToSetHosts
  417. };
  418. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  419. isHostsModified: function () {
  420. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  421. return !!(modifiedGroups.toClearHosts.length || modifiedGroups.toSetHosts.length);
  422. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  423. });