forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs.go
175 lines (142 loc) · 4.16 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package remote
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"strings"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/terraform"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/googleapi"
"google.golang.org/api/storage/v1"
)
// accountFile represents the structure of the credentials JSON
type accountFile struct {
PrivateKeyId string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientId string `json:"client_id"`
}
func parseJSON(result interface{}, contents string) error {
r := strings.NewReader(contents)
dec := json.NewDecoder(r)
return dec.Decode(result)
}
type GCSClient struct {
bucket string
path string
clientStorage *storage.Service
context context.Context
}
func gcsFactory(conf map[string]string) (Client, error) {
var account accountFile
var client *http.Client
clientScopes := []string{
"https://www.googleapis.com/auth/devstorage.full_control",
}
bucketName, ok := conf["bucket"]
if !ok {
return nil, fmt.Errorf("missing 'bucket' configuration")
}
pathName, ok := conf["path"]
if !ok {
return nil, fmt.Errorf("missing 'path' configuration")
}
credentials, ok := conf["credentials"]
if !ok {
credentials = os.Getenv("GOOGLE_CREDENTIALS")
}
if credentials != "" {
contents, _, err := pathorcontents.Read(credentials)
if err != nil {
return nil, fmt.Errorf("Error loading credentials: %s", err)
}
// Assume account_file is a JSON string
if err := parseJSON(&account, contents); err != nil {
return nil, fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
}
// Get the token for use in our requests
log.Printf("[INFO] Requesting Google token...")
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
log.Printf("[INFO] -- Scopes: %s", clientScopes)
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
conf := jwt.Config{
Email: account.ClientEmail,
PrivateKey: []byte(account.PrivateKey),
Scopes: clientScopes,
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
client = conf.Client(oauth2.NoContext)
} else {
log.Printf("[INFO] Authenticating using DefaultClient")
err := error(nil)
client, err = google.DefaultClient(oauth2.NoContext, clientScopes...)
if err != nil {
return nil, err
}
}
versionString := terraform.Version
userAgent := fmt.Sprintf(
"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)
log.Printf("[INFO] Instantiating Google Storage Client...")
clientStorage, err := storage.New(client)
if err != nil {
return nil, err
}
clientStorage.UserAgent = userAgent
return &GCSClient{
clientStorage: clientStorage,
bucket: bucketName,
path: pathName,
}, nil
}
func (c *GCSClient) Get() (*Payload, error) {
// Read the object from bucket.
log.Printf("[INFO] Reading %s/%s", c.bucket, c.path)
resp, err := c.clientStorage.Objects.Get(c.bucket, c.path).Download()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[INFO] %s/%s not found", c.bucket, c.path)
return nil, nil
}
return nil, fmt.Errorf("[WARN] Error retrieving object %s/%s: %s", c.bucket, c.path, err)
}
defer resp.Body.Close()
var buf []byte
w := bytes.NewBuffer(buf)
n, err := io.Copy(w, resp.Body)
if err != nil {
log.Fatalf("[WARN] error buffering %q: %v", c.path, err)
}
log.Printf("[INFO] Downloaded %d bytes", n)
payload := &Payload{
Data: w.Bytes(),
}
// If there was no data, then return nil
if len(payload.Data) == 0 {
return nil, nil
}
return payload, nil
}
func (c *GCSClient) Put(data []byte) error {
log.Printf("[INFO] Writing %s/%s", c.bucket, c.path)
r := bytes.NewReader(data)
_, err := c.clientStorage.Objects.Insert(c.bucket, &storage.Object{Name: c.path}).Media(r).Do()
if err != nil {
return err
}
return nil
}
func (c *GCSClient) Delete() error {
log.Printf("[INFO] Deleting %s/%s", c.bucket, c.path)
err := c.clientStorage.Objects.Delete(c.bucket, c.path).Do()
return err
}