@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
23
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
23
24
authz "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
24
25
compute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
25
26
network "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
@@ -138,7 +139,7 @@ func (g *resourceGetter) toResourceGroupResource(rg *azureresources.ResourceGrou
138
139
return & resources.Resource {
139
140
Obj : rg ,
140
141
Type : typeResourceGroup ,
141
- ID : * rg .Name ,
142
+ ID : * rg .ID ,
142
143
Name : * rg .Name ,
143
144
Deleter : g .deleteResourceGroup ,
144
145
Shared : g .clusterInfo .AzureResourceGroupShared ,
@@ -166,7 +167,7 @@ func (g *resourceGetter) listVirtualNetworksAndSubnets(ctx context.Context) ([]*
166
167
}
167
168
rs = append (rs , r )
168
169
// Add all subnets belonging to the virtual network.
169
- subnets , err := g .listSubnets (ctx , * vnet .Name )
170
+ subnets , err := g .listSubnets (ctx , * vnet .ID )
170
171
if err != nil {
171
172
return nil , err
172
173
}
@@ -177,19 +178,15 @@ func (g *resourceGetter) listVirtualNetworksAndSubnets(ctx context.Context) ([]*
177
178
178
179
func (g * resourceGetter ) toVirtualNetworkResource (vnet * network.VirtualNetwork ) (* resources.Resource , error ) {
179
180
var blocks []string
180
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
181
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
181
182
182
183
nsgs := set .New [string ]()
183
184
if vnet .Properties != nil && vnet .Properties .Subnets != nil {
184
185
for _ , sn := range vnet .Properties .Subnets {
185
186
if sn .Properties == nil || sn .Properties .NetworkSecurityGroup == nil || sn .Properties .NetworkSecurityGroup .ID == nil {
186
187
continue
187
188
}
188
- nsgID , err := azure .ParseNetworkSecurityGroupID (* sn .Properties .NetworkSecurityGroup .ID )
189
- if err != nil {
190
- return nil , fmt .Errorf ("parsing network security group ID: %s" , err )
191
- }
192
- nsgs .Insert (nsgID .NetworkSecurityGroupName )
189
+ nsgs .Insert (* sn .Properties .NetworkSecurityGroup .ID )
193
190
}
194
191
}
195
192
for nsg := range nsgs {
@@ -199,7 +196,7 @@ func (g *resourceGetter) toVirtualNetworkResource(vnet *network.VirtualNetwork)
199
196
return & resources.Resource {
200
197
Obj : vnet ,
201
198
Type : typeVirtualNetwork ,
202
- ID : * vnet .Name ,
199
+ ID : * vnet .ID ,
203
200
Name : * vnet .Name ,
204
201
Deleter : g .deleteVirtualNetwork ,
205
202
Blocks : blocks ,
@@ -211,35 +208,44 @@ func (g *resourceGetter) deleteVirtualNetwork(_ fi.Cloud, r *resources.Resource)
211
208
return g .cloud .VirtualNetwork ().Delete (context .TODO (), g .resourceGroupName (), r .Name )
212
209
}
213
210
214
- func (g * resourceGetter ) listSubnets (ctx context.Context , vnetName string ) ([]* resources.Resource , error ) {
215
- subnets , err := g .cloud .Subnet ().List (ctx , g .resourceGroupName (), vnetName )
211
+ func (g * resourceGetter ) listSubnets (ctx context.Context , vnetID string ) ([]* resources.Resource , error ) {
212
+ vnet , err := arm .ParseResourceID (vnetID )
213
+ if err != nil {
214
+ return nil , err
215
+ }
216
+ subnets , err := g .cloud .Subnet ().List (ctx , g .resourceGroupName (), vnet .Name )
216
217
if err != nil {
217
218
return nil , err
218
219
}
219
220
220
221
var rs []* resources.Resource
221
222
for _ , sn := range subnets {
222
- rs = append (rs , g .toSubnetResource (sn , vnetName ))
223
+ rs = append (rs , g .toSubnetResource (sn , vnetID ))
223
224
}
224
225
return rs , nil
225
226
}
226
227
227
- func (g * resourceGetter ) toSubnetResource (subnet * network.Subnet , vnetName string ) * resources.Resource {
228
+ func (g * resourceGetter ) toSubnetResource (subnet * network.Subnet , vnetID string ) * resources.Resource {
228
229
var blocks []string
229
- blocks = append (blocks , toKey (typeVirtualNetwork , vnetName ))
230
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
230
+ blocks = append (blocks , toKey (typeVirtualNetwork , vnetID ))
231
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
231
232
232
233
if subnet .Properties != nil && subnet .Properties .NatGateway != nil && subnet .Properties .NatGateway .ID != nil {
233
234
blocks = append (blocks , toKey (typeNatGateway , * subnet .Properties .NatGateway .ID ))
234
235
}
235
236
237
+ vnet , err := arm .ParseResourceID (vnetID )
238
+ if err != nil {
239
+ return nil
240
+ }
241
+
236
242
return & resources.Resource {
237
243
Obj : subnet ,
238
244
Type : typeSubnet ,
239
- ID : * subnet .Name ,
245
+ ID : * subnet .ID ,
240
246
Name : * subnet .Name ,
241
247
Deleter : func (_ fi.Cloud , r * resources.Resource ) error {
242
- return g .deleteSubnet (vnetName , r )
248
+ return g .deleteSubnet (vnet . Name , r )
243
249
},
244
250
Blocks : blocks ,
245
251
Shared : g .clusterInfo .AzureNetworkShared ,
@@ -269,27 +275,19 @@ func (g *resourceGetter) listNetworkSecurityGroups(ctx context.Context) ([]*reso
269
275
270
276
func (g * resourceGetter ) toNetworkSecurityGroupResource (NetworkSecurityGroup * network.SecurityGroup ) (* resources.Resource , error ) {
271
277
var blocks []string
272
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
278
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
273
279
274
280
asgs := set .New [string ]()
275
281
if NetworkSecurityGroup .Properties .SecurityRules != nil {
276
282
for _ , nsr := range NetworkSecurityGroup .Properties .SecurityRules {
277
283
if nsr .Properties .SourceApplicationSecurityGroups != nil {
278
284
for _ , sasg := range nsr .Properties .SourceApplicationSecurityGroups {
279
- asgID , err := azure .ParseApplicationSecurityGroupID (* sasg .ID )
280
- if err != nil {
281
- return nil , fmt .Errorf ("parsing application security group ID: %w" , err )
282
- }
283
- asgs .Insert (asgID .ApplicationSecurityGroupName )
285
+ asgs .Insert (* sasg .ID )
284
286
}
285
287
}
286
288
if nsr .Properties .DestinationApplicationSecurityGroups != nil {
287
289
for _ , dasg := range nsr .Properties .DestinationApplicationSecurityGroups {
288
- asgID , err := azure .ParseApplicationSecurityGroupID (* dasg .ID )
289
- if err != nil {
290
- return nil , fmt .Errorf ("parsing application security group ID: %w" , err )
291
- }
292
- asgs .Insert (asgID .ApplicationSecurityGroupName )
290
+ asgs .Insert (* dasg .ID )
293
291
}
294
292
}
295
293
}
@@ -301,7 +299,7 @@ func (g *resourceGetter) toNetworkSecurityGroupResource(NetworkSecurityGroup *ne
301
299
return & resources.Resource {
302
300
Obj : NetworkSecurityGroup ,
303
301
Type : typeNetworkSecurityGroup ,
304
- ID : * NetworkSecurityGroup .Name ,
302
+ ID : * NetworkSecurityGroup .ID ,
305
303
Name : * NetworkSecurityGroup .Name ,
306
304
Deleter : func (_ fi.Cloud , r * resources.Resource ) error {
307
305
return g .deleteNetworkSecurityGroup (r )
@@ -331,13 +329,13 @@ func (g *resourceGetter) toApplicationSecurityGroupResource(ApplicationSecurityG
331
329
return & resources.Resource {
332
330
Obj : ApplicationSecurityGroup ,
333
331
Type : typeApplicationSecurityGroup ,
334
- ID : * ApplicationSecurityGroup .Name ,
332
+ ID : * ApplicationSecurityGroup .ID ,
335
333
Name : * ApplicationSecurityGroup .Name ,
336
334
Deleter : func (_ fi.Cloud , r * resources.Resource ) error {
337
335
return g .deleteApplicationSecurityGroup (r )
338
336
},
339
337
Blocks : []string {
340
- toKey (typeResourceGroup , g .resourceGroupName ()),
338
+ toKey (typeResourceGroup , g .resourceGroupID ()),
341
339
},
342
340
}
343
341
}
@@ -366,10 +364,10 @@ func (g *resourceGetter) toRouteTableResource(rt *network.RouteTable) *resources
366
364
return & resources.Resource {
367
365
Obj : rt ,
368
366
Type : typeRouteTable ,
369
- ID : * rt .Name ,
367
+ ID : * rt .ID ,
370
368
Name : * rt .Name ,
371
369
Deleter : g .deleteRouteTable ,
372
- Blocks : []string {toKey (typeResourceGroup , g .resourceGroupName ())},
370
+ Blocks : []string {toKey (typeResourceGroup , g .resourceGroupID ())},
373
371
Shared : g .clusterInfo .AzureRouteTableShared ,
374
372
}
375
373
}
@@ -423,36 +421,32 @@ func (g *resourceGetter) listVMScaleSetsAndRoleAssignments(ctx context.Context)
423
421
func (g * resourceGetter ) toVMScaleSetResource (vmss * compute.VirtualMachineScaleSet , vms []* compute.VirtualMachineScaleSetVM ) (* resources.Resource , error ) {
424
422
// Add resources whose deletion is blocked by this VMSS.
425
423
var blocks []string
426
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
424
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
427
425
428
426
vnets := set .New [string ]()
429
427
subnets := set .New [string ]()
430
428
asgs := set .New [string ]()
431
429
lbs := set .New [string ]()
432
430
for _ , iface := range vmss .Properties .VirtualMachineProfile .NetworkProfile .NetworkInterfaceConfigurations {
433
431
for _ , ip := range iface .Properties .IPConfigurations {
434
- subnetID , err := azure . ParseSubnetID (* ip .Properties .Subnet .ID )
432
+ subnet , err := arm . ParseResourceID (* ip .Properties .Subnet .ID )
435
433
if err != nil {
436
- return nil , fmt . Errorf ( "parsing subnet ID: %w" , err )
434
+ return nil , err
437
435
}
438
- vnets .Insert (subnetID . VirtualNetworkName )
439
- subnets .Insert (subnetID . SubnetName )
436
+ vnets .Insert (subnet . Parent . String () )
437
+ subnets .Insert (subnet . String () )
440
438
if ip .Properties .ApplicationSecurityGroups != nil {
441
439
for _ , asg := range ip .Properties .ApplicationSecurityGroups {
442
- asgID , err := azure .ParseApplicationSecurityGroupID (* asg .ID )
443
- if err != nil {
444
- return nil , fmt .Errorf ("parsing application security group ID: %w" , err )
445
- }
446
- asgs .Insert (asgID .ApplicationSecurityGroupName )
440
+ asgs .Insert (* asg .ID )
447
441
}
448
442
}
449
443
if ip .Properties .LoadBalancerBackendAddressPools != nil {
450
- for _ , lb := range ip .Properties .LoadBalancerBackendAddressPools {
451
- lbID , err := azure . ParseLoadBalancerID ( * lb .ID )
444
+ for _ , lbbap := range ip .Properties .LoadBalancerBackendAddressPools {
445
+ pool , err := arm . ParseResourceID ( * lbbap .ID )
452
446
if err != nil {
453
- return nil , fmt . Errorf ( "parsing load balancer ID: %w" , err )
447
+ return nil , err
454
448
}
455
- lbs .Insert (lbID . LoadBalancerName )
449
+ lbs .Insert (pool . Parent . String () )
456
450
}
457
451
}
458
452
}
@@ -470,18 +464,10 @@ func (g *resourceGetter) toVMScaleSetResource(vmss *compute.VirtualMachineScaleS
470
464
blocks = append (blocks , toKey (typeLoadBalancer , lb ))
471
465
}
472
466
473
- for _ , vm := range vms {
474
- if disks := vm .Properties .StorageProfile .DataDisks ; disks != nil {
475
- for _ , d := range disks {
476
- blocks = append (blocks , toKey (typeDisk , * d .Name ))
477
- }
478
- }
479
- }
480
-
481
467
return & resources.Resource {
482
468
Obj : vmss ,
483
469
Type : typeVMScaleSet ,
484
- ID : * vmss .Name ,
470
+ ID : * vmss .ID ,
485
471
Name : * vmss .Name ,
486
472
Deleter : g .deleteVMScaleSet ,
487
473
Blocks : blocks ,
@@ -509,13 +495,22 @@ func (g *resourceGetter) listDisks(ctx context.Context) ([]*resources.Resource,
509
495
}
510
496
511
497
func (g * resourceGetter ) toDiskResource (disk * compute.Disk ) * resources.Resource {
498
+ var blocked []string
499
+ if disk .ManagedBy != nil {
500
+ vm , err := arm .ParseResourceID (* disk .ManagedBy )
501
+ if err == nil {
502
+ blocked = append (blocked , toKey (typeVMScaleSet , vm .Parent .String ()))
503
+ }
504
+ }
505
+
512
506
return & resources.Resource {
513
507
Obj : disk ,
514
508
Type : typeDisk ,
515
- ID : * disk .Name ,
509
+ ID : * disk .ID ,
516
510
Name : * disk .Name ,
517
511
Deleter : g .deleteDisk ,
518
- Blocks : []string {toKey (typeResourceGroup , g .resourceGroupName ())},
512
+ Blocks : []string {toKey (typeResourceGroup , g .resourceGroupID ())},
513
+ Blocked : blocked ,
519
514
}
520
515
}
521
516
@@ -548,12 +543,12 @@ func (g *resourceGetter) toRoleAssignmentResource(ra *authz.RoleAssignment, vmss
548
543
return & resources.Resource {
549
544
Obj : ra ,
550
545
Type : typeRoleAssignment ,
551
- ID : * ra .Name ,
546
+ ID : * ra .ID ,
552
547
Name : * ra .Name ,
553
548
Deleter : g .deleteRoleAssignment ,
554
549
Blocks : []string {
555
- toKey (typeResourceGroup , g .resourceGroupName ()),
556
- toKey (typeVMScaleSet , * vmss .Name ),
550
+ toKey (typeResourceGroup , g .resourceGroupID ()),
551
+ toKey (typeVMScaleSet , * vmss .ID ),
557
552
},
558
553
}
559
554
}
@@ -588,19 +583,15 @@ func (g *resourceGetter) listLoadBalancers(ctx context.Context) ([]*resources.Re
588
583
589
584
func (g * resourceGetter ) toLoadBalancerResource (loadBalancer * network.LoadBalancer ) (* resources.Resource , error ) {
590
585
var blocks []string
591
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
586
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
592
587
593
588
pips := set .New [string ]()
594
589
if loadBalancer .Properties != nil {
595
590
for _ , fip := range loadBalancer .Properties .FrontendIPConfigurations {
596
591
if fip .Properties == nil || fip .Properties .PublicIPAddress == nil {
597
592
continue
598
593
}
599
- pipID , err := azure .ParsePublicIPAddressID (* fip .Properties .PublicIPAddress .ID )
600
- if err != nil {
601
- return nil , fmt .Errorf ("parsing public IP address ID: %s" , err )
602
- }
603
- pips .Insert (pipID .PublicIPAddressName )
594
+ pips .Insert (* fip .Properties .PublicIPAddress .ID )
604
595
}
605
596
}
606
597
for pip := range pips {
@@ -610,7 +601,7 @@ func (g *resourceGetter) toLoadBalancerResource(loadBalancer *network.LoadBalanc
610
601
return & resources.Resource {
611
602
Obj : loadBalancer ,
612
603
Type : typeLoadBalancer ,
613
- ID : * loadBalancer .Name ,
604
+ ID : * loadBalancer .ID ,
614
605
Name : * loadBalancer .Name ,
615
606
Deleter : g .deleteLoadBalancer ,
616
607
Blocks : blocks ,
@@ -641,10 +632,10 @@ func (g *resourceGetter) toPublicIPAddressResource(publicIPAddress *network.Publ
641
632
return & resources.Resource {
642
633
Obj : publicIPAddress ,
643
634
Type : typePublicIPAddress ,
644
- ID : * publicIPAddress .Name ,
635
+ ID : * publicIPAddress .ID ,
645
636
Name : * publicIPAddress .Name ,
646
637
Deleter : g .deletePublicIPAddress ,
647
- Blocks : []string {toKey (typeResourceGroup , g .resourceGroupName ())},
638
+ Blocks : []string {toKey (typeResourceGroup , g .resourceGroupID ())},
648
639
}
649
640
}
650
641
@@ -674,16 +665,12 @@ func (g *resourceGetter) listNatGateways(ctx context.Context) ([]*resources.Reso
674
665
675
666
func (g * resourceGetter ) toNatGatewayResource (natGateway * network.NatGateway ) (* resources.Resource , error ) {
676
667
var blocks []string
677
- blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupName ()))
668
+ blocks = append (blocks , toKey (typeResourceGroup , g .resourceGroupID ()))
678
669
679
670
pips := set .New [string ]()
680
671
if natGateway .Properties != nil && natGateway .Properties .PublicIPAddresses != nil {
681
672
for _ , pip := range natGateway .Properties .PublicIPAddresses {
682
- pipID , err := azure .ParsePublicIPAddressID (* pip .ID )
683
- if err != nil {
684
- return nil , fmt .Errorf ("parsing public IP address ID: %s" , err )
685
- }
686
- pips .Insert (pipID .PublicIPAddressName )
673
+ pips .Insert (* pip .ID )
687
674
}
688
675
}
689
676
for pip := range pips {
0 commit comments