generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 77
/
relationship.go
164 lines (152 loc) · 5.01 KB
/
relationship.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package v1alpha1
import (
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
"gorm.io/gorm/clause"
)
// https://docs.google.com/drawings/d/1_qzQ_YxvCWPYrOBcdqGMlMwfbsZx96SBuIkbn8TfKhU/edit?pli=1
// see RELATIONSHIPDEFINITIONS table in the diagram
// swagger:response RelationshipDefinition
// TODO: Add support for Model
type RelationshipDefinition struct {
ID uuid.UUID `json:"-"`
TypeMeta
Model Model `json:"model"`
HostName string `json:"hostname"`
HostID uuid.UUID `json:"hostID"`
DisplayHostName string `json:"displayhostname"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType" gorm:"subType"`
Selectors map[string]interface{} `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type RelationshipDefinitionDB struct {
ID uuid.UUID `json:"-"`
ModelID uuid.UUID `json:"-" gorm:"modelID"`
TypeMeta
Metadata []byte `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType"`
Selectors []byte `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
// For now, only filtering by Kind and SubType are allowed.
// In the future, we will add support to query using `selectors` (using CUE)
// TODO: Add support for Model
type RelationshipFilter struct {
Kind string
Greedy bool //when set to true - instead of an exact match, kind will be prefix matched
SubType string
Version string
ModelName string
OrderOn string
Sort string //asc or desc. Default behavior is asc
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
}
// Create the filter from map[string]interface{}
func (rf *RelationshipFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
}
func GetMeshModelRelationship(db *database.Handler, f RelationshipFilter) (r []RelationshipDefinition, count int64) {
type componentDefinitionWithModel struct {
RelationshipDefinitionDB
ModelDB
CategoryDB
}
var componentDefinitionsWithModel []componentDefinitionWithModel
finder := db.Model(&RelationshipDefinitionDB{}).
Select("relationship_definition_dbs.*, model_dbs.*").
Joins("JOIN model_dbs ON relationship_definition_dbs.model_id = model_dbs.id"). //
Joins("JOIN category_dbs ON model_dbs.category_id = category_dbs.id") //
if f.Kind != "" {
if f.Greedy {
finder = finder.Where("relationship_definition_dbs.kind LIKE ?", "%"+f.Kind+"%")
} else {
finder = finder.Where("relationship_definition_dbs.kind = ?", f.Kind)
}
}
if f.SubType != "" {
finder = finder.Where("relationship_definition_dbs.sub_type = ?", f.SubType)
}
if f.ModelName != "" {
finder = finder.Where("model_dbs.name = ?", f.ModelName)
}
if f.Version != "" {
finder = finder.Where("model_dbs.version = ?", f.Version)
}
if f.OrderOn != "" {
if f.Sort == "desc" {
finder = finder.Order(clause.OrderByColumn{Column: clause.Column{Name: f.OrderOn}, Desc: true})
} else {
finder = finder.Order(f.OrderOn)
}
}
finder.Count(&count)
finder = finder.Offset(f.Offset)
if f.Limit != 0 {
finder = finder.Limit(f.Limit)
}
err := finder.
Scan(&componentDefinitionsWithModel).Error
if err != nil {
fmt.Println(err.Error()) //for debugging
}
for _, cm := range componentDefinitionsWithModel {
r = append(r, cm.RelationshipDefinitionDB.GetRelationshipDefinition(cm.ModelDB.GetModel(cm.CategoryDB.GetCategory(db))))
}
return r, count
}
func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition(m Model) (r RelationshipDefinition) {
r.ID = rdb.ID
r.TypeMeta = rdb.TypeMeta
if r.Metadata == nil {
r.Metadata = make(map[string]interface{})
}
_ = json.Unmarshal(rdb.Metadata, &r.Metadata)
if r.Selectors == nil {
r.Selectors = make(map[string]interface{})
}
_ = json.Unmarshal(rdb.Selectors, &r.Selectors)
r.SubType = rdb.SubType
r.Kind = rdb.Kind
r.Model = m
return
}
func (r RelationshipDefinition) Type() types.CapabilityType {
return types.RelationshipDefinition
}
func (r RelationshipDefinition) GetID() uuid.UUID {
return r.ID
}
func CreateRelationship(db *database.Handler, r RelationshipDefinition) (uuid.UUID, uuid.UUID, error) {
r.ID = uuid.New()
mid, err := CreateModel(db, r.Model)
if err != nil {
return uuid.UUID{}, uuid.UUID{}, err
}
rdb := r.GetRelationshipDefinitionDB()
rdb.ModelID = mid
err = db.Create(&rdb).Error
if err != nil {
return uuid.UUID{}, uuid.UUID{}, err
}
return r.ID, mid, err
}
func (r *RelationshipDefinition) GetRelationshipDefinitionDB() (rdb RelationshipDefinitionDB) {
rdb.ID = r.ID
rdb.TypeMeta = r.TypeMeta
rdb.Metadata, _ = json.Marshal(r.Metadata)
rdb.Selectors, _ = json.Marshal(r.Selectors)
rdb.Kind = r.Kind
rdb.SubType = r.SubType
rdb.ModelID = r.Model.ID
return
}