Просмотр исходного кода

Merge branch 'master' into refactoring

* master:
  - Adds endpoint field validation
  - Adds edit connection functionality
George Vrancianu 8 лет назад
Родитель
Сommit
b337dcb359

+ 45 - 6
src/actions/ConnectionsActions/ConnectionsActions.js

@@ -34,6 +34,7 @@ let ConnectionsActions = Reflux.createActions({
   resetSelections: {},
   resetSelections: {},
   newConnection: { children: ['success', 'failed'] },
   newConnection: { children: ['success', 'failed'] },
   saveEndpoint: { children: ['success', 'failed'] },
   saveEndpoint: { children: ['success', 'failed'] },
+  editEndpoint: { children: ['success', 'failed'] },
   setConnection: {},
   setConnection: {},
   validateConnection: { children: ['success', 'failed'] }
   validateConnection: { children: ['success', 'failed'] }
 })
 })
@@ -75,6 +76,7 @@ ConnectionsActions.saveEndpoint.listen((data, secretRef) => {
   }).then(ConnectionsActions.saveEndpoint.success, ConnectionsActions.saveEndpoint.failed)
   }).then(ConnectionsActions.saveEndpoint.success, ConnectionsActions.saveEndpoint.failed)
     .catch(ConnectionsActions.saveEndpoint.failed);
     .catch(ConnectionsActions.saveEndpoint.failed);
 })
 })
+
 ConnectionsActions.newConnection.listen((data) => {
 ConnectionsActions.newConnection.listen((data) => {
   if (useSecret) {
   if (useSecret) {
     let barbicanPayload = {
     let barbicanPayload = {
@@ -109,6 +111,11 @@ ConnectionsActions.loadConnections.listen(() => {
   }
   }
 })
 })
 
 
+ConnectionsActions.loadConnections.shouldEmit = () => {
+  let projectId = Reflux.GlobalState.userStore.currentUser.project.id
+  return typeof projectId !== "undefined"
+}
+
 ConnectionsActions.deleteConnection.listen((connection) => {
 ConnectionsActions.deleteConnection.listen((connection) => {
   let projectId = Reflux.GlobalState.userStore.currentUser.project.id
   let projectId = Reflux.GlobalState.userStore.currentUser.project.id
 
 
@@ -124,13 +131,45 @@ ConnectionsActions.deleteConnection.listen((connection) => {
         method: "DELETE"
         method: "DELETE"
       }).then(ConnectionsActions.deleteConnection.completed(connection), ConnectionsActions.deleteConnection.failed)
       }).then(ConnectionsActions.deleteConnection.completed(connection), ConnectionsActions.deleteConnection.failed)
     } else {
     } else {
+      console.log("ASDAJBHADFHBDAF")
       ConnectionsActions.deleteConnection.completed(connection)
       ConnectionsActions.deleteConnection.completed(connection)
     }
     }
-
   }, ConnectionsActions.deleteConnection.failed)
   }, ConnectionsActions.deleteConnection.failed)
   .catch(ConnectionsActions.deleteConnection.failed);
   .catch(ConnectionsActions.deleteConnection.failed);
 })
 })
 
 
+ConnectionsActions.editEndpoint.listen((connection, data) => {
+  let projectId = Reflux.GlobalState.userStore.currentUser.project.id
+  let payload = null
+  if (connection.connection_info && connection.connection_info.secret_ref) {
+    let uuidIndex = connection.connection_info.secret_ref.lastIndexOf("/")
+    let uuid = connection.connection_info.secret_ref.substr(uuidIndex + 1)
+    Api.sendAjaxRequest({
+      url: servicesUrl.barbican + "/v1/secrets/" + uuid,
+      method: "POST"
+    })
+    payload = {
+      endpoint: {
+        name: data.name,
+        description: data.description,
+        type: data.type,
+        connection_info: {
+          secret_ref: connection.connection_info.secret_ref
+        }
+      }
+    }
+  } else {
+    payload = { endpoint: data }
+  }
+
+  Api.sendAjaxRequest({
+    url: `${servicesUrl.coriolis}/${projectId}/endpoints/${connection.id}`,
+    method: "POST",
+    data: payload
+  }).then(ConnectionsActions.editEndpoint.success, ConnectionsActions.editEndpoint.failed)
+    .catch(ConnectionsActions.editEndpoint.failed);
+})
+
 
 
 ConnectionsActions.validateConnection.listen((endpoint, callback) => {
 ConnectionsActions.validateConnection.listen((endpoint, callback) => {
   let projectId = Reflux.GlobalState.userStore.currentUser.project.id
   let projectId = Reflux.GlobalState.userStore.currentUser.project.id
@@ -139,11 +178,11 @@ ConnectionsActions.validateConnection.listen((endpoint, callback) => {
     method: "POST",
     method: "POST",
     data: { "validate-connection": null }
     data: { "validate-connection": null }
   }).then(response => {
   }).then(response => {
-      if (callback) {
-        callback(response)
-      }
-      ConnectionsActions.validateConnection.completed(response)
-    }, ConnectionsActions.validateConnection.failed)
+    if (callback) {
+      callback(response)
+    }
+    ConnectionsActions.validateConnection.completed(response)
+  }, ConnectionsActions.validateConnection.failed)
     .catch(ConnectionsActions.validateConnection.failed);
     .catch(ConnectionsActions.validateConnection.failed);
 })
 })
 
 

