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

Shows 'Use Current for Authentication' switch

The switch appears when creating or editing an Openstack endpoint if the
option is enabled in `config.js` (`showOpenstackCurrentUserSwitch`).

If the switch is toggled an empty `connection_string` is sent to the
API.
Sergiu Miclea 7 лет назад
Родитель
Сommit
c902beea8a

+ 1 - 1
src/components/organisms/Endpoint/Endpoint.jsx

@@ -254,7 +254,7 @@ class Endpoint extends React.Component<Props, State> {
     if (!endpointStore.validation) {
     if (!endpointStore.validation) {
       return
       return
     }
     }
-    // $FlowIssue
+
     let succesful = DomUtils.copyTextToClipboard(endpointStore.validation.message)
     let succesful = DomUtils.copyTextToClipboard(endpointStore.validation.message)
 
 
     if (succesful) {
     if (succesful) {

+ 6 - 1
src/config.js

@@ -29,7 +29,12 @@ export const servicesUrl = {
 
 
 export const userDomain = 'default'
 export const userDomain = 'default'
 
 
-export const useSecret = true // use secret_ref when creating and endpoint
+// Whether to use Barbican secrets when creating a new endpoint
+export const useSecret = true
+
+// Shows the 'Use Current User/Project/Domain for Authentification' switch
+// when creating a new openstack endpoint
+export const showOpenstackCurrentUserSwitch = false
 
 
 export const navigationMenu = [
 export const navigationMenu = [
   { label: 'Replicas', value: 'replicas' },
   { label: 'Replicas', value: 'replicas' },

+ 23 - 0
src/plugins/endpoint/openstack/ConnectionSchemaPlugin.js

@@ -16,6 +16,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 import type { Schema } from '../../../types/Schema'
 import type { Schema } from '../../../types/Schema'
 import type { Field } from '../../../types/Field'
 import type { Field } from '../../../types/Field'
+import type { Endpoint } from '../../../types/Endpoint'
 
 
 import DefaultConnectionSchemaParser from '../default/ConnectionSchemaPlugin'
 import DefaultConnectionSchemaParser from '../default/ConnectionSchemaPlugin'
 
 
@@ -74,13 +75,35 @@ export default class ConnectionSchemaParser {
     createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
     createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
 
 
     customSort(fields)
     customSort(fields)
+    fields.push({
+      name: 'openstack_use_current_user',
+      type: 'boolean',
+    })
     return fields
     return fields
   }
   }
 
 
   static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
   static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
+    if (data.openstack_use_current_user) {
+      return {
+        name: data.name,
+        description: data.description,
+        connection_info: {},
+      }
+    }
     delete data.project_domain
     delete data.project_domain
     delete data.user_domain
     delete data.user_domain
     let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
     let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
     return payload
     return payload
   }
   }
+
+  static parseConnectionResponse(endpoint: Endpoint) {
+    if (!endpoint.connection_info || Object.keys(endpoint.connection_info).length === 0) {
+      return {
+        openstack_use_current_user: true,
+        ...endpoint,
+      }
+    }
+
+    return endpoint
+  }
 }
 }

+ 24 - 5
src/plugins/endpoint/openstack/ContentPlugin.jsx

@@ -17,6 +17,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import React from 'react'
 import React from 'react'
 import styled from 'styled-components'
 import styled from 'styled-components'
 
 
+import { showOpenstackCurrentUserSwitch } from '../../../config'
+
 import ToggleButtonBar from '../../../components/atoms/ToggleButtonBar'
 import ToggleButtonBar from '../../../components/atoms/ToggleButtonBar'
 import type { Field } from '../../../types/Field'
 import type { Field } from '../../../types/Field'
 import { Wrapper, Fields, FieldStyled, Row } from '../default/ContentPlugin'
 import { Wrapper, Fields, FieldStyled, Row } from '../default/ContentPlugin'
@@ -48,6 +50,10 @@ class ContentPlugin extends React.Component<Props, State> {
 
 
   previouslySelectedChoices: string[] = []
   previouslySelectedChoices: string[] = []
 
 
+  get useCurrentUser(): boolean {
+    return Boolean(this.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === 'openstack_use_current_user')))
+  }
+
   componentDidMount() {
   componentDidMount() {
     this.props.onRef(this)
     this.props.onRef(this)
   }
   }
