-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest.go
57 lines (49 loc) · 1.83 KB
/
latest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"time"
"github.com/juju/errors"
)
const jujuMetadataHTTPHeader = "Juju-Metadata"
// LatestCharmInfo returns the most up-to-date information about each
// of the identified charms at their latest revision. The revisions in
// the provided URLs are ignored. The returned map indicates charm URLs where
// the macaroon has been updated. This updated macaroon should be stored for
// use in any further requests. Note that this map may be non-empty even if
// this method returns an error (and the macaroons should be stored).
func LatestCharmInfo(client Client, charms []CharmID, metadata map[string]string) ([]CharmInfoResult, error) {
// TODO(perrito666) 2016-05-02 lp:1558657
now := time.Now().UTC()
// Do a bulk call to get the revision info for all charms.
logger.Infof("retrieving revision information for %d charms", len(charms))
revResults, err := client.LatestRevisions(charms, metadata)
if err != nil {
err = errors.Annotate(err, "while getting latest charm revision info")
logger.Infof(err.Error())
return nil, err
}
// Do a bulk call to get the latest info for each charm's resources.
// TODO(ericsnow) Only do this for charms that *have* resources.
chResources, err := client.ListResources(charms)
if err != nil {
return nil, errors.Trace(err)
}
// Extract the results.
var results []CharmInfoResult
for i, ch := range charms {
revResult := revResults[i]
resources := chResources[i]
var result CharmInfoResult
result.OriginalURL = ch.URL
result.Timestamp = now
if revResult.Err != nil {
result.Error = errors.Trace(revResult.Err)
} else {
result.LatestRevision = revResult.Revision
result.LatestResources = resources
}
results = append(results, result)
}
return results, nil
}