forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifactory.go
117 lines (102 loc) · 2.66 KB
/
artifactory.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
package remote
import (
"crypto/md5"
"fmt"
"os"
"strings"
artifactory "github.com/lusis/go-artifactory/src/artifactory.v401"
)
const ARTIF_TFSTATE_NAME = "terraform.tfstate"
func artifactoryFactory(conf map[string]string) (Client, error) {
userName, ok := conf["username"]
if !ok {
userName = os.Getenv("ARTIFACTORY_USERNAME")
if userName == "" {
return nil, fmt.Errorf(
"missing 'username' configuration or ARTIFACTORY_USERNAME environment variable")
}
}
password, ok := conf["password"]
if !ok {
password = os.Getenv("ARTIFACTORY_PASSWORD")
if password == "" {
return nil, fmt.Errorf(
"missing 'password' configuration or ARTIFACTORY_PASSWORD environment variable")
}
}
url, ok := conf["url"]
if !ok {
url = os.Getenv("ARTIFACTORY_URL")
if url == "" {
return nil, fmt.Errorf(
"missing 'url' configuration or ARTIFACTORY_URL environment variable")
}
}
repo, ok := conf["repo"]
if !ok {
return nil, fmt.Errorf(
"missing 'repo' configuration")
}
subpath, ok := conf["subpath"]
if !ok {
return nil, fmt.Errorf(
"missing 'subpath' configuration")
}
clientConf := &artifactory.ClientConfig{
BaseURL: url,
Username: userName,
Password: password,
}
nativeClient := artifactory.NewClient(clientConf)
return &ArtifactoryClient{
nativeClient: &nativeClient,
userName: userName,
password: password,
url: url,
repo: repo,
subpath: subpath,
}, nil
}
type ArtifactoryClient struct {
nativeClient *artifactory.ArtifactoryClient
userName string
password string
url string
repo string
subpath string
}
func (c *ArtifactoryClient) Get() (*Payload, error) {
p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
output, err := c.nativeClient.Get(p, make(map[string]string))
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, nil
}
return nil, err
}
// TODO: migrate to using X-Checksum-Md5 header from artifactory
// needs to be exposed by go-artifactory first
hash := md5.Sum(output)
payload := &Payload{
Data: output,
MD5: hash[:md5.Size],
}
// If there was no data, then return nil
if len(payload.Data) == 0 {
return nil, nil
}
return payload, nil
}
func (c *ArtifactoryClient) Put(data []byte) error {
p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
if _, err := c.nativeClient.Put(p, string(data), make(map[string]string)); err == nil {
return nil
} else {
return fmt.Errorf("Failed to upload state: %v", err)
}
}
func (c *ArtifactoryClient) Delete() error {
p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
err := c.nativeClient.Delete(p)
return err
}