manage_config_groups_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 () {
  273. var content = this;
  274. var self = this;
  275. this.addGroupPopup = App.ModalPopup.show({
  276. primary: Em.I18n.t('ok'),
  277. secondary: Em.I18n.t('common.cancel'),
  278. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  279. bodyClass: Ember.View.extend({
  280. templateName: require('templates/main/service/new_config_group')
  281. }),
  282. configGroupName: "",
  283. configGroupDesc: "",
  284. content: content,
  285. warningMessage: '',
  286. vaildate: function () {
  287. var warningMessage = '';
  288. if (self.get('allConfigGroupsNames').contains(this.get('configGroupName'))) {
  289. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  290. }
  291. this.set('warningMessage', warningMessage);
  292. }.observes('configGroupName'),
  293. enablePrimary: function () {
  294. return this.get('configGroupName').length > 0 && !this.get('warningMessage');
  295. }.property('warningMessage', 'configGroupName'),
  296. onPrimary: function () {
  297. this.get('content').set('configGroupName', this.get('configGroupName'));
  298. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  299. App.ajax.send({
  300. name: 'config_groups.create',
  301. sender: this.get('content'),
  302. data: {
  303. 'group_name': this.get('configGroupName'),
  304. 'service_id': this.get('content.serviceName'),
  305. 'description': this.get('configGroupDesc')
  306. },
  307. success: 'onAddNewConfigGroup'
  308. });
  309. },
  310. onSecondary: function () {
  311. this.hide();
  312. }
  313. });
  314. },
  315. /**
  316. * On successful api resonse for creating new config group
  317. */
  318. onAddNewConfigGroup: function (data) {
  319. var defaultConfigGroup = this.get('configGroups').findProperty('isDefault');
  320. var newConfigGroupData = App.ConfigGroup.create({
  321. id: data.resources[0].ConfigGroup.id,
  322. name: this.get('configGroupName'),
  323. description: this.get('configGroupDesc'),
  324. isDefault: false,
  325. parentConfigGroup: defaultConfigGroup,
  326. service: App.Service.find().findProperty('serviceName', this.get('serviceName')),
  327. hosts: [],
  328. configSiteTags: []
  329. });
  330. this.get('loadedHostsToGroupMap')[newConfigGroupData.get('name')] = [];
  331. defaultConfigGroup.get('childConfigGroups').push(newConfigGroupData);
  332. this.get('configGroups').pushObject(newConfigGroupData);
  333. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  334. this.addGroupPopup.hide();
  335. },
  336. /**
  337. * update config group apiResponse property
  338. */
  339. updateConfigGroup: function (id) {
  340. App.ajax.send({
  341. name: 'config_groups.get_config_group_by_id',
  342. sender: this,
  343. data: {
  344. 'id': id
  345. },
  346. success: 'successLoadingConfigGroup'
  347. });
  348. },
  349. successLoadingConfigGroup: function (data) {
  350. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  351. confGroup.set('apiResponse', data.ConfigGroup);
  352. },
  353. /**
  354. * duplicate config group
  355. */
  356. duplicateConfigGroup: function() {
  357. this.addConfigGroup();
  358. this.get('addGroupPopup').set('header',Em.I18n.t('services.service.config_groups.duplicate_config_group_popup.header'));
  359. this.get('addGroupPopup').set('configGroupName', this.get('selectedConfigGroup.name') + ' Copy');
  360. this.get('addGroupPopup').set('configGroupDesc', this.get('selectedConfigGroup.description') + ' (Copy)');
  361. },
  362. hostsModifiedConfigGroups: function () {
  363. var groupsToClearHosts = [];
  364. var groupsToSetHosts = [];
  365. var groups = this.get('configGroups');
  366. var loadedHostsToGroupMap = this.get('loadedHostsToGroupMap');
  367. groups.forEach(function (group) {
  368. if (!group.get('isDefault')) {
  369. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(loadedHostsToGroupMap[group.get('name')].sort()))) {
  370. groupsToClearHosts.push(group);
  371. if (group.get('hosts').length) {
  372. groupsToSetHosts.push(group);
  373. }
  374. }
  375. }
  376. });
  377. return {
  378. toClearHosts: groupsToClearHosts,
  379. toSetHosts: groupsToSetHosts
  380. };
  381. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  382. isHostsModified: function () {
  383. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  384. return !!(modifiedGroups.toClearHosts.length || modifiedGroups.toSetHosts.length);
  385. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  386. });