forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
234 lines (200 loc) · 6.31 KB
/
remove.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package spaces
import (
"github.com/juju/errors"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
apiservererrors "github.com/DavinZhang/juju/apiserver/errors"
"github.com/DavinZhang/juju/apiserver/params"
"github.com/DavinZhang/juju/controller"
"github.com/DavinZhang/juju/core/network"
"github.com/DavinZhang/juju/state"
)
// RemoveSpace describes a space that can be removed.
type RemoveSpace interface {
Refresh() error
RemoveSpaceOps() []txn.Op
}
type spaceRemoveModelOp struct {
space RemoveSpace
subnets []Subnet
}
type Subnet interface {
UpdateSpaceOps(spaceID string) []txn.Op
}
func (o *spaceRemoveModelOp) Done(err error) error {
return err
}
func NewRemoveSpaceOp(space RemoveSpace, subnets []Subnet) *spaceRemoveModelOp {
return &spaceRemoveModelOp{
space: space,
subnets: subnets,
}
}
func (o *spaceRemoveModelOp) Build(attempt int) ([]txn.Op, error) {
var totalOps []txn.Op
if attempt > 0 {
if err := o.space.Refresh(); err != nil {
return nil, errors.Trace(err)
}
}
for _, subnet := range o.subnets {
totalOps = append(totalOps, subnet.UpdateSpaceOps(network.AlphaSpaceId)...)
}
removeOps := o.space.RemoveSpaceOps()
totalOps = append(totalOps, removeOps...)
return totalOps, nil
}
// RemoveSpace removes a space.
// Returns SpaceResults if entities/settings are found which makes the deletion not possible.
func (api *API) RemoveSpace(spaceParams params.RemoveSpaceParams) (params.RemoveSpaceResults, error) {
result := params.RemoveSpaceResults{}
if err := api.ensureSpacesAreMutable(); err != nil {
return result, err
}
result.Results = make([]params.RemoveSpaceResult, len(spaceParams.SpaceParams))
for i, spaceParam := range spaceParams.SpaceParams {
spacesTag, err := names.ParseSpaceTag(spaceParam.Space.Tag)
if err != nil {
result.Results[i].Error = apiservererrors.ServerError(errors.Trace(err))
continue
}
if !api.checkSpaceIsRemovable(i, spacesTag, &result, spaceParam.Force) {
continue
}
operation, err := api.opFactory.NewRemoveSpaceOp(spacesTag.Id())
if err != nil {
result.Results[i].Error = apiservererrors.ServerError(errors.Trace(err))
continue
}
if spaceParam.DryRun {
continue
}
if err = api.backing.ApplyOperation(operation); err != nil {
result.Results[i].Error = apiservererrors.ServerError(errors.Trace(err))
continue
}
}
return result, nil
}
func (api *API) checkSpaceIsRemovable(
index int, spacesTag names.Tag, results *params.RemoveSpaceResults, force bool,
) bool {
removable := true
if spacesTag.Id() == network.AlphaSpaceName {
newErr := errors.New("the alpha space cannot be removed")
results.Results[index].Error = apiservererrors.ServerError(newErr)
return false
}
space, err := api.backing.SpaceByName(spacesTag.Id())
if err != nil {
results.Results[index].Error = apiservererrors.ServerError(errors.Trace(err))
return false
}
bindingTags, err := api.applicationTagsForSpace(space.Id())
if err != nil {
results.Results[index].Error = apiservererrors.ServerError(errors.Trace(err))
return false
}
constraintTags, err := api.entityTagsForSpaceConstraintsBlockingRemove(space.Name())
if err != nil {
results.Results[index].Error = apiservererrors.ServerError(errors.Trace(err))
return false
}
settingMatches, err := api.getSpaceControllerSettings(space.Name())
if err != nil {
results.Results[index].Error = apiservererrors.ServerError(errors.Trace(err))
return false
}
if force {
return true
}
if len(settingMatches) != 0 {
results.Results[index].ControllerSettings = settingMatches
removable = false
}
if len(bindingTags) != 0 {
results.Results[index].Bindings = convertTagsToEntities(bindingTags)
removable = false
}
if len(constraintTags) != 0 {
results.Results[index].Constraints = convertTagsToEntities(constraintTags)
removable = false
}
return removable
}
// applicationTagsForSpace returns the tags for all applications with an
// endpoint bound to a space with the input name.
func (api *API) applicationTagsForSpace(spaceID string) ([]names.Tag, error) {
applications, err := api.applicationsBoundToSpace(spaceID)
if err != nil {
return nil, errors.Trace(nil)
}
tags := make([]names.Tag, len(applications))
for i, app := range applications {
tags[i] = names.NewApplicationTag(app)
}
return tags, nil
}
func convertTagsToEntities(tags []names.Tag) []params.Entity {
entities := make([]params.Entity, len(tags))
for i, tag := range tags {
entities[i].Tag = tag.String()
}
return entities
}
// entityTagsForSpaceConstraintsBlockingRemove returns tags for entities
// with constraints for the input space name, that disallow removal of the
// space. I.e. those other than units and machines.
func (api *API) entityTagsForSpaceConstraintsBlockingRemove(spaceName string) ([]names.Tag, error) {
allTags, err := api.entityTagsForSpaceConstraints(spaceName)
if err != nil {
return nil, errors.Trace(err)
}
var notSkipping []names.Tag
for _, tag := range allTags {
if tag.Kind() == names.UnitTagKind {
continue
}
if tag.Kind() == names.MachineTagKind {
continue
}
notSkipping = append(notSkipping, tag)
}
return notSkipping, nil
}
// entityTagsForSpaceConstraints returns the tags for all entities
// with constraints that refer to the input space name.
func (api *API) entityTagsForSpaceConstraints(spaceName string) ([]names.Tag, error) {
cons, err := api.backing.ConstraintsBySpaceName(spaceName)
if err != nil {
return nil, errors.Trace(err)
}
tags := make([]names.Tag, len(cons))
for i, doc := range cons {
tag := state.TagFromDocID(doc.ID())
if tag == nil {
return nil, errors.Errorf("Could not parse id: %q", doc.ID())
}
tags[i] = tag
}
return tags, nil
}
func (api *API) getSpaceControllerSettings(spaceName string) ([]string, error) {
var matches []string
if !api.backing.IsController() {
return matches, nil
}
currentControllerConfig, err := api.backing.ControllerConfig()
if err != nil {
return matches, errors.Trace(err)
}
if mgmtSpace := currentControllerConfig.JujuManagementSpace(); mgmtSpace == spaceName {
matches = append(matches, controller.JujuManagementSpace)
}
if haSpace := currentControllerConfig.JujuHASpace(); haSpace == spaceName {
matches = append(matches, controller.JujuHASpace)
}
return matches, nil
}