launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/apiserver/common/life.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 "launchpad.net/juju-core/errors" 8 "launchpad.net/juju-core/state" 9 "launchpad.net/juju-core/state/api/params" 10 ) 11 12 // LifeGetter implements a common Life method for use by various facades. 13 type LifeGetter struct { 14 st state.EntityFinder 15 getCanRead GetAuthFunc 16 } 17 18 // NewLifeGetter returns a new LifeGetter. The GetAuthFunc will be used on 19 // each invocation of Life to determine current permissions. 20 func NewLifeGetter(st state.EntityFinder, getCanRead GetAuthFunc) *LifeGetter { 21 return &LifeGetter{ 22 st: st, 23 getCanRead: getCanRead, 24 } 25 } 26 27 func (lg *LifeGetter) oneLife(tag string) (params.Life, error) { 28 entity0, err := lg.st.FindEntity(tag) 29 if err != nil { 30 return "", mask(err, errors.IsNotFoundError) 31 } 32 entity, ok := entity0.(state.Lifer) 33 if !ok { 34 return "", NotSupportedError(tag, "life cycles") 35 } 36 return params.Life(entity.Life().String()), nil 37 } 38 39 // Life returns the life status of every supplied entity, where available. 40 func (lg *LifeGetter) Life(args params.Entities) (params.LifeResults, error) { 41 result := params.LifeResults{ 42 Results: make([]params.LifeResult, len(args.Entities)), 43 } 44 if len(args.Entities) == 0 { 45 return result, nil 46 } 47 canRead, err := lg.getCanRead() 48 if err != nil { 49 return params.LifeResults{}, mask(err) 50 } 51 for i, entity := range args.Entities { 52 err := ErrPerm 53 if canRead(entity.Tag) { 54 result.Results[i].Life, err = lg.oneLife(entity.Tag) 55 } 56 result.Results[i].Error = ServerError(err) 57 } 58 return result, nil 59 }