This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
/
new.go
110 lines (86 loc) · 2.97 KB
/
new.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
100
101
102
103
104
105
106
107
108
109
110
package rules
import (
"errors"
"github.com/oxyno-zeta/kubernetes-tagger/pkg/kubernetes-tagger/config"
)
// ErrRuleEmptyWhenCondition Error when "when condition" is empty.
var ErrRuleEmptyWhenCondition = errors.New("when condition mustn't be empty")
// ErrRuleConditionOperatorNotSupported Rule condition operator not supported.
var ErrRuleConditionOperatorNotSupported = errors.New("condition operator not supported")
// ErrRuleActionNotSupported Rule action not supported.
var ErrRuleActionNotSupported = errors.New("rule action not supported")
// ErrRuleEmptyTag Err Rule Empty Tag.
var ErrRuleEmptyTag = errors.New("tag rule mustn't be empty")
// ErrRuleQueryAndValueEmptyForAddCase Err Rule Query and Value empty for Add case.
var ErrRuleQueryAndValueEmptyForAddCase = errors.New("query and value mustn't be empty for add case")
// ErrRuleQueryAndValuePopulatedForAddCase Err Rule query and value populated for Add case.
var ErrRuleQueryAndValuePopulatedForAddCase = errors.New("query and value cannot be populated at the same time in add case")
// New Create rules array from ruleConfig with validation.
func New(ruleConfigs []*config.RuleConfig) ([]*Rule, error) {
rules := make([]*Rule, 0)
if ruleConfigs == nil {
return rules, nil
}
for _, ruleConfig := range ruleConfigs {
rule, err := newFromRuleConfig(ruleConfig)
// Check error
if err != nil {
return nil, err
}
rules = append(rules, rule)
}
return rules, nil
}
func newFromRuleConfig(ruleConfig *config.RuleConfig) (*Rule, error) {
if ruleConfig == nil {
return nil, nil // nolint: nilnil // No need
}
// Create rule
rule := &Rule{
Tag: ruleConfig.Tag,
Query: ruleConfig.Query,
Value: ruleConfig.Value,
}
switch ruleConfig.Action {
case string(RuleActionAdd):
rule.Action = RuleActionAdd
case string(RuleActionDelete):
rule.Action = RuleActionDelete
default:
return nil, ErrRuleActionNotSupported
}
// Check if rule is valid
if rule.Tag == "" {
return nil, ErrRuleEmptyTag
}
// Check add case
if rule.Action == RuleActionAdd && rule.Query == "" && rule.Value == "" {
return nil, ErrRuleQueryAndValueEmptyForAddCase
}
// Check that in add case we haven't query and value at the same time
if rule.Action == RuleActionAdd && rule.Query != "" && rule.Value != "" {
return nil, ErrRuleQueryAndValuePopulatedForAddCase
}
// Manage conditions
conditions := make([]*Condition, 0)
for _, conditionConfig := range ruleConfig.When {
if conditionConfig.Condition == "" {
return nil, ErrRuleEmptyWhenCondition
}
condition := &Condition{
Condition: conditionConfig.Condition,
Value: conditionConfig.Value,
}
switch conditionConfig.Operator {
case string(ConditionOperatorEqual):
condition.Operator = ConditionOperatorEqual
case string(ConditionOperatorNotEqual):
condition.Operator = ConditionOperatorNotEqual
default:
return nil, ErrRuleConditionOperatorNotSupported
}
conditions = append(conditions, condition)
}
rule.When = conditions
return rule, nil
}