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

RG mode: Two step list, rg itself (incl. ext), then resources (incl. ext) in it #576

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
53 changes: 36 additions & 17 deletions internal/meta/meta_rg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/Azure/aztfexport/internal/resourceset"
"github.com/Azure/aztfexport/internal/tfaddr"
"github.com/Azure/aztfexport/pkg/config"
"github.com/magodo/armid"
"github.com/magodo/azlist/azlist"
)

Expand Down Expand Up @@ -89,26 +88,26 @@ func (meta *MetaResourceGroup) ListResource(ctx context.Context) (ImportList, er
}

func (meta MetaResourceGroup) queryResourceSet(ctx context.Context, rg string) (*resourceset.AzureResourceSet, error) {
var rl []resourceset.AzureResource

// Try to get the resource group (with any extension resources) first, in case it doesn't exist.
opt := azlist.Option{
Logger: meta.logger.WithGroup("azlist"),
SubscriptionId: meta.subscriptionId,
Cred: meta.azureSDKCred,
ClientOpt: meta.azureSDKClientOpt,
Parallelism: meta.parallelism,
Recursive: true,
IncludeResourceGroup: true,
ExtensionResourceTypes: extBuilder{includeRoleAssignment: meta.includeRoleAssignment}.Build(),
ARGTable: "ResourceContainers",
}
lister, err := azlist.NewLister(opt)
if err != nil {
return nil, fmt.Errorf("building azlister: %v", err)
return nil, fmt.Errorf("building azlister for listing resource group only: %v", err)
}
result, err := lister.List(ctx, fmt.Sprintf("resourceGroup =~ %q", rg))
result, err := lister.List(ctx, fmt.Sprintf("name == %q", rg))
if err != nil {
return nil, fmt.Errorf("listing resource set: %w", err)
return nil, fmt.Errorf("listing resource group only: %w", err)
}

var rl []resourceset.AzureResource
for _, res := range result.Resources {
res := resourceset.AzureResource{
Id: res.Id,
Expand All @@ -117,15 +116,35 @@ func (meta MetaResourceGroup) queryResourceSet(ctx context.Context, rg string) (
rl = append(rl, res)
}

// Especially, if there is no resource within the resource group, the azlist will return an empty list.
// In this case, we'll have to add the resource group manually.
if len(rl) == 0 {
rl = append(rl,
resourceset.AzureResource{Id: &armid.ResourceGroup{
SubscriptionId: meta.subscriptionId,
Name: meta.resourceGroup,
}},
)
// Skip the resource listing if the resource group itself doesn't exist.
if len(result.Resources) == 0 {
return &resourceset.AzureResourceSet{}, nil
}

// List the resources within the resource group.
opt = azlist.Option{
Logger: meta.logger.WithGroup("azlist"),
SubscriptionId: meta.subscriptionId,
Cred: meta.azureSDKCred,
ClientOpt: meta.azureSDKClientOpt,
Parallelism: meta.parallelism,
ExtensionResourceTypes: extBuilder{includeRoleAssignment: meta.includeRoleAssignment}.Build(),
Recursive: true,
}
lister, err = azlist.NewLister(opt)
if err != nil {
return nil, fmt.Errorf("building azlister for listing resource group: %v", err)
}
result, err = lister.List(ctx, fmt.Sprintf("resourceGroup =~ %q", rg))
if err != nil {
return nil, fmt.Errorf("listing resource group: %w", err)
}
for _, res := range result.Resources {
res := resourceset.AzureResource{
Id: res.Id,
Properties: res.Properties,
}
rl = append(rl, res)
}

return &resourceset.AzureResourceSet{Resources: rl}, nil
Expand Down
Loading