/
updater_rackspace.go
77 lines (62 loc) · 1.77 KB
/
updater_rackspace.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
package dropship
import (
"errors"
"io"
"github.com/ncw/swift"
)
var (
ErrUnableToConnect = errors.New("RackspaceUpdater: unable to connect to rackspace.")
)
type RackspaceUpdater struct {
conn *swift.Connection
}
func NewRackspaceUpdater(config map[string]string) *RackspaceUpdater {
return &RackspaceUpdater{
conn: &swift.Connection{
// This should be your username
UserName: config["user"],
// This should be your api key
ApiKey: config["key"],
// This should be a v1 auth url, eg
// Rackspace US https://auth.api.rackspacecloud.com/v1.0
// Rackspace UK https://lon.auth.api.rackspacecloud.com/v1.0
// Memset Memstore UK https://auth.storage.memset.com/v1.0
AuthUrl: "https://auth.api.rackspacecloud.com/v1.0",
// Region to use - default is use first region if unset
Region: config["region"],
// Name of the tenant - this is likely your username
},
}
}
func (u *RackspaceUpdater) IsOutdated(hash string, opts Artifact) (bool, error) {
if u.conn == nil {
return false, ErrUnableToConnect
}
if _, ok := opts["bucket"]; !ok {
return false, errors.New("Missing field: \"bucket\"")
}
if _, ok := opts["path"]; !ok {
return false, errors.New("Missing field: \"path\"")
}
info, _, err := u.conn.Object(opts["bucket"], opts["path"])
if err != nil {
return false, err
}
if info.Hash == hash {
return false, nil
}
return true, nil
}
func (u *RackspaceUpdater) Download(opt Artifact) (io.ReadCloser, MetaData, error) {
var meta MetaData
if u.conn == nil {
return nil, meta, ErrUnableToConnect
}
r, hdrs, err := u.conn.ObjectOpen(opt["bucket"], opt["path"], true, swift.Headers{})
if err != nil {
return nil, meta, err
}
meta.Hash = hdrs["Etag"]
meta.ContentType = hdrs["Content-Type"]
return r, meta, nil
}