generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 78
/
policy.go
121 lines (108 loc) · 3 KB
/
policy.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
111
112
113
114
115
116
117
118
119
120
121
package v1alpha1
import (
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
)
type PolicyDefinition struct {
ID uuid.UUID `json:"-"`
TypeMeta
Model Model `json:"model"`
SubType string `json:"subType" yaml:"subType"`
Expression map[string]interface{} `json:"expression" yaml:"expression"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type PolicyDefinitionDB struct {
ID uuid.UUID `json:"-"`
ModelID uuid.UUID `json:"-" gorm:"modelID"`
TypeMeta
SubType string `json:"subType" yaml:"subType"`
Expression []byte `json:"expression" yaml:"expression"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type PolicyFilter struct {
Kind string
Greedy bool
SubType string
ModelName string
OrderOn string
Sort string
Limit int
Offset int
}
func (pf *PolicyFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
}
func (p PolicyDefinition) GetID() uuid.UUID {
return p.ID
}
func (p PolicyDefinition) Type() types.CapabilityType {
return types.PolicyDefinition
}
func GetMeshModelPolicy(db *database.Handler, f PolicyFilter) (pl []PolicyDefinition) {
type componentDefinitionWithModel struct {
PolicyDefinitionDB
Model
}
var componentDefinitionsWithModel []componentDefinitionWithModel
finder := db.Model(&PolicyDefinitionDB{}).
Select("policy_definition_dbs.*, models_dbs.*").
Joins("JOIN model_dbs ON model_dbs.id = policy_definition_dbs.model_id")
if f.Kind != "" {
finder = finder.Where("policy_definition_dbs.kind = ?", f.Kind)
}
if f.SubType != "" {
finder = finder.Where("policy_definition_dbs.sub_type = ?", f.SubType)
}
if f.ModelName != "" {
finder = finder.Where("model_dbs.name = ?", f.ModelName)
}
err := finder.Scan(&componentDefinitionsWithModel).Error
if err != nil {
fmt.Println(err.Error())
}
for _, cm := range componentDefinitionsWithModel {
pl = append(pl, cm.PolicyDefinitionDB.GetPolicyDefinition(cm.Model))
}
return pl
}
func (pdb *PolicyDefinitionDB) GetPolicyDefinition(m Model) (p PolicyDefinition) {
p.ID = pdb.ID
p.TypeMeta = pdb.TypeMeta
p.Model = m
p.SubType = pdb.SubType
if p.Expression == nil {
p.Expression = make(map[string]interface{})
}
_ = json.Unmarshal(pdb.Expression, &p.Expression)
return
}
func CreatePolicy(db *database.Handler, p PolicyDefinition) (uuid.UUID, uuid.UUID, error) {
p.ID = uuid.New()
mid, err := CreateModel(db, p.Model)
if err != nil {
return uuid.UUID{}, uuid.UUID{}, err
}
pdb := p.GetPolicyDefinitionDB()
pdb.ModelID = mid
err = db.Create(&pdb).Error
if err != nil {
return uuid.UUID{}, uuid.UUID{}, err
}
return pdb.ID, mid, nil
}
func (p *PolicyDefinition) GetPolicyDefinitionDB() (pdb PolicyDefinitionDB) {
pdb.ID = p.ID
pdb.TypeMeta = p.TypeMeta
pdb.SubType = p.SubType
pdb.ModelID = p.Model.ID
pdb.Expression, _ = json.Marshal(p.Expression)
return
}