From 012d789f62b5c75e853586a619f0f537cdd39ba3 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 19 Apr 2019 14:14:44 -0700 Subject: [PATCH] Add pagination to google projects data source (#3464) Signed-off-by: Modular Magician --- google/data_source_google_projects.go | 57 ++++++++++++++++----------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/google/data_source_google_projects.go b/google/data_source_google_projects.go index b4eba543c5b..1478b94a593 100644 --- a/google/data_source_google_projects.go +++ b/google/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 }