forked from juju/juju
/
provider.go
170 lines (145 loc) · 4.94 KB
/
provider.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2013 Joyent Inc.
// Licensed under the AGPLv3, see LICENCE file for details.
package joyent
import (
"log"
"github.com/joyent/gocommon/client"
joyenterrors "github.com/joyent/gocommon/errors"
"github.com/joyent/gosdc/cloudapi"
"github.com/joyent/gosign/auth"
"github.com/juju/errors"
"github.com/juju/jsonschema"
"github.com/juju/loggo"
"github.com/juju/juju/cloud"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/simplestreams"
)
var logger = loggo.GetLogger("juju.provider.joyent")
// TODO(ericsnow) gologWriter can go away once loggo.Logger has a GoLogger() method.
type gologWriter struct {
loggo.Logger
level loggo.Level
}
func newGoLogger() *log.Logger {
return log.New(&gologWriter{logger, loggo.TRACE}, "", 0)
}
func (w *gologWriter) Write(p []byte) (n int, err error) {
w.Logf(w.level, string(p))
return len(p), nil
}
type joyentProvider struct {
environProviderCredentials
}
var providerInstance = joyentProvider{}
var _ environs.EnvironProvider = providerInstance
var _ simplestreams.HasRegion = (*joyentEnviron)(nil)
// CloudSchema returns the schema used to validate input for add-cloud. Since
// this provider does not support custom clouds, this always returns nil.
func (p joyentProvider) CloudSchema() *jsonschema.Schema {
return nil
}
// Ping tests the connection to the cloud, to verify the endpoint is valid.
func (p joyentProvider) Ping(endpoint string) error {
return errors.NotImplementedf("Ping")
}
// PrepareConfig is part of the EnvironProvider interface.
func (p joyentProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) {
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
return args.Config, nil
}
const unauthorisedMessage = `
Please ensure the SSH access key you have specified is correct.
You can create or import an SSH key via the "Account Summary"
page in the Joyent console.`
// verifyCredentials issues a cheap, non-modifying request to Joyent to
// verify the configured credentials. If verification fails, a user-friendly
// error will be returned, and the original error will be logged at debug
// level.
var verifyCredentials = func(e *joyentEnviron) error {
creds, err := credentials(e.cloud)
if err != nil {
return err
}
httpClient := client.NewClient(e.cloud.Endpoint, cloudapi.DefaultAPIVersion, creds, nil)
apiClient := cloudapi.New(httpClient)
_, err = apiClient.CountMachines()
if err != nil {
logger.Debugf("joyent request failed: %v", err)
if joyenterrors.IsInvalidCredentials(err) || joyenterrors.IsNotAuthorized(err) {
return errors.New("authentication failed.\n" + unauthorisedMessage)
}
return err
}
return nil
}
func credentials(cloud environs.CloudSpec) (*auth.Credentials, error) {
credAttrs := cloud.Credential.Attributes()
sdcUser := credAttrs[credAttrSDCUser]
sdcKeyID := credAttrs[credAttrSDCKeyID]
privateKey := credAttrs[credAttrPrivateKey]
algorithm := credAttrs[credAttrAlgorithm]
if algorithm == "" {
algorithm = algorithmDefault
}
authentication, err := auth.NewAuth(sdcUser, privateKey, algorithm)
if err != nil {
return nil, errors.Errorf("cannot create credentials: %v", err)
}
return &auth.Credentials{
UserAuthentication: authentication,
SdcKeyId: sdcKeyID,
SdcEndpoint: auth.Endpoint{URL: cloud.Endpoint},
}, nil
}
func (joyentProvider) Open(args environs.OpenParams) (environs.Environ, error) {
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
env, err := newEnviron(args.Cloud, args.Config)
if err != nil {
return nil, err
}
return env, nil
}
func (joyentProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
newEcfg, err := validateConfig(cfg, old)
if err != nil {
return nil, errors.Errorf("invalid Joyent provider config: %v", err)
}
return cfg.Apply(newEcfg.attrs)
}
func GetProviderInstance() environs.EnvironProvider {
return providerInstance
}
// MetadataLookupParams returns parameters which are used to query image metadata to
// find matching image information.
func (p joyentProvider) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
if region == "" {
return nil, errors.Errorf("region must be specified")
}
return &simplestreams.MetadataLookupParams{
Region: region,
}, nil
}
func (p joyentProvider) newConfig(cfg *config.Config) (*environConfig, error) {
valid, err := p.Validate(cfg, nil)
if err != nil {
return nil, err
}
return &environConfig{valid, valid.UnknownAttrs()}, nil
}
func validateCloudSpec(spec environs.CloudSpec) error {
if err := spec.Validate(); err != nil {
return errors.Trace(err)
}
if spec.Credential == nil {
return errors.NotValidf("missing credential")
}
if authType := spec.Credential.AuthType(); authType != cloud.UserPassAuthType {
return errors.NotSupportedf("%q auth-type", authType)
}
return nil
}