-
Notifications
You must be signed in to change notification settings - Fork 23
/
provisioningstate.go
96 lines (80 loc) · 2.45 KB
/
provisioningstate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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)),
}
}