-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.go
169 lines (155 loc) · 5 KB
/
cloud.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
"github.com/juju/utils/set"
"gopkg.in/juju/names.v2"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/cloud"
)
// cloudDoc records information about the cloud that the controller operates in.
type cloudDoc struct {
DocID string `bson:"_id"`
Name string `bson:"name"`
Type string `bson:"type"`
AuthTypes []string `bson:"auth-types"`
Endpoint string `bson:"endpoint"`
IdentityEndpoint string `bson:"identity-endpoint,omitempty"`
StorageEndpoint string `bson:"storage-endpoint,omitempty"`
Regions map[string]cloudRegionSubdoc `bson:"regions,omitempty"`
CACertificates []string `bson:"ca-certificates,omitempty"`
}
// cloudRegionSubdoc records information about cloud regions.
type cloudRegionSubdoc struct {
Endpoint string `bson:"endpoint,omitempty"`
IdentityEndpoint string `bson:"identity-endpoint,omitempty"`
StorageEndpoint string `bson:"storage-endpoint,omitempty"`
}
// createCloudOp returns a list of txn.Ops that will initialize
// the cloud definition for the controller.
func createCloudOp(cloud cloud.Cloud) txn.Op {
authTypes := make([]string, len(cloud.AuthTypes))
for i, authType := range cloud.AuthTypes {
authTypes[i] = string(authType)
}
regions := make(map[string]cloudRegionSubdoc)
for _, region := range cloud.Regions {
regions[region.Name] = cloudRegionSubdoc{
region.Endpoint,
region.IdentityEndpoint,
region.StorageEndpoint,
}
}
return txn.Op{
C: cloudsC,
Id: cloud.Name,
Assert: txn.DocMissing,
Insert: &cloudDoc{
Name: cloud.Name,
Type: cloud.Type,
AuthTypes: authTypes,
Endpoint: cloud.Endpoint,
IdentityEndpoint: cloud.IdentityEndpoint,
StorageEndpoint: cloud.StorageEndpoint,
Regions: regions,
CACertificates: cloud.CACertificates,
},
}
}
func (d cloudDoc) toCloud() cloud.Cloud {
authTypes := make([]cloud.AuthType, len(d.AuthTypes))
for i, authType := range d.AuthTypes {
authTypes[i] = cloud.AuthType(authType)
}
regionNames := make(set.Strings)
for name := range d.Regions {
regionNames.Add(name)
}
regions := make([]cloud.Region, len(d.Regions))
for i, name := range regionNames.SortedValues() {
region := d.Regions[name]
regions[i] = cloud.Region{
name,
region.Endpoint,
region.IdentityEndpoint,
region.StorageEndpoint,
}
}
return cloud.Cloud{
Name: d.Name,
Type: d.Type,
AuthTypes: authTypes,
Endpoint: d.Endpoint,
IdentityEndpoint: d.IdentityEndpoint,
StorageEndpoint: d.StorageEndpoint,
Regions: regions,
CACertificates: d.CACertificates,
}
}
// Clouds returns the definitions for all clouds in the controller.
func (st *State) Clouds() (map[names.CloudTag]cloud.Cloud, error) {
coll, cleanup := st.db().GetCollection(cloudsC)
defer cleanup()
var doc cloudDoc
clouds := make(map[names.CloudTag]cloud.Cloud)
iter := coll.Find(nil).Iter()
for iter.Next(&doc) {
clouds[names.NewCloudTag(doc.Name)] = doc.toCloud()
}
if err := iter.Err(); err != nil {
return nil, errors.Annotate(err, "getting clouds")
}
return clouds, nil
}
// Cloud returns the controller's cloud definition.
func (st *State) Cloud(name string) (cloud.Cloud, error) {
coll, cleanup := st.db().GetCollection(cloudsC)
defer cleanup()
var doc cloudDoc
err := coll.FindId(name).One(&doc)
if err == mgo.ErrNotFound {
return cloud.Cloud{}, errors.NotFoundf("cloud %q", name)
}
if err != nil {
return cloud.Cloud{}, errors.Annotatef(err, "cannot get cloud %q", name)
}
return doc.toCloud(), nil
}
// AddCloud creates a cloud with the given name and details.
// Note that the Config is deliberately ignored - it's only
// relevant when bootstrapping.
func (st *State) AddCloud(c cloud.Cloud) error {
if err := validateCloud(c); err != nil {
return errors.Annotate(err, "invalid cloud")
}
ops := []txn.Op{createCloudOp(c)}
if err := st.db().RunTransaction(ops); err != nil {
if err == txn.ErrAborted {
err = errors.AlreadyExistsf("cloud %q", c.Name)
}
return err
}
return nil
}
// validateCloud checks that the supplied cloud is valid.
func validateCloud(cloud cloud.Cloud) error {
if cloud.Name == "" {
return errors.NotValidf("empty Name")
}
if cloud.Type == "" {
return errors.NotValidf("empty Type")
}
if len(cloud.AuthTypes) == 0 {
return errors.NotValidf("empty auth-types")
}
// TODO(axw) we should ensure that the cloud auth-types is a subset
// of the auth-types supported by the provider. To do that, we'll
// need a new "policy".
return nil
}
// regionSettingsGlobalKey concatenates the cloud a hash and the region string.
func regionSettingsGlobalKey(cloud, region string) string {
return cloud + "#" + region
}