github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/api/private/server/unitfacade.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package server 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/apiserver/common" 10 "github.com/juju/juju/resource" 11 "github.com/juju/juju/resource/api" 12 "github.com/juju/juju/resource/api/private" 13 ) 14 15 // FacadeVersion is the version of the current API facade. 16 // (We start at 1 to distinguish from the default value.) 17 const FacadeVersion = 1 18 19 // UnitDataStore exposes the data storage functionality needed here. 20 // All functionality is tied to the unit's application. 21 type UnitDataStore interface { 22 // ListResources lists all the resources for the application. 23 ListResources() (resource.ServiceResources, error) 24 } 25 26 // NewUnitFacade returns the resources portion of the uniter's API facade. 27 func NewUnitFacade(dataStore UnitDataStore) *UnitFacade { 28 return &UnitFacade{ 29 DataStore: dataStore, 30 } 31 } 32 33 // UnitFacade is the resources portion of the uniter's API facade. 34 type UnitFacade struct { 35 //DataStore is the data store used by the facade. 36 DataStore UnitDataStore 37 } 38 39 // GetResourceInfo returns the resource info for each of the given 40 // resource names (for the implicit application). If any one is missing then 41 // the corresponding result is set with errors.NotFound. 42 func (uf UnitFacade) GetResourceInfo(args private.ListResourcesArgs) (private.ResourcesResult, error) { 43 var r private.ResourcesResult 44 r.Resources = make([]private.ResourceResult, len(args.ResourceNames)) 45 46 resources, err := uf.DataStore.ListResources() 47 if err != nil { 48 r.Error = common.ServerError(err) 49 return r, nil 50 } 51 52 for i, name := range args.ResourceNames { 53 res, ok := lookUpResource(name, resources.Resources) 54 if !ok { 55 r.Resources[i].Error = common.ServerError(errors.NotFoundf("resource %q", name)) 56 continue 57 } 58 59 r.Resources[i].Resource = api.Resource2API(res) 60 } 61 return r, nil 62 } 63 64 func lookUpResource(name string, resources []resource.Resource) (resource.Resource, bool) { 65 for _, res := range resources { 66 if name == res.Name { 67 return res, true 68 } 69 } 70 return resource.Resource{}, false 71 }