-
Notifications
You must be signed in to change notification settings - Fork 2
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
mbfrahry
wants to merge
1
commit into
main
Choose a base branch
from
f-express-route-circuit-cleaner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
dalek/cleaners/subscriptions_delete_express_route_circuit.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 | ||
} |
82 changes: 82 additions & 0 deletions
82
...resource-manager/network/2023-05-01/expressroutecircuitauthorizations/README.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
...azure-sdk/resource-manager/network/2023-05-01/expressroutecircuitauthorizations/client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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