Skip to content

Commit

Permalink
Merge pull request #12434 from SimonRichardson/charmhub-bundle-yaml
Browse files Browse the repository at this point in the history
#12434

Charmhub API now exports bundle yaml for bundles rather than bulk
loading them into metadata yaml. This is a good fix as it means bundles
are not an after thought and it promotes bundles to first class
entities.

The change is simple it just expects the bundle-yaml json tag to be set
along with the filters and passes them through the info. The charm
facade can them check the type before performing the unmarshalling of
the data correctly.

## QA steps

```sh
$ export JUJU_DEV_FEATURE_FLAGS=charm-hub
$ juju bootstrap lxd test
$ juju add-model six --config charm-hub-url="https://api.staging.snapcraft.io"
$ juju deploy wiki-simple
```
  • Loading branch information
jujubot committed Dec 10, 2020
2 parents 62586b8 + 915f111 commit c0b7a49
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 33 deletions.
11 changes: 8 additions & 3 deletions apiserver/facades/client/charms/client.go
Expand Up @@ -270,22 +270,27 @@ func (a *API) getDownloadInfo(arg params.CharmURLAndOrigin) (params.DownloadInfo

func normalizeCharmOrigin(origin params.CharmOrigin) (params.CharmOrigin, error) {
os := origin.OS
if origin.Series != "" {
sys, err := series.GetOSFromSeries(origin.Series)
oSeries := origin.Series
if origin.Series != "" && origin.Series != "all" {
sys, err := series.GetOSFromSeries(oSeries)
if err != nil {
return params.CharmOrigin{}, errors.Trace(err)
}
// Values passed to the api are case sensitive: ubuntu succeeds and
// Ubuntu returns `"code": "revision-not-found"`
os = strings.ToLower(sys.String())
} else {
oSeries = ""
os = ""
}
arc := origin.Architecture
if origin.Architecture == "" {
if origin.Architecture == "" || origin.Architecture == "all" {
arc = arch.DefaultArchitecture
}

o := origin
o.OS = os
o.Series = oSeries
o.Architecture = arc
return o, nil
}
Expand Down
53 changes: 53 additions & 0 deletions apiserver/facades/client/charms/clientnormalize_test.go
@@ -0,0 +1,53 @@
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package charms

import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/arch"
)

type clientNormalizeOriginSuite struct {
}

var _ = gc.Suite(&clientNormalizeOriginSuite{})

func (s *clientNormalizeOriginSuite) TestNormalizeCharmOriginNoAll(c *gc.C) {
track := "1.0"
origin := params.CharmOrigin{
Source: "charm-hub",
Type: "charm",
Risk: "edge",
Track: &track,
Architecture: "all",
OS: "all",
Series: "all",
}
obtained, err := normalizeCharmOrigin(origin)
c.Assert(err, jc.ErrorIsNil)
origin.Architecture = arch.DefaultArchitecture
origin.OS = ""
origin.Series = ""
c.Assert(obtained, gc.DeepEquals, origin)
}

func (s *clientNormalizeOriginSuite) TestNormalizeCharmOriginLowerCase(c *gc.C) {
track := "1.0"
origin := params.CharmOrigin{
Source: "charm-hub",
Type: "charm",
Risk: "edge",
Track: &track,
Architecture: "s390",
OS: "Ubuntu",
Series: "focal",
}
obtained, err := normalizeCharmOrigin(origin)
c.Assert(err, jc.ErrorIsNil)
origin.OS = "ubuntu"
c.Assert(obtained, gc.DeepEquals, origin)
}
21 changes: 13 additions & 8 deletions apiserver/facades/client/charms/repositories.go
Expand Up @@ -281,20 +281,25 @@ func (c *chRepo) resolveViaChannelMap(t transport.Type, curl *charm.URL, origin
// only use the API to tell us which series we target. Until that happens
// we should fallback to one we do know and won't cause the deployment to
// fail.
if mapRevision.MetadataYAML == "" {
logger.Warningf("No metadata yaml found, using fallback computed series for %q.", curl)
return curl, origin, []string{origin.Series}, nil
}

var (
err error
meta Metadata
)
switch t {
case "charm":
if mapRevision.MetadataYAML == "" {
logger.Warningf("No metadata yaml found, using fallback computed series for %q.", curl)
return curl, origin, []string{origin.Series}, nil
}

meta, err = unmarshalCharmMetadata(mapRevision.MetadataYAML)
case "bundle":
meta, err = unmarshalBundleMetadata(mapRevision.MetadataYAML)
if mapRevision.BundleYAML == "" {
logger.Warningf("No bundle yaml found, using fallback computed series for %q.", curl)
return curl, origin, []string{origin.Series}, nil
}

meta, err = unmarshalBundleMetadata(mapRevision.BundleYAML)
default:
err = errors.Errorf("unexpected charm/bundle type %q", t)
}
Expand All @@ -317,8 +322,8 @@ func unmarshalCharmMetadata(metadataYAML string) (Metadata, error) {
return meta, nil
}

func unmarshalBundleMetadata(metadataYAML string) (Metadata, error) {
meta, err := charm.ReadBundleData(bytes.NewBufferString(metadataYAML))
func unmarshalBundleMetadata(bundleYAML string) (Metadata, error) {
meta, err := charm.ReadBundleData(bytes.NewBufferString(bundleYAML))
if err != nil {
return nil, errors.Trace(err)
}
Expand Down

0 comments on commit c0b7a49

Please sign in to comment.