Skip to content

Commit

Permalink
Merge pull request #508 from MUzairS15/MUzairS15/chore/registry
Browse files Browse the repository at this point in the history
[Registry] Add `GenerateID` func for all entity interface.
  • Loading branch information
Mohd Uzair authored Jun 18, 2024
2 parents b7fd4b6 + 2a73d8f commit e6a89b4
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 26 deletions.
5 changes: 2 additions & 3 deletions models/catalog/v1alpha1/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() != "")
}

Expand Down Expand Up @@ -99,4 +98,4 @@ func GetCatalogClasses() []ContentClass {
Project,
Community,
}
}
}
7 changes: 6 additions & 1 deletion models/meshmodel/core/v1alpha2/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions models/meshmodel/core/v1beta1/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion models/meshmodel/core/v1beta1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)

Expand Down
11 changes: 10 additions & 1 deletion models/meshmodel/core/v1beta1/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 18 additions & 9 deletions models/meshmodel/core/v1beta1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion models/meshmodel/core/v1beta1/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions models/meshmodel/entity/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 0 additions & 2 deletions models/meshmodel/registry/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
)

var (

ErrUnknownHostCode = "replace_me"
ErrEmptySchemaCode = "replace_me"
ErrMarshalingRegisteryAttemptsCode = "replace_me"
ErrWritingRegisteryAttemptsCode = "replace_me"
ErrRegisteringEntityCode = "replace_me"
ErrUnknownHostInMapCode = "replace_me"
ErrCreatingUserDataDirectoryCode = "replace_me"

)

func ErrUnknownHost(err error) error {
Expand Down
2 changes: 1 addition & 1 deletion utils/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ func (tw *TarWriter) Compress(name string, data []byte) error {
func (tw *TarWriter) Close() {
_ = tw.Writer.Flush()
_ = tw.Writer.Close()
}
}
2 changes: 1 addition & 1 deletion utils/catalog/artifacthub_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ type ArtifactHubMetadata struct {
Key1 string `yaml:"key1,omitempty"`
Key2 string `yaml:"key2,omitempty"`
} `yaml:"annotations,omitempty"`
}
}
2 changes: 1 addition & 1 deletion utils/catalog/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ func BuildArtifactHubPkg(name, downloadURL, user, version, createdAt string, cat
})

return artifacthubPkg
}
}
2 changes: 1 addition & 1 deletion utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."})
}
}
1 change: 0 additions & 1 deletion utils/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e6a89b4

Please sign in to comment.