forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exposed_endpoint.go
109 lines (89 loc) · 2.96 KB
/
exposed_endpoint.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
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// ExposedEndpointArgs is an argument struct used to create a new internal
// exposedEndpoint type that supports the ExposedEndpoint interface.
type ExposedEndpointArgs struct {
ExposeToSpaceIDs []string
ExposeToCIDRs []string
}
type exposedEndpoint struct {
Version int `yaml:"version"`
ExposeToSpaceIDs_ []string `yaml:"expose-to-spaces,omitempty"`
ExposeToCIDRs_ []string `yaml:"expose-to-cidrs,omitempty"`
}
func newExposedEndpoint(args ExposedEndpointArgs) *exposedEndpoint {
return &exposedEndpoint{
Version: 1,
ExposeToSpaceIDs_: args.ExposeToSpaceIDs,
ExposeToCIDRs_: args.ExposeToCIDRs,
}
}
// ExposeToSpaceIDs implements ExposedEndpoint.
func (exp *exposedEndpoint) ExposeToSpaceIDs() []string {
return exp.ExposeToSpaceIDs_
}
// ExposeToCIDRs implements ExposedEndpoint.
func (exp *exposedEndpoint) ExposeToCIDRs() []string {
return exp.ExposeToCIDRs_
}
func importExposedEndpointsMap(sourceMap map[string]interface{}) (map[string]*exposedEndpoint, error) {
result := make(map[string]*exposedEndpoint)
for key, value := range sourceMap {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for exposed endpoint %q, %T", key, value)
}
exp, err := importExposedEndpoint(source)
if err != nil {
return nil, errors.Trace(err)
}
result[key] = exp
}
return result, nil
}
func importExposedEndpoint(source map[string]interface{}) (*exposedEndpoint, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "expose Endpoint version schema check failed")
}
importFunc, ok := exposedEndpointDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type exposedEndpointDeserializationFunc func(map[string]interface{}) (*exposedEndpoint, error)
var exposedEndpointDeserializationFuncs = map[int]exposedEndpointDeserializationFunc{
1: importExposedEndpointV1,
}
func importExposedEndpointV1(source map[string]interface{}) (*exposedEndpoint, error) {
fields := schema.Fields{
"expose-to-spaces": schema.List(schema.String()),
"expose-to-cidrs": schema.List(schema.String()),
}
defaults := schema.Defaults{
"expose-to-spaces": schema.Omit,
"expose-to-cidrs": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "exposedEndpoint v1 schema check failed")
}
valid := coerced.(map[string]interface{})
res := &exposedEndpoint{
Version: 1,
}
if exposeToSpaceIDs, ok := valid["expose-to-spaces"]; ok {
res.ExposeToSpaceIDs_ = convertToStringSlice(exposeToSpaceIDs)
}
if exposeToCIDRs, ok := valid["expose-to-cidrs"]; ok {
res.ExposeToCIDRs_ = convertToStringSlice(exposeToCIDRs)
}
return res, nil
}