Skip to content

Commit d25a6c7

Browse files
committed
RG mode: Two step list, rg itself (incl. ext), then resources (incl. ext) in it
1 parent 6478cf6 commit d25a6c7

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

internal/meta/meta_rg.go

+36-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/Azure/aztfexport/internal/resourceset"
88
"github.com/Azure/aztfexport/internal/tfaddr"
99
"github.com/Azure/aztfexport/pkg/config"
10-
"github.com/magodo/armid"
1110
"github.com/magodo/azlist/azlist"
1211
)
1312

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

9190
func (meta MetaResourceGroup) queryResourceSet(ctx context.Context, rg string) (*resourceset.AzureResourceSet, error) {
91+
var rl []resourceset.AzureResource
92+
93+
// Try to get the resource group (with any extension resources) first, in case it doesn't exist.
9294
opt := azlist.Option{
9395
Logger: meta.logger.WithGroup("azlist"),
9496
SubscriptionId: meta.subscriptionId,
9597
Cred: meta.azureSDKCred,
9698
ClientOpt: meta.azureSDKClientOpt,
9799
Parallelism: meta.parallelism,
98-
Recursive: true,
99-
IncludeResourceGroup: true,
100100
ExtensionResourceTypes: extBuilder{includeRoleAssignment: meta.includeRoleAssignment}.Build(),
101+
ARGTable: "ResourceContainers",
101102
}
102103
lister, err := azlist.NewLister(opt)
103104
if err != nil {
104-
return nil, fmt.Errorf("building azlister: %v", err)
105+
return nil, fmt.Errorf("building azlister for listing resource group only: %v", err)
105106
}
106-
result, err := lister.List(ctx, fmt.Sprintf("resourceGroup =~ %q", rg))
107+
result, err := lister.List(ctx, fmt.Sprintf("name == %q", rg))
107108
if err != nil {
108-
return nil, fmt.Errorf("listing resource set: %w", err)
109+
return nil, fmt.Errorf("listing resource group only: %w", err)
109110
}
110-
111-
var rl []resourceset.AzureResource
112111
for _, res := range result.Resources {
113112
res := resourceset.AzureResource{
114113
Id: res.Id,
@@ -117,15 +116,35 @@ func (meta MetaResourceGroup) queryResourceSet(ctx context.Context, rg string) (
117116
rl = append(rl, res)
118117
}
119118

120-
// Especially, if there is no resource within the resource group, the azlist will return an empty list.
121-
// In this case, we'll have to add the resource group manually.
122-
if len(rl) == 0 {
123-
rl = append(rl,
124-
resourceset.AzureResource{Id: &armid.ResourceGroup{
125-
SubscriptionId: meta.subscriptionId,
126-
Name: meta.resourceGroup,
127-
}},
128-
)
119+
// Skip the resource listing if the resource group itself doesn't exist.
120+
if len(result.Resources) == 0 {
121+
return &resourceset.AzureResourceSet{}, nil
122+
}
123+
124+
// List the resources within the resource group.
125+
opt = azlist.Option{
126+
Logger: meta.logger.WithGroup("azlist"),
127+
SubscriptionId: meta.subscriptionId,
128+
Cred: meta.azureSDKCred,
129+
ClientOpt: meta.azureSDKClientOpt,
130+
Parallelism: meta.parallelism,
131+
ExtensionResourceTypes: extBuilder{includeRoleAssignment: meta.includeRoleAssignment}.Build(),
132+
Recursive: true,
133+
}
134+
lister, err = azlist.NewLister(opt)
135+
if err != nil {
136+
return nil, fmt.Errorf("building azlister for listing resource group: %v", err)
137+
}
138+
result, err = lister.List(ctx, fmt.Sprintf("resourceGroup =~ %q", rg))
139+
if err != nil {
140+
return nil, fmt.Errorf("listing resource group: %w", err)
141+
}
142+
for _, res := range result.Resources {
143+
res := resourceset.AzureResource{
144+
Id: res.Id,
145+
Properties: res.Properties,
146+
}
147+
rl = append(rl, res)
129148
}
130149

131150
return &resourceset.AzureResourceSet{Resources: rl}, nil

0 commit comments

Comments
 (0)