/
scalingpolicies.go
99 lines (78 loc) · 2.57 KB
/
scalingpolicies.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
97
98
99
package config
import (
"encoding/json"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/service/simpledb"
)
// ScalingPolicyClasses is a map of Scaling Policy Classes
type ScalingPolicyClasses map[string]ScalingPolicyClass
// ScalingPolicyClass is a single Scaling Policy Class
type ScalingPolicyClass struct {
ScalingAdjustment int `json:"scalingAdjustment" awsmClass:"Scaling Adjustment"`
AdjustmentType string `json:"adjustmentType" awsmClass:"Adjustment Type"`
Cooldown int `json:"cooldown" awsmClass:"Cooldown"`
}
// DefaultScalingPolicyClasses returns the defauly Scaling Policy Classes
func DefaultScalingPolicyClasses() ScalingPolicyClasses {
defaultScalingPolicies := make(ScalingPolicyClasses)
defaultScalingPolicies["scaleUp"] = ScalingPolicyClass{
ScalingAdjustment: 1,
AdjustmentType: "ChangeInCapacity",
Cooldown: 300,
}
defaultScalingPolicies["scaleDown"] = ScalingPolicyClass{
ScalingAdjustment: -1,
AdjustmentType: "ChangeInCapacity",
Cooldown: 300,
}
return defaultScalingPolicies
}
// SaveScalingPolicyClass reads unmarshals a byte slice and inserts it into the db
func SaveScalingPolicyClass(className string, data []byte) (class ScalingPolicyClass, err error) {
err = json.Unmarshal(data, &class)
if err != nil {
return
}
err = Insert("scalingpolicies", ScalingPolicyClasses{className: class})
return
}
// LoadScalingPolicyClass loads a Scaling Policy Class by its name
func LoadScalingPolicyClass(name string) (ScalingPolicyClass, error) {
cfgs := make(ScalingPolicyClasses)
item, err := GetItemByName("scalingpolicies", name)
if err != nil {
return cfgs[name], err
}
cfgs.Marshal([]*simpledb.Item{item})
return cfgs[name], nil
}
// LoadAllScalingPolicyClasses loads all Scaling Policies Classes
func LoadAllScalingPolicyClasses() (ScalingPolicyClasses, error) {
cfgs := make(ScalingPolicyClasses)
items, err := GetItemsByType("scalingpolicies")
if err != nil {
return cfgs, err
}
cfgs.Marshal(items)
return cfgs, nil
}
// Marshal puts items from SimpleDB into a Scaling Policy Class
func (c ScalingPolicyClasses) Marshal(items []*simpledb.Item) {
for _, item := range items {
name := strings.Replace(*item.Name, "scalingpolicies/", "", -1)
cfg := new(ScalingPolicyClass)
for _, attribute := range item.Attributes {
val := *attribute.Value
switch *attribute.Name {
case "ScalingAdjustment":
cfg.ScalingAdjustment, _ = strconv.Atoi(val)
case "AdjustmentType":
cfg.AdjustmentType = val
case "Cooldown":
cfg.Cooldown, _ = strconv.Atoi(val)
}
}
c[name] = *cfg
}
}