/
remote_gcs.go
55 lines (42 loc) · 1.16 KB
/
remote_gcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package tfstate
import (
"context"
"encoding/base64"
"io"
"path"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
func readGCSState(ctx context.Context, config map[string]interface{}, ws string) (io.ReadCloser, error) {
bucket := *strp(config["bucket"])
prefix := *strpe(config["prefix"])
credentials := *strpe(config["credentials"])
encryption_key := *strpe(config["encryption_key"])
key := path.Join(prefix, ws+".tfstate")
return readGCS(ctx, bucket, key, credentials, encryption_key)
}
func readGCS(ctx context.Context, bucket, key, credentials, encryption_key string) (io.ReadCloser, error) {
var err error
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)
var r *storage.Reader
if encryption_key != "" {
decodedKey, _ := base64.StdEncoding.DecodeString(encryption_key)
r, err = obj.Key(decodedKey).NewReader(ctx)
} else {
r, err = obj.NewReader(ctx)
}
if err != nil {
return nil, err
}
return r, nil
}