Skip to content

Commit

Permalink
Add pagination to google projects data source (#1670)
Browse files Browse the repository at this point in the history
Merged PR #1670.
  • Loading branch information
emilymye authored and modular-magician committed Apr 19, 2019
1 parent e74a54c commit 7fa80ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
57 changes: 34 additions & 23 deletions third_party/terraform/data_sources/data_source_google_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}

0 comments on commit 7fa80ee

Please sign in to comment.