Skip to content

Commit

Permalink
Merge pull request #249 from Revolyssup/models
Browse files Browse the repository at this point in the history
Add models to relationships
  • Loading branch information
Revolyssup authored Jan 9, 2023
2 parents 1661653 + ec24bde commit 48d2239
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
14 changes: 7 additions & 7 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ComponentDefinition struct {
DisplayName string `json:"display-name" gorm:"display-name"`
Format ComponentFormat `json:"format" yaml:"format"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
Model Models `json:"model"`
Model Model `json:"model"`
Schema string `json:"schema" yaml:"schema"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Expand All @@ -47,7 +47,7 @@ type ComponentDefinitionDB struct {
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type Models struct {
type Model struct {
ID uuid.UUID `json:"-"`
Name string `json:"name"`
Version string `json:"version"`
Expand All @@ -73,7 +73,7 @@ func CreateComponent(db *database.Handler, c ComponentDefinition) (uuid.UUID, er
return uuid.UUID{}, err
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Models
var model Model
componentCreationLock.Lock()
err = db.First(&model, "id = ?", modelID).Error
if err != nil && err != gorm.ErrRecordNotFound {
Expand Down Expand Up @@ -102,7 +102,7 @@ func CreateComponent(db *database.Handler, c ComponentDefinition) (uuid.UUID, er
func GetComponents(db *database.Handler, f ComponentFilter) (c []ComponentDefinition) {
var cdb []ComponentDefinitionDB
if f.ModelName != "" {
var models []Models
var models []Model
_ = db.Where("name = ?", f.ModelName).Find(&models).Error
if f.Name == "" {
_ = db.Find(&cdb).Error
Expand All @@ -119,15 +119,15 @@ func GetComponents(db *database.Handler, f ComponentFilter) (c []ComponentDefini
} else if f.Name != "" {
_ = db.Where("kind = ?", f.Name).Find(&cdb).Error
for _, compdb := range cdb {
var model Models
var model Model
db.First(&model, "id = ?", compdb.ModelID)
comp := compdb.GetComponentDefinition(model)
c = append(c, comp)
}
} else {
db.Find(&cdb)
for _, compdb := range cdb {
var model Models
var model Model
db.First(&model, "id = ?", compdb.ModelID)
comp := compdb.GetComponentDefinition(model)
c = append(c, comp)
Expand Down Expand Up @@ -160,7 +160,7 @@ func (cf *ComponentFilter) Create(m map[string]interface{}) {
cf.Name = m["name"].(string)
}

func (cmd *ComponentDefinitionDB) GetComponentDefinition(model Models) (c ComponentDefinition) {
func (cmd *ComponentDefinitionDB) GetComponentDefinition(model Model) (c ComponentDefinition) {
c.ID = cmd.ID
c.TypeMeta = cmd.TypeMeta
c.Format = cmd.Format
Expand Down
15 changes: 15 additions & 0 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v1alpha1

type ModelFilter struct {
Name string
Version string
Category string
}

// Create the filter from map[string]interface{}
func (cf *ModelFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
cf.Name = m["name"].(string)
}
49 changes: 42 additions & 7 deletions models/meshmodel/core/v1alpha1/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
"gorm.io/gorm"
)

// https://docs.google.com/drawings/d/1_qzQ_YxvCWPYrOBcdqGMlMwfbsZx96SBuIkbn8TfKhU/edit?pli=1
Expand All @@ -16,6 +17,7 @@ import (
type RelationshipDefinition struct {
ID uuid.UUID `json:"-"`
TypeMeta
Model Model `json:"model"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType" gorm:"subType"`
Selectors map[string]interface{} `json:"selectors" yaml:"selectors"`
Expand All @@ -24,7 +26,8 @@ type RelationshipDefinition struct {
}

type RelationshipDefinitionDB struct {
ID uuid.UUID `json:"-"`
ID uuid.UUID `json:"-"`
ModelID uuid.UUID `json:"-" gorm:"modelID"`
TypeMeta
Metadata []byte `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType"`
Expand All @@ -37,8 +40,9 @@ type RelationshipDefinitionDB struct {
// In the future, we will add support to query using `selectors` (using CUE)
// TODO: Add support for Model
type RelationshipFilter struct {
Kind string
SubType string
Kind string
SubType string
ModelName string
}

// Create the filter from map[string]interface{}
Expand All @@ -54,13 +58,19 @@ func GetRelationships(db *database.Handler, f RelationshipFilter) (rs []Relation
// https://gorm.io/docs/query.html#Struct-amp-Map-Conditions
_ = db.Where(&RelationshipDefinitionDB{SubType: f.SubType, TypeMeta: TypeMeta{Kind: f.Kind}}).Find(&rdb)
for _, reldb := range rdb {
rel := reldb.GetRelationshipDefinition()
rs = append(rs, rel)
var mod Model
if f.ModelName != "" {
db.Where("id = ?", reldb.ModelID).Where("name = ?", f.ModelName).Find(&mod)
}
if mod.Name != "" { //relationships with a valid model name will be returned
rel := reldb.GetRelationshipDefinition(mod)
rs = append(rs, rel)
}
}
return
}

func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition() (r RelationshipDefinition) {
func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition(m Model) (r RelationshipDefinition) {
r.ID = rdb.ID
r.TypeMeta = rdb.TypeMeta
if r.Metadata == nil {
Expand All @@ -73,6 +83,7 @@ func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition() (r Relationship
_ = json.Unmarshal(rdb.Selectors, &r.Selectors)
r.SubType = rdb.SubType
r.Kind = rdb.Kind
r.Model = m
return
}

Expand All @@ -85,8 +96,31 @@ func (r RelationshipDefinition) GetID() uuid.UUID {

func CreateRelationship(db *database.Handler, r RelationshipDefinition) (uuid.UUID, error) {
r.ID = uuid.New()
tempModelID := uuid.New()
byt, err := json.Marshal(r.Model)
if err != nil {
return uuid.UUID{}, err
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Model
componentCreationLock.Lock()
err = db.First(&model, "id = ?", modelID).Error
if err != nil && err != gorm.ErrRecordNotFound {
return uuid.UUID{}, err
}
if model.ID == tempModelID || err == gorm.ErrRecordNotFound { //The model is already not present and needs to be inserted
model = r.Model
model.ID = modelID
err = db.Create(&model).Error
if err != nil {
componentCreationLock.Unlock()
return uuid.UUID{}, err
}
}
componentCreationLock.Unlock()
rdb := r.GetRelationshipDefinitionDB()
err := db.Create(&rdb).Error
rdb.ModelID = model.ID
err = db.Create(&rdb).Error
if err != nil {
return uuid.UUID{}, err
}
Expand All @@ -100,5 +134,6 @@ func (r *RelationshipDefinition) GetRelationshipDefinitionDB() (rdb Relationship
rdb.Selectors, _ = json.Marshal(r.Selectors)
rdb.Kind = r.Kind
rdb.SubType = r.SubType
rdb.ModelID = r.Model.ID
return
}
22 changes: 19 additions & 3 deletions models/meshmodel/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewRegistryManager(db *database.Handler) (*RegistryManager, error) {
&Host{},
&v1alpha1.ComponentDefinitionDB{},
&v1alpha1.RelationshipDefinitionDB{},
&v1alpha1.Models{},
&v1alpha1.Model{},
)
if err != nil {
return nil, err
Expand All @@ -83,7 +83,7 @@ func (rm *RegistryManager) Cleanup() {
&Registry{},
&Host{},
&v1alpha1.ComponentDefinitionDB{},
&v1alpha1.Models{},
&v1alpha1.Model{},
)
}
func (rm *RegistryManager) RegisterEntity(h Host, en Entity) error {
Expand Down Expand Up @@ -150,7 +150,23 @@ func (rm *RegistryManager) GetEntities(f types.Filter) []Entity {
return nil
}
}

func (rm *RegistryManager) GetModels(f types.Filter) []v1alpha1.Model {
var mod []v1alpha1.Model
finder := rm.db.Model(&mod)
if mf, ok := f.(*v1alpha1.ModelFilter); ok {
if mf.Name != "" {
finder = finder.Where("name = ?", mf.Name)
}
if mf.Version != "" {
finder = finder.Where("version = ?", mf.Version)
}
if mf.Category != "" {
finder = finder.Where("category = ?", mf.Category)
}
}
_ = finder.Find(&mod).Error
return mod
}
func (rm *RegistryManager) GetRegistrant(e Entity) Host {
eID := e.GetID()
var reg Registry
Expand Down

0 comments on commit 48d2239

Please sign in to comment.