@@ -99,7 +105,7 @@ class ContentPlugin extends React.Component<Props, State> {
     let inputChoices = ['user_domain', 'project_domain']
     let inputChoices = ['user_domain', 'project_domain']
 
 
     const invalidFields = this.props.connectionInfoSchema.filter(field => {
     const invalidFields = this.props.connectionInfoSchema.filter(field => {
-      if (field.required) {
+      if (this.isFieldRequired(field)) {
         let value = this.getFieldValue(field)
         let value = this.getFieldValue(field)
         return !value
         return !value
       }
       }
@@ -117,11 +123,15 @@ class ContentPlugin extends React.Component<Props, State> {
   }
   }
 
 
   filterSimpleAdvanced(): Field[] {
   filterSimpleAdvanced(): Field[] {
-    let extraAdvancedFields = ['description', 'glance_api_version', 'identity_api_version']
+    let extraAdvancedFields = ['description', 'glance_api_version', 'identity_api_version', 'openstack_use_current_user']
     if (this.getApiVersion() > 2) {
     if (this.getApiVersion() > 2) {
       extraAdvancedFields = extraAdvancedFields.concat(['user_domain', 'project_domain'])
       extraAdvancedFields = extraAdvancedFields.concat(['user_domain', 'project_domain'])
     }
     }
     let ignoreFields = ['user_domain_id', 'project_domain_id', 'user_domain_name', 'project_domain_name']
     let ignoreFields = ['user_domain_id', 'project_domain_id', 'user_domain_name', 'project_domain_name']
+    if (!showOpenstackCurrentUserSwitch) {
+      ignoreFields.push('openstack_use_current_user')
+    }
+
     return this.props.connectionInfoSchema.filter(f => !ignoreFields.find(i => i === f.name)).filter(field => {
     return this.props.connectionInfoSchema.filter(f => !ignoreFields.find(i => i === f.name)).filter(field => {
       if (this.state.useAdvancedOptions) {
       if (this.state.useAdvancedOptions) {
         return true
         return true
@@ -130,6 +140,10 @@ class ContentPlugin extends React.Component<Props, State> {
     })
     })
   }
   }
 
 
+  isFieldRequired(field: Field) {
+    return this.useCurrentUser ? field.name === 'name' : field.required
+  }
+
   renderSimpleAdvancedToggle() {
   renderSimpleAdvancedToggle() {
     return (
     return (
       <ToggleButtonBarStyled
       <ToggleButtonBarStyled
@@ -146,12 +160,17 @@ class ContentPlugin extends React.Component<Props, State> {
     let fields = this.filterSimpleAdvanced()
     let fields = this.filterSimpleAdvanced()
 
 
     fields.forEach((field, i) => {
     fields.forEach((field, i) => {
+      let disabled = this.props.disabled
+        || (this.useCurrentUser && field.name !== 'name' && field.name !== 'description' && field.name !== 'openstack_use_current_user')
+      let required = this.isFieldRequired(field)
+        || (this.getApiVersion() > 2 ? field.name === 'user_domain' || field.name === 'project_domain' : false)
+
       const currentField = (
       const currentField = (
         <FieldStyled
         <FieldStyled
           {...field}
           {...field}
-          required={field.required || (this.getApiVersion() > 2 ? field.name === 'user_domain' || field.name === 'project_domain' : false)}
+          required={required}
           large
           large
-          disabled={this.props.disabled}
+          disabled={disabled}
           password={field.name === 'password'}
           password={field.name === 'password'}
           highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
           highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
           value={this.getFieldValue(field)}
           value={this.getFieldValue(field)}
@@ -167,7 +186,7 @@ class ContentPlugin extends React.Component<Props, State> {
             {currentField}
             {currentField}
           </Row>
           </Row>
         ))
         ))
-      } else if (i === this.props.connectionInfoSchema.length - 1) {
+      } else if (i === fields.length - 1) {
         rows.push((
         rows.push((
           <Row key={field.name}>
           <Row key={field.name}>
             {currentField}
             {currentField}

+ 5 - 7
src/sources/EndpointSource.js

@@ -42,7 +42,7 @@ class EdnpointSource {
       let connections = []
       let connections = []
       if (response.data.endpoints.length) {
       if (response.data.endpoints.length) {
         response.data.endpoints.forEach(endpoint => {
         response.data.endpoints.forEach(endpoint => {
-          connections.push(endpoint)
+          connections.push(SchemaParser.parseConnectionResponse(endpoint))
         })
         })
       }
       }
 
 
@@ -115,10 +115,8 @@ class EdnpointSource {
   static update(endpoint: Endpoint): Promise<Endpoint> {
   static update(endpoint: Endpoint): Promise<Endpoint> {
     let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
     let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
 
 
-    if (parsedEndpoint.connection_info && parsedEndpoint.connection_info.secret_ref) {
-      // $FlowIgnore
+    if (parsedEndpoint.connectionInfo && Object.keys(parsedEndpoint.connectionInfo).length > 0 && parsedEndpoint.connection_info.secret_ref) {
       let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
       let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
-      // $FlowIgnore
       let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
       let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
       let newEndpoint: any = {}
       let newEndpoint: any = {}
       let connectionInfo = {}
       let connectionInfo = {}
@@ -169,7 +167,7 @@ class EdnpointSource {
       method: 'PUT',
       method: 'PUT',
       data: { endpoint: parsedEndpoint },
       data: { endpoint: parsedEndpoint },
     }).then(response => {
     }).then(response => {
-      return response.data.endpoint
+      return SchemaParser.parseConnectionResponse(response.data.endpoint)
     })
     })
   }
   }
 
 
@@ -177,7 +175,7 @@ class EdnpointSource {
     let parsedEndpoint: any = skipSchemaParser ? { ...endpoint } : SchemaParser.fieldsToPayload(endpoint)
     let parsedEndpoint: any = skipSchemaParser ? { ...endpoint } : SchemaParser.fieldsToPayload(endpoint)
     let newEndpoint: any = {}
     let newEndpoint: any = {}
     let connectionInfo = {}
     let connectionInfo = {}
-    if (useSecret) {
+    if (useSecret && parsedEndpoint.connectionInfo && Object.keys(parsedEndpoint.connectionInfo).length > 0) {
       return Api.send({
       return Api.send({
         url: `${servicesUrl.barbican}/v1/secrets`,
         url: `${servicesUrl.barbican}/v1/secrets`,
         method: 'POST',
         method: 'POST',
@@ -226,7 +224,7 @@ class EdnpointSource {
         },
         },
       },
       },
     }).then(response => {
     }).then(response => {
-      return response.data.endpoint
+      return SchemaParser.parseConnectionResponse(response.data.endpoint)
     })
     })
   }
   }
 
 

+ 9 - 0
src/sources/Schemas.js

@@ -17,6 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import { ConnectionSchemaPlugin } from '../plugins/endpoint'
 import { ConnectionSchemaPlugin } from '../plugins/endpoint'
 import { defaultSchemaToFields } from '../plugins/endpoint/default/ConnectionSchemaPlugin'
 import { defaultSchemaToFields } from '../plugins/endpoint/default/ConnectionSchemaPlugin'
 import type { Schema } from '../types/Schema'
 import type { Schema } from '../types/Schema'
+import type { Endpoint } from '../types/Endpoint'
 
 
 class SchemaParser {
 class SchemaParser {
   static storedConnectionsSchemas = {}
   static storedConnectionsSchemas = {}
@@ -55,6 +56,14 @@ class SchemaParser {
 
 
     return payload
     return payload
   }
   }
+
+  static parseConnectionResponse(endpoint: Endpoint) {
+    let parseConnectionResponse = ConnectionSchemaPlugin[endpoint.type] && ConnectionSchemaPlugin[endpoint.type].parseConnectionResponse
+    if (!parseConnectionResponse) {
+      return endpoint
+    }
+    return parseConnectionResponse(endpoint)
+  }
 }
 }
 
 
 export { SchemaParser }
 export { SchemaParser }

+ 1 - 0
src/utils/LabelDictionary.js

@@ -52,6 +52,7 @@ class LabelDictionary {
     sql_server_hostname: 'SQL Server Hostname Suffix',
     sql_server_hostname: 'SQL Server Hostname Suffix',
     storage_endpoint: 'Storage Endpoint Suffix',
     storage_endpoint: 'Storage Endpoint Suffix',
     preserve_nic_ips: 'Preserve NIC IPs',
     preserve_nic_ips: 'Preserve NIC IPs',
+    openstack_use_current_user: 'Use Current User/Project/Domain for Authentification',
   }
   }
 
 
   // Fields which have enums for which dictionary labels should be used.
   // Fields which have enums for which dictionary labels should be used.