Skip to content

Add metadata field to Models table #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions models/meshmodel/core/v1alpha1/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package v1alpha1

import (
"encoding/json"
"sync"

"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"gorm.io/gorm"
)

var categoryCreationLock sync.Mutex //Each model will perform a check and if the category already doesn't exist, it will create a category. This lock will make sure that there are no race conditions.

// swagger:response Category
type Category struct {
ID uuid.UUID `json:"-"`
Name string `json:"name"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
}
type CategoryDB struct {
ID uuid.UUID `json:"-"`
Name string `json:"categoryName" gorm:"categoryName"`
Metadata []byte `json:"categoryMetadata" gorm:"categoryMetadata"`
}
type CategoryFilter struct {
Name 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
}

const DefaultCategory = "Miscellaneous"

// Create the filter from map[string]interface{}
func (cf *CategoryFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
cf.Name = m["name"].(string)
}
func CreateCategory(db *database.Handler, cat Category) (uuid.UUID, error) {
if cat.Name == "" {
cat.Name = DefaultCategory
}
byt, err := json.Marshal(cat)
if err != nil {
return uuid.UUID{}, err
}
catID := uuid.NewSHA1(uuid.UUID{}, byt)
var category CategoryDB
categoryCreationLock.Lock()
defer categoryCreationLock.Unlock()
err = db.First(&category, "id = ?", catID).Error
if err != nil && err != gorm.ErrRecordNotFound {
return uuid.UUID{}, err
}
if err == gorm.ErrRecordNotFound { //The category is already not present and needs to be inserted
cat.ID = catID
catdb := cat.GetCategoryDB(db)
err = db.Create(&catdb).Error
if err != nil {
return uuid.UUID{}, err
}
return catdb.ID, nil
}
return category.ID, nil
}

func (cdb *CategoryDB) GetCategory(db *database.Handler) (cat Category) {
cat.ID = cdb.ID
cat.Name = cdb.Name
_ = json.Unmarshal(cdb.Metadata, &cat.Metadata)
return
}
func (c *Category) GetCategoryDB(db *database.Handler) (catdb CategoryDB) {
catdb.ID = c.ID
catdb.Name = c.Name
catdb.Metadata, _ = json.Marshal(c.Metadata)
return
}
67 changes: 27 additions & 40 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -69,49 +68,33 @@ func emptySchemaCheck(schema string) (valid bool) {
}
func CreateComponent(db *database.Handler, c ComponentDefinition) (uuid.UUID, error) {
c.ID = uuid.New()
tempModelID := uuid.New()
byt, err := json.Marshal(c.Model)
mid, err := CreateModel(db, c.Model)
if err != nil {
return uuid.UUID{}, err
}
if !emptySchemaCheck(c.Schema) {
c.Metadata["hasInvalidSchema"] = true
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Model
modelCreationLock.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 = c.Model
model.ID = modelID
err = db.Create(&model).Error
if err != nil {
modelCreationLock.Unlock()
return uuid.UUID{}, err
}
}
modelCreationLock.Unlock()
cdb := c.GetComponentDefinitionDB()
cdb.ModelID = model.ID
cdb.ModelID = mid
err = db.Create(&cdb).Error
return c.ID, err
}
func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []ComponentDefinition) {
type componentDefinitionWithModel struct {
ComponentDefinitionDB
Model
ModelDB
CategoryDB
}

var componentDefinitionsWithModel []componentDefinitionWithModel
finder := db.Model(&ComponentDefinitionDB{}).
Select("component_definition_dbs.*, models.*").
Joins("JOIN models ON component_definition_dbs.model_id = models.id") //
Select("component_definition_dbs.*, model_dbs.*,category_dbs.*").
Joins("JOIN model_dbs ON component_definition_dbs.model_id = model_dbs.id").
Joins("JOIN category_dbs ON model_dbs.category_id = category_dbs.id") //
if f.Greedy {
if f.Name != "" && f.DisplayName != "" {
finder = finder.Where("component_definition_dbs.kind LIKE ? OR component_definition_dbs.display_name LIKE ?", f.Name+"%", f.DisplayName+"%")
finder = finder.Where("component_definition_dbs.kind LIKE ? OR display_name LIKE ?", f.Name+"%", f.DisplayName+"%")
} else if f.Name != "" {
finder = finder.Where("component_definition_dbs.kind LIKE ?", f.Name+"%")
} else if f.DisplayName != "" {
Expand All @@ -126,14 +109,16 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
}
}
if f.ModelName != "" && f.ModelName != "all" {
finder = finder.Where("models.name = ?", f.ModelName)
finder = finder.Where("model_dbs.name = ?", f.ModelName)
}
if f.APIVersion != "" {
finder = finder.Where("component_definition_dbs.api_version = ?", f.APIVersion)
}

if f.CategoryName != "" {
finder = finder.Where("category_dbs.name = ?", f.CategoryName)
}
if f.Version != "" {
finder = finder.Where("models.version = ?", f.Version)
finder = finder.Where("model_dbs.version = ?", f.Version)
}
if f.OrderOn != "" {
if f.Sort == "desc" {
Expand All @@ -155,23 +140,25 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
if f.Trim {
cm.Schema = ""
}
c = append(c, cm.ComponentDefinitionDB.GetComponentDefinition(cm.Model))
c = append(c, cm.ComponentDefinitionDB.GetComponentDefinition(cm.ModelDB.GetModel(cm.CategoryDB.GetCategory(db))))
}

return c
}

type ComponentFilter struct {
Name string
APIVersion string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
Trim bool //when set to true - the schema is not returned
DisplayName string
ModelName string
Version string
Sort string //asc or desc. Default behavior is asc
OrderOn string
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
Name string
APIVersion string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
Trim bool //when set to true - the schema is not returned
DisplayName string
ModelName string
CategoryName string
Version string
Sort string //asc or desc. Default behavior is asc
OrderOn string
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{}
Expand Down
77 changes: 70 additions & 7 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package v1alpha1

import (
"encoding/json"
"fmt"
"sync"

"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"gorm.io/gorm"
)

var modelCreationLock sync.Mutex //Each component/relationship will perform a check and if the model already doesn't exist, it will create a model. This lock will make sure that there are no race conditions.
Expand All @@ -19,20 +23,79 @@ type ModelFilter struct {
Offset int
}

// Create the filter from map[string]interface{}
func (cf *ModelFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
cf.Name = m["name"].(string)
}

// swagger:response Model
type Model struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Revolyssup, educate me: what's the difference between Model and ModelDB?

ID uuid.UUID `json:"-"`
Name string `json:"name"`
Version string `json:"version"`
DisplayName string `json:"displayName" gorm:"modelDisplayName"`
Category Category `json:"category"`
Metadata map[string]interface{} `json:"metadata" yaml:"modelMetadata"`
}
type ModelDB struct {
ID uuid.UUID `json:"-"`
Name string `json:"name"`
CategoryID uuid.UUID `json:"-" gorm:"categoryID"`
Name string `json:"modelName" gorm:"modelName"`
Version string `json:"version"`
DisplayName string `json:"modelDisplayName" gorm:"modelDisplayName"`
Category string `json:"category"`
SubCategory string `json:"subCategory" gorm:"subCategory"`
Metadata []byte `json:"modelMetadata" gorm:"modelMetadata"`
}

// Create the filter from map[string]interface{}
func (cf *ModelFilter) Create(m map[string]interface{}) {
if m == nil {
return
func CreateModel(db *database.Handler, cmodel Model) (uuid.UUID, error) {
byt, err := json.Marshal(cmodel)
if err != nil {
return uuid.UUID{}, err
}
cf.Name = m["name"].(string)
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model ModelDB
if cmodel.Name == "" {
return uuid.UUID{}, fmt.Errorf("empty or invalid model name passed")
}
modelCreationLock.Lock()
defer modelCreationLock.Unlock()
err = db.First(&model, "id = ?", modelID).Error
if err != nil && err != gorm.ErrRecordNotFound {
return uuid.UUID{}, err
}
if err == gorm.ErrRecordNotFound { //The model is already not present and needs to be inserted
id, err := CreateCategory(db, cmodel.Category)
if err != nil {
return uuid.UUID{}, err
}
cmodel.ID = modelID
mdb := cmodel.GetModelDB()
mdb.CategoryID = id
err = db.Create(&mdb).Error
if err != nil {
return uuid.UUID{}, err
}
return mdb.ID, nil
}
return model.ID, nil
}
func (cmd *ModelDB) GetModel(cat Category) (c Model) {
c.ID = cmd.ID
c.Category = cat
c.DisplayName = cmd.DisplayName
c.Name = cmd.Name
c.Version = cmd.Version
_ = json.Unmarshal(cmd.Metadata, &c.Metadata)
return
}
func (c *Model) GetModelDB() (cmd ModelDB) {
cmd.ID = c.ID
cmd.DisplayName = c.DisplayName
cmd.Name = c.Name
cmd.Version = c.Version
cmd.Metadata, _ = json.Marshal(c.Metadata)
return
}
27 changes: 4 additions & 23 deletions models/meshmodel/core/v1alpha1/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/core/types"
"gorm.io/gorm"
)

type PolicyDefinition struct {
Expand Down Expand Up @@ -59,15 +58,15 @@ func GetMeshModelPolicy(db *database.Handler, f PolicyFilter) (pl []PolicyDefini
var componentDefinitionsWithModel []componentDefinitionWithModel
finder := db.Model(&PolicyDefinitionDB{}).
Select("policy_definition_dbs.*, models.*").
Joins("JOIN models ON models.id = policy_definition_dbs.model_id")
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("models.name = ?", f.ModelName)
finder = finder.Where("model_dbs.name = ?", f.ModelName)
}
err := finder.Scan(&componentDefinitionsWithModel).Error
if err != nil {
Expand All @@ -94,30 +93,12 @@ func (pdb *PolicyDefinitionDB) GetPolicyDefinition(m Model) (p PolicyDefinition)

func CreatePolicy(db *database.Handler, p PolicyDefinition) (uuid.UUID, error) {
p.ID = uuid.New()
tempModelID := uuid.New()
byt, err := json.Marshal(p.Model)
mid, err := CreateModel(db, p.Model)
if err != nil {
return uuid.UUID{}, err
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Model
modelCreationLock.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 {
model = p.Model
model.ID = modelID
err = db.Create(&model).Error
if err != nil {
modelCreationLock.Unlock()
return uuid.UUID{}, err
}
}
modelCreationLock.Unlock()
pdb := p.GetPolicyDefinitionDB()
pdb.ModelID = model.ID
pdb.ModelID = mid
err = db.Create(&pdb).Error
if err != nil {
return uuid.UUID{}, err
Expand Down
Loading