From 6d4723ca4f1939f7bc1c85150d7ac86fff87de00 Mon Sep 17 00:00:00 2001 From: Yichun Ma Date: Fri, 5 May 2023 22:14:03 +0800 Subject: [PATCH] `azurerm_data_source` - optimize api with `resource_group_name` (#21661) --- .../resource/resources_data_source.go | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/services/resource/resources_data_source.go b/internal/services/resource/resources_data_source.go index 75d24b8a931b..440a04c4dadf 100644 --- a/internal/services/resource/resources_data_source.go +++ b/internal/services/resource/resources_data_source.go @@ -84,11 +84,6 @@ func dataSourceResourcesRead(d *pluginsdk.ResourceData, meta interface{}) error var filter string - if resourceGroupName != "" { - v := fmt.Sprintf("resourceGroup eq '%s'", resourceGroupName) - filter += v - } - if resourceName != "" { if strings.Contains(filter, "eq") { filter += " and " @@ -106,10 +101,20 @@ func dataSourceResourcesRead(d *pluginsdk.ResourceData, meta interface{}) error } // Use List instead of listComplete because of bug in SDK: https://github.com/Azure/azure-sdk-for-go/issues/9510 + var resourcesResp resources.ListResultPage resources := make([]map[string]interface{}, 0) - resourcesResp, err := client.List(ctx, filter, "", nil) - if err != nil { - return fmt.Errorf("getting resources: %+v", err) + if resourceGroupName != "" { + resp, err := client.ListByResourceGroup(ctx, resourceGroupName, filter, "", nil) + if err != nil { + return fmt.Errorf("getting resources by resource group: %+v", err) + } + resourcesResp = resp + } else { + resp, err := client.List(ctx, filter, "", nil) + if err != nil { + return fmt.Errorf("getting resources: %+v", err) + } + resourcesResp = resp } resources = append(resources, filterResource(resourcesResp.Values(), requiredTags)...)