Skip to content

Commit

Permalink
Allowed modifying request headers via pre_create, pre_read, pre_updat…
Browse files Browse the repository at this point in the history
…e, or pre_delete custom code (#10405)
  • Loading branch information
melinath authored Apr 11, 2024
1 parent 1c575ec commit ffaae41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions mmv1/templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"fmt"
"log"
"net/http"
"reflect"
<% if updatable?(object, object.root_properties) && object.update_mask -%>
"strings"
Expand Down Expand Up @@ -267,6 +268,7 @@ func resource<%= object.resource_name -%>Create(d *schema.ResourceData, meta int
billingProject = bp
}

headers := make(http.Header)
<%= lines(compile(pwd + '/' + object.custom_code.pre_create)) if object.custom_code.pre_create -%>
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Expand All @@ -276,6 +278,7 @@ func resource<%= object.resource_name -%>Create(d *schema.ResourceData, meta int
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Headers: headers,
<% if object.error_retry_predicates -%>
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>},
<% end -%>
Expand Down Expand Up @@ -539,13 +542,15 @@ func resource<%= object.resource_name -%>Read(d *schema.ResourceData, meta inter
billingProject = bp
}

headers := make(http.Header)
<%= lines(compile(pwd + '/' + object.custom_code.pre_read)) if object.custom_code.pre_read -%>
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "<%= object.read_verb.to_s.upcase -%>",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
<% if object.error_retry_predicates -%>
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>},
<% end -%>
Expand Down Expand Up @@ -737,6 +742,7 @@ func resource<%= object.resource_name -%>Update(d *schema.ResourceData, meta int
}

log.Printf("[DEBUG] Updating <%= object.name -%> %q: %#v", d.Id(), obj)
headers := make(http.Header)
<%= lines(compile(pwd + '/templates/terraform/update_mask.erb')) if object.update_mask -%>
<%= lines(compile(pwd + '/' + object.custom_code.pre_update)) if object.custom_code.pre_update -%>
<% if object.nested_query&.modify_by_patch -%>
Expand Down Expand Up @@ -769,6 +775,7 @@ if len(updateMask) > 0 {
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
<% if object.error_retry_predicates -%>
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>},
<% end -%>
Expand Down Expand Up @@ -911,6 +918,8 @@ if len(updateMask) > 0 {
}
headers := make(http.Header)
<%= lines(compile(pwd + '/' + object.custom_code.pre_update)) if object.custom_code.pre_update -%>
<% if object.supports_indirect_user_project_override -%>
if parts := regexp.MustCompile(`projects\/([^\/]+)\/`).FindStringSubmatch(url); parts != nil {
Expand All @@ -931,6 +940,7 @@ if len(updateMask) > 0 {
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
<% if object.error_retry_predicates -%>
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>},
<% end -%>
Expand Down Expand Up @@ -1056,6 +1066,7 @@ func resource<%= object.resource_name -%>Delete(d *schema.ResourceData, meta int
billingProject = bp
}
headers := make(http.Header)
<%= lines(compile(pwd + '/' + object.custom_code.pre_delete)) if object.custom_code.pre_delete -%>
log.Printf("[DEBUG] Deleting <%= object.name -%> %q", d.Id())
Expand All @@ -1067,6 +1078,7 @@ func resource<%= object.resource_name -%>Delete(d *schema.ResourceData, meta int
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
Headers: headers,
<% if object.error_retry_predicates -%>
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>},
<% end -%>
Expand Down
6 changes: 5 additions & 1 deletion mmv1/third_party/terraform/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ type SendRequestOptions struct {
UserAgent string
Body map[string]any
Timeout time.Duration
Headers http.Header
ErrorRetryPredicates []RetryErrorPredicateFunc
ErrorAbortPredicates []RetryErrorPredicateFunc
}

func SendRequest(opt SendRequestOptions) (map[string]interface{}, error) {
reqHeaders := make(http.Header)
reqHeaders := opt.Headers
if reqHeaders == nil {
reqHeaders = make(http.Header)
}
reqHeaders.Set("User-Agent", opt.UserAgent)
reqHeaders.Set("Content-Type", "application/json")

Expand Down

0 comments on commit ffaae41

Please sign in to comment.