From 85d817b683298ad4008deeb5844a705f93742124 Mon Sep 17 00:00:00 2001 From: Lee Calcote Date: Fri, 1 Dec 2023 18:14:40 -0600 Subject: [PATCH 1/3] feat: annotations property for models Signed-off-by: Lee Calcote --- models/meshmodel/core/v1alpha1/component.go | 5 ++--- models/meshmodel/core/v1alpha1/models.go | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index d5403135..8daeb923 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -108,7 +108,6 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon 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 display_name LIKE ?", "%"+f.Name+"%", f.DisplayName+"%") @@ -128,7 +127,7 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon if f.ModelName != "" && f.ModelName != "all" { finder = finder.Where("model_dbs.name = ?", f.ModelName) } - + if f.Annotations == "true" { finder = finder.Where("component_definition_dbs.metadata->>'isAnnotation' = true") } else if f.Annotations == "false" { @@ -188,7 +187,7 @@ type ComponentFilter struct { OrderOn string Limit int //If 0 or unspecified then all records are returned and limit is not used Offset int - Annotations string + Annotations string //When this query parameter is "true", only components with the "isAnnotation" property set to true are returned. When this query parameter is "false", all components except those considered to be annotation components are returned. Any other value of the query parameter results in both annotations as well as non-annotation models being returned. } // Create the filter from map[string]interface{} diff --git a/models/meshmodel/core/v1alpha1/models.go b/models/meshmodel/core/v1alpha1/models.go index a8d53afc..b9c92455 100644 --- a/models/meshmodel/core/v1alpha1/models.go +++ b/models/meshmodel/core/v1alpha1/models.go @@ -14,15 +14,16 @@ import ( 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. type ModelFilter struct { Name string - Registrant string //name of the registrant for a given model + Registrant string //name of the registrant for a given model DisplayName string //If Name is already passed, avoid passing Display name unless greedy=true, else the filter will translate to an AND returning only the models where name and display name match exactly. Ignore, if this behavior is expected. Greedy bool //when set to true - instead of an exact match, name will be prefix matched. Also an OR will be performed of name and display_name Version string Category 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 + Limit int //If 0 or unspecified then all records are returned and limit is not used Offset int + Annotations string //When this query parameter is "true", only models with the "isAnnotation" property set to true are returned. When this query parameter is "false", all models except those considered to be annotation models are returned. Any other value of the query parameter results in both annoations as well as non-annotation models being returned. } // Create the filter from map[string]interface{} From 327c100ed38ef6fc15e2c0c95ec5205e410aa886 Mon Sep 17 00:00:00 2001 From: Lee Calcote Date: Fri, 1 Dec 2023 19:12:47 -0600 Subject: [PATCH 2/3] feat: add db query filter Signed-off-by: Lee Calcote --- models/meshmodel/registry/registry.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/models/meshmodel/registry/registry.go b/models/meshmodel/registry/registry.go index cbad3ca8..95139cf7 100644 --- a/models/meshmodel/registry/registry.go +++ b/models/meshmodel/registry/registry.go @@ -343,6 +343,11 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1 if mf.Registrant != "" { finder = finder.Where("hosts.hostname = ?", mf.Registrant) } + if mf.Annotations == "true" { + finder = finder.Where("model_dbs.metadata->>'isAnnotation' = true") + } else if mf.Annotations == "false" { + finder = finder.Where("model_dbs.metadata->>'isAnnotation' = false") + } if mf.OrderOn != "" { if mf.Sort == "desc" { finder = finder.Order(clause.OrderByColumn{Column: clause.Column{Name: mf.OrderOn}, Desc: true}) From 803ac87d1257a1967a9fba4f0826d838790d6b79 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Sat, 2 Dec 2023 23:50:37 +0530 Subject: [PATCH 3/3] add support to filter models based on annotation property Signed-off-by: MUzairS15 --- models/meshmodel/registry/registry.go | 5 +++++ utils/manifests/getComponents.go | 1 - utils/utils.go | 3 +-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/models/meshmodel/registry/registry.go b/models/meshmodel/registry/registry.go index 95139cf7..9be106d5 100644 --- a/models/meshmodel/registry/registry.go +++ b/models/meshmodel/registry/registry.go @@ -334,6 +334,11 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1 finder = finder.Where("model_dbs.display_name = ?", mf.DisplayName) } } + if mf.Annotations == "true" { + finder = finder.Where("model_dbs.metadata->>'isAnnotation' = true") + } else if mf.Annotations == "false" { + finder = finder.Where("model_dbs.metadata->>'isAnnotation' = false") + } if mf.Version != "" { finder = finder.Where("model_dbs.version = ?", mf.Version) } diff --git a/utils/manifests/getComponents.go b/utils/manifests/getComponents.go index b9fb1bdf..9ae8e479 100644 --- a/utils/manifests/getComponents.go +++ b/utils/manifests/getComponents.go @@ -12,7 +12,6 @@ import ( k8s "github.com/layer5io/meshkit/utils/kubernetes" ) - func GetFromManifest(ctx context.Context, url string, resource int, cfg Config) (*Component, error) { manifest, err := utils.ReadFileSource(url) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index d8aa3599..0ae44d41 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -247,7 +247,6 @@ func Contains[G []K, K comparable](slice G, ele K) bool { return false } - func Cast[K any](val interface{}) (K, error) { assertedValue, ok := val.(K) if !ok { @@ -256,7 +255,7 @@ func Cast[K any](val interface{}) (K, error) { return assertedValue, nil } -func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledvalue toType, err error){ +func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledvalue toType, err error) { data, err := Marshal(val) if err != nil { return