Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_resources - does not return all matched resources sometimes #7036

Merged
merged 4 commits into from May 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}