|
3 | 3 | package apigee
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "encoding/json" |
6 | 7 | "fmt"
|
7 |
| - "log" |
8 |
| - |
9 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
10 | 9 | "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
|
11 | 10 | transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
|
| 11 | + "google.golang.org/api/googleapi" |
| 12 | + "io" |
| 13 | + "log" |
| 14 | + "net/http" |
| 15 | + "time" |
12 | 16 | )
|
13 | 17 |
|
14 | 18 | func resourceApigeeNatAddressActivate(config *transport_tpg.Config, d *schema.ResourceData, billingProject string, userAgent string) error {
|
@@ -47,3 +51,71 @@ func resourceApigeeNatAddressActivate(config *transport_tpg.Config, d *schema.Re
|
47 | 51 | }
|
48 | 52 | return nil
|
49 | 53 | }
|
| 54 | + |
| 55 | +// sendRequestRawBodyWithTimeout is derived from sendRequestWithTimeout with direct pass through of request body |
| 56 | +func sendRequestRawBodyWithTimeout(config *transport_tpg.Config, method, project, rawurl, userAgent string, body io.Reader, contentType string, timeout time.Duration, errorRetryPredicates ...transport_tpg.RetryErrorPredicateFunc) (map[string]interface{}, error) { |
| 57 | + log.Printf("[DEBUG] sendRequestRawBodyWithTimeout start") |
| 58 | + reqHeaders := make(http.Header) |
| 59 | + reqHeaders.Set("User-Agent", userAgent) |
| 60 | + reqHeaders.Set("Content-Type", contentType) |
| 61 | + |
| 62 | + if config.UserProjectOverride && project != "" { |
| 63 | + // Pass the project into this fn instead of parsing it from the URL because |
| 64 | + // both project names and URLs can have colons in them. |
| 65 | + reqHeaders.Set("X-Goog-User-Project", project) |
| 66 | + } |
| 67 | + |
| 68 | + if timeout == 0 { |
| 69 | + timeout = time.Duration(1) * time.Minute |
| 70 | + } |
| 71 | + |
| 72 | + var res *http.Response |
| 73 | + |
| 74 | + log.Printf("[DEBUG] sendRequestRawBodyWithTimeout sending request") |
| 75 | + |
| 76 | + err := transport_tpg.Retry(transport_tpg.RetryOptions{ |
| 77 | + RetryFunc: func() error { |
| 78 | + req, err := http.NewRequest(method, rawurl, body) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + req.Header = reqHeaders |
| 84 | + res, err = config.Client.Do(req) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + if err := googleapi.CheckResponse(res); err != nil { |
| 90 | + googleapi.CloseBody(res) |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | + }, |
| 96 | + Timeout: timeout, |
| 97 | + ErrorRetryPredicates: errorRetryPredicates, |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + if res == nil { |
| 104 | + return nil, fmt.Errorf("Unable to parse server response. This is most likely a terraform problem, please file a bug at https://github.com/hashicorp/terraform-provider-google/issues.") |
| 105 | + } |
| 106 | + |
| 107 | + // The defer call must be made outside of the retryFunc otherwise it's closed too soon. |
| 108 | + defer googleapi.CloseBody(res) |
| 109 | + |
| 110 | + // 204 responses will have no body, so we're going to error with "EOF" if we |
| 111 | + // try to parse it. Instead, we can just return nil. |
| 112 | + if res.StatusCode == 204 { |
| 113 | + return nil, nil |
| 114 | + } |
| 115 | + result := make(map[string]interface{}) |
| 116 | + if err := json.NewDecoder(res.Body).Decode(&result); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + log.Printf("[DEBUG] sendRequestRawBodyWithTimeout returning") |
| 120 | + return result, nil |
| 121 | +} |
0 commit comments