manage_config_groups_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. configGroup.desired_configs.forEach(function (config) {
  78. if (!groupToTypeToTagMap[configGroup.group_name]) {
  79. groupToTypeToTagMap[configGroup.group_name] = {}
  80. }
  81. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  82. });
  83. }, this);
  84. unusedHosts = App.Host.find().mapProperty('hostName');
  85. usedHosts.uniq().forEach(function (host) {
  86. unusedHosts = unusedHosts.without(host);
  87. }, this);
  88. defaultConfigGroup.set('childConfigGroups', configGroups);
  89. defaultConfigGroup.set('hosts', unusedHosts);
  90. this.set('configGroups', [defaultConfigGroup].concat(configGroups));
  91. this.loadProperties(groupToTypeToTagMap);
  92. this.set('isLoaded', true);
  93. }
  94. },
  95. onLoadConfigGroupsError: function () {
  96. console.error('Unable to load config groups for service.');
  97. },
  98. loadProperties: function (groupToTypeToTagMap) {
  99. var typeTagToGroupMap = {};
  100. var urlParams = [];
  101. for (var group in groupToTypeToTagMap) {
  102. var overrideTypeTags = groupToTypeToTagMap[group];
  103. for (var type in overrideTypeTags) {
  104. var tag = overrideTypeTags[type];
  105. typeTagToGroupMap[type + "///" + tag] = group;
  106. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  107. }
  108. }
  109. var params = urlParams.join('|');
  110. if (urlParams.length) {
  111. App.ajax.send({
  112. name: 'config.host_overrides',
  113. sender: this,
  114. data: {
  115. params: params,
  116. typeTagToGroupMap: typeTagToGroupMap
  117. },
  118. success: 'onLoadPropertiesSuccess'
  119. });
  120. }
  121. },
  122. onLoadPropertiesSuccess: function (data, opt, params) {
  123. data.items.forEach(function (configs) {
  124. var typeTagConfigs = [];
  125. App.config.loadedConfigurationsCache[configs.type + "_" + configs.tag] = configs.properties;
  126. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  127. for (var config in configs.properties) {
  128. typeTagConfigs.push({
  129. name: config,
  130. value: configs.properties[config]
  131. });
  132. }
  133. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  134. }, this);
  135. },
  136. showProperties: function () {
  137. var properies = this.get('selectedConfigGroup.propertiesList');
  138. if (properies) {
  139. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  140. }
  141. },
  142. /**
  143. * add hosts to group
  144. * @return {Array}
  145. */
  146. addHosts: function () {
  147. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  148. var group = this.get('selectedConfigGroup');
  149. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, [], function (selectedHosts) {
  150. if (selectedHosts) {
  151. var defaultHosts = group.get('parentConfigGroup.hosts');
  152. var configGroupHosts = group.get('hosts');
  153. selectedHosts.forEach(function (hostName) {
  154. configGroupHosts.pushObject(hostName);
  155. defaultHosts.removeObject(hostName);
  156. });
  157. }
  158. });
  159. },
  160. /**
  161. * delete hosts from group
  162. */
  163. deleteHosts: function () {
  164. var groupHosts = this.get('selectedConfigGroup.hosts');
  165. var defaultGroupHosts = this.get('selectedConfigGroup.parentConfigGroup.hosts');
  166. this.get('selectedHosts').forEach(function (hostName) {
  167. defaultGroupHosts.pushObject(hostName);
  168. groupHosts.removeObject(hostName);
  169. });
  170. this.set('selectedHosts', []);
  171. },
  172. /**
  173. * delete selected config group
  174. */
  175. deleteConfigGroup: function () {
  176. var selectedConfigGroup = this.get('selectedConfigGroup');
  177. if(selectedConfigGroup.get('name') == "Default") {
  178. return;
  179. }
  180. App.ajax.send({
  181. name: 'config_groups.delete_config_group',
  182. sender: this,
  183. data: {
  184. id: selectedConfigGroup.get('id')
  185. }
  186. });
  187. this.get('configGroups').removeObject(selectedConfigGroup);
  188. },
  189. /**
  190. * rename new config group
  191. */
  192. renameConfigGroup: function () {
  193. if(this.get('selectedConfigGroup.name') == "Default") {
  194. return;
  195. }
  196. var content = this;
  197. this.renameGroupPopup = App.ModalPopup.show({
  198. primary: Em.I18n.t('ok'),
  199. secondary: Em.I18n.t('common.cancel'),
  200. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  201. bodyClass: Ember.View.extend({
  202. template: Ember.Handlebars.compile('' +
  203. '<p>' +
  204. '{{t services.service.config_groups_popup.group_name_lable }}: {{view Ember.TextField valueBinding="configGroupName"}}' +
  205. '</p>')
  206. }),
  207. configGroupName: "",
  208. content: content,
  209. onPrimary: function () {
  210. this.get('content.selectedConfigGroup').set('name', this.get('configGroupName'));
  211. this.get('content.selectedConfigGroup.apiResponse').group_name = this.get('configGroupName');
  212. var configGroup = {
  213. ConfigGroup: this.get('content.selectedConfigGroup.apiResponse')
  214. };
  215. App.ajax.send({
  216. name: 'config_groups.update_config_group',
  217. sender: this,
  218. data: {
  219. id: this.get('content.selectedConfigGroup.id'),
  220. configGroup: configGroup
  221. }
  222. });
  223. this.hide();
  224. },
  225. onSecondary: function () {
  226. this.hide();
  227. }
  228. });
  229. },
  230. /**
  231. * add new config group
  232. */
  233. addConfigGroup: function () {
  234. var content = this;
  235. this.addGroupPopup = 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.add_config_group_popup.header'),
  239. bodyClass: Ember.View.extend({
  240. templateName: require('templates/main/service/new_config_group')
  241. }),
  242. configGroupName: "",
  243. configGroupDesc: "",
  244. content: content,
  245. onPrimary: function () {
  246. this.get('content').set('configGroupName', this.get('configGroupName'));
  247. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  248. App.ajax.send({
  249. name: 'config_groups.create',
  250. sender: this.get('content'),
  251. data: {
  252. 'group_name': this.get('configGroupName'),
  253. 'service_id': this.get('content.serviceName'),
  254. 'description': this.get('configGroupDesc')
  255. },
  256. success: 'onAddNewConfigGroup'
  257. });
  258. },
  259. onSecondary: function () {
  260. this.hide();
  261. }
  262. });
  263. },
  264. /**
  265. * On successful api resonse for creating new config group
  266. */
  267. onAddNewConfigGroup: function (data) {
  268. var loadedHostNamesMap = {};
  269. loadedHostNamesMap.length = 0;
  270. var newConfigGroupData = App.ConfigGroup.create({
  271. id: data.resources[0].ConfigGroup.id,
  272. name: this.get('configGroupName'),
  273. description: this.get('configGroupDesc'),
  274. isDefault: false,
  275. parentConfigGroup: null,
  276. service: this.get('serviceName'),
  277. hosts: [],
  278. configSiteTags: [],
  279. loadedHostNamesMap: loadedHostNamesMap,
  280. hostsModified: false
  281. });
  282. var defaultConfigGroup = this.get('configGroups').popObject();
  283. this.get('configGroups').pushObjects([newConfigGroupData, defaultConfigGroup]);
  284. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  285. this.addGroupPopup.hide();
  286. },
  287. /**
  288. * update config group apiResponse property
  289. */
  290. updateConfigGroup: function (id) {
  291. App.ajax.send({
  292. name: 'config_groups.get_config_group_by_id',
  293. sender: this,
  294. data: {
  295. 'id': id
  296. },
  297. success: 'successLoadingConfigGroup'
  298. });
  299. },
  300. successLoadingConfigGroup: function (data) {
  301. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  302. confGroup.set('apiResponse', data.ConfigGroup);
  303. },
  304. hostsModifiedConfigGroups: function() {
  305. var groups = this.get('configGroups');
  306. var hostsRemovedGroup = [];
  307. var hostsAddedGroup = [];
  308. var hostsChangedGroup = [];
  309. groups.forEach(function(g) {
  310. if (!g.get('isDefault')) {
  311. var loadedMap = g.get('loadedHostNamesMap');
  312. var current = g.get('hosts');
  313. var currentLength = current ? current.length : 0;
  314. if (currentLength == loadedMap.length) {
  315. if (currentLength>0) {
  316. var changed = false;
  317. current.forEach(function(c) {
  318. if (!changed && loadedMap[c] == null) {
  319. changed = true;
  320. }
  321. });
  322. if (changed) {
  323. hostsChangedGroup.push(g);
  324. }
  325. }
  326. } else {
  327. if (currentLength < loadedMap.length) {
  328. hostsRemovedGroup.push(g);
  329. } else {
  330. hostsAddedGroup.push(g);
  331. }
  332. }
  333. }
  334. });
  335. // First PUT removed hosts, then PUT added hosts, then changed hosts
  336. var modifiedGroups = [];
  337. modifiedGroups = modifiedGroups.concat(hostsRemovedGroup);
  338. modifiedGroups = modifiedGroups.concat(hostsAddedGroup);
  339. modifiedGroups = modifiedGroups.concat(hostsChangedGroup);
  340. return modifiedGroups;
  341. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  342. isHostsModified: function () {
  343. var groups = this.get('hostsModifiedConfigGroups');
  344. return groups && groups.length > 0;
  345. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  346. });