github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/common/life.go (about) 1 // Copyright 2012, 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 "github.com/juju/names/v5" 9 10 "github.com/juju/juju/api/base" 11 "github.com/juju/juju/core/life" 12 "github.com/juju/juju/rpc/params" 13 ) 14 15 // Life requests the life cycle of the given entities from the given 16 // server-side API facade via the given caller. 17 func Life(caller base.FacadeCaller, tags []names.Tag) ([]params.LifeResult, error) { 18 if len(tags) == 0 { 19 return []params.LifeResult{}, nil 20 } 21 var result params.LifeResults 22 entities := make([]params.Entity, len(tags)) 23 for i, t := range tags { 24 entities[i] = params.Entity{t.String()} 25 } 26 args := params.Entities{Entities: entities} 27 if err := caller.FacadeCall("Life", args, &result); err != nil { 28 return []params.LifeResult{}, err 29 } 30 return result.Results, nil 31 } 32 33 // OneLife requests the life cycle of the given entity from the given 34 // server-side API facade via the given caller. 35 func OneLife(caller base.FacadeCaller, tag names.Tag) (life.Value, error) { 36 result, err := Life(caller, []names.Tag{tag}) 37 if err != nil { 38 return "", err 39 } 40 if len(result) != 1 { 41 return "", errors.Errorf("expected 1 result, got %d", len(result)) 42 } 43 if err := result[0].Error; err != nil { 44 return "", err 45 } 46 return result[0].Life, nil 47 }