+ 53 - 55
src/components/AddCloudConnection/AddCloudConnection.js

@@ -1,19 +1,19 @@
 /*
 /*
-Copyright (C) 2017  Cloudbase Solutions SRL
+ Copyright (C) 2017  Cloudbase Solutions SRL
 
 
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
 
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Affero General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU Affero General Public License for more details.
 
 
-You should have received a copy of the GNU Affero General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+ You should have received a copy of the GNU Affero General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 
 import React, { PropTypes } from 'react';
 import React, { PropTypes } from 'react';
 import withStyles from 'isomorphic-style-loader/lib/withStyles';
 import withStyles from 'isomorphic-style-loader/lib/withStyles';
@@ -45,7 +45,7 @@ class AddCloudConnection extends Reflux.Component {
     this.store = ConnectionsStore
     this.store = ConnectionsStore
 
 
     this.state = {
     this.state = {
-      connectionName: null,
+      connectionName: "",
       description: null,
       description: null,
       currentCloud: this.props.cloud,
       currentCloud: this.props.cloud,
       currentCloudData: null,
       currentCloudData: null,
@@ -92,27 +92,43 @@ class AddCloudConnection extends Reflux.Component {
   }
   }
 
 
   handleSave() {
   handleSave() {
-    let credentials = Object.assign({}, this.state.currentCloudData)
-    for (let key in credentials) {
-      if (credentials[key].label) {
-        credentials[key] = credentials[key].value
+    let valid = true
+
+    for (let i in this.state.currentCloudData) {
+      if (this.state.requiredFields.indexOf(i) > -1 && !this.state.currentCloudData[i]) {
+        valid = false
       }
       }
     }
     }
-    if (this.props.type == "new") {
-      ConnectionsActions.newConnection({
-        name: this.state.connectionName,
-        description: this.state.description,
-        type: this.state.currentCloud.name,
-        connection_info: credentials
-      })
-      this.props.addHandle(this.state.connectionName);
+    if (this.state.connectionName.trim().length == 0) {
+      valid = false
+    }
+    if (!valid) {
+      NotificationActions.notify("Please fill all required fields", "error")
+      this.setState({ cloudFormsSubmitted: true })
     } else {
     } else {
-      ConnectionsActions.saveEndpoint(this.props.connection, {
-        name: this.state.connectionName,
-        description: this.state.description,
-        type: this.state.currentCloud.name,
-        connection_info: credentials
-      })
+      let credentials = Object.assign({}, this.state.currentCloudData)
+      for (let key in credentials) {
+        if (credentials[key].label) {
+          credentials[key] = credentials[key].value
+        }
+      }
+      if (this.props.type == "new") {
+        ConnectionsActions.newConnection({
+          name: this.state.connectionName,
+          description: this.state.description,
+          type: this.state.currentCloud.name,
+          connection_info: credentials
+        })
+        this.props.addHandle(this.state.connectionName);
+      } else {
+        ConnectionsActions.editEndpoint(this.props.connection, {
+          name: this.state.connectionName,
+          description: this.state.description,
+          type: this.state.currentCloud.name,
+          connection_info: credentials
+        })
+        this.props.closeHandle()
+      }
     }
     }
   }
   }
 
 
@@ -151,7 +167,7 @@ class AddCloudConnection extends Reflux.Component {
       currentCloudData: null,
       currentCloudData: null,
       currentCloud: null,
       currentCloud: null,
       requiredFields: null,
       requiredFields: null,
-      connectionName: null,
+      connectionName: "",
       description: null
       description: null
     })
     })
   }
   }
@@ -307,26 +323,6 @@ class AddCloudConnection extends Reflux.Component {
     return returnValue
     return returnValue
   }
   }
 
 
-  connectToCloud() {
-    // TODO: Validation here
-    let valid = true
-
-    for (var i in this.state.currentCloudData) {
-      if (this.state.requiredFields.indexOf(i) > -1 && !this.state.currentCloudData[i]) {
-        valid = false
-      }
-    }
-    if (!valid) {
-      NotificationActions.notify("Please fill all required fields", "error")
-      this.setState({ cloudFormsSubmitted: true })
-    } else {
-      this.setState({ isConnecting: true })
-      setTimeout(() => {
-        this.setState({ isConnecting: false, connected: true })
-      }, 1000)
-    }
-  }
-
   handleCloudFieldChange(e, field) {
   handleCloudFieldChange(e, field) {
     let currentCloudData = this.state.currentCloudData
     let currentCloudData = this.state.currentCloudData
     if (field.type == 'dropdown') {
     if (field.type == 'dropdown') {
@@ -348,10 +344,12 @@ class AddCloudConnection extends Reflux.Component {
           <div className={s.cloudImage}>
           <div className={s.cloudImage}>
             <div className={" icon large-cloud " + this.state.currentCloud.name}></div>
             <div className={" icon large-cloud " + this.state.currentCloud.name}></div>
           </div>
           </div>
-          <div className="form-group">
+          <div className={"form-group " + (this.state.cloudFormsSubmitted &&
+            this.state.connectionName.trim().length == 0 ? s.error : "")}
+          >
             <input
             <input
               type="text"
               type="text"
-              placeholder="Endpoint Name"
+              placeholder="Endpoint Name *"
               onChange={(e) => this.handleChangeName(e)}
               onChange={(e) => this.handleChangeName(e)}
               value={this.state.connectionName}
               value={this.state.connectionName}
             />
             />
@@ -417,7 +415,7 @@ class AddCloudConnection extends Reflux.Component {
       if (this.state.allClouds) {
       if (this.state.allClouds) {
         modalBody = this.renderCloudList()
         modalBody = this.renderCloudList()
       } else {
       } else {
-        modalBody = <LoadingIcon/>
+        modalBody = <LoadingIcon />
       }
       }
     } else {
     } else {
       modalBody = this.renderCloudFields(this.state.currentCloud)
       modalBody = this.renderCloudFields(this.state.currentCloud)

+ 5 - 0
src/stores/ConnectionsStore/ConnectionsStore.js

@@ -217,6 +217,7 @@ class ConnectionsStore extends Reflux.Store
     if (this.state.connections) {
     if (this.state.connections) {
       let connection = this.state.connections.filter((connection => connection.id == connectionId))[0]
       let connection = this.state.connections.filter((connection => connection.id == connectionId))[0]
       if (connection.connection_info && connection.connection_info.secret_ref) {
       if (connection.connection_info && connection.connection_info.secret_ref) {
+        console.log("secret_ref", connection.connection_info.secret_ref)
         let index = connection.connection_info.secret_ref.lastIndexOf("/")
         let index = connection.connection_info.secret_ref.lastIndexOf("/")
         let uuid = connection.connection_info.secret_ref.substr(index + 1)
         let uuid = connection.connection_info.secret_ref.substr(index + 1)
 
 
@@ -258,10 +259,14 @@ class ConnectionsStore extends Reflux.Store
   }
   }
 
 
   onDeleteConnectionCompleted(connection) {
   onDeleteConnectionCompleted(connection) {
+    console.log("DELETED", connection)
     let connections = this.state.connections
     let connections = this.state.connections
+    console.log(connections.length)
     let index = connections.indexOf(connection)
     let index = connections.indexOf(connection)
     connections.splice(index, 1)
     connections.splice(index, 1)
+    console.log(connections.length)
     this.setState({ connections: connections })
     this.setState({ connections: connections })
+
     ConnectionsActions.assignConnectionProvider()
     ConnectionsActions.assignConnectionProvider()
   }
   }