Skip to content

Commit

Permalink
fixes #7035
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
njuCZ and WodansSon committed May 21, 2020
1 parent fc45940 commit 46df426
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions azurerm/internal/services/resource/data_source_resources.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

0 comments on commit 46df426

Please sign in to comment.