Skip to content

Commit

Permalink
Download and Install actions behave the same.
Browse files Browse the repository at this point in the history
Rename installOneByRevision to executeOneByRevision, encompassing both
install and download.

Update DownloadOneByRevision to not take a base, and return a
executeOneByRevision RefreshConfig.
  • Loading branch information
hmlanigan committed Oct 22, 2021
1 parent 421951d commit 82bf4b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
12 changes: 4 additions & 8 deletions charmhub/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func InstallOneFromRevision(name string, revision int) (RefreshConfig, error) {
if err != nil {
return nil, errors.Trace(err)
}
return installByRevisionOne{
return executeOneByRevision{
action: InstallAction,
instanceKey: uuid.String(),
Name: name,
Expand All @@ -217,7 +217,7 @@ func InstallOneFromRevision(name string, revision int) (RefreshConfig, error) {
// AddResource adds resource revision data to a executeOne config.
// Used for install by revision.
func AddResource(config RefreshConfig, name string, revision int) (RefreshConfig, bool) {
c, ok := config.(installByRevisionOne)
c, ok := config.(executeOneByRevision)
if !ok {
return config, false
}
Expand Down Expand Up @@ -289,20 +289,16 @@ func DownloadOne(id string, revision int, channel string, base RefreshBase) (Ref

// DownloadOneFromRevision creates a request config using the revision and not
// the channel for requesting only one charm.
func DownloadOneFromRevision(id string, revision int, base RefreshBase) (RefreshConfig, error) {
if err := validateBase(base); err != nil {
return nil, errors.Trace(err)
}
func DownloadOneFromRevision(id string, revision int) (RefreshConfig, error) {
uuid, err := utils.NewUUID()
if err != nil {
return nil, errors.Trace(err)
}
return executeOne{
return executeOneByRevision{
action: DownloadAction,
instanceKey: uuid.String(),
ID: id,
Revision: &revision,
Base: base,
}, nil
}

Expand Down
8 changes: 2 additions & 6 deletions charmhub/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func DefineInstanceKey(c *gc.C, config RefreshConfig, key string) RefreshConfig
case executeOne:
t.instanceKey = key
return t
case installByRevisionOne:
case executeOneByRevision:
t.instanceKey = key
return t
default:
Expand Down Expand Up @@ -737,11 +737,7 @@ func (s *RefreshConfigSuite) TestDownloadOneFromChannelEnsure(c *gc.C) {

func (s *RefreshConfigSuite) TestRefreshManyBuildContextNotNil(c *gc.C) {
id1 := "foo"
config1, err := DownloadOneFromRevision(id1, 1, RefreshBase{
Name: "ubuntu",
Channel: "20.04",
Architecture: arch.DefaultArchitecture,
})
config1, err := DownloadOneFromRevision(id1, 1)
c.Assert(err, jc.ErrorIsNil)
config1 = DefineInstanceKey(c, config1, "foo-bar")

Expand Down
23 changes: 15 additions & 8 deletions charmhub/refreshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ func (c executeOne) String() string {
c.action, c.instanceKey, using, revision, channel, c.Base)
}

type installByRevisionOne struct {
Name string
Revision *int
type executeOneByRevision struct {
Name string
Revision *int
// ID is only used for download by revision
ID string
resourceRevisions []transport.RefreshResourceRevision
// instanceKey is a private unique key that we construct for CharmHub API
// asynchronous calls.
Expand All @@ -173,16 +175,20 @@ type installByRevisionOne struct {
}

// InstanceKey returns the underlying instance key.
func (c installByRevisionOne) InstanceKey() string {
func (c executeOneByRevision) InstanceKey() string {
return c.instanceKey
}

// Build a refresh request for sending to the API.
func (c installByRevisionOne) Build() (transport.RefreshRequest, error) {
var name *string

func (c executeOneByRevision) Build() (transport.RefreshRequest, error) {
var name, id *string
if c.Name != "" {
name = &c.Name
}
if c.ID != "" {
id = &c.ID
}

req := transport.RefreshRequest{
// Context is required here, even if it looks optional.
Expand All @@ -191,6 +197,7 @@ func (c installByRevisionOne) Build() (transport.RefreshRequest, error) {
Action: string(c.action),
InstanceKey: c.instanceKey,
Name: name,
ID: id,
Revision: c.Revision,
ResourceRevisions: c.resourceRevisions,
}},
Expand All @@ -200,7 +207,7 @@ func (c installByRevisionOne) Build() (transport.RefreshRequest, error) {
}

// Ensure that the request back contains the information we requested.
func (c installByRevisionOne) Ensure(responses []transport.RefreshResponse) error {
func (c executeOneByRevision) Ensure(responses []transport.RefreshResponse) error {
for _, resp := range responses {
if resp.InstanceKey == c.instanceKey {
return nil
Expand All @@ -210,7 +217,7 @@ func (c installByRevisionOne) Ensure(responses []transport.RefreshResponse) erro
}

// String describes the underlying refresh config.
func (c installByRevisionOne) String() string {
func (c executeOneByRevision) String() string {
var revision string
if c.Revision != nil {
revision = fmt.Sprintf(" with revision: %+v", c.Revision)
Expand Down

0 comments on commit 82bf4b9

Please sign in to comment.