From 46df426f1ea6cdc59e3c3a27b94eb7c321d53c30 Mon Sep 17 00:00:00 2001 From: njucz Date: Fri, 22 May 2020 04:10:31 +0800 Subject: [PATCH] fixes #7035 * fix issue: could not return all matched resources sometimes * update * fix ci * Add link to bug in code Co-authored-by: WS <20408400+WodansSon@users.noreply.github.com> --- .../resource/data_source_resources.go | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/azurerm/internal/services/resource/data_source_resources.go b/azurerm/internal/services/resource/data_source_resources.go index 5601f48217f2..2330fc1a04ac 100644 --- a/azurerm/internal/services/resource/data_source_resources.go +++ b/azurerm/internal/services/resource/data_source_resources.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" @@ -103,14 +104,32 @@ func dataSourceArmResourcesRead(d *schema.ResourceData, meta interface{}) error filter += v } + // Use List instead of listComplete because of bug in SDK: https://github.com/Azure/azure-sdk-for-go/issues/9510 resources := make([]map[string]interface{}, 0) - resourcesResp, err := client.ListComplete(ctx, filter, "", nil) + resourcesResp, err := client.List(ctx, filter, "", nil) if err != nil { return fmt.Errorf("Error getting resources: %+v", err) } - for resourcesResp.NotDone() { - res := resourcesResp.Value() + resources = append(resources, filterResource(resourcesResp.Values(), requiredTags)...) + for resourcesResp.Response().NextLink != nil && *resourcesResp.Response().NextLink != "" { + if err := resourcesResp.NextWithContext(ctx); err != nil { + return fmt.Errorf("loading Resource List: %+v", err) + } + resources = append(resources, filterResource(resourcesResp.Values(), requiredTags)...) + } + + d.SetId("resource-" + uuid.New().String()) + if err := d.Set("resources", resources); err != nil { + return fmt.Errorf("Error setting `resources`: %+v", err) + } + + return nil +} + +func filterResource(inputs []resources.GenericResourceExpanded, requiredTags map[string]interface{}) []map[string]interface{} { + var result []map[string]interface{} + for _, res := range inputs { if res.ID == nil { continue } @@ -159,7 +178,7 @@ func dataSourceArmResourcesRead(d *schema.ResourceData, meta interface{}) error } } - resources = append(resources, map[string]interface{}{ + result = append(result, map[string]interface{}{ "name": resName, "id": resID, "type": resType, @@ -169,17 +188,6 @@ func dataSourceArmResourcesRead(d *schema.ResourceData, meta interface{}) error } else { log.Printf("[DEBUG] azurerm_resources - resources %q (id: %q) skipped as a required tag is not set or has the wrong value.", *res.Name, *res.ID) } - - err = resourcesResp.NextWithContext(ctx) - if err != nil { - return fmt.Errorf("Error loading Resource List: %s", err) - } - } - - d.SetId("resource-" + uuid.New().String()) - if err := d.Set("resources", resources); err != nil { - return fmt.Errorf("Error setting `resources`: %+v", err) } - - return nil + return result }