github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/service.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resource 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/charm.v6-unstable/resource" 9 "gopkg.in/juju/names.v2" 10 ) 11 12 // ServiceResources contains the list of resources for the application and all its 13 // units. 14 type ServiceResources struct { 15 // Resources are the current version of the resource for the application that 16 // resource-get will retrieve. 17 Resources []Resource 18 19 // CharmStoreResources provides the resource info from the charm 20 // store for each of the application's resources. The information from 21 // the charm store is current as of the last time the charm store 22 // was polled. Each entry here corresponds to the same indexed entry 23 // in the Resources field. 24 CharmStoreResources []resource.Resource 25 26 // UnitResources reports the currenly-in-use version of resources for each 27 // unit. 28 UnitResources []UnitResources 29 } 30 31 // Updates returns the list of charm store resources corresponding to 32 // the application's resources that are out of date. Note that there must be 33 // a charm store resource for each of the application resources and 34 // vice-versa. If they are out of sync then an error is returned. 35 func (sr ServiceResources) Updates() ([]resource.Resource, error) { 36 storeResources, err := sr.alignStoreResources() 37 if err != nil { 38 return nil, errors.Trace(err) 39 } 40 41 var updates []resource.Resource 42 for i, res := range sr.Resources { 43 if res.Origin != resource.OriginStore { 44 continue 45 } 46 csRes := storeResources[i] 47 // If the revision is the same then all the other info must be. 48 if res.Revision == csRes.Revision { 49 continue 50 } 51 updates = append(updates, csRes) 52 } 53 return updates, nil 54 } 55 56 func (sr ServiceResources) alignStoreResources() ([]resource.Resource, error) { 57 if len(sr.CharmStoreResources) > len(sr.Resources) { 58 return nil, errors.Errorf("have more charm store resources than application resources") 59 } 60 if len(sr.CharmStoreResources) < len(sr.Resources) { 61 return nil, errors.Errorf("have fewer charm store resources than application resources") 62 } 63 64 var store []resource.Resource 65 for _, res := range sr.Resources { 66 found := false 67 for _, chRes := range sr.CharmStoreResources { 68 if chRes.Name == res.Name { 69 store = append(store, chRes) 70 found = true 71 break 72 } 73 } 74 if !found { 75 return nil, errors.Errorf("charm store resource %q not found", res.Name) 76 } 77 } 78 return store, nil 79 } 80 81 // UnitResources conains the list of resources used by a unit. 82 type UnitResources struct { 83 // Tag is the tag of the unit. 84 Tag names.UnitTag 85 86 // Resources are the resource versions currently in use by this unit. 87 Resources []Resource 88 89 // DownloadProgress indicates the number of bytes of the unit's 90 // resources, identified by name, that have been downloaded so far 91 // by the uniter. This only applies to resources that are currently 92 // being downloaded to the unit. All other resources for the unit 93 // will not be found in the map. 94 DownloadProgress map[string]int64 95 }