LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package common | 4 package common |
5 | 5 |
6 import ( | 6 import ( |
7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
8 "launchpad.net/juju-core/state/api/params" | 8 "launchpad.net/juju-core/state/api/params" |
9 ) | 9 ) |
10 | 10 |
11 // DeadEnsurer implements a common EnsureDead method for use by | 11 // DeadEnsurer implements a common EnsureDead method for use by |
12 // various facades. | 12 // various facades. |
13 type DeadEnsurer struct { | 13 type DeadEnsurer struct { |
14 » st DeadEnsurerer | 14 » st state.EntityFinder |
15 getCanModify GetAuthFunc | 15 getCanModify GetAuthFunc |
16 } | |
17 | |
18 type DeadEnsurerer interface { | |
19 // state.State implements DeadEnsurer to provide ways for us to | |
20 // call object.EnsureDead (for machines, units, etc). This is used | |
21 // to allow us to test with mocks without having to actually bring | |
22 // up state. | |
23 DeadEnsurer(tag string) (state.DeadEnsurer, error) | |
24 } | 16 } |
25 | 17 |
26 // NewDeadEnsurer returns a new DeadEnsurer. The GetAuthFunc will be | 18 // NewDeadEnsurer returns a new DeadEnsurer. The GetAuthFunc will be |
27 // used on each invocation of EnsureDead to determine current | 19 // used on each invocation of EnsureDead to determine current |
28 // permissions. | 20 // permissions. |
29 func NewDeadEnsurer(st DeadEnsurerer, getCanModify GetAuthFunc) *DeadEnsurer { | 21 func NewDeadEnsurer(st state.EntityFinder, getCanModify GetAuthFunc) *DeadEnsure
r { |
30 return &DeadEnsurer{ | 22 return &DeadEnsurer{ |
31 st: st, | 23 st: st, |
32 getCanModify: getCanModify, | 24 getCanModify: getCanModify, |
33 } | 25 } |
34 } | 26 } |
35 | 27 |
36 func (d *DeadEnsurer) ensureEntityDead(tag string) error { | 28 func (d *DeadEnsurer) ensureEntityDead(tag string) error { |
37 » deadEnsurer, err := d.st.DeadEnsurer(tag) | 29 » entity0, err := d.st.FindEntity(tag) |
38 if err != nil { | 30 if err != nil { |
39 return err | 31 return err |
40 } | 32 } |
41 » return deadEnsurer.EnsureDead() | 33 » entity, ok := entity0.(state.EnsureDeader) |
| 34 » if !ok { |
| 35 » » return NotSupportedError(tag, "ensuring death") |
| 36 » } |
| 37 » return entity.EnsureDead() |
42 } | 38 } |
43 | 39 |
44 // EnsureDead calls EnsureDead on each given entity from state. It | 40 // EnsureDead calls EnsureDead on each given entity from state. It |
45 // will fail if the entity is not present. If it's Alive, nothing will | 41 // will fail if the entity is not present. If it's Alive, nothing will |
46 // happen (see state/EnsureDead() for units or machines). | 42 // happen (see state/EnsureDead() for units or machines). |
47 func (d *DeadEnsurer) EnsureDead(args params.Entities) (params.ErrorResults, err
or) { | 43 func (d *DeadEnsurer) EnsureDead(args params.Entities) (params.ErrorResults, err
or) { |
48 result := params.ErrorResults{ | 44 result := params.ErrorResults{ |
49 Results: make([]params.ErrorResult, len(args.Entities)), | 45 Results: make([]params.ErrorResult, len(args.Entities)), |
50 } | 46 } |
51 if len(args.Entities) == 0 { | 47 if len(args.Entities) == 0 { |
52 return result, nil | 48 return result, nil |
53 } | 49 } |
54 canModify, err := d.getCanModify() | 50 canModify, err := d.getCanModify() |
55 if err != nil { | 51 if err != nil { |
56 return params.ErrorResults{}, err | 52 return params.ErrorResults{}, err |
57 } | 53 } |
58 for i, entity := range args.Entities { | 54 for i, entity := range args.Entities { |
59 err := ErrPerm | 55 err := ErrPerm |
60 if canModify(entity.Tag) { | 56 if canModify(entity.Tag) { |
61 err = d.ensureEntityDead(entity.Tag) | 57 err = d.ensureEntityDead(entity.Tag) |
62 } | 58 } |
63 result.Results[i].Error = ServerError(err) | 59 result.Results[i].Error = ServerError(err) |
64 } | 60 } |
65 return result, nil | 61 return result, nil |
66 } | 62 } |
LEFT | RIGHT |