-
Notifications
You must be signed in to change notification settings - Fork 23
/
port_ranges.go
199 lines (168 loc) · 5.48 KB
/
port_ranges.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// PortRanges represents a collection of port ranges that are open.
type PortRanges interface {
ByUnit() map[string]UnitPortRanges
}
// UnitPortRanges represents the of port ranges opened by a particular Unit for
// one or more endpoints.
type UnitPortRanges interface {
ByEndpoint() map[string][]UnitPortRange
}
// UnitPortRange represents a contiguous port range opened by a unit.
type UnitPortRange interface {
FromPort() int
ToPort() int
Protocol() string
}
// OpenedPortRangeArgs is an argument struct used to add a new port range to
// a machine.
type OpenedPortRangeArgs struct {
UnitName string
EndpointName string
FromPort int
ToPort int
Protocol string
}
type deployedPortRanges struct {
Version int `yaml:"version"`
// The set of opened port ranges by unit.
ByUnit_ map[string]*unitPortRanges `yaml:"machine-port-ranges"`
}
func newDeployedPortRanges() *deployedPortRanges {
return &deployedPortRanges{
Version: 1,
ByUnit_: make(map[string]*unitPortRanges),
}
}
// ByUnit implements deployedPortRanges.
func (p *deployedPortRanges) ByUnit() map[string]UnitPortRanges {
res := make(map[string]UnitPortRanges, len(p.ByUnit_))
for unitName, upr := range p.ByUnit_ {
res[unitName] = upr
}
return res
}
type unitPortRanges struct {
// The set of opened port ranges for each endpoint.
ByEndpoint_ map[string][]*unitPortRange `yaml:"unit-port-ranges"`
}
func newUnitPortRanges() *unitPortRanges {
return &unitPortRanges{
ByEndpoint_: make(map[string][]*unitPortRange),
}
}
// ByEndpoint implements UnitPortRanges.
func (p *unitPortRanges) ByEndpoint() map[string][]UnitPortRange {
res := make(map[string][]UnitPortRange, len(p.ByEndpoint_))
for endpointName, portRanges := range p.ByEndpoint_ {
rangeList := make([]UnitPortRange, len(portRanges))
for i, portRange := range portRanges {
rangeList[i] = portRange
}
res[endpointName] = rangeList
}
return res
}
type unitPortRange struct {
FromPort_ int `yaml:"from-port"`
ToPort_ int `yaml:"to-port"`
Protocol_ string `yaml:"protocol"`
}
func newUnitPortRange(fromPort, toPort int, protocol string) *unitPortRange {
return &unitPortRange{
FromPort_: fromPort,
ToPort_: toPort,
Protocol_: protocol,
}
}
// FromPort implements UnitPortRange.
func (p unitPortRange) FromPort() int {
return p.FromPort_
}
// ToPort implements UnitPortRange.
func (p unitPortRange) ToPort() int {
return p.ToPort_
}
// Protocol implements UnitPortRange.
func (p unitPortRange) Protocol() string {
return p.Protocol_
}
func importMachinePortRanges(source map[string]interface{}) (*deployedPortRanges, error) {
checker := versionedMapChecker("machine-port-ranges")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "machine-port-ranges version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := machinePortRangeDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceMap := valid["machine-port-ranges"].(map[string]interface{})
return importFunc(sourceMap)
}
type machinePortRangeDeserializationFunc func(map[string]interface{}) (*deployedPortRanges, error)
var machinePortRangeDeserializationFuncs = map[int]machinePortRangeDeserializationFunc{
1: importMachinePortRangeV1,
}
func importMachinePortRangeV1(source map[string]interface{}) (*deployedPortRanges, error) {
unitChecker := schema.FieldMap(schema.Fields{
"unit-port-ranges": schema.StringMap(
schema.List(schema.StringMap(schema.Any())),
),
}, nil) // no defaults
portRangeChecker := schema.FieldMap(schema.Fields{
"from-port": schema.Int(),
"to-port": schema.Int(),
"protocol": schema.String(),
}, nil) // no defaults
mpr := &deployedPortRanges{
Version: 1,
ByUnit_: make(map[string]*unitPortRanges),
}
for unitName, unitSource := range source {
coercedUnit, err := unitChecker.Coerce(unitSource, nil)
if err != nil {
return nil, errors.Annotatef(err, "machine-port-range v1 schema check failed")
}
validFieldMap := coercedUnit.(map[string]interface{})
validEndpointMap := validFieldMap["unit-port-ranges"].(map[string]interface{})
upr := &unitPortRanges{
ByEndpoint_: make(map[string][]*unitPortRange),
}
for endpointName, portRangeListSource := range validEndpointMap {
validPortRangeListSource := portRangeListSource.([]interface{})
portRangeList := make([]*unitPortRange, len(validPortRangeListSource))
for i, portRangeSource := range validPortRangeListSource {
validPortRangeSource := portRangeSource.(map[string]interface{})
pr, err := importUnitPortRangeV1(validPortRangeSource, portRangeChecker)
if err != nil {
return nil, errors.Annotatef(err, "unable to parse V1 unit port range")
}
portRangeList[i] = pr
}
upr.ByEndpoint_[endpointName] = portRangeList
}
mpr.ByUnit_[unitName] = upr
}
return mpr, nil
}
func importUnitPortRangeV1(source map[string]interface{}, checker schema.Checker) (*unitPortRange, error) {
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "unit-port-range v1 schema check failed")
}
valid := coerced.(map[string]interface{})
return &unitPortRange{
FromPort_: int(valid["from-port"].(int64)),
ToPort_: int(valid["to-port"].(int64)),
Protocol_: valid["protocol"].(string),
}, nil
}