Skip to content

Commit

Permalink
Allow the ability to specify branch when deploying charms
Browse files Browse the repository at this point in the history
The following is the initial work for deploying charms with channel
branches. As channel branches are non-discoverable it means that both
arch and series (platform) are required when using a branch in a
channel.

The code was pretty much wired up for this already, it was just about
exposing this via the api parameters.
  • Loading branch information
SimonRichardson committed Feb 23, 2022
1 parent f82afb5 commit 143a762
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 10 deletions.
27 changes: 23 additions & 4 deletions api/common/charm/charmorigin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Origin struct {
Revision *int
// Track is a CharmHub channel track.
Track *string
// Branch is the CharmHub channel branch
Branch *string
// Architecture describes the architecture intended to be used by the charm.
Architecture string
// OS describes the OS intended to be used by the charm.
Expand Down Expand Up @@ -69,9 +71,14 @@ func (o Origin) CharmChannel() charm.Channel {
if o.Track != nil {
track = *o.Track
}
var branch string
if o.Branch != nil {
branch = *o.Branch
}
return charm.Channel{
Track: track,
Risk: charm.Risk(o.Risk),
Track: track,
Risk: charm.Risk(o.Risk),
Branch: branch,
}
}

Expand All @@ -86,6 +93,7 @@ func (o Origin) ParamsCharmOrigin() params.CharmOrigin {
Revision: o.Revision,
Risk: o.Risk,
Track: o.Track,
Branch: o.Branch,
Architecture: o.Architecture,
OS: o.OS,
Series: o.Series,
Expand All @@ -99,11 +107,16 @@ func (o Origin) CoreCharmOrigin() corecharm.Origin {
if o.Track != nil {
track = *o.Track
}
var branch string
if o.Branch != nil {
branch = *o.Branch
}
var channel *charm.Channel
if o.Risk != "" {
channel = &charm.Channel{
Risk: charm.Risk(o.Risk),
Track: track,
Risk: charm.Risk(o.Risk),
Track: track,
Branch: branch,
}
}
return corecharm.Origin{
Expand Down Expand Up @@ -133,6 +146,7 @@ func APICharmOrigin(origin params.CharmOrigin) Origin {
Risk: origin.Risk,
Revision: origin.Revision,
Track: origin.Track,
Branch: origin.Branch,
Architecture: origin.Architecture,
OS: origin.OS,
Series: origin.Series,
Expand All @@ -151,6 +165,10 @@ func CoreCharmOrigin(origin corecharm.Origin) Origin {
if ch.Track != "" {
track = &ch.Track
}
var branch *string
if ch.Branch != "" {
branch = &ch.Branch
}
return Origin{
Source: OriginSource(origin.Source),
Type: origin.Type,
Expand All @@ -159,6 +177,7 @@ func CoreCharmOrigin(origin corecharm.Origin) Origin {
Revision: origin.Revision,
Risk: string(ch.Risk),
Track: track,
Branch: branch,
Architecture: origin.Platform.Architecture,
OS: origin.Platform.OS,
Series: origin.Platform.Series,
Expand Down
11 changes: 7 additions & 4 deletions api/common/charm/charmorigin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ var _ = gc.Suite(&originSuite{})

func (originSuite) TestCoreChannel(c *gc.C) {
track := "latest"
branch := "foo"
origin := commoncharm.Origin{
Risk: "edge",
Track: &track,
Risk: "edge",
Track: &track,
Branch: &branch,
}
c.Assert(origin.CharmChannel(), gc.DeepEquals, charm.Channel{
Risk: charm.Edge,
Track: "latest",
Risk: charm.Edge,
Track: "latest",
Branch: "foo",
})
}

Expand Down
2 changes: 2 additions & 0 deletions apiserver/facades/client/charms/clientnormalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ var _ = gc.Suite(&clientNormalizeOriginSuite{})

func (s *clientNormalizeOriginSuite) TestNormalizeCharmOriginNoAll(c *gc.C) {
track := "1.0"
branch := "foo"
origin := params.CharmOrigin{
Source: "charm-hub",
Type: "charm",
Risk: "edge",
Track: &track,
Branch: &branch,
Architecture: "all",
OS: "all",
Series: "all",
Expand Down
14 changes: 12 additions & 2 deletions apiserver/facades/client/charms/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func convertOrigin(origin corecharm.Origin) params.CharmOrigin {
if origin.Channel != nil && origin.Channel.Track != "" {
track = &origin.Channel.Track
}
var branch *string
if origin.Channel != nil && origin.Channel.Branch != "" {
branch = &origin.Channel.Branch
}
var risk string
if origin.Channel != nil {
risk = string(origin.Channel.Risk)
Expand All @@ -27,6 +31,7 @@ func convertOrigin(origin corecharm.Origin) params.CharmOrigin {
Risk: risk,
Revision: origin.Revision,
Track: track,
Branch: branch,
Architecture: origin.Platform.Architecture,
OS: origin.Platform.OS,
Series: origin.Platform.Series,
Expand All @@ -39,15 +44,20 @@ func convertParamsOrigin(origin params.CharmOrigin) corecharm.Origin {
if origin.Track != nil {
track = *origin.Track
}
var branch string
if origin.Branch != nil {
branch = *origin.Branch
}
return corecharm.Origin{
Source: corecharm.Source(origin.Source),
Type: origin.Type,
ID: origin.ID,
Hash: origin.Hash,
Revision: origin.Revision,
Channel: &charm.Channel{
Track: track,
Risk: charm.Risk(origin.Risk),
Track: track,
Risk: charm.Risk(origin.Risk),
Branch: branch,
},
Platform: corecharm.Platform{
Architecture: origin.Architecture,
Expand Down
9 changes: 9 additions & 0 deletions apiserver/facades/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,9 @@
"architecture": {
"type": "string"
},
"branch": {
"type": "string"
},
"hash": {
"type": "string"
},
Expand Down Expand Up @@ -15732,6 +15735,9 @@
"architecture": {
"type": "string"
},
"branch": {
"type": "string"
},
"hash": {
"type": "string"
},
Expand Down Expand Up @@ -39275,6 +39281,9 @@
"architecture": {
"type": "string"
},
"branch": {
"type": "string"
},
"hash": {
"type": "string"
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/juju/application/utils/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func DeduceOrigin(url *charm.URL, channel charm.Channel, platform corecharm.Plat
if channel.Track != "" {
track = &channel.Track
}
var branch *string
if channel.Branch != "" {
branch = &channel.Branch
}
var revision *int
if url.Revision != -1 {
revision = &url.Revision
Expand All @@ -64,6 +68,7 @@ func DeduceOrigin(url *charm.URL, channel charm.Channel, platform corecharm.Plat
Revision: revision,
Risk: string(channel.Risk),
Track: track,
Branch: branch,
Architecture: architecture,
OS: platform.OS,
Series: platform.Series,
Expand Down
5 changes: 5 additions & 0 deletions core/charm/repository/charmhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ func (c *CharmHubRepository) selectNextBasesFromReleases(releases []transport.Re
return nil, errors.Errorf("no releases available")
}
if origin.Platform.Series == "" {
// If the user passed in a branch, but not enough information about the
// arch and series, then we can help by giving a better error message.
if origin.Channel != nil && origin.Channel.Branch != "" {
return nil, errors.Errorf("ambiguous arch and series with channel track/risk/branch. specify both arch and series along with channel.")
}
// If the origin is empty, then we want to help the user out
// by display a series of suggestions to try.
suggestions := c.composeSuggestions(releases, origin)
Expand Down
1 change: 1 addition & 0 deletions rpc/params/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CharmOrigin struct {
// Revision is the charm revision number.
Revision *int `json:"revision,omitempty"`
Track *string `json:"track,omitempty"`
Branch *string `json:"branch,omitempty"`

Architecture string `json:"architecture,omitempty"`
OS string `json:"os,omitempty"`
Expand Down

0 comments on commit 143a762

Please sign in to comment.