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

Add GCS remote backend #21

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Usage of tfstate-lookup:
tfstate file path or URL (default "terraform.tfstate")
```

Supported URL schemes are http(s), s3 or file.
Supported URL schemes are http(s), s3, gs or file.

```console
$ tfstate-lookup -s .terraform/terraform.tfstate aws_vpc.main.id
Expand Down Expand Up @@ -53,7 +53,7 @@ $ tfstate-lookup aws_vpc.main
}
```

A remote state is supported only S3 backend currently.
A remote state is supported only S3 and GCS backend currently.

## Usage (Go package)

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module github.com/fujiwara/tfstate-lookup
go 1.14

require (
cloud.google.com/go/storage v1.15.0
github.com/aws/aws-sdk-go v1.30.7
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/google/go-cmp v0.3.1
github.com/google/go-cmp v0.5.5
github.com/itchyny/gojq v0.9.0
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/pkg/errors v0.9.1
github.com/tebeka/strftime v0.1.3 // indirect
google.golang.org/api v0.45.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
3 changes: 3 additions & 0 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func ReadURL(loc string) (*TFState, error) {
case "s3":
key := strings.TrimPrefix(u.Path, "/")
src, err = readS3("", u.Host, key)
case "gs":
key := strings.TrimPrefix(u.Path, "/")
src, err = readGCS(u.Host, key, "")
case "file":
src, err = os.Open(u.Path)
case "":
Expand Down
79 changes: 15 additions & 64 deletions tfstate/remote.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package tfstate

import (
"context"
"fmt"
"io"
"net/http"
"path"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func readRemoteState(b *backend, ws string) (io.ReadCloser, error) {
switch b.Type {
case "s3":
return readS3State(b.Config, ws)
default:
return nil, fmt.Errorf("backend type %s is not supported", b.Type)
}
}

func strp(v interface{}) *string {
if v == nil {
return nil
Expand All @@ -32,56 +15,24 @@ func strp(v interface{}) *string {
return nil
}

func readS3State(config map[string]interface{}, ws string) (io.ReadCloser, error) {
region, bucket, key := *strp(config["region"]), *strp(config["bucket"]), *strp(config["key"])
if ws != defaultWorkspace {
if prefix := strp(config["workspace_key_prefix"]); prefix != nil {
key = path.Join(*prefix, ws, key)
} else {
key = path.Join(defaultWorkspeceKeyPrefix, ws, key)
}
}
return readS3(region, bucket, key)
}

func readS3(region, bucket, key string) (io.ReadCloser, error) {
var err error
if region == "" {
region, err = s3manager.GetBucketRegion(
context.Background(),
session.Must(session.NewSession()),
bucket,
"",
)
if err != nil {
return nil, err
}
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(region),
},
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return nil, err
func strpe(v interface{}) *string {
empty := ""
if v == nil {
return &empty
}
svc := s3.New(sess)

result, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
if vs, ok := v.(string); ok {
return &vs
}
return result.Body, nil
return &empty
}

func readHTTP(u string) (io.ReadCloser, error) {
resp, err := http.Get(u)
if err != nil {
return nil, err
func readRemoteState(b *backend, ws string) (io.ReadCloser, error) {
switch b.Type {
case "gcs":
return readGCSState(b.Config, ws)
case "s3":
return readS3State(b.Config, ws)
default:
return nil, fmt.Errorf("backend type %s is not supported", b.Type)
}
return resp.Body, nil
}
45 changes: 45 additions & 0 deletions tfstate/remote_gcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tfstate

import (
"context"
"io"
"path"

"cloud.google.com/go/storage"
"google.golang.org/api/option"
)

func readGCSState(config map[string]interface{}, ws string) (io.ReadCloser, error) {
bucket, prefix, credentials := *strp(config["bucket"]), *strpe(config["prefix"]), *strpe(config["credentials"])

key := path.Join(prefix, ws+".tfstate")

return readGCS(bucket, key, credentials)
}

func readGCS(bucket, key, credentials string) (io.ReadCloser, error) {
var err error

ctx := context.Background()

var client *storage.Client
if credentials != "" {
client, err = storage.NewClient(ctx, option.WithCredentialsFile(credentials))
} else {
client, err = storage.NewClient(ctx)
}

if err != nil {
return nil, err
}

bkt := client.Bucket(bucket)
obj := bkt.Object(key)

r, err := obj.NewReader(ctx)
if err != nil {
return nil, err
}

return r, nil
}
14 changes: 14 additions & 0 deletions tfstate/remote_http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tfstate

import (
"io"
"net/http"
)

func readHTTP(u string) (io.ReadCloser, error) {
resp, err := http.Get(u)
if err != nil {
return nil, err
}
return resp.Body, nil
}
58 changes: 58 additions & 0 deletions tfstate/remote_s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tfstate

import (
"context"
"io"
"path"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func readS3State(config map[string]interface{}, ws string) (io.ReadCloser, error) {
region, bucket, key := *strp(config["region"]), *strp(config["bucket"]), *strp(config["key"])
if ws != defaultWorkspace {
if prefix := strp(config["workspace_key_prefix"]); prefix != nil {
key = path.Join(*prefix, ws, key)
} else {
key = path.Join(defaultWorkspeceKeyPrefix, ws, key)
}
}
return readS3(region, bucket, key)
}

func readS3(region, bucket, key string) (io.ReadCloser, error) {
var err error
if region == "" {
region, err = s3manager.GetBucketRegion(
context.Background(),
session.Must(session.NewSession()),
bucket,
"",
)
if err != nil {
return nil, err
}
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(region),
},
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return nil, err
}
svc := s3.New(sess)

result, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
return result.Body, nil
}