Skip to content

Commit

Permalink
Merge pull request #174 from meshery/updateWriteFunction
Browse files Browse the repository at this point in the history
Update write function
  • Loading branch information
Jougan-0 authored Sep 21, 2024
2 parents 29d42c9 + 9f2f334 commit 9856034
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 5 deletions.
8 changes: 6 additions & 2 deletions models/v1alpha3/relationship/relationship_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) WriteRelationshipDefinition(relDirPath string, fileType string) error {
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
}
err := utils.WriteJSONToFile[RelationshipDefinition](relPath, r)
return err
}
Expand Down
28 changes: 26 additions & 2 deletions models/v1beta1/component/component_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,38 @@ 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
}
return true, nil
}
func (c *ComponentDefinition) ReplaceSVGData(baseDir string) error {

compStyle := c.Styles
if compStyle != nil {
svgColor, err := utils.ReadSVGData(baseDir, compStyle.SvgColor)
if err == nil {
compStyle.SvgColor = svgColor
} else {
return err
}
svgWhite, err := utils.ReadSVGData(baseDir, compStyle.SvgWhite)
if err == nil {
compStyle.SvgWhite = svgWhite
} else {
return err
}
}
c.Styles = compStyle
return nil
}
36 changes: 36 additions & 0 deletions models/v1beta1/import.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion models/v1beta1/model/model_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]))
}
Expand Down Expand Up @@ -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
}
156 changes: 156 additions & 0 deletions schemas/constructs/openapi/mesheryHandlers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
openapi: 3.0.0
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:
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

0 comments on commit 9856034

Please sign in to comment.