/
shim.go
133 lines (114 loc) · 3.36 KB
/
shim.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provisioner
import (
jujucharm "github.com/juju/charm/v12"
"github.com/juju/errors"
"github.com/juju/juju/network/containerizer"
"github.com/juju/juju/state"
)
// MachineShim implements Machine.
// It is required for use of the containerizer and to mock container
// creation tests here.
type MachineShim struct {
*state.Machine
}
// NewMachine wraps the given state.machine in a MachineShim.
func NewMachine(m *state.Machine) *MachineShim {
return &MachineShim{m}
}
// AllLinkLayerDevices implements Machine by wrapping each
// state.LinkLayerDevice reference in returned collection
// with the containerizer LinkLayerDevice implementation.
func (m *MachineShim) AllLinkLayerDevices() ([]containerizer.LinkLayerDevice, error) {
devs, err := m.Machine.AllLinkLayerDevices()
if err != nil {
return nil, errors.Trace(err)
}
wrapped := make([]containerizer.LinkLayerDevice, len(devs))
for i, d := range devs {
wrapped[i] = containerizer.NewLinkLayerDevice(d)
}
return wrapped, nil
}
// AllDeviceAddresses implements Machine by wrapping each
// state.Address reference in returned collection with
// the containerizer Address implementation.
func (m *MachineShim) AllDeviceAddresses() ([]containerizer.Address, error) {
addrs, err := m.Machine.AllDeviceAddresses()
if err != nil {
return nil, errors.Trace(err)
}
wrapped := make([]containerizer.Address, len(addrs))
for i, a := range addrs {
wrapped[i] = containerizer.NewAddress(a)
}
return wrapped, nil
}
// Raw returns the inner state.Machine reference.
func (m *MachineShim) Raw() *state.Machine {
return m.Machine
}
// Units implements Machine by wrapping each
// state.Unit reference in returned collection with
// the Unit implementation.
func (m *MachineShim) Units() ([]Unit, error) {
units, err := m.Machine.Units()
if err != nil {
return nil, errors.Trace(err)
}
wrapped := make([]Unit, len(units))
for i, u := range units {
wrapped[i] = &unitShim{u}
}
return wrapped, nil
}
// unitShim implements Unit.
// It is required to mock the return of Units from MachineShim.
type unitShim struct {
*state.Unit
}
var _ Unit = (*unitShim)(nil)
// Application implements Unit by wrapping each
// state.Application reference in returned collection with
// the Application implementation.
func (u *unitShim) Application() (Application, error) {
app, err := u.Unit.Application()
if err != nil {
return nil, errors.Trace(err)
}
return &applicationShim{app}, nil
}
// applicationShim implements Application.
// It is required to mock the return a Charm from applicationShim.
type applicationShim struct {
*state.Application
}
var _ Application = (*applicationShim)(nil)
// Charm implements Application by wrapping each
// state.Charm reference in returned collection with
// the Charm implementation.
func (a *applicationShim) Charm() (Charm, bool, error) {
charm, ok, err := a.Application.Charm()
if err != nil {
return nil, ok, errors.Trace(err)
}
newCharm := &charmShim{
Charm: charm,
}
return newCharm, ok, nil
}
type charmShim struct {
*state.Charm
}
func (s *charmShim) LXDProfile() *jujucharm.LXDProfile {
profile := s.Charm.LXDProfile()
if profile == nil {
return nil
}
return &jujucharm.LXDProfile{
Description: profile.Description,
Config: profile.Config,
Devices: profile.Devices,
}
}