refactor(tfcloud): rebase

main
Rémi Doreau 2021-05-03 14:01:38 +02:00 committed by sundowndev
parent d0104515b1
commit 53a17a381a
3 changed files with 6 additions and 6 deletions

View File

@ -51,7 +51,7 @@ func GetBackend(config config.SupplierConfig, opts *Options) (Backend, error) {
case BackendKeyHTTPS:
return NewHTTPReader(&http.Client{}, fmt.Sprintf("%s://%s", config.Backend, config.Path), opts)
case BackendKeyTFCloud:
return NewTFCloudReader(config.Path, opts)
return NewTFCloudReader(&http.Client{}, config.Path, opts)
default:
return nil, errors.Errorf("Unsupported backend '%s'", backend)
}

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
pkghttp "github.com/cloudskiff/driftctl/pkg/http"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -25,7 +26,7 @@ type TFCloudBody struct {
Data TFCloudData `json:"data"`
}
func NewTFCloudReader(workspaceId string, opts *Options) (*HTTPBackend, error) {
func NewTFCloudReader(client pkghttp.HTTPClient, workspaceId string, opts *Options) (*HTTPBackend, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/workspaces/%s/current-state-version", TFCloudAPI, workspaceId), nil)
if err != nil {
@ -35,7 +36,6 @@ func NewTFCloudReader(workspaceId string, opts *Options) (*HTTPBackend, error) {
req.Header.Add("Content-Type", "application/vnd.api+json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", opts.TFCloudToken))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
@ -59,5 +59,5 @@ func NewTFCloudReader(workspaceId string, opts *Options) (*HTTPBackend, error) {
logrus.WithFields(logrus.Fields{"hosted-state-download-url": rawURL}).Trace("Terraform Cloud backend response")
opt := Options{}
return NewHTTPReader(rawURL, &opt)
return NewHTTPReader(client, rawURL, &opt)
}

View File

@ -93,7 +93,7 @@ func TestNewTFCloudReader(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.mock()
got, err := NewTFCloudReader(tt.args.workspaceId, tt.args.options)
got, err := NewTFCloudReader(&http.Client{}, tt.args.workspaceId, tt.args.options)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return
@ -101,7 +101,7 @@ func TestNewTFCloudReader(t *testing.T) {
assert.NoError(t, err)
}
assert.NotNil(t, got)
assert.Equal(t, tt.wantURL, got.url)
assert.Equal(t, tt.wantURL, got.request.URL.String())
})
}
}