Skip to content

Commit

Permalink
Merge pull request #122 from wallyworld/remoteapp-consume-version
Browse files Browse the repository at this point in the history
#122

The remote application entities were missing the "consume-version" field.
Here we add it, and default to 1 for older exports.
  • Loading branch information
jujubot authored Feb 3, 2023
2 parents e1ecc72 + c8f4489 commit 2881212
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func (m *model) AddRemoteApplication(args RemoteApplicationArgs) RemoteApplicati

func (m *model) setRemoteApplications(appList []*remoteApplication) {
m.RemoteApplications_ = remoteApplications{
Version: 2,
Version: 3,
RemoteApplications: appList,
}
}
Expand Down
6 changes: 4 additions & 2 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ func (s *ModelSerializationSuite) TestSerializesRemoteApplications(c *gc.C) {
URL: "other.mysql",
SourceModel: names.NewModelTag("some-model"),
IsConsumerProxy: true,
ConsumeVersion: 666,
})
rapp.AddEndpoint(RemoteEndpointArgs{
Name: "db",
Expand All @@ -848,7 +849,8 @@ func (s *ModelSerializationSuite) TestSerializesRemoteApplications(c *gc.C) {

expected := `
remote-applications:
- endpoints:
- consume-version: 666
endpoints:
endpoints:
- interface: mysql
name: db
Expand All @@ -868,7 +870,7 @@ remote-applications:
value: running
version: 2
url: other.mysql
version: 2
version: 3
`[1:]
c.Assert(string(bytes), gc.Equals, expected)
}
Expand Down
26 changes: 26 additions & 0 deletions remoteapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RemoteApplication interface {
URL() string
SourceModelTag() names.ModelTag
IsConsumerProxy() bool
ConsumeVersion() int
Macaroon() string

Endpoints() []RemoteEndpoint
Expand All @@ -41,6 +42,7 @@ type remoteApplication struct {
OfferUUID_ string `yaml:"offer-uuid"`
URL_ string `yaml:"url"`
SourceModelUUID_ string `yaml:"source-model-uuid"`
ConsumeVersion_ int `yaml:"consume-version,omitempty"`
Macaroon_ string `yaml:"macaroon,omitempty"`
Endpoints_ remoteEndpoints `yaml:"endpoints,omitempty"`
IsConsumerProxy_ bool `yaml:"is-consumer-proxy,omitempty"`
Expand All @@ -57,6 +59,7 @@ type RemoteApplicationArgs struct {
URL string
SourceModel names.ModelTag
IsConsumerProxy bool
ConsumeVersion int
Macaroon string
Bindings map[string]string
}
Expand All @@ -68,6 +71,7 @@ func newRemoteApplication(args RemoteApplicationArgs) *remoteApplication {
URL_: args.URL,
SourceModelUUID_: args.SourceModel.Id(),
IsConsumerProxy_: args.IsConsumerProxy,
ConsumeVersion_: args.ConsumeVersion,
Macaroon_: args.Macaroon,
Bindings_: args.Bindings,
}
Expand Down Expand Up @@ -106,6 +110,11 @@ func (a *remoteApplication) IsConsumerProxy() bool {
return a.IsConsumerProxy_
}

// ConsumeVersion implements RemoteApplication.
func (a *remoteApplication) ConsumeVersion() int {
return a.ConsumeVersion_
}

// Macaroon (RemoteApplication) returns the macaroon
// that authorises access to the remote application.
func (a *remoteApplication) Macaroon() string {
Expand Down Expand Up @@ -218,6 +227,7 @@ func importRemoteApplicationList(sourceList []interface{}, checker schema.Checke
var remoteApplicationFieldsFuncs = map[int]fieldsFunc{
1: remoteApplicationV1Fields,
2: remoteApplicationV2Fields,
3: remoteApplicationV3Fields,
}

func newRemoteApplicationFromValid(valid map[string]interface{}, version int) (*remoteApplication, error) {
Expand Down Expand Up @@ -262,6 +272,15 @@ func newRemoteApplicationFromValid(valid map[string]interface{}, version int) (*
if bindings, ok := valid["bindings"]; ok {
result.Bindings_ = convertToStringMap(bindings)
}

if version < 3 {
result.ConsumeVersion_ = 1
} else {
v, ok := valid["consume-version"].(int64)
if ok {
result.ConsumeVersion_ = int(v)
}
}
return result, nil
}

Expand Down Expand Up @@ -294,3 +313,10 @@ func remoteApplicationV2Fields() (schema.Fields, schema.Defaults) {
defaults["macaroon"] = schema.Omit
return fields, defaults
}

func remoteApplicationV3Fields() (schema.Fields, schema.Defaults) {
fields, defaults := remoteApplicationV2Fields()
fields["consume-version"] = schema.Int()
defaults["consume-version"] = 0
return fields, defaults
}
15 changes: 14 additions & 1 deletion remoteapplication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func minimalRemoteApplicationMapWithoutStatus() map[interface{}]interface{} {
"url": "http://a.url",
"source-model-uuid": "abcd-1234",
"is-consumer-proxy": true,
"consume-version": 666,
"endpoints": map[interface{}]interface{}{
"version": 1,
"endpoints": []interface{}{map[interface{}]interface{}{
Expand Down Expand Up @@ -114,6 +115,7 @@ func minimalRemoteApplicationWithoutStatus() *remoteApplication {
URL: "http://a.url",
SourceModel: names.NewModelTag("abcd-1234"),
IsConsumerProxy: true,
ConsumeVersion: 666,
Bindings: map[string]string{"lana": "private"},
})
a.AddEndpoint(RemoteEndpointArgs{
Expand Down Expand Up @@ -241,20 +243,31 @@ func (*RemoteApplicationSerializationSuite) TestMinimalMatchesWithoutStatus(c *g

func (s *RemoteApplicationSerializationSuite) TestRoundTripVersion1(c *gc.C) {
rIn := minimalRemoteApplication()
rIn.ConsumeVersion_ = 0
rOut := s.exportImport(c, 1, rIn)
rIn.ConsumeVersion_ = 1
c.Assert(rOut, jc.DeepEquals, rIn)
}

func (s *RemoteApplicationSerializationSuite) TestRoundTripVersion2(c *gc.C) {
rIn := minimalRemoteApplication()
rIn.ConsumeVersion_ = 0
rIn.Macaroon_ = "mac"
rOut := s.exportImport(c, 2, rIn)
rIn.ConsumeVersion_ = 1
c.Assert(rOut, jc.DeepEquals, rIn)
}

func (s *RemoteApplicationSerializationSuite) TestRoundTripVersion3(c *gc.C) {
rIn := minimalRemoteApplication()
rIn.Macaroon_ = "mac"
rOut := s.exportImport(c, 3, rIn)
c.Assert(rOut, jc.DeepEquals, rIn)
}

func (s *RemoteApplicationSerializationSuite) TestRoundTripWithoutStatus(c *gc.C) {
rIn := minimalRemoteApplicationWithoutStatus()
rOut := s.exportImport(c, 1, rIn)
rOut := s.exportImport(c, 3, rIn)
c.Assert(rOut, jc.DeepEquals, rIn)
}

Expand Down

0 comments on commit 2881212

Please sign in to comment.