manage_config_groups_controller.js 16 KB

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