Chris Roberts e958c6183a Adds initial HCP config support
Adds initial basic support for HCP based configuration in vagrant-go.
The initalization process has been updated to remove Vagrantfile parsing
from the client, moving it to the runner using init jobs for the basis
and the project (if there is one). Detection is done on the file based
on extension for Ruby based parsing or HCP based parsing.

Current HCP parsing is extremely simple and currently just a base to
build off. Config components will be able to implement an `Init`
function to handle receiving configuration data from a non-native source
file. This will be extended to include a default approach for injecting
defined data in the future.

Some cleanup was done in the state around validations. Some logging
adjustments were applied on the Ruby side for better behavior
consistency.

VirtualBox provider now caches locale detection to prevent multiple
checks every time the driver is initialized.
2023-09-07 17:26:10 -07:00

139 lines
2.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package downloader
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/vagrant-plugin-sdk/component"
)
// Type is an enum of all the available http methods
type HTTPMethod int64
const (
GET HTTPMethod = iota
DELETE
HEAD
POST
PUT
)
type Downloader struct {
config DownloaderConfig
}
type DownloaderConfig struct {
Dest string
Headers http.Header
Method HTTPMethod
RetryCount int
RequestBody []byte
Src string
UrlQueryParams map[string]string
}
func (d *Downloader) InitFunc() any {
return d.Init
}
func (d *Downloader) Init(in *component.ConfigData) (*component.ConfigData, error) {
return in, nil
}
func (d *Downloader) StructFunc() any {
return d.Struct
}
func (d *Downloader) MergeFunc() any {
return d.Merge
}
func (d *Downloader) FinalizeFunc() any {
return d.Finalize
}
func (d *Downloader) Struct() *DownloaderConfig {
return &DownloaderConfig{}
}
func (d *Downloader) Merge(val *component.ConfigMerge) *component.ConfigData {
return val.Base
}
func (d *Downloader) Finalize(val *component.ConfigData) *component.ConfigData {
return val
}
func (d *Downloader) Register() (*component.ConfigRegistration, error) {
return &component.ConfigRegistration{
Identifier: "downloader",
}, nil
}
func (d *Downloader) DownloadFunc() interface{} {
return d.Download
}
func (d *Downloader) Download() (err error) {
client := retryablehttp.NewClient()
client.RetryMax = d.config.RetryCount
var req *retryablehttp.Request
// Create request with request body if one is provided
if d.config.RequestBody != nil {
req, err = retryablehttp.NewRequest(
d.config.Method.String(), d.config.Src, bytes.NewBuffer(d.config.RequestBody),
)
if err != nil {
return err
}
} else {
// If no request body is provided then create an empty request
req, err = retryablehttp.NewRequest(
d.config.Method.String(), d.config.Src, nil,
)
}
// Add query params if provided
if d.config.UrlQueryParams != nil {
q := req.URL.Query()
for k, v := range d.config.UrlQueryParams {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
// Set headers
req.Header = d.config.Headers
// Add headers to redirects
client.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
for key, val := range via[0].Header {
req.Header[key] = val
}
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = os.WriteFile(d.config.Dest, data, 0644)
return
}
var (
_ component.Downloader = (*Downloader)(nil)
_ component.Config = (*Downloader)(nil)
)