7
7
"github.com/Azure/aztfexport/internal/resourceset"
8
8
"github.com/Azure/aztfexport/internal/tfaddr"
9
9
"github.com/Azure/aztfexport/pkg/config"
10
- "github.com/magodo/armid"
11
10
"github.com/magodo/azlist/azlist"
12
11
)
13
12
@@ -89,26 +88,26 @@ func (meta *MetaResourceGroup) ListResource(ctx context.Context) (ImportList, er
89
88
}
90
89
91
90
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.
92
94
opt := azlist.Option {
93
95
Logger : meta .logger .WithGroup ("azlist" ),
94
96
SubscriptionId : meta .subscriptionId ,
95
97
Cred : meta .azureSDKCred ,
96
98
ClientOpt : meta .azureSDKClientOpt ,
97
99
Parallelism : meta .parallelism ,
98
- Recursive : true ,
99
- IncludeResourceGroup : true ,
100
100
ExtensionResourceTypes : extBuilder {includeRoleAssignment : meta .includeRoleAssignment }.Build (),
101
+ ARGTable : "ResourceContainers" ,
101
102
}
102
103
lister , err := azlist .NewLister (opt )
103
104
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 )
105
106
}
106
- result , err := lister .List (ctx , fmt .Sprintf ("resourceGroup =~ %q" , rg ))
107
+ result , err := lister .List (ctx , fmt .Sprintf ("name == %q" , rg ))
107
108
if err != nil {
108
- return nil , fmt .Errorf ("listing resource set : %w" , err )
109
+ return nil , fmt .Errorf ("listing resource group only : %w" , err )
109
110
}
110
-
111
- var rl []resourceset.AzureResource
112
111
for _ , res := range result .Resources {
113
112
res := resourceset.AzureResource {
114
113
Id : res .Id ,
@@ -117,15 +116,35 @@ func (meta MetaResourceGroup) queryResourceSet(ctx context.Context, rg string) (
117
116
rl = append (rl , res )
118
117
}
119
118
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 )
129
148
}
130
149
131
150
return & resourceset.AzureResourceSet {Resources : rl }, nil
0 commit comments