Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Express Route Circuit Subscription Cleaner #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 70 additions & 34 deletions clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/netapp/2022-05-01/netappaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/netapp/2022-05-01/volumes"
"github.com/hashicorp/go-azure-sdk/resource-manager/netapp/2022-05-01/volumesreplication"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitauthorizations"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitconnections"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitpeerings"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuits"
"github.com/hashicorp/go-azure-sdk/resource-manager/notificationhubs/2017-04-01/namespaces"
paloAltoNetworks "github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29"
resourceGraph "github.com/hashicorp/go-azure-sdk/resource-manager/resourcegraph/2022-10-01/resources"
Expand Down Expand Up @@ -42,23 +46,27 @@ type MicrosoftGraphClient struct {
}

type ResourceManagerClient struct {
DataProtection *dataProtection.Client
LocksClient *managementlocks.ManagementLocksClient
MachineLearningWorkspacesClient *workspaces.WorkspacesClient
ManagedHSMsClient *managedhsms.ManagedHsmsClient
ManagementClient *managementgroups.ManagementGroupsClient
NetAppAccountClient *netappaccounts.NetAppAccountsClient
NetAppCapacityPoolClient *capacitypools.CapacityPoolsClient
NetAppVolumeClient *volumes.VolumesClient
NetAppVolumeReplicationClient *volumesreplication.VolumesReplicationClient
NotificationHubNamespaceClient *namespaces.NamespacesClient
PaloAlto *paloAltoNetworks.Client
ResourceGraphClient *resourceGraph.ResourcesClient
ResourcesGroupsClient *resourcegroups.ResourceGroupsClient
ServiceBus *serviceBus.Client
StorageSyncClient *storagesyncservicesresource.StorageSyncServicesResourceClient
StorageSyncGroupClient *syncgroupresource.SyncGroupResourceClient
StorageSyncCloudEndpointClient *cloudendpointresource.CloudEndpointResourceClient
DataProtection *dataProtection.Client
ExpressRouteCircuitsClient *expressroutecircuits.ExpressRouteCircuitsClient
ExpressRouteCircuitAuthorizationsClient *expressroutecircuitauthorizations.ExpressRouteCircuitAuthorizationsClient
ExpressRouteCircuitConnectionsClient *expressroutecircuitconnections.ExpressRouteCircuitConnectionsClient
ExpressRouteCircuitPeeringsClient *expressroutecircuitpeerings.ExpressRouteCircuitPeeringsClient
LocksClient *managementlocks.ManagementLocksClient
MachineLearningWorkspacesClient *workspaces.WorkspacesClient
ManagedHSMsClient *managedhsms.ManagedHsmsClient
ManagementClient *managementgroups.ManagementGroupsClient
NetAppAccountClient *netappaccounts.NetAppAccountsClient
NetAppCapacityPoolClient *capacitypools.CapacityPoolsClient
NetAppVolumeClient *volumes.VolumesClient
NetAppVolumeReplicationClient *volumesreplication.VolumesReplicationClient
NotificationHubNamespaceClient *namespaces.NamespacesClient
PaloAlto *paloAltoNetworks.Client
ResourceGraphClient *resourceGraph.ResourcesClient
ResourcesGroupsClient *resourcegroups.ResourceGroupsClient
ServiceBus *serviceBus.Client
StorageSyncClient *storagesyncservicesresource.StorageSyncServicesResourceClient
StorageSyncGroupClient *syncgroupresource.SyncGroupResourceClient
StorageSyncCloudEndpointClient *cloudendpointresource.CloudEndpointResourceClient
}

type Credentials struct {
Expand Down Expand Up @@ -167,6 +175,30 @@ func buildResourceManagerClient(ctx context.Context, creds auth.Credentials, env
c.Authorizer = resourceManagerAuthorizer
})

expressRouteCircuitsClient, err := expressroutecircuits.NewExpressRouteCircuitsClientWithBaseURI(environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Express Route Circuits client: %+v", err)
}
expressRouteCircuitsClient.Client.Authorizer = resourceManagerAuthorizer

expressRouteCircuitAuthorizationsClient, err := expressroutecircuitauthorizations.NewExpressRouteCircuitAuthorizationsClientWithBaseURI(environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Express Route Circuit Authorizations client: %+v", err)
}
expressRouteCircuitAuthorizationsClient.Client.Authorizer = resourceManagerAuthorizer

