From 98f16fc97c08bb004f2d9b808571789a13a5081f Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Sat, 3 Feb 2024 16:56:47 -0500 Subject: [PATCH] export httpClient from the API Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- proxmox/api/client.go | 7 ++++ proxmoxtf/resource/file.go | 67 +++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/proxmox/api/client.go b/proxmox/api/client.go index 976cbad8..4c28e5c4 100644 --- a/proxmox/api/client.go +++ b/proxmox/api/client.go @@ -52,6 +52,9 @@ type Client interface { // IsRootTicket returns true if the authenticator is configured to use the root directly using a login ticket. // (root using token is weaker, cannot change VM arch) IsRootTicket() bool + + // HTTP returns a lower-level HTTP client. + HTTP() *http.Client } // Connection represents a connection to the Proxmox Virtual Environment API. @@ -298,6 +301,10 @@ func (c *client) IsRootTicket() bool { return c.auth.IsRootTicket() } +func (c *client) HTTP() *http.Client { + return c.conn.httpClient +} + // validateResponseCode ensures that a response is valid. func validateResponseCode(res *http.Response) error { if res.StatusCode < 200 || res.StatusCode >= 300 { diff --git a/proxmoxtf/resource/file.go b/proxmoxtf/resource/file.go index d82bcaa2..ca19ef43 100644 --- a/proxmoxtf/resource/file.go +++ b/proxmoxtf/resource/file.go @@ -739,7 +739,7 @@ func fileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D readFileAttrs := readFile if fileIsURL(d) { - readFileAttrs = readURL + readFileAttrs = readURL(capi.API().HTTP()) } var diags diag.Diagnostics @@ -838,48 +838,55 @@ func readFile( //nolint:nonamedreturns func readURL( + httClient *http.Client, +) func( ctx context.Context, sourceFilePath string, ) (fileModificationDate string, fileSize int64, fileTag string, err error) { - res, err := http.Head(sourceFilePath) - if err != nil { - return - } - - defer utils.CloseOrLogError(ctx)(res.Body) - - fileSize = res.ContentLength - httpLastModified := res.Header.Get("Last-Modified") - - if httpLastModified != "" { - var timeParsed time.Time - timeParsed, err = time.Parse(time.RFC1123, httpLastModified) - + return func( + ctx context.Context, + sourceFilePath string, + ) (fileModificationDate string, fileSize int64, fileTag string, err error) { + res, err := httClient.Head(sourceFilePath) if err != nil { - timeParsed, err = time.Parse(time.RFC1123Z, httpLastModified) - if err != nil { - return - } + return } - fileModificationDate = timeParsed.UTC().Format(time.RFC3339) - } + defer utils.CloseOrLogError(ctx)(res.Body) - httpTag := res.Header.Get("ETag") + fileSize = res.ContentLength + httpLastModified := res.Header.Get("Last-Modified") - if httpTag != "" { - httpTagParts := strings.Split(httpTag, "\"") + if httpLastModified != "" { + var timeParsed time.Time + timeParsed, err = time.Parse(time.RFC1123, httpLastModified) - if len(httpTagParts) > 1 { - fileTag = httpTagParts[1] + if err != nil { + timeParsed, err = time.Parse(time.RFC1123Z, httpLastModified) + if err != nil { + return + } + } + + fileModificationDate = timeParsed.UTC().Format(time.RFC3339) + } + + httpTag := res.Header.Get("ETag") + + if httpTag != "" { + httpTagParts := strings.Split(httpTag, "\"") + + if len(httpTagParts) > 1 { + fileTag = httpTagParts[1] + } else { + fileTag = "" + } } else { fileTag = "" } - } else { - fileTag = "" - } - return + return + } } func fileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {