forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
201 lines (162 loc) · 4.96 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package manta
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"path"
"strings"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/state/remote"
"github.com/joyent/triton-go/storage"
)
const (
mantaDefaultRootStore = "/stor"
lockFileName = "tflock"
)
type RemoteClient struct {
storageClient *storage.StorageClient
directoryName string
keyName string
statePath string
}
func (c *RemoteClient) Get() (*remote.Payload, error) {
output, err := c.storageClient.Objects().Get(context.Background(), &storage.GetObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, c.keyName),
})
if err != nil {
if strings.Contains(err.Error(), "ResourceNotFound") {
return nil, nil
}
return nil, err
}
defer output.ObjectReader.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, output.ObjectReader); err != nil {
return nil, fmt.Errorf("Failed to read remote state: %s", err)
}
payload := &remote.Payload{
Data: buf.Bytes(),
}
// If there was no data, then return nil
if len(payload.Data) == 0 {
return nil, nil
}
return payload, nil
}
func (c *RemoteClient) Put(data []byte) error {
contentType := "application/json"
contentLength := int64(len(data))
params := &storage.PutObjectInput{
ContentType: contentType,
ContentLength: uint64(contentLength),
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, c.keyName),
ObjectReader: bytes.NewReader(data),
}
log.Printf("[DEBUG] Uploading remote state to Manta: %#v", params)
err := c.storageClient.Objects().Put(context.Background(), params)
if err != nil {
return err
}
return nil
}
func (c *RemoteClient) Delete() error {
err := c.storageClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, c.keyName),
})
return err
}
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
//At Joyent, we want to make sure that the State directory exists before we interact with it
//We don't expect users to have to create it in advance
//The order of operations of Backend State as follows:
// * Get - if this doesn't exist then we continue as though it's new
// * Lock - we make sure that the state directory exists as it's the entrance to writing to Manta
// * Put - put the state up there
// * Unlock - unlock the directory
//We can always guarantee that the user can put their state in the specified location because of this
err := c.storageClient.Dir().Put(context.Background(), &storage.PutDirectoryInput{
DirectoryName: path.Join(mantaDefaultRootStore, c.directoryName),
})
if err != nil {
return "", err
}
//firstly we want to check that a lock doesn't already exist
lockErr := &state.LockError{}
lockInfo, err := c.getLockInfo()
if err != nil {
if !strings.Contains(err.Error(), "ResourceNotFound") {
lockErr.Err = fmt.Errorf("failed to retrieve lock info: %s", err)
return "", lockErr
}
}
if lockInfo != nil {
lockErr := &state.LockError{
Err: fmt.Errorf("A lock is already acquired"),
Info: lockInfo,
}
return "", lockErr
}
info.Path = path.Join(c.directoryName, lockFileName)
if info.ID == "" {
lockID, err := uuid.GenerateUUID()
if err != nil {
return "", err
}
info.ID = lockID
}
data := info.Marshal()
contentType := "application/json"
contentLength := int64(len(data))
params := &storage.PutObjectInput{
ContentType: contentType,
ContentLength: uint64(contentLength),
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, lockFileName),
ObjectReader: bytes.NewReader(data),
}
log.Printf("[DEBUG] Creating manta state lock: %#v", params)
err = c.storageClient.Objects().Put(context.Background(), params)
if err != nil {
return "", err
}
return info.ID, nil
}
func (c *RemoteClient) Unlock(id string) error {
lockErr := &state.LockError{}
lockInfo, err := c.getLockInfo()
if err != nil {
lockErr.Err = fmt.Errorf("failed to retrieve lock info: %s", err)
return lockErr
}
lockErr.Info = lockInfo
if lockInfo.ID != id {
lockErr.Err = fmt.Errorf("lock id %q does not match existing lock", id)
return lockErr
}
err = c.storageClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, lockFileName),
})
return err
}
func (c *RemoteClient) getLockInfo() (*state.LockInfo, error) {
output, err := c.storageClient.Objects().Get(context.Background(), &storage.GetObjectInput{
ObjectPath: path.Join(mantaDefaultRootStore, c.directoryName, lockFileName),
})
if err != nil {
return nil, err
}
defer output.ObjectReader.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, output.ObjectReader); err != nil {
return nil, fmt.Errorf("Failed to read lock info: %s", err)
}
lockInfo := &state.LockInfo{}
err = json.Unmarshal(buf.Bytes(), lockInfo)
if err != nil {
return nil, err
}
return lockInfo, nil
}