55
66import { OpenShiftObject , Command } from "../odo" ;
77import { OpenShiftItem } from './openshiftItem' ;
8- import { window , commands , env } from 'vscode' ;
8+ import { window , commands , env , QuickPickItem } from 'vscode' ;
99import { CliExitData , Cli } from "../cli" ;
1010import open = require( "open" ) ;
1111import { TokenStore } from "../util/credentialManager" ;
12+ import { KubeConfigUtils } from '../util/kubeUtils' ;
1213
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+ }
1332export class Cluster extends OpenShiftItem {
1433
1534 static async logout ( ) : Promise < string > {
@@ -61,13 +80,18 @@ export class Cluster extends OpenShiftItem {
6180 }
6281
6382 static async getUrl ( ) : Promise < string | null > {
83+ const k8sConfig = new KubeConfigUtils ( ) ;
6484 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 ;
7195 }
7296
7397 static async login ( ) : Promise < string > {
@@ -103,25 +127,42 @@ export class Cluster extends OpenShiftItem {
103127 static async credentialsLogin ( skipConfirmation : boolean = false ) : Promise < string > {
104128 let password : string ;
105129 const response = await Cluster . requestLoginConfirmation ( skipConfirmation ) ;
130+
106131 if ( response !== 'Yes' ) return null ;
132+
107133 const clusterURL = await Cluster . getUrl ( ) ;
134+
108135 if ( ! clusterURL ) return null ;
136+
109137 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+
116153 if ( getUserName ) password = await TokenStore . getItem ( 'login' , username ) ;
154+
117155 if ( ! username ) return null ;
156+
118157 const passwd = await window . showInputBox ( {
119158 ignoreFocusOut : true ,
120159 password : true ,
121160 prompt : "Provide Password for basic authentication to the API server" ,
122161 value : password
123162 } ) ;
163+
124164 if ( ! passwd ) return null ;
165+
125166 return Promise . resolve ( )
126167 . then ( ( ) => Cluster . odo . execute ( Command . odoLoginWithUsernamePassword ( clusterURL , username , passwd ) ) )
127168 . then ( ( result ) => Cluster . save ( username , passwd , password , result ) )
@@ -171,4 +212,4 @@ export class Cluster extends OpenShiftItem {
171212 return Promise . reject ( result . stderr ) ;
172213 }
173214 }
174- }
215+ }
0 commit comments