/
shims.go
96 lines (83 loc) · 2.43 KB
/
shims.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package subnets
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/apiserver/common/networkingcommon"
"github.com/juju/juju/core/network"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
)
func NewStateShim(st *state.State) (*stateShim, error) {
m, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
return &stateShim{EnvironConfigGetter: stateenvirons.EnvironConfigGetter{Model: m},
State: st, modelTag: m.ModelTag()}, nil
}
// stateShim forwards and adapts state.State methods to Backing
// method.
type stateShim struct {
stateenvirons.EnvironConfigGetter
*state.State
modelTag names.ModelTag
}
func (s *stateShim) ModelTag() names.ModelTag {
return s.modelTag
}
func (s *stateShim) AllSubnets() ([]networkingcommon.BackingSubnet, error) {
results, err := s.State.AllSubnets()
if err != nil {
return nil, errors.Trace(err)
}
subnets := make([]networkingcommon.BackingSubnet, len(results))
for i, result := range results {
subnets[i] = result
}
return subnets, nil
}
func (s *stateShim) SubnetByCIDR(cidr string) (networkingcommon.BackingSubnet, error) {
result, err := s.State.SubnetByCIDR(cidr)
if err != nil {
return nil, errors.Trace(err)
}
return result, nil
}
// SubnetsByCIDR wraps each result of a call to state.SubnetsByCIDR
// in a subnet shim and returns the result.
func (s *stateShim) SubnetsByCIDR(cidr string) ([]networkingcommon.BackingSubnet, error) {
subnets, err := s.State.SubnetsByCIDR(cidr)
if err != nil {
return nil, errors.Trace(err)
}
if len(subnets) == 0 {
return nil, nil
}
result := make([]networkingcommon.BackingSubnet, len(subnets))
for i, subnet := range subnets {
result[i] = subnet
}
return result, nil
}
func (s *stateShim) AvailabilityZones() (network.AvailabilityZones, error) {
// TODO (hml) 2019-09-13
// now available... include.
// AvailabilityZones() is defined in the common.ZonedEnviron interface
return nil, nil
}
func (s *stateShim) SetAvailabilityZones(_ network.AvailabilityZones) error {
return nil
}
func (s *stateShim) AllSpaces() ([]networkingcommon.BackingSpace, error) {
results, err := s.State.AllSpaces()
if err != nil {
return nil, errors.Trace(err)
}
spaces := make([]networkingcommon.BackingSpace, len(results))
for i, result := range results {
spaces[i] = result
}
return spaces, nil
}