forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opfactory.go
71 lines (60 loc) · 2.1 KB
/
opfactory.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package spaces
import (
"github.com/juju/errors"
"github.com/DavinZhang/juju/apiserver/common/networkingcommon"
"github.com/DavinZhang/juju/state"
)
// OpFactory describes a source of model operations
// required by the spaces API.
type OpFactory interface {
// NewRemoveSpaceOp returns an operation for removing a space.
NewRemoveSpaceOp(fromName string) (state.ModelOperation, error)
// NewRenameSpaceOp returns an operation for renaming a space.
NewRenameSpaceOp(spaceName, toName string) (state.ModelOperation, error)
// NewMoveSubnetsOp returns an operation for updating a space with new CIDRs.
NewMoveSubnetsOp(spaceID string, subnets []MovingSubnet) (MoveSubnetsOp, error)
}
type opFactory struct {
st *state.State
}
func newOpFactory(st *state.State) OpFactory {
return &opFactory{st: st}
}
// NewRemoveSpaceOp (OpFactory) returns an operation
// for removing a space.
func (f *opFactory) NewRemoveSpaceOp(spaceName string) (state.ModelOperation, error) {
space, err := f.st.SpaceByName(spaceName)
if err != nil {
return nil, errors.Trace(err)
}
subnets, err := space.Subnets()
if err != nil {
return nil, errors.Trace(err)
}
n := make([]Subnet, len(subnets))
for i, subnet := range subnets {
n[i] = subnet
}
return NewRemoveSpaceOp(space, n), nil
}
// NewRenameSpaceOp (OpFactory) returns an operation
// for renaming a space.
func (f *opFactory) NewRenameSpaceOp(fromName, toName string) (state.ModelOperation, error) {
space, err := f.st.SpaceByName(fromName)
if err != nil {
return nil, errors.Trace(err)
}
return NewRenameSpaceOp(
f.st.IsController(), f.st.NewControllerSettings(), &renameSpaceState{f.st}, space, toName), nil
}
// NewMoveSubnetsOp (OpFactory) returns an operation
// to move a list of subnets to a space.
func (f *opFactory) NewMoveSubnetsOp(spaceName string, subnets []MovingSubnet) (MoveSubnetsOp, error) {
space, err := f.st.SpaceByName(spaceName)
if err != nil {
return nil, errors.Trace(err)
}
return NewMoveSubnetsOp(networkingcommon.NewSpaceShim(space), subnets), nil
}