diff --git a/mmv1/templates/terraform/resource.erb b/mmv1/templates/terraform/resource.erb index ea2b7cb1e269..e3e557e1ac7e 100644 --- a/mmv1/templates/terraform/resource.erb +++ b/mmv1/templates/terraform/resource.erb @@ -24,6 +24,7 @@ import ( "fmt" "log" + "net/http" "reflect" <% if updatable?(object, object.root_properties) && object.update_mask -%> "strings" @@ -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, @@ -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 -%> @@ -539,6 +542,7 @@ 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, @@ -546,6 +550,7 @@ func resource<%= object.resource_name -%>Read(d *schema.ResourceData, meta inter Project: billingProject, RawURL: url, UserAgent: userAgent, + Headers: headers, <% if object.error_retry_predicates -%> ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{<%= object.error_retry_predicates.join(',') -%>}, <% end -%> @@ -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 -%> @@ -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 -%> @@ -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 { @@ -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 -%> @@ -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()) @@ -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 -%> diff --git a/mmv1/third_party/terraform/transport/transport.go b/mmv1/third_party/terraform/transport/transport.go index 01f0fd1a1aa6..4de2a7c6a05c 100644 --- a/mmv1/third_party/terraform/transport/transport.go +++ b/mmv1/third_party/terraform/transport/transport.go @@ -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")