Skip to content

Commit aff6bc6

Browse files
sethvargodanawillow
authored andcommitted
Retry while listing enabled services (#1573)
This fixes GH-1562 for realz
1 parent 5f0dcf4 commit aff6bc6

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

google/resource_google_project_services.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,37 @@ func getConfigServices(d *schema.ResourceData) (services []string) {
187187

188188
// Retrieve a project's services from the API
189189
func getApiServices(pid string, config *Config, ignore map[string]struct{}) ([]string, error) {
190-
var apiServices []string
191-
192190
if ignore == nil {
193191
ignore = make(map[string]struct{})
194192
}
195193

196-
ctx := context.Background()
197-
if err := config.clientServiceUsage.Services.
198-
List("projects/"+pid).
199-
Fields("services/name").
200-
Filter("state:ENABLED").
201-
Pages(ctx, func(r *serviceusage.ListServicesResponse) error {
202-
for _, v := range r.Services {
203-
// services are returned as "projects/PROJECT/services/NAME"
204-
parts := strings.Split(v.Name, "/")
205-
if len(parts) > 0 {
206-
name := parts[len(parts)-1]
207-
if _, ok := ignore[name]; !ok {
208-
apiServices = append(apiServices, name)
194+
var apiServices []string
195+
196+
if err := retryTime(func() error {
197+
// Reset the list of apiServices in case of a retry. A partial page failure
198+
// could result in duplicate services.
199+
apiServices = make([]string, 0, 10)
200+
201+
ctx := context.Background()
202+
return config.clientServiceUsage.Services.
203+
List("projects/"+pid).
204+
Fields("services/name").
205+
Filter("state:ENABLED").
206+
Pages(ctx, func(r *serviceusage.ListServicesResponse) error {
207+
for _, v := range r.Services {
208+
// services are returned as "projects/PROJECT/services/NAME"
209+
parts := strings.Split(v.Name, "/")
210+
if len(parts) > 0 {
211+
name := parts[len(parts)-1]
212+
if _, ok := ignore[name]; !ok {
213+
apiServices = append(apiServices, name)
214+
}
209215
}
210216
}
211-
}
212217

213-
return nil
214-
}); err != nil {
218+
return nil
219+
})
220+
}, 10); err != nil {
215221
return nil, errwrap.Wrapf("failed to list services: {{err}}", err)
216222
}
217223

0 commit comments

Comments
 (0)