manage_config_groups_controller.js 13 KB

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