manage_config_groups_controller.js 13 KB

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