hosts.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: function () {
  66. return !this.get('isLoaded');
  67. }.property('isLoaded'),
  68. onSecondary: function () {
  69. callback(null);
  70. this.hide();
  71. },
  72. bodyClass: App.TableView.extend({
  73. templateName: require('templates/common/configs/overrideWindow'),
  74. controllerBinding: 'App.router.mainServiceInfoConfigsController',
  75. isPaginate: true,
  76. filteredContent: [],
  77. filteredContentObs: function() {
  78. Em.run.once(this, this.filteredContentObsOnce);
  79. }.observes('parentView.availableHosts.@each.filtered'),
  80. filteredContentObsOnce: function() {
  81. var filteredContent = this.get('parentView.availableHosts').filterProperty('filtered') || [];
  82. this.set('filteredContent', filteredContent);
  83. },
  84. filterText: '',
  85. filterTextPlaceholder: Em.I18n.t('hosts.selectHostsDialog.filter.placeHolder'),
  86. filterColumn: null,
  87. filterColumns: Ember.A([
  88. Ember.Object.create({id: 'ip', name: 'IP Address', selected: true}),
  89. Ember.Object.create({id: 'cpu', name: 'CPU', selected: false}),
  90. Ember.Object.create({id: 'memory', name: 'RAM', selected: false}),
  91. Ember.Object.create({id: 'osArch', name: 'OS Architecture', selected: false}),
  92. Ember.Object.create({id: 'osType', name: 'OS Type', selected: false}),
  93. Ember.Object.create({id: 'diskTotal', name: 'Total Disks Capacity', selected: false}),
  94. Ember.Object.create({id: 'disksMounted', name: '# of Disk Mounts', selected: false})
  95. ]),
  96. showOnlySelectedHosts: false,
  97. filterComponents: validComponents,
  98. filterComponent: null,
  99. isDisabled: function () {
  100. return !this.get('parentView.isLoaded');
  101. }.property('parentView.isLoaded'),
  102. didInsertElement: function() {
  103. var defaultFilterColumn = this.get('filterColumns').findProperty('selected');
  104. this.set('filterColumn', defaultFilterColumn);
  105. initialHosts.setEach('filtered', true);
  106. this.set('parentView.availableHosts', initialHosts);
  107. this.set('parentView.isLoaded', true);
  108. this.filteredContentObsOnce();
  109. },
  110. /**
  111. * Default filter-method isn't needed
  112. */
  113. filter: Em.K,
  114. filterHosts: function () {
  115. var filterText = this.get('filterText');
  116. var showOnlySelectedHosts = this.get('showOnlySelectedHosts');
  117. var filterComponent = this.get('filterComponent');
  118. var filterColumn = this.get('filterColumn');
  119. this.get('parentView.availableHosts').forEach(function (host) {
  120. var skip = showOnlySelectedHosts && !host.get('selected');
  121. var value = host.get('host').get(filterColumn.id);
  122. var hostComponentNames = host.get('hostComponentNames');
  123. host.set('filterColumnValue', value);
  124. if (!skip && filterText) {
  125. if ((value == null || !value.toString().match(filterText)) && !host.get('host.publicHostName').match(filterText)) {
  126. skip = true;
  127. }
  128. }
  129. if (!skip && filterComponent) {
  130. if (hostComponentNames.length > 0) {
  131. skip = !hostComponentNames.contains(filterComponent.get('componentName'));
  132. }
  133. }
  134. host.set('filtered', !skip);
  135. }, this);
  136. this.set('startIndex', 1);
  137. }.observes('parentView.availableHosts', 'filterColumn', 'filterText', 'filterComponent', 'filterComponent.componentName', 'showOnlySelectedHosts'),
  138. hostSelectMessage: function () {
  139. var hosts = this.get('parentView.availableHosts');
  140. var selectedHosts = hosts.filterProperty('selected', true);
  141. return this.t('hosts.selectHostsDialog.selectedHostsLink').format(selectedHosts.get('length'), hosts.get('length'))
  142. }.property('parentView.availableHosts.@each.selected'),
  143. selectFilterColumn: function (event) {
  144. if (event != null && event.context != null && event.context.id != null) {
  145. var filterColumn = this.get('filterColumn');
  146. if (filterColumn != null) {
  147. filterColumn.set('selected', false);
  148. }
  149. event.context.set('selected', true);
  150. this.set('filterColumn', event.context);
  151. }
  152. },
  153. selectFilterComponent: function (event) {
  154. if (event != null && event.context != null && event.context.componentName != null) {
  155. var currentFilter = this.get('filterComponent');
  156. if (currentFilter != null) {
  157. currentFilter.set('selected', false);
  158. }
  159. if (currentFilter != null && currentFilter.componentName === event.context.componentName) {
  160. // selecting the same filter deselects it.
  161. this.set('filterComponent', null);
  162. } else {
  163. this.set('filterComponent', event.context);
  164. event.context.set('selected', true);
  165. }
  166. }
  167. },
  168. allHostsSelected: false,
  169. toggleSelectAllHosts: function (event) {
  170. this.get('parentView.availableHosts').filterProperty('filtered').setEach('selected', this.get('allHostsSelected'));
  171. }.observes('allHostsSelected'),
  172. toggleShowSelectedHosts: function () {
  173. var currentFilter = this.get('filterComponent');
  174. if (currentFilter != null) {
  175. currentFilter.set('selected', false);
  176. }
  177. this.setProperties({
  178. filterComponent: null,
  179. filterText: null
  180. });
  181. this.toggleProperty('showOnlySelectedHosts');
  182. }
  183. })
  184. });
  185. },
  186. /**
  187. * Bulk setting of for rack id
  188. * @param {Object} operationData - data about bulk operation (action, hostComponents etc)
  189. * @param {Ember.Enumerable} hosts - list of affected hosts
  190. */
  191. setRackInfo: function (operationData, hosts, rackId) {
  192. var self = this;
  193. var hostNames = hosts.mapProperty('hostName');
  194. return App.ModalPopup.show({
  195. header: Em.I18n.t('hosts.host.details.setRackId'),
  196. disablePrimary: true,
  197. rackId: rackId,
  198. bodyClass: Em.View.extend({
  199. templateName: require('templates/main/host/rack_id_popup'),
  200. errorMessage: null,
  201. isValid: true,
  202. validation: function () {
  203. this.set('isValid', validator.isValidRackId(this.get('parentView.rackId')));
  204. this.set('errorMessage', this.get('isValid') ? '' : Em.I18n.t('hostPopup.setRackId.invalid'));
  205. this.set('parentView.disablePrimary', !this.get('isValid'));
  206. }.observes('parentView.rackId')
  207. }),
  208. onPrimary: function() {
  209. var rackId = this.get('rackId');
  210. if (hostNames.length) {
  211. App.ajax.send({
  212. name: 'bulk_request.hosts.update_rack_id',
  213. sender: self,
  214. data: {
  215. hostNames: hostNames.join(','),
  216. requestInfo: operationData.message,
  217. rackId: rackId,
  218. hostNamesArray: hostNames
  219. },
  220. success: 'successRackId',
  221. error: 'errorRackId'
  222. });
  223. }
  224. this.hide();
  225. }
  226. });
  227. },
  228. /**
  229. * Success callback for set rack id request
  230. */
  231. successRackId: function (response, request, params) {
  232. App.Host.find().forEach(function(host){
  233. if (params.hostNamesArray.contains(host.get('hostName'))) {
  234. host.set('rack', params.rackId)
  235. }
  236. });
  237. },
  238. /**
  239. * Warn user that the rack id will not be updated
  240. */
  241. errorRackId: function () {
  242. App.showAlertPopup(Em.I18n.t('common.error'), Em.I18n.t('hostPopup.setRackId.error'));
  243. }
  244. };