github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 "gopkg.in/juju/charm.v4" 9 10 "github.com/juju/juju/api" 11 "github.com/juju/juju/apiserver/common" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/state" 14 "github.com/juju/utils/set" 15 ) 16 17 func init() { 18 common.RegisterStandardFacade("Charms", 1, 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 } 30 31 // API implements the charms interface and is the concrete 32 // implementation of the api end point. 33 type API struct { 34 access charmsAccess 35 authorizer common.Authorizer 36 } 37 38 // NewAPI returns a new charms API facade. 39 func NewAPI( 40 st *state.State, 41 resources *common.Resources, 42 authorizer common.Authorizer, 43 ) (*API, error) { 44 if !authorizer.AuthClient() { 45 return nil, common.ErrPerm 46 } 47 48 return &API{ 49 access: getState(st), 50 authorizer: authorizer, 51 }, nil 52 } 53 54 var CharmStore charm.Repository = charm.Store 55 56 // CharmInfo returns information about the requested charm. 57 func (a *API) CharmInfo(args params.CharmInfo) (api.CharmInfo, error) { 58 curl, err := charm.ParseURL(args.CharmURL) 59 if err != nil { 60 return api.CharmInfo{}, err 61 } 62 aCharm, err := a.access.Charm(curl) 63 if err != nil { 64 return api.CharmInfo{}, err 65 } 66 info := api.CharmInfo{ 67 Revision: aCharm.Revision(), 68 URL: curl.String(), 69 Config: aCharm.Config(), 70 Meta: aCharm.Meta(), 71 Actions: aCharm.Actions(), 72 } 73 return info, nil 74 } 75 76 // List returns a list of charm URLs currently in the state. 77 // If supplied parameter contains any names, the result will be filtered 78 // to return only the charms with supplied names. 79 func (a *API) List(args params.CharmsList) (params.CharmsListResult, error) { 80 charms, err := a.access.AllCharms() 81 if err != nil { 82 return params.CharmsListResult{}, errors.Annotatef(err, " listing charms ") 83 } 84 85 names := set.NewStrings(args.Names...) 86 checkName := !names.IsEmpty() 87 charmURLs := []string{} 88 for _, aCharm := range charms { 89 charmURL := aCharm.URL() 90 if checkName { 91 if !names.Contains(charmURL.Name) { 92 continue 93 } 94 } 95 charmURLs = append(charmURLs, charmURL.String()) 96 } 97 return params.CharmsListResult{CharmURLs: charmURLs}, nil 98 }