Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read tfe state. #141

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func ReadURL(ctx context.Context, loc string) (*TFState, error) {
src, err = os.Open(u.Path)
case "remote":
split := strings.Split(u.Path, "/")
src, err = readTFE(ctx, u.Host, split[1], split[2], "")
src, err = readTFE(ctx, u.Host, split[1], split[2], os.Getenv("TFE_TOKEN"))
case "":
return ReadFile(ctx, u.Path)
default:
Expand Down
7 changes: 7 additions & 0 deletions tfstate/remote_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ func readHTTP(ctx context.Context, u string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
return readHTTPWithRequest(ctx, req)
}

func readHTTPWithRequest(ctx context.Context, req *http.Request) (io.ReadCloser, error) {
if c := req.Context(); c != ctx {
req = req.WithContext(ctx)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand Down
24 changes: 11 additions & 13 deletions tfstate/remote_tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tfstate
import (
"context"
"io"
"net/http"

tfe "github.com/hashicorp/go-tfe"
"github.com/pkg/errors"
Expand Down Expand Up @@ -35,18 +36,11 @@ func readTFE(ctx context.Context, hostname string, organization string, ws strin
address = "https://" + hostname
}

var err error
var client *tfe.Client
if token != "" {
client, err = tfe.NewClient(&tfe.Config{
Address: address,
Token: token,
})
} else {
client, err = tfe.NewClient(&tfe.Config{
Address: address,
})
}
client, err := tfe.NewClient(&tfe.Config{
Address: address,
Token: token,
})
if err != nil {
return nil, err
}
Expand All @@ -59,6 +53,10 @@ func readTFE(ctx context.Context, hostname string, organization string, ws strin
if err != nil {
return nil, err
}

return readHTTP(ctx, state.DownloadURL)
req, err := http.NewRequest(http.MethodGet, state.DownloadURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
return readHTTPWithRequest(ctx, req)
}