Skip to content

Commit

Permalink
Merge pull request #137 from wallyworld/rename-storage-constraint
Browse files Browse the repository at this point in the history
#137

Application storage constraints are incorrectly named - it should be "storage directive". 
This PR does that rename.
  • Loading branch information
jujubot authored Mar 20, 2024
2 parents 1339f5b + e6b56fa commit 448ffa6
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 152 deletions.
49 changes: 34 additions & 15 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Application interface {
LeadershipSettings() map[string]interface{}

MetricsCredentials() []byte
StorageConstraints() map[string]StorageConstraint
StorageDirectives() map[string]StorageDirective

Resources() []Resource
AddResource(ResourceArgs) Resource
Expand Down Expand Up @@ -126,8 +126,8 @@ type application struct {

Annotations_ `yaml:"annotations,omitempty"`

Constraints_ *constraints `yaml:"constraints,omitempty"`
StorageConstraints_ map[string]*storageconstraint `yaml:"storage-constraints,omitempty"`
Constraints_ *constraints `yaml:"constraints,omitempty"`
StorageDirectives_ map[string]*storageDirective `yaml:"storage-directives,omitempty"`

// CAAS application fields.
PasswordHash_ string `yaml:"password-hash,omitempty"`
Expand Down Expand Up @@ -174,7 +174,7 @@ type ApplicationArgs struct {
CharmConfig map[string]interface{}
Leader string
LeadershipSettings map[string]interface{}
StorageConstraints map[string]StorageConstraintArgs
StorageDirectives map[string]StorageDirectiveArgs
MetricsCredentials []byte
ProvisioningState *ProvisioningStateArgs
}
Expand Down Expand Up @@ -209,10 +209,10 @@ func newApplication(args ApplicationArgs) *application {
}
app.setUnits(nil)
app.setResources(nil)
if len(args.StorageConstraints) > 0 {
app.StorageConstraints_ = make(map[string]*storageconstraint)
for key, value := range args.StorageConstraints {
app.StorageConstraints_[key] = newStorageConstraint(value)
if len(args.StorageDirectives) > 0 {
app.StorageDirectives_ = make(map[string]*storageDirective)
for key, value := range args.StorageDirectives {
app.StorageDirectives_[key] = newStorageDirective(value)
}
}
if len(args.ExposedEndpoints) > 0 {
Expand Down Expand Up @@ -337,10 +337,10 @@ func (a *application) LeadershipSettings() map[string]interface{} {
return a.LeadershipSettings_
}

// StorageConstraints implements Application.
func (a *application) StorageConstraints() map[string]StorageConstraint {
result := make(map[string]StorageConstraint)
for key, value := range a.StorageConstraints_ {
// StorageDirectives implements Application.
func (a *application) StorageDirectives() map[string]StorageDirective {
result := make(map[string]StorageDirective)
for key, value := range a.StorageDirectives_ {
result[key] = value
}
return result
Expand Down Expand Up @@ -638,6 +638,7 @@ var applicationDeserializationFuncs = map[int]applicationDeserializationFunc{
9: importApplicationV9,
10: importApplicationV10,
11: importApplicationV11,
12: importApplicationV12,
}

func applicationV1Fields() (schema.Fields, schema.Defaults) {
Expand Down Expand Up @@ -759,6 +760,15 @@ func applicationV11Fields() (schema.Fields, schema.Defaults) {
return fields, defaults
}

func applicationV12Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV11Fields()
delete(fields, "storage-constraints")
defaults["storage-constraints"] = schema.Omit
fields["storage-directives"] = schema.StringMap(schema.StringMap(schema.Any()))
defaults["storage-directives"] = schema.Omit
return fields, defaults
}

func importApplicationV1(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV1Fields()
return importApplication(fields, defaults, 1, source)
Expand Down Expand Up @@ -814,6 +824,11 @@ func importApplicationV11(source map[string]interface{}) (*application, error) {
return importApplication(fields, defaults, 11, source)
}

func importApplicationV12(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV12Fields()
return importApplication(fields, defaults, 12, source)
}

func importApplication(fields schema.Fields, defaults schema.Defaults, importVersion int, source map[string]interface{}) (*application, error) {
checker := schema.FieldMap(fields, defaults)

Expand Down Expand Up @@ -945,12 +960,16 @@ func importApplication(fields schema.Fields, defaults schema.Defaults, importVer
result.Constraints_ = constraints
}

if constraintsMap, ok := valid["storage-constraints"]; ok {
constraints, err := importStorageConstraints(constraintsMap.(map[string]interface{}))
storageKey := "storage-directives"
if importVersion < 12 {
storageKey = "storage-constraints"
}
if constraintsMap, ok := valid[storageKey]; ok {
directives, err := importStorageDirectives(constraintsMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.StorageConstraints_ = constraints
result.StorageDirectives_ = directives
}

if cloudServiceMap, ok := valid["cloud-service"]; ok {
Expand Down
16 changes: 8 additions & 8 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (s *ApplicationSerializationSuite) TestNewApplication(c *gc.C) {
c.Assert(application.HasResources(), jc.IsTrue)
c.Assert(application.DesiredScale(), gc.Equals, 2)
c.Assert(application.CloudService(), gc.IsNil)
c.Assert(application.StorageConstraints(), gc.HasLen, 0)
c.Assert(application.StorageDirectives(), gc.HasLen, 0)
c.Assert(application.MinUnits(), gc.Equals, 42)
c.Assert(application.EndpointBindings(), jc.DeepEquals, args.EndpointBindings)
c.Assert(application.ApplicationConfig(), jc.DeepEquals, args.ApplicationConfig)
Expand Down Expand Up @@ -362,7 +362,7 @@ func (s *ApplicationSerializationSuite) exportImportVersion(c *gc.C, application
}

func (s *ApplicationSerializationSuite) exportImportLatest(c *gc.C, application_ *application) *application {
return s.exportImportVersion(c, application_, 11)
return s.exportImportVersion(c, application_, 12)
}

func (s *ApplicationSerializationSuite) TestV1ParsingReturnsLatest(c *gc.C) {
Expand Down Expand Up @@ -502,25 +502,25 @@ func (s *ApplicationSerializationSuite) TestConstraints(c *gc.C) {
c.Assert(application.Constraints(), jc.DeepEquals, newConstraints(args))
}

func (s *ApplicationSerializationSuite) TestStorageConstraints(c *gc.C) {
func (s *ApplicationSerializationSuite) TestStorageDirectives(c *gc.C) {
args := minimalApplicationArgs(IAAS)
args.StorageConstraints = map[string]StorageConstraintArgs{
args.StorageDirectives = map[string]StorageDirectiveArgs{
"first": {Pool: "first", Size: 1234, Count: 1},
"second": {Pool: "second", Size: 4321, Count: 7},
}
initial := minimalApplication(args)

application := s.exportImportLatest(c, initial)

constraints := application.StorageConstraints()
c.Assert(constraints, gc.HasLen, 2)
first, found := constraints["first"]
directives := application.StorageDirectives()
c.Assert(directives, gc.HasLen, 2)
first, found := directives["first"]
c.Assert(found, jc.IsTrue)
c.Check(first.Pool(), gc.Equals, "first")
c.Check(first.Size(), gc.Equals, uint64(1234))
c.Check(first.Count(), gc.Equals, uint64(1))

second, found := constraints["second"]
second, found := directives["second"]
c.Assert(found, jc.IsTrue)
c.Check(second.Pool(), gc.Equals, "second")
c.Check(second.Size(), gc.Equals, uint64(4321))
Expand Down
4 changes: 2 additions & 2 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ type StoragePool interface {
Attributes() map[string]interface{}
}

// StorageConstraint represents the user-specified constraints for
// StorageDirective represents the user-specified storage directive for
// provisioning storage instances for an application unit.
type StorageConstraint interface {
type StorageDirective interface {
// Pool is the name of the storage pool from which to provision the
// storage instances.
Pool() string
Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (m *model) AddApplication(args ApplicationArgs) Application {

func (m *model) setApplications(applicationList []*application) {
m.Applications_ = applications{
Version: 11,
Version: 12,
Applications_: applicationList,
}
}
Expand Down
111 changes: 0 additions & 111 deletions storageconstraint.go

This file was deleted.

Loading

0 comments on commit 448ffa6

Please sign in to comment.