forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
115 lines (92 loc) · 2.81 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
package swift
import (
"bytes"
"crypto/md5"
"log"
"os"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
"github.com/hashicorp/terraform/state/remote"
)
const (
TFSTATE_NAME = "tfstate.tf"
TFSTATE_LOCK_NAME = "tfstate.lock"
)
// RemoteClient implements the Client interface for an Openstack Swift server.
type RemoteClient struct {
client *gophercloud.ServiceClient
container string
archive bool
archiveContainer string
expireSecs int
}
func (c *RemoteClient) Get() (*remote.Payload, error) {
log.Printf("[DEBUG] Getting object %s in container %s", TFSTATE_NAME, c.container)
result := objects.Download(c.client, c.container, TFSTATE_NAME, nil)
// Extract any errors from result
_, err := result.Extract()
// 404 response is to be expected if the object doesn't already exist!
if _, ok := err.(gophercloud.ErrDefault404); ok {
log.Println("[DEBUG] Object doesn't exist to download.")
return nil, nil
}
bytes, err := result.ExtractContent()
if err != nil {
return nil, err
}
hash := md5.Sum(bytes)
payload := &remote.Payload{
Data: bytes,
MD5: hash[:md5.Size],
}
return payload, nil
}
func (c *RemoteClient) Put(data []byte) error {
if err := c.ensureContainerExists(); err != nil {
return err
}
log.Printf("[DEBUG] Putting object %s in container %s", TFSTATE_NAME, c.container)
reader := bytes.NewReader(data)
createOpts := objects.CreateOpts{
Content: reader,
}
if c.expireSecs != 0 {
log.Printf("[DEBUG] ExpireSecs = %d", c.expireSecs)
createOpts.DeleteAfter = c.expireSecs
}
result := objects.Create(c.client, c.container, TFSTATE_NAME, createOpts)
return result.Err
}
func (c *RemoteClient) Delete() error {
log.Printf("[DEBUG] Deleting object %s in container %s", TFSTATE_NAME, c.container)
result := objects.Delete(c.client, c.container, TFSTATE_NAME, nil)
return result.Err
}
func (c *RemoteClient) ensureContainerExists() error {
containerOpts := &containers.CreateOpts{}
if c.archive {
log.Printf("[DEBUG] Creating archive container %s", c.archiveContainer)
result := containers.Create(c.client, c.archiveContainer, nil)
if result.Err != nil {
log.Printf("[DEBUG] Error creating archive container %s: %s", c.archiveContainer, result.Err)
return result.Err
}
log.Printf("[DEBUG] Enabling Versioning on container %s", c.container)
containerOpts.VersionsLocation = c.archiveContainer
}
log.Printf("[DEBUG] Creating container %s", c.container)
result := containers.Create(c.client, c.container, containerOpts)
if result.Err != nil {
return result.Err
}
return nil
}
func multiEnv(ks []string) string {
for _, k := range ks {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}