4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import { Identity } from "@azure/arm-msi" ;
7
- import { uiUtils } from "@microsoft/vscode-azext-azureutils" ;
7
+ import { createManagedServiceIdentityClient } from "@microsoft/vscode-azext-azureutils" ;
8
8
import { callWithTelemetryAndErrorHandling , createContextValue , createSubscriptionContext , nonNullProp , type IActionContext } from "@microsoft/vscode-azext-utils" ;
9
9
import { type AzureResource , type AzureSubscription , type ViewPropertiesModel } from "@microsoft/vscode-azureresources-api" ;
10
- import { ThemeIcon , TreeItem , TreeItemCollapsibleState } from "vscode" ;
11
- import { createAzureResource } from "../api/DefaultAzureResourceProvider" ;
10
+ import { TreeItem , TreeItemCollapsibleState } from "vscode" ;
12
11
import { getAzureResourcesService } from "../services/AzureResourcesService" ;
13
- import { DefaultAzureResourceItem } from "../tree/azure/DefaultAzureResourceItem" ;
14
- import { GenericItem } from "../tree/GenericItem" ;
15
12
import { ResourceGroupsItem } from "../tree/ResourceGroupsItem" ;
16
- import { createAuthorizationManagementClient , createManagedServiceIdentityClient } from "../utils/azureClients" ;
17
13
import { getIconPath } from "../utils/azureUtils" ;
18
- import { localize } from "../utils/localize" ;
19
- import { RoleAssignmentsItem } from "./RoleAssignmentsItem" ;
20
- import { RoleDefinitionsItem } from "./RoleDefinitionsItem" ;
14
+ import { SourceResourceIdentityItem } from "./SourceResourceIdentityItem" ;
15
+ import { TargetServiceRoleAssignmentItem } from "./TargetServiceRoleAssignmentItem" ;
21
16
22
17
export class ManagedIdentityItem implements ResourceGroupsItem {
23
18
static readonly contextValue : string = 'managedIdentityItem' ;
@@ -42,60 +37,28 @@ export class ManagedIdentityItem implements ResourceGroupsItem {
42
37
return createContextValue ( values ) ;
43
38
}
44
39
45
- async getChildren ( ) : Promise < ( GenericItem | RoleDefinitionsItem | RoleAssignmentsItem ) [ ] > {
40
+ async getChildren < TreeElementBase > ( ) : Promise < TreeElementBase [ ] > {
46
41
const result = await callWithTelemetryAndErrorHandling ( 'managedIdentityItem.getChildren' , async ( context : IActionContext ) => {
47
42
const subContext = createSubscriptionContext ( this . subscription ) ;
48
43
const msiClient = await createManagedServiceIdentityClient ( [ context , subContext ] ) ;
49
44
const msi : Identity = await msiClient . userAssignedIdentities . get ( nonNullProp ( this . resource , 'resourceGroup' ) , this . resource . name ) ;
50
45
51
46
const resources = await getAzureResourcesService ( ) . listResources ( context , this . subscription ) ;
52
- const assignedRoleAssignment = new RoleAssignmentsItem ( localize ( 'sourceResources' , 'Source resources' ) , this . subscription , msi ) ;
53
- const accessRoleAssignment = new RoleAssignmentsItem ( localize ( 'targetServices' , 'Target services' ) , this . subscription , msi ) ;
47
+ const sourceResourceItem = new SourceResourceIdentityItem ( this . subscription , msi , resources ) ;
48
+ const targetServiceItem = new TargetServiceRoleAssignmentItem ( this . subscription , msi ) ;
54
49
55
- const assignedResources = resources . filter ( ( r ) => {
56
- // verify the msi is assigned to the resource by checking if the msi id is in the userAssignedIdentities
57
- const userAssignedIdentities = r . identity ?. userAssignedIdentities ;
58
- if ( ! userAssignedIdentities ) {
59
- return false ;
60
- }
61
-
62
- if ( ! msi . id ) {
63
- return false ;
64
- }
65
-
66
- return userAssignedIdentities [ msi . id ] !== undefined
67
- } ) . map ( ( r ) => {
68
- const azureResoure = createAzureResource ( this . subscription , r ) ;
69
- return new DefaultAzureResourceItem ( azureResoure ) ;
70
- } ) ;
71
-
72
- const authClient = await createAuthorizationManagementClient ( [ context , subContext ] ) ;
73
- const roleAssignment = await uiUtils . listAllIterator ( authClient . roleAssignments . listForSubscription ( ) ) ;
74
- // filter the role assignments to only show the ones that are assigned to the msi
75
- const filteredBySub = roleAssignment . filter ( ( ra ) => ra . principalId === msi . principalId ) ;
76
-
77
- const targetResources = await accessRoleAssignment . getRoleDefinitionsItems ( context , filteredBySub ) ;
78
50
const children = [ ] ;
79
51
80
- if ( assignedResources . length > 0 ) {
52
+ if ( sourceResourceItem . getChildren ( ) . length > 0 ) {
81
53
// if there weren't any assigned resources, don't show that section
82
- assignedRoleAssignment . addChildren ( assignedResources ) ;
83
- children . push ( assignedRoleAssignment ) ;
54
+ children . push ( sourceResourceItem ) ;
84
55
}
85
56
86
- accessRoleAssignment . addChildren ( targetResources ) ;
87
- children . push ( accessRoleAssignment ) ;
88
- accessRoleAssignment . addChild ( new GenericItem ( localize ( 'showResources' , 'Show resources from other subscriptions...' ) ,
89
- {
90
- id : accessRoleAssignment . id + '/showResourcesFromOtherSubscriptions' ,
91
- iconPath : new ThemeIcon ( 'sync' ) ,
92
- commandId : 'azureResources.loadAllSubscriptionRoleAssignments' ,
93
- commandArgs : [ accessRoleAssignment ]
94
- } ) )
57
+ children . push ( targetServiceItem ) ;
95
58
return children ;
96
59
} ) ;
97
60
98
- return result ?? [ ] ;
61
+ return result as TreeElementBase [ ] ?? [ ] ;
99
62
}
100
63
101
64
getTreeItem ( ) : TreeItem {
0 commit comments