1
- import { ConnectionString } from 'connection-string'
2
1
import { isUndefined , toString } from 'lodash'
3
2
import React from 'react'
4
3
import { FormikErrors } from 'formik'
5
- import { REDIS_URI_SCHEMES } from 'uiSrc/constants'
6
4
import { InstanceType } from 'uiSrc/slices/interfaces'
7
5
import {
8
6
ADD_NEW ,
@@ -13,7 +11,7 @@ import {
13
11
SshPassType
14
12
} from 'uiSrc/pages/home/constants'
15
13
import { DbConnectionInfo } from 'uiSrc/pages/home/interfaces'
16
- import { Nullable } from 'uiSrc/utils'
14
+ import { Nullable , parseRedisUrl } from 'uiSrc/utils'
17
15
18
16
export const getTlsSettings = ( values : DbConnectionInfo ) => ( {
19
17
useTls : values . tls ,
@@ -100,7 +98,7 @@ export const applySSHDatabase = (database: any, values: DbConnectionInfo) => {
100
98
database . ssh = true
101
99
database . sshOptions = {
102
100
host : sshHost ,
103
- port : + sshPort ,
101
+ port : sshPort ? + sshPort : undefined ,
104
102
username : sshUsername ,
105
103
}
106
104
@@ -201,74 +199,58 @@ export const autoFillFormDetails = (
201
199
instanceType : InstanceType
202
200
) : boolean => {
203
201
try {
204
- const details = new ConnectionString ( content )
202
+ const details = parseRedisUrl ( content )
205
203
206
- /* If a protocol exists, it should be a redis protocol */
207
- if ( details . protocol && ! REDIS_URI_SCHEMES . includes ( details . protocol ) ) return false
208
- /*
209
- * Auto fill logic:
210
- * 1) If the port is parsed, we are sure that the user has indeed copied a connection string.
211
- * '172.18.0.2:12000' => {host: '172,18.0.2', port: 12000}
212
- * 'redis-12000.cluster.local:12000' => {host: 'redis-12000.cluster.local', port: 12000}
213
- * 'lorem ipsum' => {host: undefined, port: undefined}
214
- * 2) If the port is `undefined` but a redis URI scheme is present as protocol, we follow
215
- * the "Scheme semantics" as mentioned in the official URI schemes.
216
- * i) redis:// - https://www.iana.org/assignments/uri-schemes/prov/redis
217
- * ii) rediss:// - https://www.iana.org/assignments/uri-schemes/prov/rediss
218
- */
219
- if (
220
- details . port !== undefined
221
- || REDIS_URI_SCHEMES . includes ( details . protocol || '' )
222
- ) {
223
- const getUpdatedInitialValues = ( ) => {
224
- switch ( instanceType ) {
225
- case InstanceType . RedisEnterpriseCluster : {
226
- return ( {
227
- host : details . hostname || initialValues . host || 'localhost' ,
228
- port : `${ details . port || initialValues . port || 9443 } ` ,
229
- username : details . user || '' ,
230
- password : details . password || '' ,
231
- } )
232
- }
204
+ if ( ! details ) return false
205
+
206
+ const getUpdatedInitialValues = ( ) => {
207
+ switch ( instanceType ) {
208
+ case InstanceType . RedisEnterpriseCluster : {
209
+ return ( {
210
+ host : details . host || initialValues . host || 'localhost' ,
211
+ port : `${ details . port || initialValues . port || 9443 } ` ,
212
+ username : details . username || '' ,
213
+ password : details . password || '' ,
214
+ } )
215
+ }
233
216
234
- case InstanceType . Sentinel : {
235
- return ( {
236
- host : details . hostname || initialValues . host || 'localhost' ,
237
- port : `${ details . port || initialValues . port || 9443 } ` ,
238
- username : details . user || '' ,
239
- password : details . password ,
240
- tls : details . protocol === 'rediss' ,
241
- } )
242
- }
217
+ case InstanceType . Sentinel : {
218
+ return ( {
219
+ host : details . host || initialValues . host || 'localhost' ,
220
+ port : `${ details . port || initialValues . port || 9443 } ` ,
221
+ username : details . username || '' ,
222
+ password : details . password ,
223
+ tls : details . protocol === 'rediss' ,
224
+ } )
225
+ }
243
226
244
- case InstanceType . Standalone : {
245
- return ( getFormValues ( {
246
- name : details . host || initialValues . name || 'localhost:6379' ,
247
- host : details . hostname || initialValues . host || 'localhost' ,
248
- port : `${ details . port || initialValues . port || 9443 } ` ,
249
- username : details . user || '' ,
250
- password : details . password ,
251
- tls : details . protocol === 'rediss' ,
252
- ssh : false ,
253
- sshPassType : SshPassType . Password
254
- } ) )
255
- }
256
- default : {
257
- return { }
258
- }
227
+ case InstanceType . Standalone : {
228
+ return ( getFormValues ( {
229
+ name : details . hostname || initialValues . name || 'localhost:6379' ,
230
+ host : details . host || initialValues . host || 'localhost' ,
231
+ port : `${ details . port || initialValues . port || 9443 } ` ,
232
+ username : details . username || '' ,
233
+ password : details . password ,
234
+ tls : details . protocol === 'rediss' ,
235
+ db : details . dbNumber ,
236
+ ssh : false ,
237
+ sshPassType : SshPassType . Password
238
+ } ) )
239
+ }
240
+ default : {
241
+ return { }
259
242
}
260
243
}
261
- setInitialValues ( getUpdatedInitialValues ( ) )
262
- /*
244
+ }
245
+ setInitialValues ( getUpdatedInitialValues ( ) )
246
+ /*
263
247
* autofill was successfull so return true
264
248
*/
265
- return true
266
- }
249
+ return true
267
250
} catch ( err ) {
268
251
/* The pasted content is not a connection URI so ignore. */
269
252
return false
270
253
}
271
- return false
272
254
}
273
255
274
256
export const getSubmitButtonContent = ( errors : FormikErrors < DbConnectionInfo > , submitIsDisabled ?: boolean ) => {
0 commit comments