Skip to content

Commit

Permalink
upgrade to [email protected], change how regions work
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Mar 26, 2024
1 parent 2a487d6 commit 40fa2e0
Show file tree
Hide file tree
Showing 39 changed files with 165 additions and 137 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/recoveryservices/armrecoveryservicesbackup v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/security/armsecurity v0.11.0
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/ekristen/libnuke v0.6.0
github.com/ekristen/libnuke v0.12.0
github.com/gotidy/ptr v1.4.0
github.com/hashicorp/go-azure-helpers v0.66.1
github.com/hashicorp/go-azure-sdk v0.20240125.1100331
github.com/manicminer/hamilton v0.61.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.3.0
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/ekristen/libnuke v0.6.0 h1:VsNkdrprFfbFSsJnwQEynzww7JWR/icKox/sso7ucT0=
github.com/ekristen/libnuke v0.6.0/go.mod h1:WhYx7LDAkvkXwwfhWCASRn7fbifF8kfyhNsUj5zCCVs=
github.com/ekristen/libnuke v0.12.0 h1:Dsk+ckT9sh9QZTLq5m8kOA1KFJGJxSv0TLnfe3YeL1o=
github.com/ekristen/libnuke v0.12.0/go.mod h1:sBdA04l9IMMejQw5gO9k6o/a0GffSYhgZYaUSdRjIac=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -282,6 +284,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
15 changes: 8 additions & 7 deletions pkg/commands/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package list

import (
"fmt"
"github.com/ekristen/azure-nuke/pkg/nuke"
"sort"

"github.com/fatih/color"
"github.com/urfave/cli/v2"
"sort"

"github.com/ekristen/libnuke/pkg/registry"

"github.com/ekristen/azure-nuke/pkg/commands/global"
"github.com/ekristen/azure-nuke/pkg/common"

"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/azure-nuke/pkg/nuke"

_ "github.com/ekristen/azure-nuke/resources"
)

