1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"gopkg.in/juju/charm.v6-unstable/resource"
9
"gopkg.in/juju/names.v2"
12
// ServiceResources contains the list of resources for the application and all its
14
type ServiceResources struct {
15
// Resources are the current version of the resource for the application that
16
// resource-get will retrieve.
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
26
// UnitResources reports the currenly-in-use version of resources for each
28
UnitResources []UnitResources
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()
38
return nil, errors.Trace(err)
41
var updates []resource.Resource
42
for i, res := range sr.Resources {
43
if res.Origin != resource.OriginStore {
46
csRes := storeResources[i]
47
// If the revision is the same then all the other info must be.
48
if res.Revision == csRes.Revision {
51
updates = append(updates, csRes)
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")
60
if len(sr.CharmStoreResources) < len(sr.Resources) {
61
return nil, errors.Errorf("have fewer charm store resources than application resources")
64
var store []resource.Resource
65
for _, res := range sr.Resources {
67
for _, chRes := range sr.CharmStoreResources {
68
if chRes.Name == res.Name {
69
store = append(store, chRes)
75
return nil, errors.Errorf("charm store resource %q not found", res.Name)
81
// UnitResources conains the list of resources used by a unit.
82
type UnitResources struct {
83
// Tag is the tag of the unit.
86
// Resources are the resource versions currently in use by this unit.
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