manage_config_groups_controller.js 14 KB

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