Skip to content

Commit c902bee

Browse files
author
Sergiu Miclea
committed
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.
1 parent 2098452 commit c902bee

7 files changed

Lines changed: 69 additions & 14 deletions

File tree

src/components/organisms/Endpoint/Endpoint.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class Endpoint extends React.Component<Props, State> {
254254
if (!endpointStore.validation) {
255255
return
256256
}
257-
// $FlowIssue
257+
258258
let succesful = DomUtils.copyTextToClipboard(endpointStore.validation.message)
259259

260260
if (succesful) {

src/config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export const servicesUrl = {
2929

3030
export const userDomain = 'default'
3131

32-
export const useSecret = true // use secret_ref when creating and endpoint
32+
// Whether to use Barbican secrets when creating a new endpoint
33+
export const useSecret = true
34+
35+
// Shows the 'Use Current User/Project/Domain for Authentification' switch
36+
// when creating a new openstack endpoint
37+
export const showOpenstackCurrentUserSwitch = false
3338

3439
export const navigationMenu = [
3540
{ label: 'Replicas', value: 'replicas' },

src/plugins/endpoint/openstack/ConnectionSchemaPlugin.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import type { Schema } from '../../../types/Schema'
1818
import type { Field } from '../../../types/Field'
19+
import type { Endpoint } from '../../../types/Endpoint'
1920

2021
import DefaultConnectionSchemaParser from '../default/ConnectionSchemaPlugin'
2122

@@ -74,13 +75,35 @@ export default class ConnectionSchemaParser {
7475
createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
7576

7677
customSort(fields)
78+
fields.push({
79+
name: 'openstack_use_current_user',
80+
type: 'boolean',
81+
})
7782
return fields
7883
}
7984

8085
static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
86+
if (data.openstack_use_current_user) {
87+
return {
88+
name: data.name,
89+
description: data.description,
90+
connection_info: {},
91+
}
92+
}
8193
delete data.project_domain
8294
delete data.user_domain
8395
let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
8496
return payload
8597
}
98+
99+
static parseConnectionResponse(endpoint: Endpoint) {
100+
if (!endpoint.connection_info || Object.keys(endpoint.connection_info).length === 0) {
101+
return {
102+
openstack_use_current_user: true,
103+
...endpoint,
104+
}
105+
}
106+
107+
return endpoint
108+
}
86109
}

src/plugins/endpoint/openstack/ContentPlugin.jsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
import React from 'react'
1818
import styled from 'styled-components'
1919

20+
import { showOpenstackCurrentUserSwitch } from '../../../config'
21+
2022
import ToggleButtonBar from '../../../components/atoms/ToggleButtonBar'
2123
import type { Field } from '../../../types/Field'
2224
import { Wrapper, Fields, FieldStyled, Row } from '../default/ContentPlugin'
@@ -48,6 +50,10 @@ class ContentPlugin extends React.Component<Props, State> {
4850

4951
previouslySelectedChoices: string[] = []
5052

53+
get useCurrentUser(): boolean {
54+
return Boolean(this.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === 'openstack_use_current_user')))
55+
}
56+
5157
componentDidMount() {
5258
this.props.onRef(this)
5359
}
@@ -99,7 +105,7 @@ class ContentPlugin extends React.Component<Props, State> {
99105
let inputChoices = ['user_domain', 'project_domain']
100106

101107
const invalidFields = this.props.connectionInfoSchema.filter(field => {
102-
if (field.required) {
108+
if (this.isFieldRequired(field)) {
103109
let value = this.getFieldValue(field)
104110
return !value
105111
}
@@ -117,11 +123,15 @@ class ContentPlugin extends React.Component<Props, State> {
117123
}
118124

119125
filterSimpleAdvanced(): Field[] {
120-
let extraAdvancedFields = ['description', 'glance_api_version', 'identity_api_version']
126+
let extraAdvancedFields = ['description', 'glance_api_version', 'identity_api_version', 'openstack_use_current_user']
121127
if (this.getApiVersion() > 2) {
122128
extraAdvancedFields = extraAdvancedFields.concat(['user_domain', 'project_domain'])
123129
}
124130
let ignoreFields = ['user_domain_id', 'project_domain_id', 'user_domain_name', 'project_domain_name']
131+
if (!showOpenstackCurrentUserSwitch) {
132+
ignoreFields.push('openstack_use_current_user')
133+
}
134+
125135
return this.props.connectionInfoSchema.filter(f => !ignoreFields.find(i => i === f.name)).filter(field => {
126136
if (this.state.useAdvancedOptions) {
127137
return true
@@ -130,6 +140,10 @@ class ContentPlugin extends React.Component<Props, State> {
130140
})
131141
}
132142

143+
isFieldRequired(field: Field) {
144+
return this.useCurrentUser ? field.name === 'name' : field.required
145+
}
146+
133147
renderSimpleAdvancedToggle() {
134148
return (
135149
<ToggleButtonBarStyled
@@ -146,12 +160,17 @@ class ContentPlugin extends React.Component<Props, State> {
146160
let fields = this.filterSimpleAdvanced()
147161

148162
fields.forEach((field, i) => {
163+
let disabled = this.props.disabled
164+
|| (this.useCurrentUser && field.name !== 'name' && field.name !== 'description' && field.name !== 'openstack_use_current_user')
165+
let required = this.isFieldRequired(field)
166+
|| (this.getApiVersion() > 2 ? field.name === 'user_domain' || field.name === 'project_domain' : false)
167+
149168
const currentField = (
150169
<FieldStyled
151170
{...field}
152-
required={field.required || (this.getApiVersion() > 2 ? field.name === 'user_domain' || field.name === 'project_domain' : false)}
171+
required={required}
153172
large
154-
disabled={this.props.disabled}
173+
disabled={disabled}
155174
password={field.name === 'password'}
156175
highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
157176
value={this.getFieldValue(field)}
@@ -167,7 +186,7 @@ class ContentPlugin extends React.Component<Props, State> {
167186
{currentField}
168187
</Row>
169188
))
170-
} else if (i === this.props.connectionInfoSchema.length - 1) {
189+
} else if (i === fields.length - 1) {
171190
rows.push((
172191
<Row key={field.name}>
173192
{currentField}

src/sources/EndpointSource.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class EdnpointSource {
4242
let connections = []
4343
if (response.data.endpoints.length) {
4444
response.data.endpoints.forEach(endpoint => {
45-
connections.push(endpoint)
45+
connections.push(SchemaParser.parseConnectionResponse(endpoint))
4646
})
4747
}
4848

@@ -115,10 +115,8 @@ class EdnpointSource {
115115
static update(endpoint: Endpoint): Promise<Endpoint> {
116116
let parsedEndpoint = SchemaParser.fieldsToPayload(endpoint)
117117

118-
if (parsedEndpoint.connection_info && parsedEndpoint.connection_info.secret_ref) {
119-
// $FlowIgnore
118+
if (parsedEndpoint.connectionInfo && Object.keys(parsedEndpoint.connectionInfo).length > 0 && parsedEndpoint.connection_info.secret_ref) {
120119
let uuidIndex = parsedEndpoint.connection_info.secret_ref.lastIndexOf('/')
121-
// $FlowIgnore
122120
let uuid = parsedEndpoint.connection_info.secret_ref.substr(uuidIndex + 1)
123121
let newEndpoint: any = {}
124122
let connectionInfo = {}
@@ -169,15 +167,15 @@ class EdnpointSource {
169167
method: 'PUT',
170168
data: { endpoint: parsedEndpoint },
171169
}).then(response => {
172-
return response.data.endpoint
170+
return SchemaParser.parseConnectionResponse(response.data.endpoint)
173171
})
174172
}
175173

176174
static add(endpoint: Endpoint, skipSchemaParser: boolean = false): Promise<Endpoint> {
177175
let parsedEndpoint: any = skipSchemaParser ? { ...endpoint } : SchemaParser.fieldsToPayload(endpoint)
178176
let newEndpoint: any = {}
179177
let connectionInfo = {}
180-
if (useSecret) {
178+
if (useSecret && parsedEndpoint.connectionInfo && Object.keys(parsedEndpoint.connectionInfo).length > 0) {
181179
return Api.send({
182180
url: `${servicesUrl.barbican}/v1/secrets`,
183181
method: 'POST',
@@ -226,7 +224,7 @@ class EdnpointSource {
226224
},
227225
},
228226
}).then(response => {
229-
return response.data.endpoint
227+
return SchemaParser.parseConnectionResponse(response.data.endpoint)
230228
})
231229
}
232230

src/sources/Schemas.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
import { ConnectionSchemaPlugin } from '../plugins/endpoint'
1818
import { defaultSchemaToFields } from '../plugins/endpoint/default/ConnectionSchemaPlugin'
1919
import type { Schema } from '../types/Schema'
20+
import type { Endpoint } from '../types/Endpoint'
2021

2122
class SchemaParser {
2223
static storedConnectionsSchemas = {}
@@ -55,6 +56,14 @@ class SchemaParser {
5556

5657
return payload
5758
}
59+
60+
static parseConnectionResponse(endpoint: Endpoint) {
61+
let parseConnectionResponse = ConnectionSchemaPlugin[endpoint.type] && ConnectionSchemaPlugin[endpoint.type].parseConnectionResponse
62+
if (!parseConnectionResponse) {
63+
return endpoint
64+
}
65+
return parseConnectionResponse(endpoint)
66+
}
5867
}
5968

6069
export { SchemaParser }

src/utils/LabelDictionary.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class LabelDictionary {
5252
sql_server_hostname: 'SQL Server Hostname Suffix',
5353
storage_endpoint: 'Storage Endpoint Suffix',
5454
preserve_nic_ips: 'Preserve NIC IPs',
55+
openstack_use_current_user: 'Use Current User/Project/Domain for Authentification',
5556
}
5657

5758
// Fields which have enums for which dictionary labels should be used.

0 commit comments

Comments
 (0)