func execute(c *cli.Context) error {
ls := resource.GetNames()
ls := registry.GetNames()

sort.Strings(ls)

for _, name := range ls {
reg := resource.GetRegistration(name)
reg := registry.GetRegistration(name)

if reg.AlternativeResource != "" {
color.New(color.Bold).Printf("%-55s\n", name)
Expand All @@ -34,7 +35,7 @@ func execute(c *cli.Context) error {
c = color.FgHiGreen
} else if reg.Scope == nuke.Subscription {
c = color.FgHiBlue
} else if reg.Scope == nuke.ResourceGroup {
} else if reg.Scope == nuke.Subscription {
c = color.FgHiMagenta
} else {

Expand Down
76 changes: 23 additions & 53 deletions pkg/commands/nuke/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ package nuke
import (
"context"
"fmt"
libscanner "github.com/ekristen/libnuke/pkg/scanner"
"log"
"slices"
"strings"
"time"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

libconfig "github.com/ekristen/libnuke/pkg/config"
libnuke "github.com/ekristen/libnuke/pkg/nuke"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/azure-nuke/pkg/azure"
"github.com/ekristen/azure-nuke/pkg/commands/global"
"github.com/ekristen/azure-nuke/pkg/common"
"github.com/ekristen/azure-nuke/pkg/config"
"github.com/ekristen/azure-nuke/pkg/nuke"
libconfig "github.com/ekristen/libnuke/pkg/config"
libnuke "github.com/ekristen/libnuke/pkg/nuke"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"log"
"slices"
"time"
)

type log2LogrusWriter struct {
Expand Down Expand Up @@ -65,7 +70,7 @@ func execute(c *cli.Context) error {

parsedConfig, err := config.New(libconfig.Options{
Path: c.Path("config"),
Deprecations: resource.GetDeprecatedResourceTypeMapping(),
Deprecations: registry.GetDeprecatedResourceTypeMapping(),
})
if err != nil {
return err
Expand All @@ -85,10 +90,11 @@ func execute(c *cli.Context) error {
n := libnuke.New(params, filters, parsedConfig.Settings)

n.SetRunSleep(5 * time.Second)
n.SetLogger(logrus.WithField("component", "nuke"))

tenantConfig := parsedConfig.Accounts[c.String("tenant-id")]
tenantResourceTypes := types.ResolveResourceTypes(
resource.GetNamesForScope(nuke.Tenant),
registry.GetNamesForScope(nuke.Tenant),
[]types.Collection{
n.Parameters.Includes,
parsedConfig.ResourceTypes.GetIncludes(),
Expand All @@ -104,23 +110,7 @@ func execute(c *cli.Context) error {
)

subResourceTypes := types.ResolveResourceTypes(
resource.GetNamesForScope(nuke.Subscription),
[]types.Collection{
n.Parameters.Includes,
parsedConfig.ResourceTypes.GetIncludes(),
tenantConfig.ResourceTypes.GetIncludes(),
},
[]types.Collection{
n.Parameters.Excludes,
parsedConfig.ResourceTypes.Excludes,
tenantConfig.ResourceTypes.Excludes,
},
nil,
nil,
)

rgResourceTypes := types.ResolveResourceTypes(
resource.GetNamesForScope(nuke.ResourceGroup),
registry.GetNamesForScope(nuke.Subscription),
[]types.Collection{
n.Parameters.Includes,
parsedConfig.ResourceTypes.GetIncludes(),
Expand All @@ -136,7 +126,7 @@ func execute(c *cli.Context) error {
)

if slices.Contains(parsedConfig.Regions, "global") {
if err := n.RegisterScanner(nuke.Tenant, libnuke.NewScanner("tenant/all", tenantResourceTypes, &nuke.ListerOpts{
if err := n.RegisterScanner(nuke.Tenant, libscanner.New("tenant/all", tenantResourceTypes, &nuke.ListerOpts{
Authorizers: authorizers,
TenantId: tenant.ID,
})); err != nil {
Expand All @@ -147,39 +137,19 @@ func execute(c *cli.Context) error {
logrus.Debug("registering scanner for tenant subscription resources")
for _, subscriptionId := range tenant.SubscriptionIds {
logrus.Debug("registering scanner for subscription resources")
if err := n.RegisterScanner(nuke.Subscription, libnuke.NewScanner("tenant/sub", subResourceTypes, &nuke.ListerOpts{
parts := strings.Split(subscriptionId, "-")
if err := n.RegisterScanner(nuke.Subscription, libscanner.New(fmt.Sprintf("sub/%s", parts[:1][0]), subResourceTypes, &nuke.ListerOpts{
Authorizers: tenant.Authorizers,
TenantId: tenant.ID,
SubscriptionId: subscriptionId,
Locations: parsedConfig.Regions,
})); err != nil {
return err
}
}

for _, region := range parsedConfig.Regions {
if region == "global" {
continue
}

for _, subscriptionId := range tenant.SubscriptionIds {
logrus.Debug("registering scanner for subscription resources")

for i, resourceGroup := range tenant.ResourceGroups[subscriptionId] {
logrus.Debugf("registering scanner for resource group resources: rg/%s", resourceGroup)
if err := n.RegisterScanner(nuke.ResourceGroup, libnuke.NewScanner(fmt.Sprintf("%s/rg%d", region, i), rgResourceTypes, &nuke.ListerOpts{
Authorizers: tenant.Authorizers,
TenantId: tenant.ID,
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Location: region,
})); err != nil {
return err
}
}
}
}

logrus.Debug("running ...")

return n.Run(c.Context)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/nuke/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package nuke

import (
"github.com/ekristen/azure-nuke/pkg/azure"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/registry"
)

const (
Tenant resource.Scope = "tenant"
Subscription resource.Scope = "subscription"
ResourceGroup resource.Scope = "resource-group"
Tenant registry.Scope = "tenant"
Subscription registry.Scope = "subscription"
ResourceGroup registry.Scope = "resource-group"
)

type ListerOpts struct {
Authorizers *azure.Authorizers
TenantId string
SubscriptionId string
ResourceGroup string
Location string
Locations []string
}
3 changes: 2 additions & 1 deletion resources/aad-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,7 +18,7 @@ import (
const AzureAdGroupResource = "AzureADGroup"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: AzureAdGroupResource,
Scope: nuke.Tenant,
Lister: &AzureAdGroupLister{},
Expand Down
3 changes: 2 additions & 1 deletion resources/aad-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,7 +18,7 @@ import (
const AzureADUserResource = "AzureADUser"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: AzureADUserResource,
Scope: nuke.Tenant,
Lister: &AzureADUserLister{},
Expand Down
5 changes: 3 additions & 2 deletions resources/app-service-plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2021-03-01/web"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,9 +18,9 @@ import (
const AppServicePlanResource = "AppServicePlan"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: AppServicePlanResource,
Scope: nuke.ResourceGroup,
Scope: nuke.Subscription,
Lister: &AppServicePlanLister{},
})
}
Expand Down
3 changes: 2 additions & 1 deletion resources/application-certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,7 +18,7 @@ import (
const ApplicationCertificateResource = "ApplicationCertificate"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: ApplicationCertificateResource,
Scope: nuke.Tenant,
Lister: &ApplicationCertificateLister{},
Expand Down
3 changes: 2 additions & 1 deletion resources/application-federated-credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,7 +18,7 @@ import (
const ApplicationFederatedCredentialResource = "ApplicationFederatedCredential"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: ApplicationFederatedCredentialResource,
Scope: nuke.Tenant,
Lister: &ApplicationFederatedCredentialLister{},
Expand Down
3 changes: 2 additions & 1 deletion resources/application-secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -17,7 +18,7 @@ import (
const ApplicationSecretResource = "ApplicationSecret"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: ApplicationSecretResource,
Scope: nuke.Tenant,
Lister: &ApplicationSecretLister{},
Expand Down
5 changes: 3 additions & 2 deletions resources/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package resources

import (
"context"
"github.com/gotidy/ptr"

"github.com/gotidy/ptr"
"github.com/sirupsen/logrus"

"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/manicminer/hamilton/msgraph"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -18,7 +19,7 @@ import (
const ApplicationResource = "Application"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: ApplicationResource,
Scope: nuke.Tenant,
Lister: &ApplicationLister{},
Expand Down
3 changes: 2 additions & 1 deletion resources/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/consumption/2021-10-01/budgets"
"github.com/hashicorp/go-azure-sdk/sdk/environments"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -21,7 +22,7 @@ import (
const BudgetResource = "Budget"

func init() {
resource.Register(&resource.Registration{
registry.Register(&registry.Registration{
Name: BudgetResource,
Scope: nuke.Subscription,
Lister: &BudgetLister{},
Expand Down
Loading

0 comments on commit 40fa2e0

Please sign in to comment.