Skip to content

Commit

Permalink
Include resource revision when downloading for charmhub.
Browse files Browse the repository at this point in the history
Fix LP 1982406. Update the test to use a non zero revision. Setting the
value to a default for int was a bad idea and masked the problem.
  • Loading branch information
hmlanigan committed Jul 21, 2022
1 parent 0761c60 commit b6ecfa7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
36 changes: 15 additions & 21 deletions resource/charmhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"context"
"net/url"

charmresource "github.com/juju/charm/v8/resource"
"github.com/juju/errors"
"github.com/kr/pretty"

"github.com/juju/charm/v8"
charmresource "github.com/juju/charm/v8/resource"

"github.com/juju/juju/charmhub"
"github.com/juju/juju/charmhub/transport"
corelogger "github.com/juju/juju/core/logger"
Expand Down Expand Up @@ -79,30 +77,26 @@ func (ch *CharmHubClient) GetResource(req ResourceRequest) (ResourceData, error)
ch.logger.Tracef("GetResource(%s)", pretty.Sprint(req))
var data ResourceData

// GetResource is called after a charm is installed, therefore the
// origin must have an ID. Error if not.
origin := req.CharmID.Origin

stChannel := origin.Channel
if stChannel == nil {
return data, errors.Errorf("missing channel for %q", req.CharmID.URL.Name)
}
channel, err := charm.MakeChannel(stChannel.Track, stChannel.Risk, stChannel.Branch)
if err != nil {
return data, errors.Trace(err)
}

if req.CharmID.URL == nil {
return data, errors.Errorf("missing charm url for resource %q", req.Name)
if origin.Revision == nil {
return data, errors.BadRequestf("empty charm origin revision")
}

cfg, err := charmhub.DownloadOneFromChannel(origin.ID, channel.String(), charmhub.RefreshBase{
Architecture: origin.Platform.Architecture,
Name: origin.Platform.OS,
Channel: origin.Platform.Series,
})
// The charm revision isn't really required here, just handy for
// getting the correct resource revision. Using a channel would
// limit resource revisions found. The resource revision is set
// during deploy when a resolving resources for add pending resources.
// This also closes a timing window where a charm and resource
// is updated in the channel in between deploy and resource use.
cfg, err := charmhub.DownloadOneFromRevision(origin.ID, *origin.Revision)
if err != nil {
return data, errors.Trace(err)
}

if newCfg, ok := charmhub.AddResource(cfg, req.Name, req.Revision); ok {
cfg = newCfg
}
refreshResp, err := ch.client.Refresh(context.TODO(), cfg)
if err != nil {
return data, errors.Trace(err)
Expand Down
12 changes: 7 additions & 5 deletions resource/charmhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func (s *CharmHubSuite) TestGetResource(c *gc.C) {

cl := s.newCharmHubClient()
curl, _ := charm.ParseURL("ch:postgresql")
rev := 42
result, err := cl.GetResource(resource.ResourceRequest{
CharmID: resource.CharmID{
URL: curl,
Origin: state.CharmOrigin{
ID: "mycharmhubid",
Channel: &state.Channel{Risk: "stable"},
ID: "mycharmhubid",
Channel: &state.Channel{Risk: "stable"},
Revision: &rev,
Platform: &state.Platform{
Architecture: "amd64",
OS: "ubuntu",
Expand All @@ -48,7 +50,7 @@ func (s *CharmHubSuite) TestGetResource(c *gc.C) {
},
},
Name: "wal-e",
Revision: 0,
Revision: 8,
})
c.Assert(err, jc.ErrorIsNil)

Expand All @@ -59,7 +61,7 @@ func (s *CharmHubSuite) TestGetResource(c *gc.C) {
Type: 1,
},
Origin: 2,
Revision: 0,
Revision: 8,
Fingerprint: fp,
Size: 0,
})
Expand Down Expand Up @@ -90,7 +92,7 @@ func (s *CharmHubSuite) expectRefresh() {
Size: 0,
URL: "https://api.staging.charmhub.io/api/v1/resources/download/charm_jmeJLrjWpJX9OglKSeUHCwgyaCNuoQjD.wal-e_0"},
Name: "wal-e",
Revision: 0,
Revision: 8,
Type: "file",
},
},
Expand Down

0 comments on commit b6ecfa7

Please sign in to comment.