github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/ensuredead.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/state" 12 ) 13 14 // DeadEnsurer implements a common EnsureDead method for use by 15 // various facades. 16 type DeadEnsurer struct { 17 st state.EntityFinder 18 getCanModify GetAuthFunc 19 } 20 21 // NewDeadEnsurer returns a new DeadEnsurer. The GetAuthFunc will be 22 // used on each invocation of EnsureDead to determine current 23 // permissions. 24 func NewDeadEnsurer(st state.EntityFinder, getCanModify GetAuthFunc) *DeadEnsurer { 25 return &DeadEnsurer{ 26 st: st, 27 getCanModify: getCanModify, 28 } 29 } 30 31 func (d *DeadEnsurer) ensureEntityDead(tag names.Tag) error { 32 entity0, err := d.st.FindEntity(tag) 33 if err != nil { 34 return err 35 } 36 entity, ok := entity0.(state.EnsureDeader) 37 if !ok { 38 return NotSupportedError(tag, "ensuring death") 39 } 40 return entity.EnsureDead() 41 } 42 43 // EnsureDead calls EnsureDead on each given entity from state. It 44 // will fail if the entity is not present. If it's Alive, nothing will 45 // happen (see state/EnsureDead() for units or machines). 46 func (d *DeadEnsurer) EnsureDead(args params.Entities) (params.ErrorResults, error) { 47 result := params.ErrorResults{ 48 Results: make([]params.ErrorResult, len(args.Entities)), 49 } 50 if len(args.Entities) == 0 { 51 return result, nil 52 } 53 canModify, err := d.getCanModify() 54 if err != nil { 55 return params.ErrorResults{}, errors.Trace(err) 56 } 57 for i, entity := range args.Entities { 58 tag, err := names.ParseTag(entity.Tag) 59 if err != nil { 60 return params.ErrorResults{}, errors.Trace(err) 61 } 62 63 err = ErrPerm 64 if canModify(tag) { 65 err = d.ensureEntityDead(tag) 66 } 67 result.Results[i].Error = ServerError(err) 68 } 69 return result, nil 70 }