manage_config_groups_controller.js 13 KB

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