-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from hpidcock/provisioning-state-v4
Provisioning state forward port to v4
- Loading branch information
Showing
5 changed files
with
272 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2023 Canonical Ltd. | ||
// Licensed under the LGPLv3, see LICENCE file for details. | ||
|
||
package description | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
"github.com/juju/schema" | ||
) | ||
|
||
type ProvisioningState interface { | ||
Scaling() bool | ||
ScaleTarget() int | ||
} | ||
|
||
type provisioningState struct { | ||
Version_ int `yaml:"version"` | ||
Scaling_ bool `yaml:"scaling"` | ||
ScaleTarget_ int `yaml:"scale-target"` | ||
} | ||
|
||
func (i *provisioningState) Scaling() bool { | ||
return i.Scaling_ | ||
} | ||
|
||
func (i *provisioningState) ScaleTarget() int { | ||
return i.ScaleTarget_ | ||
} | ||
|
||
// ProvisioningStateArgs is an argument struct used to create a | ||
// new internal provisioningState type that supports the ProvisioningState interface. | ||
type ProvisioningStateArgs struct { | ||
Scaling bool | ||
ScaleTarget int | ||
} | ||
|
||
func newProvisioningState(args *ProvisioningStateArgs) *provisioningState { | ||
if args == nil { | ||
return nil | ||
} | ||
return &provisioningState{ | ||
Version_: 1, | ||
Scaling_: args.Scaling, | ||
ScaleTarget_: args.ScaleTarget, | ||
} | ||
} | ||
|
||
func importProvisioningState(source map[string]interface{}) (*provisioningState, error) { | ||
version, err := getVersion(source) | ||
if err != nil { | ||
return nil, errors.Annotate(err, "provisioning-state version schema check failed") | ||
} | ||
importFunc, ok := provisioningStateDeserializationFuncs[version] | ||
if !ok { | ||
return nil, errors.NotValidf("version %d", version) | ||
} | ||
return importFunc(source) | ||
} | ||
|
||
type provisioningStateDeserializationFunc func(map[string]interface{}) (*provisioningState, error) | ||
|
||
var provisioningStateDeserializationFuncs = map[int]provisioningStateDeserializationFunc{ | ||
1: importProvisioningStateV1, | ||
} | ||
|
||
func importProvisioningStateV1(source map[string]interface{}) (*provisioningState, error) { | ||
fields, defaults := provisioningStateV1Schema() | ||
checker := schema.FieldMap(fields, defaults) | ||
|
||
coerced, err := checker.Coerce(source, nil) | ||
if err != nil { | ||
return nil, errors.Annotatef(err, "provisioning-state v1 schema check failed") | ||
} | ||
|
||
return provisioningStateV1(coerced.(map[string]interface{})), nil | ||
} | ||
|
||
func provisioningStateV1Schema() (schema.Fields, schema.Defaults) { | ||
fields := schema.Fields{ | ||
"scaling": schema.Bool(), | ||
"scale-target": schema.Int(), | ||
} | ||
defaults := schema.Defaults{ | ||
"scaling": false, | ||
"scale-target": 0, | ||
} | ||
return fields, defaults | ||
} | ||
|
||
func provisioningStateV1(valid map[string]interface{}) *provisioningState { | ||
return &provisioningState{ | ||
Version_: 1, | ||
Scaling_: valid["scaling"].(bool), | ||
ScaleTarget_: int(valid["scale-target"].(int64)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2023 Canonical Ltd. | ||
// Licensed under the LGPLv3, see LICENCE file for details. | ||
|
||
package description | ||
|
||
import ( | ||
jc "github.com/juju/testing/checkers" | ||
gc "gopkg.in/check.v1" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type ProvisioningStateSerializationSuite struct { | ||
SerializationSuite | ||
} | ||
|
||
var _ = gc.Suite(&ProvisioningStateSerializationSuite{}) | ||
|
||
func (s *ProvisioningStateSerializationSuite) SetUpTest(c *gc.C) { | ||
s.importName = "provisioning-state" | ||
s.importFunc = func(m map[string]interface{}) (interface{}, error) { | ||
return importProvisioningState(m) | ||
} | ||
} | ||
|
||
func (s *ProvisioningStateSerializationSuite) TestNewProvisioningState(c *gc.C) { | ||
args := ProvisioningStateArgs{ | ||
Scaling: true, | ||
ScaleTarget: 10, | ||
} | ||
instance := newProvisioningState(&args) | ||
c.Assert(instance.Scaling(), jc.IsTrue) | ||
c.Assert(instance.ScaleTarget(), gc.Equals, 10) | ||
} | ||
|
||
func minimalProvisioningStateMap() map[interface{}]interface{} { | ||
return map[interface{}]interface{}{ | ||
"version": 1, | ||
"scaling": true, | ||
"scale-target": 10, | ||
} | ||
} | ||
|
||
func minimalProvisioningStateArgs() *ProvisioningStateArgs { | ||
return &ProvisioningStateArgs{ | ||
Scaling: true, | ||
ScaleTarget: 10, | ||
} | ||
} | ||
|
||
func minimalProvisioningState() *provisioningState { | ||
return newProvisioningState(minimalProvisioningStateArgs()) | ||
} | ||
|
||
func maximalProvisioningStateMap() map[interface{}]interface{} { | ||
return map[interface{}]interface{}{ | ||
"version": 1, | ||
"scaling": true, | ||
"scale-target": 10, | ||
} | ||
} | ||
|
||
func maximalProvisioningStateArgs() *ProvisioningStateArgs { | ||
return &ProvisioningStateArgs{ | ||
Scaling: true, | ||
ScaleTarget: 10, | ||
} | ||
} | ||
|
||
func maximalProvisioningState() *provisioningState { | ||
return newProvisioningState(maximalProvisioningStateArgs()) | ||
} | ||
|
||
func (s *ProvisioningStateSerializationSuite) TestMinimalMatches(c *gc.C) { | ||
bytes, err := yaml.Marshal(minimalProvisioningState()) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
var source map[interface{}]interface{} | ||
err = yaml.Unmarshal(bytes, &source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
c.Assert(source, jc.DeepEquals, minimalProvisioningStateMap()) | ||
} | ||
|
||
func (s *ProvisioningStateSerializationSuite) TestMaximalMatches(c *gc.C) { | ||
bytes, err := yaml.Marshal(maximalProvisioningState()) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
var source map[interface{}]interface{} | ||
err = yaml.Unmarshal(bytes, &source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
c.Assert(source, jc.DeepEquals, maximalProvisioningStateMap()) | ||
} | ||
|
||
func (s *ProvisioningStateSerializationSuite) TestParsingSerializedData(c *gc.C) { | ||
initial := maximalProvisioningState() | ||
bytes, err := yaml.Marshal(initial) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
var source map[string]interface{} | ||
err = yaml.Unmarshal(bytes, &source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
instance, err := importProvisioningState(source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
c.Assert(instance, jc.DeepEquals, initial) | ||
} | ||
|
||
func (s *ProvisioningStateSerializationSuite) exportImportVersion(c *gc.C, origin_ *provisioningState, version int) *provisioningState { | ||
origin_.Version_ = version | ||
bytes, err := yaml.Marshal(origin_) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
var source map[string]interface{} | ||
err = yaml.Unmarshal(bytes, &source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
origin, err := importProvisioningState(source) | ||
c.Assert(err, jc.ErrorIsNil) | ||
return origin | ||
} |