Skip to content

Commit

Permalink
Add retries for all synchronous MM generated resources
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisst authored and paddycarver committed Dec 14, 2018
1 parent 53f29e8 commit 494ba48
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions google/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package google
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
"time"

"google.golang.org/api/googleapi"
)
Expand Down Expand Up @@ -35,39 +37,57 @@ func sendRequest(config *Config, method, rawurl string, body map[string]interfac
reqHeaders.Set("User-Agent", config.userAgent)
reqHeaders.Set("Content-Type", "application/json")

var buf bytes.Buffer
if body != nil {
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
return nil, err
}
}

u, err := addQueryParams(rawurl, map[string]string{"alt": "json"})
var res *http.Response
err := retryTimeDuration(
func() error {
var buf bytes.Buffer
if body != nil {
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
return err
}
}

u, err := addQueryParams(rawurl, map[string]string{"alt": "json"})
if err != nil {
return err
}
req, err := http.NewRequest(method, u, &buf)
if err != nil {
return err
}

req.Header = reqHeaders
res, err = config.client.Do(req)
if err != nil {
return err
}

if err := googleapi.CheckResponse(res); err != nil {
googleapi.CloseBody(res)
return err
}

return nil
},
// TODO chrisst - use the timeouts specified at the resource level so that a user can override this.
time.Duration(5)*time.Minute,
)
if err != nil {
return nil, err
}

req, err := http.NewRequest(method, u, &buf)
if err != nil {
return nil, err
}
req.Header = reqHeaders
res, err := config.client.Do(req)
if err != nil {
return nil, err
if res == nil {
return nil, fmt.Errorf("Unable to parse server response. This is most likely a terraform problem, please file a bug at https://github.com/terraform-providers/terraform-provider-google/issues.")
}
// The defer call must be made outside of the retryFunc otherwise it's closed too soon.
defer googleapi.CloseBody(res)
if err := googleapi.CheckResponse(res); err != nil {
return nil, err
}

// 204 responses will have no body, so we're going to error with "EOF" if we
// try to parse it. Instead, we can just return nil.
if res.StatusCode == 204 {
return nil, nil
}

result := make(map[string]interface{})
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, err
Expand Down

0 comments on commit 494ba48

Please sign in to comment.