manage_clusters_controller.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // array of original clusters to compare with changed ones
  25. originalClusters: [],
  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. var originalClusters = [];
  36. App.TargetCluster.find().forEach(function (cluster) {
  37. var newCluster = {
  38. name: cluster.get('name'),
  39. execute: cluster.get('execute'),
  40. workflow: cluster.get('workflow'),
  41. readonly: cluster.get('readonly'),
  42. staging: cluster.get('staging'),
  43. working: cluster.get('working'),
  44. temp: cluster.get('temp')
  45. };
  46. clusters.push(Ember.Object.create(newCluster));
  47. originalClusters.push(Ember.Object.create(newCluster));
  48. }, this);
  49. this.set('clusters', clusters);
  50. this.set('originalClusters', originalClusters);
  51. }
  52. }.observes('isLoaded'),
  53. selectedCluster: null,
  54. addCluster: function () {
  55. var self = this;
  56. App.showPromptPopup(Em.I18n.t('mirroring.manageClusters.specifyName'),
  57. function (clusterName) {
  58. self.get('clusters').pushObject(Ember.Object.create({
  59. name: clusterName,
  60. execute: '',
  61. workflow: '',
  62. readonly: '',
  63. staging: '',
  64. working: '',
  65. temp: ''
  66. }));
  67. }
  68. );
  69. },
  70. removeCluster: function () {
  71. var self = this;
  72. App.showConfirmationPopup(function () {
  73. self.set('clusters', self.get('clusters').without(self.get('selectedCluster')));
  74. });
  75. },
  76. save: function () {
  77. // define clusters need to be deleted, modified or created
  78. var clusters = this.get('clusters');
  79. var originalClusters = this.get('originalClusters');
  80. var originalClustersNames = originalClusters.mapProperty('name');
  81. var clustersToModify = [];
  82. var clustersToCreate = [];
  83. clusters.forEach(function (cluster) {
  84. var clusterName = cluster.get('name');
  85. if (originalClustersNames.contains(clusterName)) {
  86. if (JSON.stringify(cluster) !== JSON.stringify(originalClusters.findProperty('name', clusterName))) {
  87. clustersToModify.push(clusterName);
  88. }
  89. originalClustersNames = originalClustersNames.without(clusterName);
  90. } else {
  91. clustersToCreate.push(clusterName);
  92. }
  93. }, this);
  94. var clustersToDelete = originalClustersNames;
  95. var queriesCount = clustersToCreate.length + clustersToDelete.length + clustersToModify.length;
  96. this.set('queriesCount', queriesCount);
  97. // send request to delete, modify or create cluster
  98. if (queriesCount) {
  99. this.get('queryErrors').clear();
  100. clustersToDelete.forEach(function (cluster) {
  101. App.ajax.send({
  102. name: 'mirroring.delete_instance',
  103. sender: this,
  104. data: {
  105. name: cluster,
  106. type: 'cluster'
  107. },
  108. success: 'onQueryResponse',
  109. error: 'onQueryResponse'
  110. });
  111. }, this);
  112. clustersToCreate.forEach(function (cluster) {
  113. App.ajax.send({
  114. name: 'mirroring.submit_instance',
  115. sender: this,
  116. data: {
  117. type: 'cluster',
  118. instance: this.formatClusterXML(clusters.findProperty('name', cluster))
  119. },
  120. success: 'onQueryResponse',
  121. error: 'onQueryResponse'
  122. });
  123. }, this);
  124. clustersToModify.forEach(function (cluster) {
  125. App.ajax.send({
  126. name: 'mirroring.update_instance',
  127. sender: this,
  128. data: {
  129. name: cluster,
  130. type: 'cluster',
  131. instance: this.formatClusterXML(clusters.findProperty('name', cluster))
  132. },
  133. success: 'onQueryResponse',
  134. error: 'onQueryResponse'
  135. });
  136. }, this);
  137. } else {
  138. this.get('popup').hide();
  139. }
  140. },
  141. // close popup after getting response from all queries or show popup with errors
  142. onQueryResponse: function () {
  143. var queryErrors = this.get('queryErrors');
  144. if (arguments.length === 4) {
  145. queryErrors.push(arguments[2]);
  146. }
  147. var queriesCount = this.get('queriesCount');
  148. this.set('queriesCount', --queriesCount);
  149. if (queriesCount < 1) {
  150. if (queryErrors.length) {
  151. App.showAlertPopup(Em.I18n.t('common.error'), Em.I18n.t('mirroring.manageClusters.error') + ': ' + queryErrors.join(', '));
  152. } else {
  153. this.get('popup').hide();
  154. }
  155. }
  156. },
  157. /**
  158. * Return XML-formatted string made from cluster object
  159. * @param {Object} cluster - object with cluster data
  160. * @return {String}
  161. */
  162. formatClusterXML: function (cluster) {
  163. return '<?xml version="1.0"?><cluster colo="default" description="" name="' + cluster.get('name') +
  164. '" xmlns="uri:falcon:cluster:0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><interfaces><interface type="readonly" endpoint="' + cluster.get('readonly') +
  165. '" version="2.2.0.2.0.6.0-76" /><interface type="execute" endpoint="' + cluster.get('execute') +
  166. '" version="2.2.0.2.0.6.0-76" /><interface type="workflow" endpoint="' + cluster.get('workflow') +
  167. '" version="3.1.4" /></interfaces><locations><location name="staging" path="' + cluster.get('staging') +
  168. '" /><location name="temp" path="' + cluster.get('temp') +
  169. '" /><location name="working" path="' + cluster.get('working') +
  170. '" /></locations></cluster>';
  171. }
  172. });