forked from TritonDataCenter/triton-terraform
/
config.go
80 lines (66 loc) · 1.59 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
package main
import (
"errors"
"github.com/joyent/gocommon/client"
"github.com/joyent/gosdc/cloudapi"
"github.com/joyent/gosign/auth"
"io/ioutil"
)
var (
// ErrNoKey is returned by init when .Key is blank
ErrNoKey = errors.New("key not set")
)
// Config manages state within the provider
type Config struct {
Account string `mapstructure:"account"`
Key string `mapstructure:"key"`
KeyID string `mapstructure:"key_id"`
URL string `mapstructure:"url"`
creds *auth.Credentials
}
func (c *Config) init() error {
if c.Key == "" {
return ErrNoKey
}
key, err := ExpandUser(c.Key)
if err != nil {
return err
}
c.Key = key
return nil
}
// Creds returns the credentials needed to connect to Triton
func (c *Config) Creds() (*auth.Credentials, error) {
if c.creds != nil {
return c.creds, nil
}
keyData, err := ioutil.ReadFile(c.Key)
if err != nil {
return nil, err
}
userauth, err := auth.NewAuth(c.Account, string(keyData), "rsa-sha256")
if err != nil {
return nil, err
}
// TODO: this is missing MantaKeyId (string) and MantaEndpoint (string). These
// will need to be added when Manta support is added to the provider.
c.creds = &auth.Credentials{
UserAuthentication: userauth,
SdcKeyId: c.KeyID,
SdcEndpoint: auth.Endpoint{URL: c.URL},
}
return c.creds, nil
}
// Cloud returns a configured cloudapi.Client instance
func (c *Config) Cloud() (*cloudapi.Client, error) {
creds, err := c.Creds()
if err != nil {
return nil, err
}
return cloudapi.New(client.NewClient(
c.URL,
cloudapi.DefaultAPIVersion,
creds,
&cloudapi.Logger,
)), nil
}