Skip to content

Commit

Permalink
Added google_apigee_api resource (GoogleCloudPlatform#12036)
Browse files Browse the repository at this point in the history
  • Loading branch information
apichick authored and BBBmau committed Nov 5, 2024
1 parent 64aef33 commit 7489708
Show file tree
Hide file tree
Showing 9 changed files with 956 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ var generatedResources = map[string]*schema.Resource{
var handwrittenResources = map[string]*schema.Resource{
// ####### START handwritten resources ###########
"google_app_engine_application": appengine.ResourceAppEngineApplication(),
"google_apigee_api": apigee.ResourceApigeeApi(),
"google_apigee_sharedflow": apigee.ResourceApigeeSharedFlow(),
"google_apigee_sharedflow_deployment": apigee.ResourceApigeeSharedFlowDeployment(),
"google_apigee_flowhook": apigee.ResourceApigeeFlowhook(),
Expand Down
76 changes: 74 additions & 2 deletions mmv1/third_party/terraform/services/apigee/apigee_utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package apigee

import (
"encoding/json"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"google.golang.org/api/googleapi"
"io"
"log"
"net/http"
"time"
)

func resourceApigeeNatAddressActivate(config *transport_tpg.Config, d *schema.ResourceData, billingProject string, userAgent string) error {
Expand Down Expand Up @@ -45,3 +49,71 @@ func resourceApigeeNatAddressActivate(config *transport_tpg.Config, d *schema.Re
}
return nil
}

// sendRequestRawBodyWithTimeout is derived from sendRequestWithTimeout with direct pass through of request body
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) {
log.Printf("[DEBUG] sendRequestRawBodyWithTimeout start")
reqHeaders := make(http.Header)
reqHeaders.Set("User-Agent", userAgent)
reqHeaders.Set("Content-Type", contentType)

if config.UserProjectOverride && project != "" {
// Pass the project into this fn instead of parsing it from the URL because
// both project names and URLs can have colons in them.
reqHeaders.Set("X-Goog-User-Project", project)
}

if timeout == 0 {
timeout = time.Duration(1) * time.Minute
}

var res *http.Response

log.Printf("[DEBUG] sendRequestRawBodyWithTimeout sending request")

err := transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() error {
req, err := http.NewRequest(method, rawurl, body)
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
},
Timeout: timeout,
ErrorRetryPredicates: errorRetryPredicates,
})
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/hashicorp/terraform-provider-google/issues.")
}

// The defer call must be made outside of the retryFunc otherwise it's closed too soon.
defer googleapi.CloseBody(res)

// 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
}
log.Printf("[DEBUG] sendRequestRawBodyWithTimeout returning")
return result, nil
}
Loading

0 comments on commit 7489708

Please sign in to comment.