4
4
*/
5
5
6
6
import * as vscode from 'vscode'
7
- import { AwsContext } from '../shared/awsContext '
7
+ import { Auth , AuthNode , useIamCredentials } from '../credentials/auth '
8
8
import { getIdeProperties } from '../shared/extensionUtilities'
9
+ import { getIcon } from '../shared/icons'
9
10
import { getLogger , Logger } from '../shared/logger'
10
11
import { RegionProvider } from '../shared/regions/regionProvider'
11
12
import { RefreshableAwsTreeProvider } from '../shared/treeview/awsTreeProvider'
12
13
import { AWSCommandTreeNode } from '../shared/treeview/nodes/awsCommandTreeNode'
13
14
import { AWSTreeNodeBase } from '../shared/treeview/nodes/awsTreeNodeBase'
14
- import { makeChildrenNodes } from '../shared/treeview/utils'
15
+ import { makeChildrenNodes , TreeShim } from '../shared/treeview/utils'
15
16
import { intersection , toMap , updateInPlace } from '../shared/utilities/collectionUtils'
17
+ import { once } from '../shared/utilities/functionUtils'
16
18
import { localize } from '../shared/utilities/vsCodeUtils'
17
19
import { RegionNode } from './regionNode'
18
20
@@ -23,14 +25,6 @@ export class AwsExplorer implements vscode.TreeDataProvider<AWSTreeNodeBase>, Re
23
25
private readonly _onDidChangeTreeData : vscode . EventEmitter < AWSTreeNodeBase | undefined >
24
26
private readonly regionNodes : Map < string , RegionNode >
25
27
26
- private readonly ROOT_NODE_SIGN_IN = new AWSCommandTreeNode (
27
- undefined ,
28
- localize ( 'AWS.explorerNode.connecting.label' , 'Connecting...' ) ,
29
- 'aws.login' ,
30
- undefined ,
31
- localize ( 'AWS.explorerNode.connecting.tooltip' , 'Connecting...' )
32
- )
33
-
34
28
private readonly ROOT_NODE_ADD_REGION = new AWSCommandTreeNode (
35
29
undefined ,
36
30
localize ( 'AWS.explorerNode.addRegion' , 'Add regions to {0} Explorer...' , getIdeProperties ( ) . company ) ,
@@ -45,33 +39,19 @@ export class AwsExplorer implements vscode.TreeDataProvider<AWSTreeNodeBase>, Re
45
39
46
40
public constructor (
47
41
private readonly extContext : vscode . ExtensionContext ,
48
- private readonly awsContext : AwsContext ,
49
- private readonly regionProvider : RegionProvider
42
+ private readonly regionProvider : RegionProvider ,
43
+ private readonly auth = Auth . instance
50
44
) {
51
45
this . _onDidChangeTreeData = new vscode . EventEmitter < AWSTreeNodeBase | undefined > ( )
52
46
this . onDidChangeTreeData = this . _onDidChangeTreeData . event
53
47
this . regionNodes = new Map < string , RegionNode > ( )
54
- this . extContext . subscriptions . push (
55
- this . awsContext . onDidChangeContext ( e => {
56
- if ( ! e . accountId ) {
57
- this . ROOT_NODE_SIGN_IN . label = localize (
58
- 'AWS.explorerNode.signIn' ,
59
- 'Connect to {0}...' ,
60
- getIdeProperties ( ) . company
61
- )
62
- this . ROOT_NODE_SIGN_IN . tooltip = localize (
63
- 'AWS.explorerNode.signIn.tooltip' ,
64
- 'Click here to select credentials for the {0} Toolkit' ,
65
- getIdeProperties ( ) . company
66
- )
67
- }
68
- } )
69
- )
48
+
70
49
this . extContext . subscriptions . push (
71
50
this . regionProvider . onDidChange ( ( ) => {
72
51
this . logger . verbose ( 'Refreshing AWS Explorer due to Region Provider updates' )
73
52
this . refresh ( )
74
- } )
53
+ } ) ,
54
+ this . auth . onDidChangeActiveConnection ( ( ) => this . refresh ( ) )
75
55
)
76
56
}
77
57
@@ -120,28 +100,42 @@ export class AwsExplorer implements vscode.TreeDataProvider<AWSTreeNodeBase>, Re
120
100
this . _onDidChangeTreeData . fire ( node )
121
101
}
122
102
103
+ private readonly getAuthNode = once ( ( ) => new TreeShim ( new AuthNode ( this . auth ) ) )
123
104
private async getRootNodes ( ) : Promise < AWSTreeNodeBase [ ] > {
124
- if ( ! ( await this . awsContext . getCredentials ( ) ) ) {
125
- return [ this . ROOT_NODE_SIGN_IN ]
105
+ const conn = this . auth . activeConnection
106
+ if ( conn !== undefined && conn . type !== 'iam' ) {
107
+ // TODO: this should show up as a child node?
108
+ const selectIamNode = useIamCredentials . build ( this . auth ) . asTreeNode ( {
109
+ // label: `No IAM credentials linked to ${conn.label}`,
110
+ // iconPath: getIcon('vscode-circle-slash'),
111
+ label : 'Select IAM Credentials to View Resources' ,
112
+ iconPath : getIcon ( 'vscode-sync' ) ,
113
+ } )
114
+
115
+ return [ this . getAuthNode ( ) , new TreeShim ( selectIamNode ) ]
116
+ } else if ( conn === undefined || conn . state !== 'valid' ) {
117
+ return [ this . getAuthNode ( ) ]
126
118
}
127
119
128
120
const partitionRegions = this . regionProvider . getRegions ( )
129
121
const userVisibleRegionCodes = this . regionProvider . getExplorerRegions ( )
130
122
const regionMap = toMap ( partitionRegions , r => r . id )
131
123
132
- return await makeChildrenNodes ( {
133
- getChildNodes : async ( ) => {
134
- updateInPlace (
135
- this . regionNodes ,
136
- intersection ( regionMap . keys ( ) , userVisibleRegionCodes ) ,
137
- key => this . regionNodes . get ( key ) ! . update ( regionMap . get ( key ) ! ) ,
138
- key => new RegionNode ( regionMap . get ( key ) ! , this . regionProvider )
139
- )
124
+ updateInPlace (
125
+ this . regionNodes ,
126
+ intersection ( regionMap . keys ( ) , userVisibleRegionCodes ) ,
127
+ key => this . regionNodes . get ( key ) ! . update ( regionMap . get ( key ) ! ) ,
128
+ key => new RegionNode ( regionMap . get ( key ) ! , this . regionProvider )
129
+ )
130
+
131
+ if ( this . regionNodes . size === 0 ) {
132
+ return [ this . getAuthNode ( ) , this . ROOT_NODE_ADD_REGION ]
133
+ }
140
134
141
- return [ ... this . regionNodes . values ( ) ]
142
- } ,
143
- getNoChildrenPlaceholderNode : async ( ) => this . ROOT_NODE_ADD_REGION ,
144
- sort : ( nodeA , nodeB ) => nodeA . regionName . localeCompare ( nodeB . regionName ) ,
135
+ return await makeChildrenNodes ( {
136
+ getChildNodes : async ( ) => [ this . getAuthNode ( ) , ... this . regionNodes . values ( ) ] ,
137
+ sort : ( a , b ) =>
138
+ a instanceof TreeShim ? - 1 : b instanceof TreeShim ? 1 : a . regionName . localeCompare ( b . regionName ) ,
145
139
} )
146
140
}
147
141
}
0 commit comments