From 7fa80eecff473df08785a73e7ee555e31feca1a7 Mon Sep 17 00:00:00 2001 From: emily Date: Fri, 19 Apr 2019 14:17:50 -0700 Subject: [PATCH] Add pagination to google projects data source (#1670) Merged PR #1670. --- build/terraform | 2 +- build/terraform-beta | 2 +- .../data_source_google_projects.go | 57 +++++++++++-------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/build/terraform b/build/terraform index 462c3678425f..6711efe903a9 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 462c3678425f5b4156738b793b9f4b5d947fed77 +Subproject commit 6711efe903a9afb9f05ff20347dd60b53070b640 diff --git a/build/terraform-beta b/build/terraform-beta index 77f415b46ec1..bf41c4847dbd 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 77f415b46ec1c5aca4c2a825a844d62ef1a188d6 +Subproject commit bf41c4847dbdb3e6b0300ef87379621fb057fe2d diff --git a/third_party/terraform/data_sources/data_source_google_projects.go b/third_party/terraform/data_sources/data_source_google_projects.go index b4eba543c5bf..1478b94a5937 100644 --- a/third_party/terraform/data_sources/data_source_google_projects.go +++ b/third_party/terraform/data_sources/data_source_google_projects.go @@ -33,21 +33,34 @@ func datasourceGoogleProjectsRead(d *schema.ResourceData, meta interface{}) erro config := meta.(*Config) params := make(map[string]string) + projects := make([]map[string]interface{}, 0) - params["filter"] = d.Get("filter").(string) - url := "https://cloudresourcemanager.googleapis.com/v1/projects" + for { + params["filter"] = d.Get("filter").(string) + url := "https://cloudresourcemanager.googleapis.com/v1/projects" - url, err := addQueryParams(url, params) - if err != nil { - return err - } + url, err := addQueryParams(url, params) + if err != nil { + return err + } - res, err := sendRequest(config, "GET", url, nil) - if err != nil { - return fmt.Errorf("Error retrieving projects: %s", err) + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return fmt.Errorf("Error retrieving projects: %s", err) + } + + pageProjects := flattenDatasourceGoogleProjectsList(res["projects"]) + projects = append(projects, pageProjects...) + + pToken, ok := res["nextPageToken"] + if ok && pToken != nil && pToken.(string) != "" { + params["pageToken"] = pToken.(string) + } else { + break + } } - if err := d.Set("projects", flattenDatasourceGoogleProjectsProjects(res["projects"], d)); err != nil { + if err := d.Set("projects", projects); err != nil { return fmt.Errorf("Error retrieving projects: %s", err) } @@ -56,23 +69,21 @@ func datasourceGoogleProjectsRead(d *schema.ResourceData, meta interface{}) erro return nil } -func flattenDatasourceGoogleProjectsProjects(v interface{}, d *schema.ResourceData) interface{} { +func flattenDatasourceGoogleProjectsList(v interface{}) []map[string]interface{} { if v == nil { - return v + return make([]map[string]interface{}, 0) } - l := v.([]interface{}) - transformed := make([]interface{}, 0, len(l)) - for _, raw := range l { - original := raw.(map[string]interface{}) - if len(original) < 1 { - // Do not include empty json objects coming back from the api - continue + ls := v.([]interface{}) + projects := make([]map[string]interface{}, 0, len(ls)) + for _, raw := range ls { + p := raw.(map[string]interface{}) + if pId, ok := p["projectId"]; ok { + projects = append(projects, map[string]interface{}{ + "project_id": pId, + }) } - transformed = append(transformed, map[string]interface{}{ - "project_id": original["projectId"], - }) } - return transformed + return projects }