manage_config_groups_controller.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 newConfigGroup = App.ConfigGroup.create({
  56. id: configGroup.id,
  57. name: configGroup.group_name,
  58. description: configGroup.description,
  59. isDefault: false,
  60. parentConfigGroup: defaultConfigGroup,
  61. service: App.Service.find().findProperty('serviceName', configGroup.tag),
  62. hosts: configGroup.hosts.mapProperty('host_name'),
  63. configSiteTags: [],
  64. properties: []
  65. });
  66. usedHosts = usedHosts.concat(newConfigGroup.get('hosts'));
  67. configGroups.push(newConfigGroup);
  68. configGroup.desired_configs.forEach(function (config) {
  69. if (!groupToTypeToTagMap[configGroup.group_name]) {
  70. groupToTypeToTagMap[configGroup.group_name] = {}
  71. }
  72. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  73. });
  74. }, this);
  75. unusedHosts = App.Host.find().mapProperty('hostName');
  76. usedHosts.uniq().forEach(function (host) {
  77. unusedHosts = unusedHosts.without(host);
  78. }, this);
  79. defaultConfigGroup.set('childConfigGroups', configGroups);
  80. defaultConfigGroup.set('hosts', unusedHosts);
  81. this.set('configGroups', [defaultConfigGroup].concat(configGroups));
  82. this.loadProperties(groupToTypeToTagMap);
  83. this.set('isLoaded', true);
  84. }
  85. },
  86. onLoadConfigGroupsError: function () {
  87. console.error('Unable to load config groups for service.');
  88. },
  89. loadProperties: function (groupToTypeToTagMap) {
  90. var typeTagToGroupMap = {};
  91. var urlParams = [];
  92. for (var group in groupToTypeToTagMap) {
  93. var overrideTypeTags = groupToTypeToTagMap[group];
  94. for (var type in overrideTypeTags) {
  95. var tag = overrideTypeTags[type];
  96. typeTagToGroupMap[type + "///" + tag] = group;
  97. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  98. }
  99. }
  100. var params = urlParams.join('|');
  101. if (urlParams.length) {
  102. App.ajax.send({
  103. name: 'config.host_overrides',
  104. sender: this,
  105. data: {
  106. params: params,
  107. typeTagToGroupMap: typeTagToGroupMap
  108. },
  109. success: 'onLoadPropertiesSuccess'
  110. });
  111. }
  112. },
  113. onLoadPropertiesSuccess: function (data, opt, params) {
  114. data.items.forEach(function (configs) {
  115. var typeTagConfigs = [];
  116. App.config.loadedConfigurationsCache[configs.type + "_" + configs.tag] = configs.properties;
  117. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  118. for (var config in configs.properties) {
  119. typeTagConfigs.push({
  120. name: config,
  121. value: configs.properties[config]
  122. });
  123. }
  124. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  125. }, this);
  126. },
  127. showProperties: function () {
  128. var properies = this.get('selectedConfigGroup.propertiesList');
  129. if (properies) {
  130. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  131. }
  132. },
  133. /**
  134. * add hosts to group
  135. * @return {Array}
  136. */
  137. addHosts: function () {
  138. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  139. var group = this.get('selectedConfigGroup');
  140. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, [], function (selectedHosts) {
  141. if (selectedHosts) {
  142. var defaultHosts = group.get('parentConfigGroup.hosts');
  143. var configGroupHosts = group.get('hosts');
  144. selectedHosts.forEach(function (hostName) {
  145. configGroupHosts.pushObject(hostName);
  146. defaultHosts.removeObject(hostName);
  147. });
  148. }
  149. });
  150. },
  151. /**
  152. * delete hosts from group
  153. */
  154. deleteHosts: function () {
  155. var groupHosts = this.get('selectedConfigGroup.hosts');
  156. var defaultGroupHosts = this.get('selectedConfigGroup.parentConfigGroup.hosts');
  157. this.get('selectedHosts').forEach(function (hostName) {
  158. defaultGroupHosts.pushObject(hostName);
  159. groupHosts.removeObject(hostName);
  160. });
  161. this.set('selectedHosts', []);
  162. }
  163. });