expressRouteCircuitConnectionsClient, err := expressroutecircuitconnections.NewExpressRouteCircuitConnectionsClientWithBaseURI(environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Express Route Circuit Connections client: %+v", err)
}
expressRouteCircuitConnectionsClient.Client.Authorizer = resourceManagerAuthorizer

expressRouteCircuitPeeringsClient, err := expressroutecircuitpeerings.NewExpressRouteCircuitPeeringsClientWithBaseURI(environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Express Route Circuit Peerings client: %+v", err)
}
expressRouteCircuitPeeringsClient.Client.Authorizer = resourceManagerAuthorizer

locksClient, err := managementlocks.NewManagementLocksClientWithBaseURI(environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ManagementLocks client: %+v", err)
Expand Down Expand Up @@ -263,22 +295,26 @@ func buildResourceManagerClient(ctx context.Context, creds auth.Credentials, env
storageSyncCloudEndpointClient.Client.Authorizer = resourceManagerAuthorizer

return &ResourceManagerClient{
DataProtection: dataProtectionClient,
LocksClient: locksClient,
MachineLearningWorkspacesClient: workspacesClient,
ManagedHSMsClient: managedHsmsClient,
ManagementClient: managementClient,
NetAppAccountClient: netAppAccountClient,
NetAppCapacityPoolClient: netAppCapacityPoolClient,
NetAppVolumeClient: netAppVolumeClient,
NetAppVolumeReplicationClient: netAppVolumeReplicationClient,
NotificationHubNamespaceClient: notificationHubNamespacesClient,
PaloAlto: paloAltoClient,
ResourceGraphClient: resourceGraphClient,
ResourcesGroupsClient: resourcesClient,
ServiceBus: serviceBusClient,
StorageSyncClient: storageSyncClient,
StorageSyncGroupClient: storageSyncGroupClient,
StorageSyncCloudEndpointClient: storageSyncCloudEndpointClient,
DataProtection: dataProtectionClient,
ExpressRouteCircuitsClient: expressRouteCircuitsClient,
ExpressRouteCircuitAuthorizationsClient: expressRouteCircuitAuthorizationsClient,
ExpressRouteCircuitConnectionsClient: expressRouteCircuitConnectionsClient,
ExpressRouteCircuitPeeringsClient: expressRouteCircuitPeeringsClient,
LocksClient: locksClient,
MachineLearningWorkspacesClient: workspacesClient,
ManagedHSMsClient: managedHsmsClient,
ManagementClient: managementClient,
NetAppAccountClient: netAppAccountClient,
NetAppCapacityPoolClient: netAppCapacityPoolClient,
NetAppVolumeClient: netAppVolumeClient,
NetAppVolumeReplicationClient: netAppVolumeReplicationClient,
NotificationHubNamespaceClient: notificationHubNamespacesClient,
PaloAlto: paloAltoClient,
ResourceGraphClient: resourceGraphClient,
ResourcesGroupsClient: resourcesClient,
ServiceBus: serviceBusClient,
StorageSyncClient: storageSyncClient,
StorageSyncGroupClient: storageSyncGroupClient,
StorageSyncCloudEndpointClient: storageSyncCloudEndpointClient,
}, nil
}
1 change: 1 addition & 0 deletions dalek/cleaners/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var SubscriptionCleaners = []SubscriptionCleaner{
deleteExpressRouteCircuitsSubscriptionCleaner{},
deleteNetAppSubscriptionCleaner{},
deleteStorageSyncSubscriptionCleaner{},
deleteResourceGroupsInSubscriptionCleaner{},
Expand Down
142 changes: 142 additions & 0 deletions dalek/cleaners/subscriptions_delete_express_route_circuit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cleaners

import (
"context"
"fmt"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitauthorizations"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitconnections"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitpeerings"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-05-01/expressroutecircuits"
"github.com/tombuildsstuff/azurerm-dalek/clients"
"github.com/tombuildsstuff/azurerm-dalek/dalek/options"
)

type deleteExpressRouteCircuitsSubscriptionCleaner struct{}

var _ SubscriptionCleaner = deleteExpressRouteCircuitsSubscriptionCleaner{}

func (p deleteExpressRouteCircuitsSubscriptionCleaner) Name() string {
return "Removing Express Route Circuit"
}

func (p deleteExpressRouteCircuitsSubscriptionCleaner) Cleanup(ctx context.Context, subscriptionId commonids.SubscriptionId, client *clients.AzureClient, opts options.Options) error {
expressRouteCircuitsClient := client.ResourceManager.ExpressRouteCircuitsClient
expressRouteCircuitAuthorizationsClient := client.ResourceManager.ExpressRouteCircuitAuthorizationsClient
expressRouteCircuitConnectionsClient := client.ResourceManager.ExpressRouteCircuitConnectionsClient
expressRouteCircuitPeeringsClient := client.ResourceManager.ExpressRouteCircuitPeeringsClient

expressRouteCircuits, err := expressRouteCircuitsClient.ListAllComplete(ctx, subscriptionId)
if err != nil {
return fmt.Errorf("listing Express Route Circuits for %s: %+v", subscriptionId, err)
}

for _, expressRouteCircuit := range expressRouteCircuits.Items {
if expressRouteCircuit.Id == nil {
continue
}

expressRouteCircuitIdForAuthorizations, err := expressroutecircuitauthorizations.ParseExpressRouteCircuitID(*expressRouteCircuit.Id)
if err != nil {
return err
}

authorizations, err := expressRouteCircuitAuthorizationsClient.ListComplete(ctx, *expressRouteCircuitIdForAuthorizations)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this'll need to account for the resource group prefix too - see #30

if err != nil {
return fmt.Errorf("listing Express Route Circuit Authorizations for %s: %+v", expressRouteCircuitIdForAuthorizations, err)
}

for _, authorization := range authorizations.Items {
if authorization.Id == nil {
continue
}

authorizationId, err := expressroutecircuitauthorizations.ParseAuthorizationID(*authorization.Id)
if err != nil {
return err
}

if !opts.ActuallyDelete {
log.Printf("[DEBUG] Would have deleted %s..", authorizationId)
continue
}

if err = expressRouteCircuitAuthorizationsClient.DeleteThenPoll(ctx, *authorizationId); err != nil {
log.Printf("[DEBUG] Unable to delete %s: %+v", authorizationId, err)
}
}

expressRouteCircuitIdForPeerings, err := expressroutecircuitpeerings.ParseExpressRouteCircuitID(*expressRouteCircuit.Id)
if err != nil {
return err
}

peerings, err := expressRouteCircuitPeeringsClient.ListComplete(ctx, *expressRouteCircuitIdForPeerings)
if err != nil {
return fmt.Errorf("listing Express Route Circuit Peerings for %s: %+v", expressRouteCircuitIdForPeerings, err)
}

for _, peering := range peerings.Items {
if peering.Id == nil {
continue
}

peeringId, err := commonids.ParseExpressRouteCircuitPeeringID(*peering.Id)
if err != nil {
return err
}

connections, err := expressRouteCircuitConnectionsClient.ListComplete(ctx, *peeringId)
if err != nil {
return fmt.Errorf("listing express route circuit connections for %s: %+v", peeringId, err)
}

for _, connection := range connections.Items {
if connection.Id == nil {
continue
}

connectionid, err := expressroutecircuitconnections.ParsePeeringConnectionID(*connection.Id)
if err != nil {
return err
}

if !opts.ActuallyDelete {
log.Printf("[DEBUG] Would have deleted %s..", connectionid)
continue
}

if err = expressRouteCircuitConnectionsClient.DeleteThenPoll(ctx, *connectionid); err != nil {
log.Printf("[DEBUG] Unable to delete %s: %+v", connectionid, err)
}
}

if !opts.ActuallyDelete {
log.Printf("[DEBUG] Would have deleted %s..", peeringId)
continue
}

if err = expressRouteCircuitPeeringsClient.DeleteThenPoll(ctx, *peeringId); err != nil {
log.Printf("[DEBUG] Unable to delete %s: %+v", peeringId, err)
}
}

expressRouteCircuitId, err := expressroutecircuits.ParseExpressRouteCircuitID(*expressRouteCircuit.Id)
if err != nil {
return err
}

if !opts.ActuallyDelete {
log.Printf("[DEBUG] Would have deleted %s..", expressRouteCircuitId)
continue
}

if err = expressRouteCircuitsClient.DeleteThenPoll(ctx, *expressRouteCircuitId); err != nil {
log.Printf("[DEBUG] Unable to delete %s: %+v", expressRouteCircuitId, err)
}
}

return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading