forked from juju/juju
/
undertaker.go
51 lines (43 loc) · 1.35 KB
/
undertaker.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
)
var ErrModelNotDying = errors.New("model is not dying")
// ProcessDyingModel checks if the model is Dying and empty, and if so,
// transitions the model to Dead.
//
// If the model is non-empty because it is the controller model and still
// contains hosted models, an error satisfying IsHasHostedModelsError will
// be returned. If the model is otherwise non-empty, an error satisfying
// IsNonEmptyModelError will be returned.
func (st *State) ProcessDyingModel() (err error) {
model, err := st.Model()
if err != nil {
return errors.Trace(err)
}
if model.Life() != Dying {
return errors.Trace(ErrModelNotDying)
}
if st.IsController() {
// We should not mark the controller model as Dead until
// all hosted models have been removed, otherwise the
// hosted model environs may not have been destroyed.
modelUUIDs, err := st.AllModelUUIDsIncludingDead()
if err != nil {
return errors.Trace(err)
}
if n := len(modelUUIDs) - 1; n > 0 {
return errors.Trace(hasHostedModelsError(n))
}
}
modelEntityRefsDoc, err := model.getEntityRefs()
if err != nil {
return errors.Trace(err)
}
if _, err := checkModelEntityRefsEmpty(modelEntityRefsDoc); err != nil {
return errors.Trace(err)
}
return nil
}