github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/charms/client.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charms 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/utils/set" 9 "gopkg.in/juju/charm.v6-unstable" 10 11 "github.com/juju/juju/api" 12 "github.com/juju/juju/apiserver/common" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/state" 15 ) 16 17 func init() { 18 common.RegisterStandardFacade("Charms", 2, NewAPI) 19 } 20 21 var getState = func(st *state.State) charmsAccess { 22 return stateShim{st} 23 } 24 25 // Charms defines the methods on the charms API end point. 26 type Charms interface { 27 List(args params.CharmsList) (params.CharmsListResult, error) 28 CharmInfo(args params.CharmInfo) (api.CharmInfo, error) 29 IsMetered(args params.CharmInfo) (bool, error) 30 } 31 32 // API implements the charms interface and is the concrete 33 // implementation of the api end point. 34 type API struct { 35 access charmsAccess 36 authorizer common.Authorizer 37 } 38 39 // NewAPI returns a new charms API facade. 40 func NewAPI( 41 st *state.State, 42 resources *common.Resources, 43 authorizer common.Authorizer, 44 ) (*API, error) { 45 if !authorizer.AuthClient() { 46 return nil, common.ErrPerm 47 } 48 49 return &API{ 50 access: getState(st), 51 authorizer: authorizer, 52 }, nil 53 } 54 55 // CharmInfo returns information about the requested charm. 56 func (a *API) CharmInfo(args params.CharmInfo) (api.CharmInfo, error) { 57 curl, err := charm.ParseURL(args.CharmURL) 58 if err != nil { 59 return api.CharmInfo{}, err 60 } 61 aCharm, err := a.access.Charm(curl) 62 if err != nil { 63 return api.CharmInfo{}, err 64 } 65 info := api.CharmInfo{ 66 Revision: aCharm.Revision(), 67 URL: curl.String(), 68 Config: aCharm.Config(), 69 Meta: aCharm.Meta(), 70 Actions: aCharm.Actions(), 71 } 72 return info, nil 73 } 74 75 // List returns a list of charm URLs currently in the state. 76 // If supplied parameter contains any names, the result will be filtered 77 // to return only the charms with supplied names. 78 func (a *API) List(args params.CharmsList) (params.CharmsListResult, error) { 79 charms, err := a.access.AllCharms() 80 if err != nil { 81 return params.CharmsListResult{}, errors.Annotatef(err, " listing charms ") 82 } 83 84 names := set.NewStrings(args.Names...) 85 checkName := !names.IsEmpty() 86 charmURLs := []string{} 87 for _, aCharm := range charms { 88 charmURL := aCharm.URL() 89 if checkName { 90 if !names.Contains(charmURL.Name) { 91 continue 92 } 93 } 94 charmURLs = append(charmURLs, charmURL.String()) 95 } 96 return params.CharmsListResult{CharmURLs: charmURLs}, nil 97 } 98 99 // IsMetered returns whether or not the charm is metered. 100 func (a *API) IsMetered(args params.CharmInfo) (params.IsMeteredResult, error) { 101 curl, err := charm.ParseURL(args.CharmURL) 102 if err != nil { 103 return params.IsMeteredResult{false}, err 104 } 105 aCharm, err := a.access.Charm(curl) 106 if err != nil { 107 return params.IsMeteredResult{false}, err 108 } 109 if aCharm.Metrics() != nil && len(aCharm.Metrics().Metrics) > 0 { 110 return params.IsMeteredResult{true}, nil 111 } 112 return params.IsMeteredResult{false}, nil 113 }