Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Add support for actually storing minimum and maximum values set in in…
Browse files Browse the repository at this point in the history
…teger and float rules
  • Loading branch information
geauxvirtual committed Oct 23, 2015
1 parent 3a26544 commit e29101d
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 3 deletions.
20 changes: 19 additions & 1 deletion control/plugin/cpolicy/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ func (f *FloatRule) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Key string `json:"key"`
Required bool `json:"required"`
Default ctypes.ConfigValue `json:"default"`
Default ctypes.ConfigValue `json:"default,omitempty"`
Minimum ctypes.ConfigValue `json:"minimum,omitempty"`
Maximum ctypes.ConfigValue `json:"maximum,omitempty"`
Type string `json:"type"`
}{
Key: f.key,
Required: f.required,
Default: f.Default(),
Minimum: f.Minimum(),
Maximum: f.Maximum(),
Type: "float",
})
}
Expand Down Expand Up @@ -191,3 +195,17 @@ func (f *FloatRule) SetMinimum(m float64) {
func (f *FloatRule) SetMaximum(m float64) {
f.maximum = &m
}

func (i *FloatRule) Minimum() ctypes.ConfigValue {
if i.minimum != nil {
return &ctypes.ConfigValueFloat{Value: *i.minimum}
}
return nil
}

func (i *FloatRule) Maximum() ctypes.ConfigValue {
if i.maximum != nil {
return &ctypes.ConfigValueFloat{Value: *i.maximum}
}
return nil
}
20 changes: 19 additions & 1 deletion control/plugin/cpolicy/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ func (i *IntRule) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Key string `json:"key"`
Required bool `json:"required"`
Default ctypes.ConfigValue `json:"default"`
Default ctypes.ConfigValue `json:"default,omitempty"`
Minimum ctypes.ConfigValue `json:"minimum,omitempty"`
Maximum ctypes.ConfigValue `json:"maximum,omitempty"`
Type string `json:"type"`
}{
Key: i.key,
Required: i.required,
Default: i.Default(),
Minimum: i.Minimum(),
Maximum: i.Maximum(),
Type: "integer",
})
}
Expand Down Expand Up @@ -194,3 +198,17 @@ func (i *IntRule) SetMinimum(m int) {
func (i *IntRule) SetMaximum(m int) {
i.maximum = &m
}

func (i *IntRule) Minimum() ctypes.ConfigValue {
if i.minimum != nil {
return &ctypes.ConfigValueInt{Value: *i.minimum}
}
return nil
}

func (i *IntRule) Maximum() ctypes.ConfigValue {
if i.maximum != nil {
return &ctypes.ConfigValueInt{Value: *i.maximum}
}
return nil
}
22 changes: 22 additions & 0 deletions control/plugin/cpolicy/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type RuleTable struct {
Type string
Default interface{}
Required bool
Minimum interface{}
Maximum interface{}
}

func (p *ConfigPolicyNode) RulesAsTable() []RuleTable {
Expand All @@ -141,6 +143,8 @@ func (p *ConfigPolicyNode) RulesAsTable() []RuleTable {
Type: r.Type(),
Default: r.Default(),
Required: r.Required(),
Minimum: r.Minimum(),
Maximum: r.Maximum(),
})
}
return rt
Expand Down Expand Up @@ -220,6 +224,16 @@ func addRulesToConfigPolicyNode(rules map[string]interface{}, cpn *ConfigPolicyN
def := int(def_)
r.default_ = &def
}
if m, ok := rule["minimum"]; ok {
min_, _ := m.(float64)
min := int(min_)
r.minimum = &min
}
if m, ok := rule["maximum"]; ok {
max_, _ := m.(float64)
max := int(max_)
r.maximum = &max
}
cpn.Add(r)
case "string":
r, _ := NewStringRule(k, req)
Expand All @@ -237,6 +251,14 @@ func addRulesToConfigPolicyNode(rules map[string]interface{}, cpn *ConfigPolicyN
def, _ := d.(float64)
r.default_ = &def
}
if m, ok := rule["minimum"]; ok {
min, _ := m.(float64)
r.minimum = &min
}
if m, ok := rule["maximum"]; ok {
max, _ := m.(float64)
r.maximum = &max
}
cpn.Add(r)
default:
return errors.New("unknown type")
Expand Down
99 changes: 99 additions & 0 deletions control/plugin/cpolicy/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,103 @@ func TestConfigPolicyNode(t *testing.T) {
So(m2, ShouldBeNil)
})

