forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
186 lines (154 loc) · 4.53 KB
/
client.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
176
177
178
179
180
181
182
183
184
185
186
package gcs
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"cloud.google.com/go/storage"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/state/remote"
"golang.org/x/net/context"
)
// remoteClient is used by "state/remote".State to read and write
// blobs representing state.
// Implements "state/remote".ClientLocker
type remoteClient struct {
storageContext context.Context
storageClient *storage.Client
bucketName string
stateFilePath string
lockFilePath string
encryptionKey []byte
}
func (c *remoteClient) Get() (payload *remote.Payload, err error) {
stateFileReader, err := c.stateFile().NewReader(c.storageContext)
if err != nil {
if err == storage.ErrObjectNotExist {
return nil, nil
} else {
return nil, fmt.Errorf("Failed to open state file at %v: %v", c.stateFileURL(), err)
}
}
defer stateFileReader.Close()
stateFileContents, err := ioutil.ReadAll(stateFileReader)
if err != nil {
return nil, fmt.Errorf("Failed to read state file from %v: %v", c.stateFileURL(), err)
}
stateFileAttrs, err := c.stateFile().Attrs(c.storageContext)
if err != nil {
return nil, fmt.Errorf("Failed to read state file attrs from %v: %v", c.stateFileURL(), err)
}
result := &remote.Payload{
Data: stateFileContents,
MD5: stateFileAttrs.MD5,
}
return result, nil
}
func (c *remoteClient) Put(data []byte) error {
err := func() error {
stateFileWriter := c.stateFile().NewWriter(c.storageContext)
if _, err := stateFileWriter.Write(data); err != nil {
return err
}
return stateFileWriter.Close()
}()
if err != nil {
return fmt.Errorf("Failed to upload state to %v: %v", c.stateFileURL(), err)
}
return nil
}
func (c *remoteClient) Delete() error {
if err := c.stateFile().Delete(c.storageContext); err != nil {
return fmt.Errorf("Failed to delete state file %v: %v", c.stateFileURL(), err)
}
return nil
}
// Lock writes to a lock file, ensuring file creation. Returns the generation
// number, which must be passed to Unlock().
func (c *remoteClient) Lock(info *state.LockInfo) (string, error) {
// update the path we're using
// we can't set the ID until the info is written
info.Path = c.lockFileURL()
infoJson, err := json.Marshal(info)
if err != nil {
return "", err
}
lockFile := c.lockFile()
w := lockFile.If(storage.Conditions{DoesNotExist: true}).NewWriter(c.storageContext)
err = func() error {
if _, err := w.Write(infoJson); err != nil {
return err
}
return w.Close()
}()
if err != nil {
return "", c.lockError(fmt.Errorf("writing %q failed: %v", c.lockFileURL(), err))
}
info.ID = strconv.FormatInt(w.Attrs().Generation, 10)
return info.ID, nil
}
func (c *remoteClient) Unlock(id string) error {
gen, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return err
}
if err := c.lockFile().If(storage.Conditions{GenerationMatch: gen}).Delete(c.storageContext); err != nil {
return c.lockError(err)
}
return nil
}
func (c *remoteClient) lockError(err error) *state.LockError {
lockErr := &state.LockError{
Err: err,
}
info, infoErr := c.lockInfo()
if infoErr != nil {
lockErr.Err = multierror.Append(lockErr.Err, infoErr)
} else {
lockErr.Info = info
}
return lockErr
}
// lockInfo reads the lock file, parses its contents and returns the parsed
// LockInfo struct.
func (c *remoteClient) lockInfo() (*state.LockInfo, error) {
r, err := c.lockFile().NewReader(c.storageContext)
if err != nil {
return nil, err
}
defer r.Close()
rawData, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
info := &state.LockInfo{}
if err := json.Unmarshal(rawData, info); err != nil {
return nil, err
}
// We use the Generation as the ID, so overwrite the ID in the json.
// This can't be written into the Info, since the generation isn't known
// until it's written.
attrs, err := c.lockFile().Attrs(c.storageContext)
if err != nil {
return nil, err
}
info.ID = strconv.FormatInt(attrs.Generation, 10)
return info, nil
}
func (c *remoteClient) stateFile() *storage.ObjectHandle {
h := c.storageClient.Bucket(c.bucketName).Object(c.stateFilePath)
if len(c.encryptionKey) > 0 {
return h.Key(c.encryptionKey)
}
return h
}
func (c *remoteClient) stateFileURL() string {
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.stateFilePath)
}
func (c *remoteClient) lockFile() *storage.ObjectHandle {
return c.storageClient.Bucket(c.bucketName).Object(c.lockFilePath)
}
func (c *remoteClient) lockFileURL() string {
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.lockFilePath)
}