5
5
6
6
import { OpenShiftObject , Command } from "../odo" ;
7
7
import { OpenShiftItem } from './openshiftItem' ;
8
- import { window , commands , env } from 'vscode' ;
8
+ import { window , commands , env , QuickPickItem } from 'vscode' ;
9
9
import { CliExitData , Cli } from "../cli" ;
10
10
import open = require( "open" ) ;
11
11
import { TokenStore } from "../util/credentialManager" ;
12
+ import { KubeConfigUtils } from '../util/kubeUtils' ;
12
13
14
+
15
+ class CreateUrlItem implements QuickPickItem {
16
+
17
+ constructor ( ) { }
18
+
19
+ get label ( ) : string { return `$(plus) Provide new URL...` ; }
20
+ get description ( ) : string { return '' ; }
21
+
22
+ }
23
+
24
+ class CreateUserItem implements QuickPickItem {
25
+
26
+ constructor ( ) { }
27
+
28
+ get label ( ) : string { return `$(plus) Add new user...` ; }
29
+ get description ( ) : string { return '' ; }
30
+
31
+ }
13
32
export class Cluster extends OpenShiftItem {
14
33
15
34
static async logout ( ) : Promise < string > {
@@ -61,13 +80,18 @@ export class Cluster extends OpenShiftItem {
61
80
}
62
81
63
82
static async getUrl ( ) : Promise < string | null > {
83
+ const k8sConfig = new KubeConfigUtils ( ) ;
64
84
const clusterURl = await Cluster . getUrlFromClipboard ( ) ;
65
- return await window . showInputBox ( {
66
- value : clusterURl ,
67
- ignoreFocusOut : true ,
68
- prompt : "Provide URL of the cluster to connect" ,
69
- validateInput : ( value : string ) => Cluster . validateUrl ( 'Invalid URL provided' , value )
70
- } ) ;
85
+ const createUrl = new CreateUrlItem ( ) ;
86
+ const clusterItems = await k8sConfig . getServers ( ) ;
87
+ const choice = await window . showQuickPick ( [ createUrl , ...clusterItems ] , { placeHolder : "Provide Cluster URL to connect" } ) ;
88
+ return ( choice . label === createUrl . label ) ?
89
+ await window . showInputBox ( {
90
+ value : clusterURl ,
91
+ ignoreFocusOut : true ,
92
+ prompt : "Provide new Cluster URL to connect" ,
93
+ validateInput : ( value : string ) => Cluster . validateUrl ( 'Invalid URL provided' , value )
94
+ } ) : choice . label ;
71
95
}
72
96
73
97
static async login ( ) : Promise < string > {
@@ -103,25 +127,42 @@ export class Cluster extends OpenShiftItem {
103
127
static async credentialsLogin ( skipConfirmation : boolean = false ) : Promise < string > {
104
128
let password : string ;
105
129
const response = await Cluster . requestLoginConfirmation ( skipConfirmation ) ;
130
+
106
131
if ( response !== 'Yes' ) return null ;
132
+
107
133
const clusterURL = await Cluster . getUrl ( ) ;
134
+
108
135
if ( ! clusterURL ) return null ;
136
+
109
137
const getUserName = await TokenStore . getUserName ( ) ;
110
- const username = await window . showInputBox ( {
111
- ignoreFocusOut : true ,
112
- prompt : "Provide Username for basic authentication to the API server" ,
113
- value : getUserName ,
114
- validateInput : ( value : string ) => Cluster . emptyName ( 'User name cannot be empty' , value )
115
- } ) ;
138
+ const k8sConfig = new KubeConfigUtils ( ) ;
139
+ const users = await k8sConfig . getClusterUsers ( clusterURL ) ;
140
+ const addUser = new CreateUserItem ( ) ;
141
+ const choice = await window . showQuickPick ( [ addUser , ...users ] , { placeHolder : "Select username for basic authentication to the API server" } ) ;
142
+
143
+ if ( ! choice ) return null ;
144
+
145
+ const username = ( choice . label === addUser . label ) ?
146
+ await window . showInputBox ( {
147
+ ignoreFocusOut : true ,
148
+ prompt : "Provide Username for basic authentication to the API server" ,
149
+ value : "" ,
150
+ validateInput : ( value : string ) => Cluster . emptyName ( 'User name cannot be empty' , value )
151
+ } ) : choice . label ;
152
+
116
153
if ( getUserName ) password = await TokenStore . getItem ( 'login' , username ) ;
154
+
117
155
if ( ! username ) return null ;
156
+
118
157
const passwd = await window . showInputBox ( {
119
158
ignoreFocusOut : true ,
120
159
password : true ,
121
160
prompt : "Provide Password for basic authentication to the API server" ,
122
161
value : password
123
162
} ) ;
163
+
124
164
if ( ! passwd ) return null ;
165
+
125
166
return Promise . resolve ( )
126
167
. then ( ( ) => Cluster . odo . execute ( Command . odoLoginWithUsernamePassword ( clusterURL , username , passwd ) ) )
127
168
. then ( ( result ) => Cluster . save ( username , passwd , password , result ) )
@@ -171,4 +212,4 @@ export class Cluster extends OpenShiftItem {
171
212
return Promise . reject ( result . stderr ) ;
172
213
}
173
214
}
174
- }
215
+ }
0 commit comments