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

Removed explicit 'azure' code in ConnectionsStore - CORWEB-62

Added support for multiple login types (like the ones used by Azure)
Note: CORWEB-62 depends on CORWEB-25 and must be merged after it.

Merge branch 'master' of https://github.com/smiclea/coriolis-web into CORWEB-62
Sergiu Miclea 8 лет назад
Родитель
Сommit
f70fde44ad
2 измененных файлов с 83 добавлено и 101 удалено
  1. 2 0
      src/constants/CloudLabels.js
  2. 81 101
      src/stores/ConnectionsStore/ConnectionsStore.js

+ 2 - 0
src/constants/CloudLabels.js

@@ -50,6 +50,8 @@ export const defaultLabels = {
   resource_group: "Resource Group",
   worker_size: "Worker Size",
   subscription_id: "Subscription ID",
+  user_credentials: "User Credentials",
+  service_principal_credentials: "Service Principal Credentials",
   tenant_id: "Tenant ID",
   client_id: "Client ID",
   client_secret: "Client Secret",

+ 81 - 101
src/stores/ConnectionsStore/ConnectionsStore.js

@@ -255,123 +255,103 @@ class ConnectionsStore extends Reflux.Store
   }
 
   static processCloud(cloudData, providerName = null, type = 'connection') {
-    if (providerName != "azure" && providerName != null && cloudData.oneOf) {
+    if (!cloudData.hasOwnProperty('type')) {
       cloudData = cloudData.oneOf[0]
     }
 
-    if (providerName == "azure" && type == "connection") {
-      let userCredentialFields = {
-        properties: { subscription_id: cloudData.properties.subscription_id },
-        required: cloudData.properties.user_credentials.required
+    let fields = []
+    let sortedFields = [{}, {}]
+    for (let propName in cloudData.properties) {
+      let field = {
+        name: propName,
+        label: Helper.convertCloudFieldLabel(propName)
       }
 
-      userCredentialFields.required.push("subscription_id")
-
-      for (var i in cloudData.properties.user_credentials.properties) {
-        userCredentialFields.properties[i] = cloudData.properties.user_credentials.properties[i]
-      }
-
-      let servicePrincipalCredentialFields = {
-        properties: { subscription_id: cloudData.properties.subscription_id },
-        required: cloudData.properties.service_principal_credentials.required
+      if (cloudData.properties[propName].default) {
+        field.default = cloudData.properties[propName].default
       }
-      for (var i in cloudData.properties.service_principal_credentials.properties) {
-        servicePrincipalCredentialFields.properties[i] = cloudData.properties.service_principal_credentials.properties[i]
-      }
-      servicePrincipalCredentialFields.required.push("subscription_id")
-
-      let newCloudData = {
-        type: "switch-radio",
-        name: "login_type",
-        options: [
-          {
-            label: "User Credentials",
-            value: "user_credentials",
-            fields: this.processCloud(userCredentialFields),
-            default: true
-          },
-          {
-            label: "Service Principal Credentials",
-            value: "service_principal_credentials",
-            fields: this.processCloud(servicePrincipalCredentialFields)
-          }
-        ]
-      }
-
-      return [newCloudData]
-    } else {
-      let fields = []
-      let sortedFields = [{}, {}]
-      for (let propName in cloudData.properties) {
-        let field = {
-          name: propName,
-          label: Helper.convertCloudFieldLabel(propName)
-        }
-
-        if (cloudData.properties[propName].default) {
-          field.default = cloudData.properties[propName].default
-        }
-        switch (cloudData.properties[propName].type) {
-          case "boolean":
+      switch (cloudData.properties[propName].type) {
+        case "boolean":
+          // Values need to be strings, due to a limitation in react-dropdown
+          field.options = [
+            {label: 'Yes', value: 'true'}, 
+            {label: 'No', value: 'false'}
+          ]
+
+          field.default = (field.default === 'true' || field.default === true) ? 'true' : 'false'
+          break
+
+        case "string":
+          field.type = "text"
+          break
+
+        case "integer":
+          if (cloudData.properties[propName].minimum && cloudData.properties[propName].maximum) {
             field.type = "dropdown"
-
-            // Values need to be strings, due to a limitation in react-dropdown
-            field.options = [
-              {label: 'Yes', value: 'true'}, 
-              {label: 'No', value: 'false'}
-            ]
-
-            field.default = (field.default === 'true' || field.default === true) ? 'true' : 'false'
-            break
-
-          case "string":
-            field.type = "text"
-            break
-
-          case "integer":
-            if (cloudData.properties[propName].minimum && cloudData.properties[propName].maximum) {
-              field.type = "dropdown"
-              field.options = []
-              for (let i = cloudData.properties[propName].minimum; i <= cloudData.properties[propName].maximum; i++) {
-                // Values need to be strings, due to a limitation in react-dropdown
-                field.options.push({
-                    label: i.toString(),
-                    value: i.toString()
-                  },
-                )
-              }
-            } else {
-              field.type = "text"
+            field.options = []
+            for (let i = cloudData.properties[propName].minimum; i <= cloudData.properties[propName].maximum; i++) {
+              // Values need to be strings, due to a limitation in react-dropdown
+              field.options.push({
+                  label: i.toString(),
+                  value: i.toString()
+                },
+              )
             }
+          } else {
+            field.type = "text"
+          }
+          break
+          case "object":
+            field.value = field.name
+            field.default = true
+            field.fields = this.processCloud(cloudData.properties[propName])
             break
         }
 
-        field.defaultValue = cloudData.properties[propName].default;
-        field.dataType = cloudData.properties[propName].type;
-
-        if (field.name == 'username') {
-          field.required = true
-          sortedFields[0] = field
-        } else if (field.name == 'password') {
-          field.type = "password"
-          field.required = true
-          sortedFields[1] = field
-        } else if (cloudData.required.indexOf(field.name) > -1) {
-          field.required = true
-          sortedFields.push(field)
-        } else {
-          fields.push(field)
-        }
+      field.defaultValue = cloudData.properties[propName].default;
+      field.dataType = cloudData.properties[propName].type;
+
+      if (field.name == 'username') {
+        field.required = true
+        sortedFields[0] = field
+      } else if (field.name == 'password') {
+        field.type = "password"
+        field.required = true
+        sortedFields[1] = field
+      } else if (cloudData.required && cloudData.required.indexOf(field.name) > -1) {
+        field.required = true
+        sortedFields.push(field)
+      } else {
+        fields.push(field)
       }
-      //in case we don't have username and password
-      if (Object.keys(sortedFields[0]).length === 0 && Object.keys(sortedFields[0]).length === 0) {
-        sortedFields.shift()
-        sortedFields.shift()
+    }
+    //in case we don't have username and password
+    if (Object.keys(sortedFields[0]).length === 0 && Object.keys(sortedFields[0]).length === 0) {
+      sortedFields.shift()
+      sortedFields.shift()
+    }
+
+    // If a hierarchical structure has been generated, 
+    // group all top level nodes (fields of type object) in a 'login_type' switch (basically multpiple login types are now supported),
+    // put all non-nodes (fields not of type object) as children of top level nodes
+    let objectFields = fields.filter(field => field.dataType === 'object')
+    if (objectFields.length > 1) {
+      let otherFields = fields.filter(field => field.dataType !== 'object' && field.name !== 'secret_ref')
+      let loginRadioField = {
+        type: "switch-radio",
+        name: "login_type",
+        options: objectFields
       }
-      console.log("sortedFields.concat(fields)", sortedFields.concat(fields))
+      
+      loginRadioField.options.forEach(option => {
+        option.fields = option.fields.concat(otherFields)
+      })
 
+      return [loginRadioField]
+    } else {
       return sortedFields.concat(fields)
     }
+    
   }
 }