github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/utils/charm.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package utils 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/charm.v6-unstable" 9 10 "github.com/juju/juju/state" 11 ) 12 13 // CharmMetadata returns the charm metadata for the identified service. 14 func CharmMetadata(st *state.State, serviceID string) (*charm.Meta, error) { 15 return charmMetadata(NewCharmState(st), serviceID) 16 } 17 18 func charmMetadata(st CharmState, serviceID string) (*charm.Meta, error) { 19 service, err := st.Service(serviceID) 20 if err != nil { 21 return nil, errors.Annotatef(err, "while looking up service %q", serviceID) 22 } 23 24 ch, err := service.Charm() 25 if err != nil { 26 return nil, errors.Annotatef(err, "while looking up charm info for service %q", serviceID) 27 } 28 29 meta := ch.Meta() 30 31 return meta, nil 32 } 33 34 // CharmState exposes the methods of state.State used here. 35 type CharmState interface { 36 Service(id string) (CharmService, error) 37 } 38 39 // CharmService exposes the methods of state.Service used here. 40 type CharmService interface { 41 Charm() (Charm, error) 42 } 43 44 // Charm exposes the methods of state.Charm used here. 45 type Charm interface { 46 Meta() *charm.Meta 47 } 48 49 // NewCharmState returns a new CharmState for the given state.State. 50 func NewCharmState(st *state.State) CharmState { 51 return &charmState{raw: st} 52 } 53 54 type charmState struct { 55 raw *state.State 56 } 57 58 // Service implements CharmState. 59 func (st charmState) Service(id string) (CharmService, error) { 60 raw, err := st.raw.Service(id) 61 if err != nil { 62 return nil, errors.Trace(err) 63 } 64 return &charmService{raw: raw}, nil 65 } 66 67 type charmService struct { 68 raw *state.Service 69 } 70 71 // Charm implements CharmService. 72 func (svc charmService) Charm() (Charm, error) { 73 raw, _, err := svc.raw.Charm() 74 if err != nil { 75 return nil, errors.Trace(err) 76 } 77 return &charmInfo{raw}, nil 78 } 79 80 type charmInfo struct { 81 *state.Charm 82 }