hosts.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. require('views/common/table_view');
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. module.exports = {
  21. /**
  22. * Launches a dialog to select hosts from the provided available hosts.
  23. *
  24. * Once the user clicks OK or Cancel, the callback is called with the
  25. * array of hosts (App.Host[]) selected. If the dialog was cancelled
  26. * or closed, <code>null</code> is provided to the callback. Else
  27. * an array (maybe empty) will be provided to the callback.
  28. *
  29. * @param initialHosts {App.Host[]} List of hosts to pick from
  30. * @param selectedHosts {App.Host[]} List of hosts already selected from the available hosts
  31. * @param selectAtleastOneHost {boolean} If true atleast one host has to be selected
  32. * @param validComponents {App.HostComponent[]} List of host-component types to pick from.
  33. * @param callback Callback function which is invoked when dialog
  34. * @param popupDescription {Object} Consist header and message for popup
  35. * Example: {header: 'header', dialogMessage: 'message'}
  36. * is closed, cancelled or OK is pressed.
  37. */
  38. launchHostsSelectionDialog : function(initialHosts, selectedHosts,
  39. selectAtleastOneHost, validComponents, callback, popupDescription) {
  40. // set default popup description
  41. var defaultPopupDescription = {
  42. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  43. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message')
  44. };
  45. if (popupDescription !== null) {
  46. popupDescription = $.extend(true, defaultPopupDescription, popupDescription);
  47. }
  48. App.ModalPopup.show({
  49. classNames: [ 'sixty-percent-width-modal' ],
  50. elementId: 'host-selection-dialog',
  51. header: popupDescription.header,
  52. dialogMessage: popupDescription.dialogMessage,
  53. warningMessage: null,
  54. availableHosts: [],
  55. onPrimary: function () {
  56. this.set('warningMessage', null);
  57. var arrayOfSelectedHosts = this.get('availableHosts').filterProperty('selected', true).mapProperty('host.id');
  58. if (selectAtleastOneHost && arrayOfSelectedHosts.length < 1) {
  59. this.set('warningMessage', Em.I18n.t('hosts.selectHostsDialog.message.warning'));
  60. return;
  61. }
  62. callback(arrayOfSelectedHosts);
  63. this.hide();
  64. },
  65. disablePrimary: Em.computed.not('isLoaded'),
  66. onSecondary: function () {
  67. callback(null);
  68. this.hide();
  69. },
  70. bodyClass: App.TableView.extend({
  71. templateName: require('templates/common/configs/overrideWindow'),
  72. controllerBinding: 'App.router.mainServiceInfoConfigsController',
  73. isPaginate: true,
  74. filteredContent: [],
  75. filteredContentObs: function() {
  76. Em.run.once(this, this.filteredContentObsOnce);
  77. }.observes('parentView.availableHosts.@each.filtered'),
  78. filteredContentObsOnce: function() {
  79. var filteredContent = this.get('parentView.availableHosts').filterProperty('filtered') || [];
  80. this.set('filteredContent', filteredContent);
  81. },
  82. filterText: '',
  83. filterTextPlaceholder: Em.I18n.t('hosts.selectHostsDialog.filter.placeHolder'),
  84. filterColumn: null,
  85. filterColumns: Ember.A([
  86. Ember.Object.create({id: 'ip', name: 'IP Address', selected: true}),
  87. Ember.Object.create({id: 'cpu', name: 'CPU', selected: false}),
  88. Ember.Object.create({id: 'memory', name: 'RAM', selected: false}),
  89. Ember.Object.create({id: 'osArch', name: 'OS Architecture', selected: false}),
  90. Ember.Object.create({id: 'osType', name: 'OS Type', selected: false}),
  91. Ember.Object.create({id: 'diskTotal', name: 'Total Disks Capacity', selected: false}),
  92. Ember.Object.create({id: 'disksMounted', name: '# of Disk Mounts', selected: false})
  93. ]),
  94. showOnlySelectedHosts: false,
  95. filterComponents: validComponents,
  96. filterComponent: null,
  97. isDisabled: Em.computed.not('parentView.isLoaded'),
  98. didInsertElement: function() {
  99. var defaultFilterColumn = this.get('filterColumns').findProperty('selected');
  100. this.set('filterColumn', defaultFilterColumn);
  101. initialHosts.setEach('filtered', true);
  102. this.set('parentView.availableHosts', initialHosts);
  103. this.set('parentView.isLoaded', true);
  104. this.filteredContentObsOnce();
  105. },
  106. /**
  107. * Default filter-method isn't needed
  108. */
  109. filter: Em.K,
  110. filterHosts: function () {
  111. var filterText = this.get('filterText');
  112. var showOnlySelectedHosts = this.get('showOnlySelectedHosts');
  113. var filterComponent = this.get('filterComponent');
  114. var filterColumn = this.get('filterColumn');
  115. this.get('parentView.availableHosts').forEach(function (host) {
  116. var skip = showOnlySelectedHosts && !host.get('selected');
  117. var value = host.get('host').get(filterColumn.id);
  118. var hostComponentNames = host.get('hostComponentNames');
  119. host.set('filterColumnValue', value);
  120. if (!skip && filterText) {
  121. if ((value == null || !value.toString().match(filterText)) && !host.get('host.publicHostName').match(filterText)) {
  122. skip = true;
  123. }
  124. }
  125. if (!skip && filterComponent) {
  126. if (hostComponentNames.length > 0) {
  127. skip = !hostComponentNames.contains(filterComponent.get('componentName'));
  128. }
  129. }
  130. host.set('filtered', !skip);
  131. }, this);
  132. this.set('startIndex', 1);
  133. }.observes('parentView.availableHosts', 'filterColumn', 'filterText', 'filterComponent', 'filterComponent.componentName', 'showOnlySelectedHosts'),
  134. hostSelectMessage: function () {
  135. var hosts = this.get('parentView.availableHosts');
  136. var selectedHosts = hosts.filterProperty('selected', true);
  137. return this.t('hosts.selectHostsDialog.selectedHostsLink').format(selectedHosts.get('length'), hosts.get('length'))
  138. }.property('parentView.availableHosts.@each.selected'),
  139. selectFilterColumn: function (event) {
  140. if (event != null && event.context != null && event.context.id != null) {
  141. var filterColumn = this.get('filterColumn');
  142. if (filterColumn != null) {
  143. filterColumn.set('selected', false);
  144. }
  145. event.context.set('selected', true);
  146. this.set('filterColumn', event.context);
  147. }
  148. },
  149. selectFilterComponent: function (event) {
  150. if (event != null && event.context != null && event.context.componentName != null) {
  151. var currentFilter = this.get('filterComponent');
  152. if (currentFilter != null) {
  153. currentFilter.set('selected', false);
  154. }
  155. if (currentFilter != null && currentFilter.componentName === event.context.componentName) {
  156. // selecting the same filter deselects it.
  157. this.set('filterComponent', null);
  158. } else {
  159. this.set('filterComponent', event.context);
  160. event.context.set('selected', true);
  161. }
  162. }
  163. },
  164. allHostsSelected: false,
  165. toggleSelectAllHosts: function (event) {
  166. this.get('parentView.availableHosts').filterProperty('filtered').setEach('selected', this.get('allHostsSelected'));
  167. }.observes('allHostsSelected'),
  168. toggleShowSelectedHosts: function () {
  169. var currentFilter = this.get('filterComponent');
  170. if (currentFilter != null) {
  171. currentFilter.set('selected', false);
  172. }
  173. this.setProperties({
  174. filterComponent: null,
  175. filterText: null
  176. });
  177. this.toggleProperty('showOnlySelectedHosts');
  178. }
  179. })
  180. });
  181. },
  182. /**
  183. * Bulk setting of for rack id
  184. * @param {Object} operationData - data about bulk operation (action, hostComponents etc)
  185. * @param {Ember.Enumerable} hosts - list of affected hosts
  186. */
  187. setRackInfo: function (operationData, hosts, rackId) {
  188. var self = this;
  189. var hostNames = hosts.mapProperty('hostName');
  190. return App.ModalPopup.show({
  191. header: Em.I18n.t('hosts.host.details.setRackId'),
  192. disablePrimary: true,
  193. rackId: rackId,
  194. bodyClass: Em.View.extend({
  195. templateName: require('templates/main/host/rack_id_popup'),
  196. errorMessage: null,
  197. isValid: true,
  198. validation: function () {
  199. this.set('isValid', validator.isValidRackId(this.get('parentView.rackId')));
  200. this.set('errorMessage', this.get('isValid') ? '' : Em.I18n.t('hostPopup.setRackId.invalid'));
  201. this.set('parentView.disablePrimary', !this.get('isValid'));
  202. }.observes('parentView.rackId')
  203. }),
  204. onPrimary: function() {
  205. var rackId = this.get('rackId');
  206. if (hostNames.length) {
  207. App.ajax.send({
  208. name: 'bulk_request.hosts.update_rack_id',
  209. sender: self,
  210. data: {
  211. hostNames: hostNames.join(','),
  212. requestInfo: operationData.message,
  213. rackId: rackId,
  214. hostNamesArray: hostNames
  215. },
  216. success: 'successRackId',
  217. error: 'errorRackId'
  218. });
  219. }
  220. this.hide();
  221. }
  222. });
  223. },
  224. /**
  225. * Success callback for set rack id request
  226. */
  227. successRackId: function (response, request, params) {
  228. App.Host.find().forEach(function(host){
  229. if (params.hostNamesArray.contains(host.get('hostName'))) {
  230. host.set('rack', params.rackId)
  231. }
  232. });
  233. },
  234. /**
  235. * Warn user that the rack id will not be updated
  236. */
  237. errorRackId: function () {
  238. App.showAlertPopup(Em.I18n.t('common.error'), Em.I18n.t('hostPopup.setRackId.error'));
  239. }
  240. };