manage_clusters_controller.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. App.MainMirroringManageClustersController = Em.ArrayController.extend({
  20. name: 'mainMirroringManageClustersController',
  21. // link to popup object
  22. popup: null,
  23. clusters: [],
  24. clustersToDelete: [],
  25. clustersToCreate: [],
  26. queriesCount: 0,
  27. // array of error messages
  28. queryErrors: [],
  29. isLoaded: function () {
  30. return App.router.get('mainMirroringController.isLoaded');
  31. }.property('App.router.mainMirroringController.isLoaded'),
  32. onLoad: function () {
  33. if (this.get('isLoaded')) {
  34. var clusters = [];
  35. App.TargetCluster.find().forEach(function (cluster) {
  36. var newCluster = {
  37. name: cluster.get('name'),
  38. execute: cluster.get('execute'),
  39. workflow: cluster.get('workflow'),
  40. write: cluster.get('write'),
  41. readonly: cluster.get('readonly'),
  42. staging: cluster.get('staging'),
  43. working: cluster.get('working'),
  44. temp: cluster.get('temp')
  45. };
  46. // Source cluster should be shown on top
  47. if (cluster.get('name') === App.get('clusterName')) {
  48. clusters.unshift(Ember.Object.create(newCluster));
  49. } else {
  50. clusters.push(Ember.Object.create(newCluster));
  51. }
  52. }, this);
  53. this.set('clusters', clusters);
  54. this.get('clustersToDelete').clear();
  55. this.get('clustersToCreate').clear();
  56. }
  57. }.observes('isLoaded'),
  58. selectedCluster: null,
  59. // Disable input fields for already created clusters
  60. isEditDisabled: function () {
  61. return !this.get('clustersToCreate').mapProperty('name').contains(this.get('selectedCluster.name'));
  62. }.property('selectedCluster.name', 'clustersToCreate.@each.name'),
  63. addCluster: function () {
  64. var self = this;
  65. App.showPromptPopup(Em.I18n.t('mirroring.manageClusters.specifyName'),
  66. function (clusterName) {
  67. var newCluster = Ember.Object.create({
  68. name: clusterName,
  69. execute: '',
  70. workflow: '',
  71. write: '',
  72. readonly: '',
  73. staging: '',
  74. working: '',
  75. temp: ''
  76. });
  77. self.get('clusters').pushObject(newCluster);
  78. self.get('clustersToCreate').pushObject(newCluster);
  79. }
  80. );
  81. },
  82. removeCluster: function () {
  83. var self = this;
  84. App.showConfirmationPopup(function () {
  85. if (self.get('clustersToCreate').mapProperty('name').contains(self.get('selectedCluster.name'))) {
  86. self.set('clustersToCreate', self.get('clustersToCreate').without(self.get('selectedCluster')));
  87. } else {
  88. self.get('clustersToDelete').push(self.get('selectedCluster'));
  89. }
  90. self.set('clusters', self.get('clusters').without(self.get('selectedCluster')));
  91. });
  92. },
  93. save: function () {
  94. // define clusters need to be deleted, modified or created
  95. var clusters = this.get('clusters');
  96. var clustersToCreate = this.get('clustersToCreate');
  97. var clustersToDelete = this.get('clustersToDelete');
  98. var queriesCount = clustersToCreate.length + clustersToDelete.length;
  99. this.set('queriesCount', queriesCount);
  100. // send request to delete, modify or create cluster
  101. if (queriesCount) {
  102. this.get('queryErrors').clear();
  103. clustersToDelete.forEach(function (cluster) {
  104. App.ajax.send({
  105. name: 'mirroring.delete_entity',
  106. sender: this,
  107. data: {
  108. name: cluster.get('name'),
  109. type: 'cluster',
  110. falconServer: App.get('falconServerURL')
  111. },
  112. success: 'onQueryResponse',
  113. error: 'onQueryResponse'
  114. });
  115. }, this);
  116. clustersToCreate.forEach(function (cluster) {
  117. App.ajax.send({
  118. name: 'mirroring.submit_entity',
  119. sender: this,
  120. data: {
  121. type: 'cluster',
  122. entity: this.formatClusterXML(cluster),
  123. falconServer: App.get('falconServerURL')
  124. },
  125. success: 'onQueryResponse',
  126. error: 'onQueryResponse'
  127. });
  128. }, this);
  129. } else {
  130. this.get('popup').hide();
  131. }
  132. },
  133. // close popup after getting response from all queries or show popup with errors
  134. onQueryResponse: function () {
  135. var queryErrors = this.get('queryErrors');
  136. if (arguments.length === 4) {
  137. queryErrors.push(arguments[2]);
  138. }
  139. var queriesCount = this.get('queriesCount');
  140. this.set('queriesCount', --queriesCount);
  141. if (queriesCount < 1) {
  142. if (queryErrors.length) {
  143. App.showAlertPopup(Em.I18n.t('common.error'), Em.I18n.t('mirroring.manageClusters.error') + ': ' + queryErrors.join(', '));
  144. } else {
  145. this.get('popup').hide();
  146. }
  147. }
  148. },
  149. /**
  150. * Return XML-formatted string made from cluster object
  151. * @param {Object} cluster - object with cluster data
  152. * @return {String}
  153. */
  154. formatClusterXML: function (cluster) {
  155. return '<?xml version="1.0"?><cluster colo="local" description="" name="' + cluster.get('name') +
  156. '" xmlns="uri:falcon:cluster:0.1"><interfaces><interface type="readonly" endpoint="' + cluster.get('readonly') +
  157. '" version="2.2.0" /><interface type="execute" endpoint="' + cluster.get('execute') +
  158. '" version="2.2.0" /><interface type="workflow" endpoint="' + cluster.get('workflow') +
  159. '" version="4.0.0" />' + '<interface type="messaging" endpoint="tcp://' + App.get('falconServerURL') + ':61616?daemon=true" version="5.1.6" />' +
  160. '<interface type="write" endpoint="' + cluster.get('write') + '" version="2.2.0" />' +
  161. '</interfaces><locations><location name="staging" path="' + cluster.get('staging') +
  162. '" /><location name="temp" path="' + cluster.get('temp') +
  163. '" /><location name="working" path="' + cluster.get('working') +
  164. '" /></locations></cluster>';
  165. }
  166. });