manage_config_groups_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * delete selected config group
  179. */
  180. deleteConfigGroup: function () {
  181. var selectedConfigGroup = this.get('selectedConfigGroup');
  182. if(selectedConfigGroup.get('name') == "Default") {
  183. return;
  184. }
  185. App.ajax.send({
  186. name: 'config_groups.delete_config_group',
  187. sender: this,
  188. data: {
  189. id: selectedConfigGroup.get('id')
  190. }
  191. });
  192. this.get('configGroups').removeObject(selectedConfigGroup);
  193. },
  194. /**
  195. * rename new config group
  196. */
  197. renameConfigGroup: function () {
  198. if(this.get('selectedConfigGroup.name') == "Default") {
  199. return;
  200. }
  201. var content = this;
  202. this.renameGroupPopup = App.ModalPopup.show({
  203. primary: Em.I18n.t('ok'),
  204. secondary: Em.I18n.t('common.cancel'),
  205. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  206. bodyClass: Ember.View.extend({
  207. template: Ember.Handlebars.compile('' +
  208. '<p>' +
  209. '{{t services.service.config_groups_popup.group_name_lable }}: {{view Ember.TextField valueBinding="configGroupName"}}' +
  210. '</p>')
  211. }),
  212. configGroupName: "",
  213. content: content,
  214. onPrimary: function () {
  215. this.get('content.selectedConfigGroup').set('name', this.get('configGroupName'));
  216. this.get('content.selectedConfigGroup.apiResponse').group_name = this.get('configGroupName');
  217. var configGroup = {
  218. ConfigGroup: this.get('content.selectedConfigGroup.apiResponse')
  219. };
  220. App.ajax.send({
  221. name: 'config_groups.update_config_group',
  222. sender: this,
  223. data: {
  224. id: this.get('content.selectedConfigGroup.id'),
  225. configGroup: configGroup
  226. }
  227. });
  228. this.hide();
  229. },
  230. onSecondary: function () {
  231. this.hide();
  232. }
  233. });
  234. },
  235. /**
  236. * add new config group
  237. */
  238. addConfigGroup: function () {
  239. var content = this;
  240. this.addGroupPopup = App.ModalPopup.show({
  241. primary: Em.I18n.t('ok'),
  242. secondary: Em.I18n.t('common.cancel'),
  243. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  244. bodyClass: Ember.View.extend({
  245. templateName: require('templates/main/service/new_config_group')
  246. }),
  247. configGroupName: "",
  248. configGroupDesc: "",
  249. content: content,
  250. onPrimary: function () {
  251. this.get('content').set('configGroupName', this.get('configGroupName'));
  252. this.get('content').set('configGroupDesc', this.get('configGroupDesc'));
  253. App.ajax.send({
  254. name: 'config_groups.create',
  255. sender: this.get('content'),
  256. data: {
  257. 'group_name': this.get('configGroupName'),
  258. 'service_id': this.get('content.serviceName'),
  259. 'description': this.get('configGroupDesc')
  260. },
  261. success: 'onAddNewConfigGroup'
  262. });
  263. },
  264. onSecondary: function () {
  265. this.hide();
  266. }
  267. });
  268. },
  269. /**
  270. * On successful api resonse for creating new config group
  271. */
  272. onAddNewConfigGroup: function (data) {
  273. var loadedHostNamesMap = {};
  274. loadedHostNamesMap.length = 0;
  275. var newConfigGroupData = App.ConfigGroup.create({
  276. id: data.resources[0].ConfigGroup.id,
  277. name: this.get('configGroupName'),
  278. description: this.get('configGroupDesc'),
  279. isDefault: false,
  280. parentConfigGroup: null,
  281. service: this.get('serviceName'),
  282. hosts: [],
  283. configSiteTags: [],
  284. loadedHostNamesMap: loadedHostNamesMap,
  285. hostsModified: false
  286. });
  287. var defaultConfigGroup = this.get('configGroups').popObject();
  288. this.get('configGroups').pushObjects([newConfigGroupData, defaultConfigGroup]);
  289. this.updateConfigGroup(data.resources[0].ConfigGroup.id);
  290. this.addGroupPopup.hide();
  291. },
  292. /**
  293. * update config group apiResponse property
  294. */
  295. updateConfigGroup: function (id) {
  296. App.ajax.send({
  297. name: 'config_groups.get_config_group_by_id',
  298. sender: this,
  299. data: {
  300. 'id': id
  301. },
  302. success: 'successLoadingConfigGroup'
  303. });
  304. },
  305. successLoadingConfigGroup: function (data) {
  306. var confGroup = this.get('configGroups').findProperty('id', data.ConfigGroup.id);
  307. confGroup.set('apiResponse', data.ConfigGroup);
  308. },
  309. hostsModifiedConfigGroups: function() {
  310. var groups = this.get('configGroups');
  311. var hostsRemovedGroup = [];
  312. var hostsAddedGroup = [];
  313. var hostsChangedGroup = [];
  314. groups.forEach(function(g) {
  315. if (!g.get('isDefault')) {
  316. var loadedMap = g.get('loadedHostNamesMap');
  317. var current = g.get('hosts');
  318. var currentLength = current ? current.length : 0;
  319. if (currentLength == loadedMap.length) {
  320. if (currentLength>0) {
  321. var changed = false;
  322. current.forEach(function(c) {
  323. if (!changed && loadedMap[c] == null) {
  324. changed = true;
  325. }
  326. });
  327. if (changed) {
  328. hostsChangedGroup.push(g);
  329. }
  330. }
  331. } else {
  332. if (currentLength < loadedMap.length) {
  333. hostsRemovedGroup.push(g);
  334. } else {
  335. hostsAddedGroup.push(g);
  336. }
  337. }
  338. }
  339. });
  340. // First PUT removed hosts, then PUT added hosts, then changed hosts
  341. var modifiedGroups = [];
  342. modifiedGroups = modifiedGroups.concat(hostsRemovedGroup);
  343. modifiedGroups = modifiedGroups.concat(hostsAddedGroup);
  344. modifiedGroups = modifiedGroups.concat(hostsChangedGroup);
  345. return modifiedGroups;
  346. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.@each'),
  347. isHostsModified: function () {
  348. var groups = this.get('hostsModifiedConfigGroups');
  349. return groups && groups.length > 0;
  350. }.property('hostsModifiedConfigGroups', 'hostsModifiedConfigGroups.length')
  351. });