From 42036de4236fbe253110adcbe8d48b178ce3cc8c Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 16 Sep 2024 15:39:13 +0530 Subject: [PATCH 1/5] update wrtie to file system to suppport both yaml and json w/signoff Signed-off-by: Jougan-0 --- models/v1alpha3/relationship/relationship_helper.go | 8 ++++++-- models/v1beta1/component/component_helper.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/models/v1alpha3/relationship/relationship_helper.go b/models/v1alpha3/relationship/relationship_helper.go index a6b9e65156..d4d0ccda7b 100644 --- a/models/v1alpha3/relationship/relationship_helper.go +++ b/models/v1alpha3/relationship/relationship_helper.go @@ -50,8 +50,12 @@ func (r *RelationshipDefinition) UpdateStatus(db *database.Handler, status entit return nil } -func (r RelationshipDefinition) WriteComponentDefinition(relDirPath string) error { - relPath := filepath.Join(relDirPath, string(r.Kind), string(r.Type())+".json") +func (r RelationshipDefinition) WriteComponentDefinition(relDirPath string, fileType string) error { + relPath := filepath.Join(relDirPath, string(r.Kind), string(r.Type())+fileType) + if fileType == "yaml" { + err := utils.WriteYamlToFile[RelationshipDefinition](relPath, r) + return err + } err := utils.WriteJSONToFile[RelationshipDefinition](relPath, r) return err } diff --git a/models/v1beta1/component/component_helper.go b/models/v1beta1/component/component_helper.go index 408f120aa5..ea25699c35 100644 --- a/models/v1beta1/component/component_helper.go +++ b/models/v1beta1/component/component_helper.go @@ -65,12 +65,16 @@ func (m *ComponentDefinition) UpdateStatus(db *database.Handler, status entity.E return nil } -func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string) (bool, error) { +func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string, fileType string) (bool, error) { if c.Component.Kind == "" { return false, nil } - componentPath := filepath.Join(componentDirPath, c.Component.Kind+".json") + componentPath := filepath.Join(componentDirPath, c.Component.Kind+fileType) if _, err := os.Stat(componentPath); err != nil { + if fileType == "yaml" { + err := utils.WriteYamlToFile[ComponentDefinition](componentPath, c) + return false, err + } err := utils.WriteJSONToFile[ComponentDefinition](componentPath, c) return false, err } From 17216b9eb927612cf80a771eeef02e4e3295ffd2 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 16 Sep 2024 16:01:33 +0530 Subject: [PATCH 2/5] feat: convert from path with actual svg w/signoff Signed-off-by: Jougan-0 --- .../relationship/relationship_helper.go | 2 +- models/v1beta1/component/component_helper.go | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/models/v1alpha3/relationship/relationship_helper.go b/models/v1alpha3/relationship/relationship_helper.go index d4d0ccda7b..987acbe9da 100644 --- a/models/v1alpha3/relationship/relationship_helper.go +++ b/models/v1alpha3/relationship/relationship_helper.go @@ -50,7 +50,7 @@ func (r *RelationshipDefinition) UpdateStatus(db *database.Handler, status entit return nil } -func (r RelationshipDefinition) WriteComponentDefinition(relDirPath string, fileType string) error { +func (r RelationshipDefinition) WriteRelationshipDefinition(relDirPath string, fileType string) error { relPath := filepath.Join(relDirPath, string(r.Kind), string(r.Type())+fileType) if fileType == "yaml" { err := utils.WriteYamlToFile[RelationshipDefinition](relPath, r) diff --git a/models/v1beta1/component/component_helper.go b/models/v1beta1/component/component_helper.go index ea25699c35..f47ed3ac45 100644 --- a/models/v1beta1/component/component_helper.go +++ b/models/v1beta1/component/component_helper.go @@ -11,6 +11,7 @@ import ( "github.com/layer5io/meshkit/database" "github.com/layer5io/meshkit/models/meshmodel/entity" "github.com/layer5io/meshkit/utils" + "github.com/meshery/schemas/models/v1beta1/model" "gorm.io/gorm/clause" ) @@ -80,3 +81,57 @@ func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string, f } return true, nil } +func ReplaceSVGData(model *model.ModelDefinition, baseDir string) error { + // Function to read SVG data from file + readSVGData := func(path string) (string, error) { + path = baseDir + path // adjust path as needed + svgData, err := os.ReadFile(path) + if err != nil { + return "", err + } + return string(svgData), nil + } + // Replace SVG paths with actual data in metadata + metadata := model.Metadata + if metadata.SvgColor != "" { + svgData, err := readSVGData(metadata.SvgColor) + if err == nil { + metadata.SvgColor = svgData + } else { + return err + } + } + if metadata.SvgWhite != "" { + svgData, err := readSVGData(metadata.SvgWhite) + if err == nil { + metadata.SvgWhite = svgData + } else { + return err + } + } + components, ok := model.Components.([]ComponentDefinition) + if !ok { + return fmt.Errorf("invalid type for Components field") + } + // Replace SVG paths with actual data in components + for i := range components { + compStyle := components[i].Styles + if compStyle != nil { + svgColor, err := readSVGData(compStyle.SvgColor) + if err == nil { + compStyle.SvgColor = svgColor + } else { + return err + } + svgWhite, err := readSVGData(compStyle.SvgWhite) + if err == nil { + compStyle.SvgWhite = svgWhite + } else { + return err + } + } + components[i].Styles = compStyle + } + model.Components = components + return nil +} From 0317a8c3a98cd37c742157d2c6a7f5433ff89f35 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 16 Sep 2024 17:59:25 +0530 Subject: [PATCH 3/5] update svg function w/signoff Signed-off-by: Jougan-0 --- .../relationship/relationship_helper.go | 2 +- models/v1beta1/component/component_helper.go | 55 ++++--------------- models/v1beta1/model/model_helper.go | 24 +++++++- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/models/v1alpha3/relationship/relationship_helper.go b/models/v1alpha3/relationship/relationship_helper.go index 987acbe9da..e6f8ecac92 100644 --- a/models/v1alpha3/relationship/relationship_helper.go +++ b/models/v1alpha3/relationship/relationship_helper.go @@ -51,7 +51,7 @@ func (r *RelationshipDefinition) UpdateStatus(db *database.Handler, status entit } func (r RelationshipDefinition) WriteRelationshipDefinition(relDirPath string, fileType string) error { - relPath := filepath.Join(relDirPath, string(r.Kind), string(r.Type())+fileType) + relPath := filepath.Join(relDirPath, fmt.Sprintf("%s-%s.%s", r.Kind, utils.GetRandomAlphabetsOfDigit(3), fileType)) if fileType == "yaml" { err := utils.WriteYamlToFile[RelationshipDefinition](relPath, r) return err diff --git a/models/v1beta1/component/component_helper.go b/models/v1beta1/component/component_helper.go index f47ed3ac45..e4aace4b8a 100644 --- a/models/v1beta1/component/component_helper.go +++ b/models/v1beta1/component/component_helper.go @@ -11,7 +11,6 @@ import ( "github.com/layer5io/meshkit/database" "github.com/layer5io/meshkit/models/meshmodel/entity" "github.com/layer5io/meshkit/utils" - "github.com/meshery/schemas/models/v1beta1/model" "gorm.io/gorm/clause" ) @@ -70,7 +69,7 @@ func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string, f if c.Component.Kind == "" { return false, nil } - componentPath := filepath.Join(componentDirPath, c.Component.Kind+fileType) + componentPath := filepath.Join(componentDirPath, c.Component.Kind+"."+fileType) if _, err := os.Stat(componentPath); err != nil { if fileType == "yaml" { err := utils.WriteYamlToFile[ComponentDefinition](componentPath, c) @@ -81,57 +80,23 @@ func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string, f } return true, nil } -func ReplaceSVGData(model *model.ModelDefinition, baseDir string) error { - // Function to read SVG data from file - readSVGData := func(path string) (string, error) { - path = baseDir + path // adjust path as needed - svgData, err := os.ReadFile(path) - if err != nil { - return "", err - } - return string(svgData), nil - } - // Replace SVG paths with actual data in metadata - metadata := model.Metadata - if metadata.SvgColor != "" { - svgData, err := readSVGData(metadata.SvgColor) +func (c *ComponentDefinition) ReplaceSVGData(baseDir string) error { + + compStyle := c.Styles + if compStyle != nil { + svgColor, err := utils.ReadSVGData(baseDir, compStyle.SvgColor) if err == nil { - metadata.SvgColor = svgData + compStyle.SvgColor = svgColor } else { return err } - } - if metadata.SvgWhite != "" { - svgData, err := readSVGData(metadata.SvgWhite) + svgWhite, err := utils.ReadSVGData(baseDir, compStyle.SvgWhite) if err == nil { - metadata.SvgWhite = svgData + compStyle.SvgWhite = svgWhite } else { return err } } - components, ok := model.Components.([]ComponentDefinition) - if !ok { - return fmt.Errorf("invalid type for Components field") - } - // Replace SVG paths with actual data in components - for i := range components { - compStyle := components[i].Styles - if compStyle != nil { - svgColor, err := readSVGData(compStyle.SvgColor) - if err == nil { - compStyle.SvgColor = svgColor - } else { - return err - } - svgWhite, err := readSVGData(compStyle.SvgWhite) - if err == nil { - compStyle.SvgWhite = svgWhite - } else { - return err - } - } - components[i].Styles = compStyle - } - model.Components = components + c.Styles = compStyle return nil } diff --git a/models/v1beta1/model/model_helper.go b/models/v1beta1/model/model_helper.go index abbb19d2da..1e1fc703e8 100644 --- a/models/v1beta1/model/model_helper.go +++ b/models/v1beta1/model/model_helper.go @@ -42,7 +42,7 @@ func (m *ModelDefinition) GenerateID() (uuid.UUID, error) { if err != nil { return uuid.UUID{}, err } - + hash := md5.Sum(byt) return uuid.FromString(hex.EncodeToString(hash[:])) } @@ -138,3 +138,25 @@ func registerModel(db *database.Handler, regID, modelID uuid.UUID) error { } return nil } +func (m *ModelDefinition) ReplaceSVGData(baseDir string) error { + + metadata := m.Metadata + if metadata.SvgColor != "" { + svgData, err := utils.ReadSVGData(baseDir, metadata.SvgColor) + if err == nil { + metadata.SvgColor = svgData + } else { + return err + } + } + if metadata.SvgWhite != "" { + svgData, err := utils.ReadSVGData(baseDir, metadata.SvgWhite) + if err == nil { + metadata.SvgWhite = svgData + } else { + return err + } + } + m.Metadata = metadata + return nil +} From 8e06d5f7ea1b885bb31c47775ffe376d0bf7b293 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Wed, 18 Sep 2024 23:56:27 +0530 Subject: [PATCH 4/5] generate schemas for importRequest w/signoff Signed-off-by: Jougan-0 --- models/v1beta1/import.go | 20 ++++ .../constructs/openapi/mesheryHandlers.yml | 109 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 models/v1beta1/import.go create mode 100644 schemas/constructs/openapi/mesheryHandlers.yml diff --git a/models/v1beta1/import.go b/models/v1beta1/import.go new file mode 100644 index 0000000000..a1500a711d --- /dev/null +++ b/models/v1beta1/import.go @@ -0,0 +1,20 @@ +// Package v1beta1 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.3.0 DO NOT EDIT. +package v1beta1 + +// ImportBody defines model for ImportBody. +type ImportBody struct { + FileName string `json:"file_name"` + + // ModelFile This represents the binary content of the file as a byte array + ModelFile []byte `json:"model_file"` + Url string `json:"url"` +} + +// ImportRequest defines model for ImportRequest. +type ImportRequest struct { + ImportBody ImportBody `json:"importBody"` + Register bool `json:"register"` + UploadType string `json:"uploadType"` +} diff --git a/schemas/constructs/openapi/mesheryHandlers.yml b/schemas/constructs/openapi/mesheryHandlers.yml new file mode 100644 index 0000000000..c01b621595 --- /dev/null +++ b/schemas/constructs/openapi/mesheryHandlers.yml @@ -0,0 +1,109 @@ +openapi: 3.0.0 +info: + title: Meshmodels API + description: API for registering and exporting mesh models + version: 1.0.0 + +paths: + /api/meshmodels/register: + post: + summary: Register mesh models + operationId: RegisterMeshmodels + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/ImportRequest' + responses: + '200': + description: Successful registration + content: + application/json: + schema: + type: object + properties: + message: + type: string + '400': + description: Invalid request format + '500': + description: Internal server error + + /api/meshmodels/export: + get: + summary: Export a mesh model + operationId: ExportModel + parameters: + - in: query + name: id + schema: + type: string + required: true + - in: query + name: name + schema: + type: string + - in: query + name: version + schema: + type: string + - in: query + name: output_format + schema: + type: string + enum: [json, yaml, oci] + default: oci + - in: query + name: file_type + schema: + type: string + enum: [oci, tar, gzip] + default: oci + responses: + '200': + description: Successful export + content: + application/octet-stream: + schema: + type: string + format: binary + '400': + description: Invalid request format + '500': + description: Internal server error + +components: + schemas: + ImportRequest: + type: object + required: + - importBody + - uploadType + - register + properties: + importBody: + $ref: '#/components/schemas/ImportBody' + uploadType: + type: string + register: + type: boolean + nullable: false + + ImportBody: + type: object + required: + - model_file + - url + - file_name + properties: + model_file: + type: string + format: byte + description: "This represents the binary content of the file as a byte array" + url: + type: string + + file_name: + type: string + From 9f2f334d8507f916b2e6679940db06dec6f35f35 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Thu, 19 Sep 2024 18:47:02 +0530 Subject: [PATCH 5/5] schema for model form data w/signoff Signed-off-by: Jougan-0 --- models/v1beta1/import.go | 34 ++++-- .../constructs/openapi/mesheryHandlers.yml | 115 ++++++++++++------ 2 files changed, 106 insertions(+), 43 deletions(-) diff --git a/models/v1beta1/import.go b/models/v1beta1/import.go index a1500a711d..d10daa3f3b 100644 --- a/models/v1beta1/import.go +++ b/models/v1beta1/import.go @@ -5,16 +5,32 @@ package v1beta1 // ImportBody defines model for ImportBody. type ImportBody struct { - FileName string `json:"file_name"` - - // ModelFile This represents the binary content of the file as a byte array - ModelFile []byte `json:"model_file"` - Url string `json:"url"` + FileName string `json:"file_name" yaml:"file_name"` + Model Model `json:"model" yaml:"model"` + + // ModelFile represents the binary content of the file as a byte array + ModelFile []byte `json:"model_file" yaml:"model_file"` + Url string `json:"url" yaml:"url"` } -// ImportRequest defines model for ImportRequest. type ImportRequest struct { - ImportBody ImportBody `json:"importBody"` - Register bool `json:"register"` - UploadType string `json:"uploadType"` + ImportBody ImportBody `json:"importBody" yaml:"importBody"` + Register bool `json:"register" yaml:"register"` + UploadType string `json:"uploadType" yaml:"uploadType"` +} + +type Model struct { + Category string `json:"category" yaml:"category"` + IsAnnotation bool `json:"isAnnotation" yaml:"isAnnotation"` + Model string `json:"model" yaml:"model"` + ModelDisplayName string `json:"modelDisplayName" yaml:"modelDisplayName"` + PrimaryColor string `json:"primaryColor" yaml:"primaryColor"` + PublishToRegistry bool `json:"publishToRegistry" yaml:"publishToRegistry"` + Registrant string `json:"registrant" yaml:"registrant"` + SecondaryColor string `json:"secondaryColor" yaml:"secondaryColor"` + Shape string `json:"shape" yaml:"shape"` + SubCategory string `json:"subCategory" yaml:"subCategory"` + SvgColor string `json:"svgColor" yaml:"svgColor"` + SvgComplete string `json:"svgComplete" yaml:"svgComplete"` + SvgWhite string `json:"svgWhite" yaml:"svgWhite"` } diff --git a/schemas/constructs/openapi/mesheryHandlers.yml b/schemas/constructs/openapi/mesheryHandlers.yml index c01b621595..afb930068a 100644 --- a/schemas/constructs/openapi/mesheryHandlers.yml +++ b/schemas/constructs/openapi/mesheryHandlers.yml @@ -3,6 +3,87 @@ info: title: Meshmodels API description: API for registering and exporting mesh models version: 1.0.0 +components: + schemas: + ImportRequest: + type: object + required: + - importBody + - uploadType + - register + properties: + importBody: + $ref: '#/components/schemas/ImportBody' + uploadType: + type: string + register: + type: boolean + nullable: false + + ImportBody: + type: object + required: + - model_file + - url + - file_name + - model + properties: + model_file: + type: string + format: byte + description: "This represents the binary content of the file as a byte array" + url: + type: string + file_name: + type: string + model: + $ref: '#/components/schemas/Model' + + Model: + type: object + required: + - modelDisplayName + - registrant + - model + - category + - subCategory + - shape + - primaryColor + - secondaryColor + - svgColor + - svgWhite + - svgComplete + - isAnnotation + - publishToRegistry + properties: + modelDisplayName: + type: string + registrant: + type: string + model: + type: string + category: + type: string + subCategory: + type: string + shape: + type: string + primaryColor: + type: string + pattern: "^#[0-9A-Fa-f]{6}$" + secondaryColor: + type: string + pattern: "^#[0-9A-Fa-f]{6}$" + svgColor: + type: string + svgWhite: + type: string + svgComplete: + type: string + isAnnotation: + type: boolean + publishToRegistry: + type: boolean paths: /api/meshmodels/register: @@ -73,37 +154,3 @@ paths: '500': description: Internal server error -components: - schemas: - ImportRequest: - type: object - required: - - importBody - - uploadType - - register - properties: - importBody: - $ref: '#/components/schemas/ImportBody' - uploadType: - type: string - register: - type: boolean - nullable: false - - ImportBody: - type: object - required: - - model_file - - url - - file_name - properties: - model_file: - type: string - format: byte - description: "This represents the binary content of the file as a byte array" - url: - type: string - - file_name: - type: string -