Convey("Test integer value between minimum and maximum for rule succeeds", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["port"] = ctypes.ConfigValueInt{Value: 5}

r1, _ := NewIntegerRule("port", false)
r1.SetMinimum(0)
r1.SetMaximum(10)

n.Add(r1)

m2, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 0)
So((*m2)["port"].(ctypes.ConfigValueInt).Value, ShouldEqual, 5)
})
Convey("Test integer value outside of maximum for rule fails", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["port"] = ctypes.ConfigValueInt{Value: 5}

r1, _ := NewIntegerRule("port", false)
r1.SetMinimum(0)
r1.SetMaximum(4)

n.Add(r1)

_, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 1)
})
Convey("Test integer value outside of minimum for rule fails", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["port"] = ctypes.ConfigValueInt{Value: 5}

r1, _ := NewIntegerRule("port", false)
r1.SetMinimum(7)
r1.SetMaximum(100)

n.Add(r1)

_, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 1)
})
Convey("Test float value between minimum and maximum for rule succeeds", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["num"] = ctypes.ConfigValueFloat{Value: 5.06}

r1, _ := NewFloatRule("num", false)
r1.SetMinimum(3.14)
r1.SetMaximum(10.17)

n.Add(r1)

m2, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 0)
So((*m2)["num"].(ctypes.ConfigValueFloat).Value, ShouldEqual, 5.06)
})
Convey("Test float value outside of maximum for rule fails", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["num"] = ctypes.ConfigValueFloat{Value: 5.97}

r1, _ := NewFloatRule("num", false)
r1.SetMinimum(0)
r1.SetMaximum(3.14)

n.Add(r1)

_, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 1)
})
Convey("Test float value outside of minimum for rule fails", t, func() {
n := NewPolicyNode()

m := map[string]ctypes.ConfigValue{}
m["num"] = ctypes.ConfigValueFloat{Value: 2.26}

r1, _ := NewFloatRule("num", false)
r1.SetMinimum(3.14)
r1.SetMaximum(42)

n.Add(r1)

_, pe := n.Process(m)

So(len(pe.Errors()), ShouldEqual, 1)
})

}
2 changes: 2 additions & 0 deletions control/plugin/cpolicy/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Rule interface {
Default() ctypes.ConfigValue
Required() bool
Type() string
Minimum() ctypes.ConfigValue
Maximum() ctypes.ConfigValue
}

type rule struct {
Expand Down
8 changes: 8 additions & 0 deletions control/plugin/cpolicy/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ func (s *StringRule) Default() ctypes.ConfigValue {
func (s *StringRule) Required() bool {
return s.required
}

func (s *StringRule) Minimum() ctypes.ConfigValue {
return nil
}

func (s *StringRule) Maximum() ctypes.ConfigValue {
return nil
}
4 changes: 4 additions & 0 deletions mgmt/rest/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para
Type: r.Type,
Default: r.Default,
Required: r.Required,
Minimum: r.Minimum,
Maximum: r.Maximum,
})
}
mb.Policy = policies
Expand All @@ -107,6 +109,8 @@ func respondWithMetrics(mets []core.CatalogedMetric, w http.ResponseWriter) {
Type: r.Type,
Default: r.Default,
Required: r.Required,
Minimum: r.Minimum,
Maximum: r.Maximum,
})
}
b = append(b, rbody.Metric{
Expand Down
4 changes: 3 additions & 1 deletion mgmt/rest/rbody/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ const (
type PolicyTable struct {
Name string `json:"name"`
Type string `json:"type"`
Default interface{} `json:"default"`
Default interface{} `json:"default,omitempty"`
Required bool `json:"required"`
Minimum interface{} `json:"minimum,omitempty"`
Maximum interface{} `json:"maximum,omitempty"`
}

type Metric struct {
Expand Down

0 comments on commit e29101d

Please sign in to comment.