diff --git a/models/catalog/v1alpha1/catalog.go b/models/catalog/v1alpha1/catalog.go index ba4c9b5b..1395a2cf 100644 --- a/models/catalog/v1alpha1/catalog.go +++ b/models/catalog/v1alpha1/catalog.go @@ -31,7 +31,6 @@ type CatalogData struct { func (cd *CatalogData) Scan(value interface{}) error { if value == nil { - cd = &CatalogData{} return nil } data, err := utils.Cast[[]byte](value) @@ -64,7 +63,7 @@ func (cd *CatalogData) IsNil() bool { return cd == nil || (len(cd.Compatibility) == 0 && cd.PatternCaveats == "" && cd.PatternInfo == "" && - cd.Type == "" && + cd.Type == "" && cd.ContentClass.String() != "") } @@ -99,4 +98,4 @@ func GetCatalogClasses() []ContentClass { Project, Community, } -} \ No newline at end of file +} diff --git a/models/meshmodel/core/v1alpha2/relationship.go b/models/meshmodel/core/v1alpha2/relationship.go index 2fafec42..67d0dcea 100644 --- a/models/meshmodel/core/v1alpha2/relationship.go +++ b/models/meshmodel/core/v1alpha2/relationship.go @@ -37,6 +37,11 @@ func (r RelationshipDefinition) TableName() string { func (r RelationshipDefinition) Type() entity.EntityType { return entity.RelationshipDefinition } + +func (r *RelationshipDefinition) GenerateID() (uuid.UUID, error) { + return uuid.New(), nil +} + func (r RelationshipDefinition) GetID() uuid.UUID { return r.ID } @@ -46,7 +51,7 @@ func (r *RelationshipDefinition) GetEntityDetail() string { } func (r *RelationshipDefinition) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) { - r.ID = uuid.New() + r.ID, _ = r.GenerateID() mid, err := r.Model.Create(db, hostID) if err != nil { return uuid.UUID{}, err diff --git a/models/meshmodel/core/v1beta1/category.go b/models/meshmodel/core/v1beta1/category.go index 8c870f24..8d60a202 100644 --- a/models/meshmodel/core/v1beta1/category.go +++ b/models/meshmodel/core/v1beta1/category.go @@ -30,6 +30,19 @@ const DefaultCategory = "Uncategorized" func (cat Category) Type() entity.EntityType { return entity.Category } + +func (cat *Category) GenerateID() (uuid.UUID, error) { + categoryIdentifier := Category{ + Name: cat.Name, + } + byt, err := json.Marshal(categoryIdentifier) + if err != nil { + return uuid.UUID{}, err + } + catID := uuid.NewSHA1(uuid.UUID{}, byt) + return catID, nil +} + func (cat Category) GetID() uuid.UUID { return cat.ID } @@ -42,11 +55,11 @@ func (cat *Category) Create(db *database.Handler, _ uuid.UUID) (uuid.UUID, error if cat.Name == "" { cat.Name = DefaultCategory } - byt, err := json.Marshal(cat) + + catID, err := cat.GenerateID() if err != nil { - return uuid.UUID{}, err + return catID, err } - catID := uuid.NewSHA1(uuid.UUID{}, byt) var category Category categoryCreationLock.Lock() defer categoryCreationLock.Unlock() diff --git a/models/meshmodel/core/v1beta1/component.go b/models/meshmodel/core/v1beta1/component.go index 07958119..bdfab291 100644 --- a/models/meshmodel/core/v1beta1/component.go +++ b/models/meshmodel/core/v1beta1/component.go @@ -57,6 +57,10 @@ func (c ComponentDefinition) Type() entity.EntityType { return entity.ComponentDefinition } +func (c *ComponentDefinition) GenerateID() (uuid.UUID, error) { + return uuid.New(), nil +} + func (c ComponentDefinition) GetID() uuid.UUID { return c.ID } @@ -66,7 +70,7 @@ func (c *ComponentDefinition) GetEntityDetail() string { } func (c *ComponentDefinition) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) { - c.ID = uuid.New() + c.ID, _ = c.GenerateID() isAnnotation, _ := c.Metadata["isAnnotation"].(bool) diff --git a/models/meshmodel/core/v1beta1/host.go b/models/meshmodel/core/v1beta1/host.go index ed6572d2..2c64e03b 100644 --- a/models/meshmodel/core/v1beta1/host.go +++ b/models/meshmodel/core/v1beta1/host.go @@ -58,12 +58,21 @@ type HostFilter struct { Offset int } -func (h *Host) Create(db *database.Handler) (uuid.UUID, error) { +func (h *Host) GenerateID() (uuid.UUID, error) { byt, err := json.Marshal(h) if err != nil { return uuid.UUID{}, err } hID := uuid.NewSHA1(uuid.UUID{}, byt) + return hID, nil +} + +func (h *Host) Create(db *database.Handler) (uuid.UUID, error) { + + hID, err := h.GenerateID() + if err != nil { + return uuid.UUID{}, err + } var host Host hostCreationLock.Lock() defer hostCreationLock.Unlock() diff --git a/models/meshmodel/core/v1beta1/models.go b/models/meshmodel/core/v1beta1/models.go index e0424527..3924ebd4 100644 --- a/models/meshmodel/core/v1beta1/models.go +++ b/models/meshmodel/core/v1beta1/models.go @@ -48,15 +48,8 @@ func (m Model) TableName() string { func (m Model) Type() entity.EntityType { return entity.Model } -func (m Model) GetID() uuid.UUID { - return m.ID -} -func (m *Model) GetEntityDetail() string { - return fmt.Sprintf("type: %s, model: %s, definition version: %s, version: %s", m.Type(), m.Name, m.Version, m.Model.Version) -} - -func (m *Model) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) { +func (m *Model) GenerateID() (uuid.UUID, error) { modelIdentifier := Model{ Registrant: m.Registrant, VersionMeta: m.VersionMeta, @@ -69,7 +62,23 @@ func (m *Model) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error if err != nil { return uuid.UUID{}, err } - modelID := uuid.NewSHA1(uuid.UUID{}, byt) + return uuid.NewSHA1(uuid.UUID{}, byt), nil +} + +func (m Model) GetID() uuid.UUID { + return m.ID +} + +func (m *Model) GetEntityDetail() string { + return fmt.Sprintf("type: %s, model: %s, definition version: %s, version: %s", m.Type(), m.Name, m.Version, m.Model.Version) +} + +func (m *Model) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) { + modelID, err := m.GenerateID() + if err != nil { + return modelID, err + } + var model Model if m.Name == "" { return uuid.UUID{}, fmt.Errorf("empty or invalid model name passed") diff --git a/models/meshmodel/core/v1beta1/policy.go b/models/meshmodel/core/v1beta1/policy.go index d0e53bf1..a4f68015 100644 --- a/models/meshmodel/core/v1beta1/policy.go +++ b/models/meshmodel/core/v1beta1/policy.go @@ -29,6 +29,10 @@ func (p PolicyDefinition) GetID() uuid.UUID { return p.ID } +func (p *PolicyDefinition) GenerateID() (uuid.UUID, error) { + return uuid.New(), nil +} + func (p PolicyDefinition) Type() entity.EntityType { return entity.PolicyDefinition } @@ -38,7 +42,8 @@ func (p *PolicyDefinition) GetEntityDetail() string { } func (p *PolicyDefinition) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) { - p.ID = uuid.New() + p.ID, _ = p.GenerateID() + mid, err := p.Model.Create(db, hostID) if err != nil { return uuid.UUID{}, err diff --git a/models/meshmodel/entity/types.go b/models/meshmodel/entity/types.go index d41f9638..8ab09c76 100644 --- a/models/meshmodel/entity/types.go +++ b/models/meshmodel/entity/types.go @@ -26,6 +26,7 @@ type Entity interface { // ComponentDefinitions and PolicyDefinitions are examples of entities Type() EntityType GetEntityDetail() string + GenerateID() (uuid.UUID, error) GetID() uuid.UUID Create(db *database.Handler, hostID uuid.UUID) (entityID uuid.UUID, err error) } diff --git a/models/meshmodel/registry/error.go b/models/meshmodel/registry/error.go index ccb068b0..6046a223 100644 --- a/models/meshmodel/registry/error.go +++ b/models/meshmodel/registry/error.go @@ -7,7 +7,6 @@ import ( ) var ( - ErrUnknownHostCode = "replace_me" ErrEmptySchemaCode = "replace_me" ErrMarshalingRegisteryAttemptsCode = "replace_me" @@ -15,7 +14,6 @@ var ( ErrRegisteringEntityCode = "replace_me" ErrUnknownHostInMapCode = "replace_me" ErrCreatingUserDataDirectoryCode = "replace_me" - ) func ErrUnknownHost(err error) error { diff --git a/utils/archive.go b/utils/archive.go index fbfcfe6d..8a50989e 100644 --- a/utils/archive.go +++ b/utils/archive.go @@ -39,4 +39,4 @@ func (tw *TarWriter) Compress(name string, data []byte) error { func (tw *TarWriter) Close() { _ = tw.Writer.Flush() _ = tw.Writer.Close() -} \ No newline at end of file +} diff --git a/utils/catalog/artifacthub_metadata.go b/utils/catalog/artifacthub_metadata.go index 733392bd..de4c2f41 100644 --- a/utils/catalog/artifacthub_metadata.go +++ b/utils/catalog/artifacthub_metadata.go @@ -66,4 +66,4 @@ type ArtifactHubMetadata struct { Key1 string `yaml:"key1,omitempty"` Key2 string `yaml:"key2,omitempty"` } `yaml:"annotations,omitempty"` -} \ No newline at end of file +} diff --git a/utils/catalog/package.go b/utils/catalog/package.go index 2aeedd02..f94123d7 100644 --- a/utils/catalog/package.go +++ b/utils/catalog/package.go @@ -52,4 +52,4 @@ func BuildArtifactHubPkg(name, downloadURL, user, version, createdAt string, cat }) return artifacthubPkg -} \ No newline at end of file +} diff --git a/utils/error.go b/utils/error.go index e9ab7233..ca04497a 100644 --- a/utils/error.go +++ b/utils/error.go @@ -164,4 +164,4 @@ func ErrExtractZip(err error, path string) error { func ErrReadDir(err error, dirPath string) error { return errors.New(ErrReadDirCode, errors.Alert, []string{"error reading directory"}, []string{err.Error()}, []string{fmt.Sprintf("Directory does not exist at the location %s", dirPath), "Insufficient permissions"}, []string{"Verify that directory exist at the provided location", "Verify sufficient directory read permission."}) -} \ No newline at end of file +} diff --git a/utils/store/store.go b/utils/store/store.go index 6ce54628..9d2f6229 100644 --- a/utils/store/store.go +++ b/utils/store/store.go @@ -34,7 +34,6 @@ func (s *GenerticThreadSafeStore[K]) Delete(key string) { delete(s.data, key) } - func (s *GenerticThreadSafeStore[K]) GetAllPairs() map[string]K { values := make(map[string]K, 0)