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

Add pagination to google projects data source #3464

Merged
merged 1 commit into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions google/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
}