2019-06-25 18:16:02 -07:00
|
|
|
package info
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//PolicyInfo defines policy information
|
|
|
|
type PolicyInfo struct {
|
2019-06-26 18:04:50 -07:00
|
|
|
// Name is policy name
|
|
|
|
Name string
|
2019-06-26 18:16:06 -07:00
|
|
|
// RKind represents the resource kind
|
|
|
|
RKind string
|
|
|
|
// RName is resource name
|
|
|
|
RName string
|
2019-06-26 18:04:50 -07:00
|
|
|
// Namespace is the ns of resource
|
|
|
|
// empty on non-namespaced resources
|
2019-06-26 12:41:42 -07:00
|
|
|
RNamespace string
|
|
|
|
Rules []*RuleInfo
|
|
|
|
success bool
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewPolicyInfo returns a new policy info
|
2019-06-26 12:41:42 -07:00
|
|
|
func NewPolicyInfo(policyName string, rKind string, rName string, rNamespace string) *PolicyInfo {
|
2019-06-25 18:16:02 -07:00
|
|
|
return &PolicyInfo{
|
2019-06-26 12:41:42 -07:00
|
|
|
Name: policyName,
|
|
|
|
RKind: rKind,
|
|
|
|
RName: rName,
|
|
|
|
RNamespace: rNamespace,
|
|
|
|
success: true, // fail to be set explicity
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//IsSuccessful checks if policy is succesful
|
|
|
|
// the policy is set to fail, if any of the rules have failed
|
|
|
|
func (pi *PolicyInfo) IsSuccessful() bool {
|
|
|
|
return pi.success
|
|
|
|
}
|
|
|
|
|
|
|
|
//ErrorRules returns error msgs from all rule
|
|
|
|
func (pi *PolicyInfo) ErrorRules() string {
|
|
|
|
errorMsgs := []string{}
|
2019-06-25 23:58:28 -07:00
|
|
|
for _, r := range pi.Rules {
|
2019-06-25 18:16:02 -07:00
|
|
|
if !r.IsSuccessful() {
|
2019-06-25 18:38:00 -07:00
|
|
|
errorMsgs = append(errorMsgs, r.ToString())
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(errorMsgs, ";")
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:58:28 -07:00
|
|
|
type RuleType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Mutation RuleType = iota
|
|
|
|
Validation
|
|
|
|
Generation
|
|
|
|
)
|
|
|
|
|
2019-06-25 18:16:02 -07:00
|
|
|
//RuleInfo defines rule struct
|
|
|
|
type RuleInfo struct {
|
2019-06-25 23:58:28 -07:00
|
|
|
Name string
|
|
|
|
Msgs []string
|
2019-06-26 12:19:11 -07:00
|
|
|
RuleType RuleType
|
2019-06-25 23:58:28 -07:00
|
|
|
success bool
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|
|
|
|
|
2019-06-25 18:38:00 -07:00
|
|
|
//ToString reule information
|
|
|
|
func (ri *RuleInfo) ToString() string {
|
|
|
|
str := "rulename: " + ri.Name
|
|
|
|
msgs := strings.Join(ri.Msgs, ";")
|
|
|
|
return strings.Join([]string{str, msgs}, ";")
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:16:02 -07:00
|
|
|
//NewRuleInfo creates a new RuleInfo
|
2019-06-25 23:58:28 -07:00
|
|
|
func NewRuleInfo(ruleName string, ruleType RuleType) *RuleInfo {
|
2019-06-25 18:16:02 -07:00
|
|
|
return &RuleInfo{
|
2019-06-25 23:58:28 -07:00
|
|
|
Name: ruleName,
|
|
|
|
Msgs: []string{},
|
2019-06-26 12:19:11 -07:00
|
|
|
RuleType: ruleType,
|
2019-06-25 23:58:28 -07:00
|
|
|
success: true, // fail to be set explicity
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Fail set the rule as failed
|
|
|
|
func (ri *RuleInfo) Fail() {
|
|
|
|
ri.success = false
|
|
|
|
}
|
|
|
|
|
|
|
|
//IsSuccessful checks if rule is succesful
|
|
|
|
func (ri *RuleInfo) IsSuccessful() bool {
|
|
|
|
return ri.success
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add add msg
|
|
|
|
func (ri *RuleInfo) Add(msg string) {
|
|
|
|
ri.Msgs = append(ri.Msgs, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Addf add msg with args
|
|
|
|
func (ri *RuleInfo) Addf(msg string, args ...interface{}) {
|
|
|
|
ri.Msgs = append(ri.Msgs, fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//RulesSuccesfuly check if the any rule has failed or not
|
|
|
|
func RulesSuccesfuly(rules []*RuleInfo) bool {
|
|
|
|
for _, r := range rules {
|
|
|
|
if !r.success {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddRuleInfos sets the rule information
|
|
|
|
func (pi *PolicyInfo) AddRuleInfos(rules []*RuleInfo) {
|
|
|
|
if !RulesSuccesfuly(rules) {
|
|
|
|
pi.success = false
|
|
|
|
}
|
2019-06-26 15:31:18 -07:00
|
|
|
|
|
|
|
pi.Rules = append(pi.Rules, rules...)
|
2019-06-25 18:16:02 -07:00
|
|
|
}
|