/
config.go
95 lines (89 loc) · 2.2 KB
/
config.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
package swift
import (
"errors"
"net/http"
"net/url"
"github.com/graymeta/stow"
"github.com/ncw/swift"
)
// Config key constants.
const (
ConfigUsername = "username"
ConfigKey = "key"
ConfigTenantName = "tenant_name"
ConfigTenantAuthURL = "tenant_auth_url"
)
// Kind is the kind of Location this package provides.
const Kind = "swift"
func init() {
validatefn := func(config stow.Config) error {
_, ok := config.Config(ConfigUsername)
if !ok {
return errors.New("missing account username")
}
_, ok = config.Config(ConfigKey)
if !ok {
return errors.New("missing api key")
}
_, ok = config.Config(ConfigTenantName)
if !ok {
return errors.New("missing tenant name")
}
_, ok = config.Config(ConfigTenantAuthURL)
if !ok {
return errors.New("missing tenant auth url")
}
return nil
}
makefn := func(config stow.Config) (stow.Location, error) {
_, ok := config.Config(ConfigUsername)
if !ok {
return nil, errors.New("missing account username")
}
_, ok = config.Config(ConfigKey)
if !ok {
return nil, errors.New("missing api key")
}
_, ok = config.Config(ConfigTenantName)
if !ok {
return nil, errors.New("missing tenant name")
}
_, ok = config.Config(ConfigTenantAuthURL)
if !ok {
return nil, errors.New("missing tenant auth url")
}
l := &location{
config: config,
}
var err error
l.client, err = newSwiftClient(l.config)
if err != nil {
return nil, err
}
return l, nil
}
kindfn := func(u *url.URL) bool {
return u.Scheme == Kind
}
stow.Register(Kind, makefn, kindfn, validatefn)
}
func newSwiftClient(cfg stow.Config) (*swift.Connection, error) {
username, _ := cfg.Config(ConfigUsername)
key, _ := cfg.Config(ConfigKey)
tenantName, _ := cfg.Config(ConfigTenantName)
tenantAuthURL, _ := cfg.Config(ConfigTenantAuthURL)
client := swift.Connection{
UserName: username,
ApiKey: key,
AuthUrl: tenantAuthURL,
//Domain: "domain", // Name of the domain (v3 auth only)
Tenant: tenantName, // Name of the tenant (v2 auth only)
// Add Default transport
Transport: http.DefaultTransport,
}
err := client.Authenticate()
if err != nil {
return nil, errors.New("Unable to authenticate")
}
return &client, nil
}