forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externalcontroller.go
164 lines (139 loc) · 4.76 KB
/
externalcontroller.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 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/schema"
)
// ExternalController represents the state of a controller hosting
// other models.
type ExternalController interface {
ID() names.ControllerTag
Alias() string
Addrs() []string
CACert() string
Models() []string
}
type externalControllers struct {
Version int `yaml:"version"`
ExternalControllers []*externalController `yaml:"external-controllers"`
}
type externalController struct {
ID_ string `yaml:"id"`
Alias_ string `yaml:"alias,omitempty"`
Addrs_ []string `yaml:"addrs"`
CACert_ string `yaml:"ca-cert"`
Models_ []string `yaml:"models"`
}
// ExternalControllerArgs is an argument struct used to add a external
// controller to a model.
type ExternalControllerArgs struct {
Tag names.ControllerTag
Alias string
Addrs []string
CACert string
Models []string
}
func newExternalController(args ExternalControllerArgs) *externalController {
return &externalController{
ID_: args.Tag.Id(),
Alias_: args.Alias,
Addrs_: args.Addrs,
CACert_: args.CACert,
Models_: args.Models,
}
}
// ID returns the controller tag for the external controller.
func (e *externalController) ID() names.ControllerTag {
return names.NewControllerTag(e.ID_)
}
// Alias returns the controller name for the external controller.
func (e *externalController) Alias() string {
return e.Alias_
}
// Addrs returns the addresses for the external controller.
func (e *externalController) Addrs() []string {
return e.Addrs_
}
// CACert returns the ca cert for the external controller.
func (e *externalController) CACert() string {
return e.CACert_
}
// Models returns the list of models for the external controller.
func (e *externalController) Models() []string {
return e.Models_
}
func importExternalControllers(source interface{}) ([]*externalController, error) {
checker := versionedChecker("external-controllers")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "external controllers version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := externalControllerDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["external-controllers"].([]interface{})
return importExternalControllerList(sourceList, importFunc)
}
func importExternalControllerList(sourceList []interface{}, importFunc externalControllerDeserializationFunc) ([]*externalController, error) {
result := make([]*externalController, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for external controller %d, %T", i, value)
}
externalController, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "external controller %d", i)
}
result[i] = externalController
}
return result, nil
}
var externalControllerDeserializationFuncs = map[int]externalControllerDeserializationFunc{
1: importExternalControllerV1,
}
type externalControllerDeserializationFunc func(interface{}) (*externalController, error)
func externalControllerV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"id": schema.String(),
"alias": schema.String(),
"addrs": schema.List(schema.String()),
"ca-cert": schema.String(),
"models": schema.List(schema.String()),
}
defaults := schema.Defaults{
"alias": schema.Omit,
}
return fields, defaults
}
func importExternalController(fields schema.Fields, defaults schema.Defaults, importVersion int, source interface{}) (*externalController, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "external controller v%d schema check failed", importVersion)
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &externalController{
ID_: valid["id"].(string),
Addrs_: convertToStringSlice(valid["addrs"]),
CACert_: valid["ca-cert"].(string),
Models_: convertToStringSlice(valid["models"]),
}
// Alias is optional through out juju and because of that, it isn't a
// requirement of external controller migrations.
if alias, ok := valid["alias"]; ok {
result.Alias_ = alias.(string)
}
return result, nil
}
func importExternalControllerV1(source interface{}) (*externalController, error) {
fields, defaults := externalControllerV1Fields()
return importExternalController(fields, defaults, 1, source)
}