/
environprovider.go
164 lines (145 loc) · 4.9 KB
/
environprovider.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package maas
import (
stdcontext "context"
"fmt"
"net/url"
"github.com/juju/errors"
"github.com/juju/gomaasapi/v2"
"github.com/juju/jsonschema"
"github.com/juju/loggo"
"github.com/juju/juju/cloud"
"github.com/juju/juju/environs"
environscloudspec "github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
)
var cloudSchema = &jsonschema.Schema{
Type: []jsonschema.Type{jsonschema.ObjectType},
Required: []string{cloud.EndpointKey, cloud.AuthTypesKey},
// Order doesn't matter since there's only one thing to ask about. Add
// order if this changes.
Properties: map[string]*jsonschema.Schema{
cloud.AuthTypesKey: {
// don't need a prompt, since there's only one choice.
Type: []jsonschema.Type{jsonschema.ArrayType},
Enum: []interface{}{[]string{string(cloud.OAuth1AuthType)}},
},
cloud.EndpointKey: {
Singular: "the API endpoint url",
Type: []jsonschema.Type{jsonschema.StringType},
Format: jsonschema.FormatURI,
},
},
}
// Logger for the MAAS provider.
var logger = loggo.GetLogger("juju.provider.maas")
type EnvironProvider struct {
environProviderCredentials
// GetCapabilities is a function that connects to MAAS to return its set of
// capabilities.
GetCapabilities Capabilities
}
var _ environs.EnvironProvider = (*EnvironProvider)(nil)
var providerInstance EnvironProvider
// Version is part of the EnvironProvider interface.
func (EnvironProvider) Version() int {
return 0
}
func (EnvironProvider) Open(_ stdcontext.Context, args environs.OpenParams) (environs.Environ, error) {
logger.Debugf("opening model %q.", args.Config.Name())
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
env, err := NewEnviron(args.Cloud, args.Config, nil)
if err != nil {
return nil, errors.Annotate(err, "creating MAAS environ")
}
return env, nil
}
// CloudSchema returns the schema for adding new clouds of this type.
func (p EnvironProvider) CloudSchema() *jsonschema.Schema {
return cloudSchema
}
// Ping tests the connection to the cloud, to verify the endpoint is valid.
func (p EnvironProvider) Ping(ctx context.ProviderCallContext, endpoint string) error {
var err error
base, version, includesVersion := gomaasapi.SplitVersionedURL(endpoint)
if includesVersion {
err = p.checkMaas(base, version)
if err == nil {
return nil
}
} else {
// No version info in the endpoint - try both in preference order.
err = p.checkMaas(endpoint, apiVersion2)
if err == nil {
return nil
}
}
return errors.Annotatef(err, "No MAAS server running at %s", endpoint)
}
func (p EnvironProvider) checkMaas(endpoint, ver string) error {
c, err := gomaasapi.NewAnonymousClient(endpoint, ver)
if err != nil {
logger.Debugf("Can't create maas API %s client for %q: %v", ver, endpoint, err)
return errors.Trace(err)
}
maas := gomaasapi.NewMAAS(*c)
_, err = p.GetCapabilities(maas, endpoint)
return errors.Trace(err)
}
// PrepareConfig is specified in the EnvironProvider interface.
func (p EnvironProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) {
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
var attrs map[string]interface{}
if _, ok := args.Config.StorageDefaultBlockSource(); !ok {
attrs = map[string]interface{}{
config.StorageDefaultBlockSourceKey: maasStorageProviderType,
}
}
if len(attrs) == 0 {
return args.Config, nil
}
return args.Config.Apply(attrs)
}
// DetectRegions is specified in the environs.CloudRegionDetector interface.
func (p EnvironProvider) DetectRegions() ([]cloud.Region, error) {
return nil, errors.NotFoundf("regions")
}
func validateCloudSpec(spec environscloudspec.CloudSpec) error {
if err := spec.Validate(); err != nil {
return errors.Trace(err)
}
if _, err := parseCloudEndpoint(spec.Endpoint); err != nil {
return errors.Annotate(err, "validating endpoint")
}
if spec.Credential == nil {
return errors.NotValidf("missing credential")
}
if authType := spec.Credential.AuthType(); authType != cloud.OAuth1AuthType {
return errors.NotSupportedf("%q auth-type", authType)
}
if _, err := parseOAuthToken(*spec.Credential); err != nil {
return errors.Annotate(err, "validating MAAS OAuth token")
}
return nil
}
func parseCloudEndpoint(endpoint string) (server string, _ error) {
// For MAAS, the cloud endpoint may be either a full URL
// for the MAAS server, or just the IP/host.
if endpoint == "" {
return "", errors.New("MAAS server not specified")
}
server = endpoint
if url, err := url.Parse(server); err != nil || url.Scheme == "" {
server = fmt.Sprintf("http://%s/MAAS", endpoint)
if _, err := url.Parse(server); err != nil {
return "", errors.NotValidf("endpoint %q", endpoint)
}
}
return server, nil
}