Skip to content

Commit

Permalink
Merge branch 'master' into describe-unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushrakesh authored Jul 10, 2024
2 parents cf3097d + cc30fc1 commit b971e59
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 13 deletions.
2 changes: 1 addition & 1 deletion models/meshmodel/core/v1alpha2/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gorm.io/gorm/clause"
)

const SchemaVersion = "core.meshery.io/v1alpha2"
const RelationshipSchemaVersion = "relationships.meshery.io/v1alpha2"

type RelationshipDefinition struct {
ID uuid.UUID `json:"id"`
Expand Down
2 changes: 2 additions & 0 deletions models/meshmodel/core/v1beta1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
JSON ComponentFormat = "JSON"
YAML ComponentFormat = "YAML"
CUE ComponentFormat = "CUE"

ComponentSchemaVersion = "components.meshery.io/v1beta1"
)

// Contains information as extracted from the core underlying component eg: Pod's apiVersion, kind and schema
Expand Down
18 changes: 12 additions & 6 deletions models/meshmodel/core/v1beta1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"gorm.io/gorm/clause"
)

const SchemaVersion = "core.meshery.io/v1beta1"
const ModelSchemaVersion = "models.meshery.io/v1beta1"

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.

Expand Down Expand Up @@ -120,14 +120,20 @@ func (m *Model) UpdateStatus(db *database.Handler, status entity.EntityStatus) e
return nil
}

func (c Model) WriteModelDefinition(modelDefPath string) error {
err := utils.CreateDirectory(modelDefPath)
// WriteModelDefinition writes out the model to the given `modelDefPath` in the `outputType` format.
// `outputType` can be `yaml` or `json`.
// Usage: model.WriteModelDefinition("./modelName/model.yaml", "yaml")
func (c Model) WriteModelDefinition(modelDefPath string, outputType string) error {
err := utils.CreateDirectory(filepath.Dir(modelDefPath))
if err != nil {
return err
}

modelFilePath := filepath.Join(modelDefPath, "model.json")
err = utils.WriteJSONToFile[Model](modelFilePath, c)
if(outputType == "json"){
err = utils.WriteJSONToFile[Model](modelDefPath, c)
}
if(outputType == "yaml"){
err = utils.WriteYamlToFile[Model](modelDefPath, c)
}
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var Configs = []CuePathConfig{DefaultPathConfig, DefaultPathConfig2}

func Generate(crd string) (v1beta1.ComponentDefinition, error) {
component := v1beta1.ComponentDefinition{}
component.SchemaVersion = v1beta1.SchemaVersion
component.SchemaVersion = v1beta1.ComponentSchemaVersion

component.Metadata = make(map[string]interface{})
crdCue, err := utils.YamlToCue(crd)
Expand Down
75 changes: 70 additions & 5 deletions utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,34 @@ var (
ErrCreateDirCode = "meshkit-11182"
// ErrDecodeYamlCode represents the error which is generated when yaml
// decode process fails
ErrDecodeYamlCode = "meshkit-11183"
ErrExtractTarXZCode = "meshkit-11184"
ErrExtractZipCode = "meshkit-11185"
ErrReadDirCode = "meshkit-11186"
ErrCompressToTarGZCode = "meshkit-11248"
ErrDecodeYamlCode = "meshkit-11183"
ErrExtractTarXZCode = "meshkit-11184"
ErrExtractZipCode = "meshkit-11185"
ErrReadDirCode = "meshkit-11186"
ErrInvalidSchemaVersionCode = "replace_me"
ErrFileWalkDirCode = "replace_me"
ErrRelPathCode = "replace_me"
ErrCopyFileCode = "replace_me"
ErrCloseFileCode = "replace_me"
ErrCompressToTarGZCode = "meshkit-11248"
)
var (
ErrExtractType = errors.New(
ErrUnmarshalTypeCode,
errors.Alert,
[]string{"Invalid extraction type"},
[]string{"The file type to be extracted is neither `tar.gz` nor `zip`."},
[]string{"Invalid object format. The file is not of type `zip` or `tar.gz`."},
[]string{"Make sure to check that the file type is `zip` or `tar.gz`."},
)
ErrInvalidSchemaVersion = errors.New(
ErrInvalidSchemaVersionCode,
errors.Alert,
[]string{"Invalid schema version"},
[]string{"The `schemaVersion` key in the JSON file is either empty or has an incorrect value."},
[]string{"The JSON file schema is not of type 'relationship' or 'component'.", "The `schemaVersion` key in the JSON should be either `relationships.meshery.io` or `component.meshery.io`."},
[]string{"Verify that the `schemaVersion` key in the JSON has the correct value."},
)
)

func ErrCueLookup(err error) error {
Expand Down Expand Up @@ -165,3 +188,45 @@ 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."})
}

func ErrFileWalkDir(err error, path string) error {
return errors.New(
ErrFileWalkDirCode,
errors.Alert,
[]string{"Error while walking through directory"},
[]string{err.Error()},
[]string{fmt.Sprintf("The directory %s does not exist.", path)},
[]string{"Verify that the correct directory path is provided."},
)
}
func ErrRelPath(err error, path string) error {
return errors.New(
ErrRelPathCode,
errors.Alert,
[]string{"Error determining relative path"},
[]string{err.Error()},
[]string{("The provided directory path is incorrect."), "The user might not have sufficient permission."},
[]string{"Verify the provided directory path is correct and if the user has sufficent permission."},
)
}
func ErrCopyFile(err error) error {
return errors.New(
ErrCopyFileCode,
errors.Alert,
[]string{"Error copying file"},
[]string{err.Error()},
[]string{("The file might not be accessible or the source and destination files are the same."), "The file might be corrupted."},
[]string{("Ensure the source and destination files are accessible and try again."), "Verify the integrity of the file and try again."},
)
}

func ErrCloseFile(err error) error {
return errors.New(
ErrCloseFileCode,
errors.Alert,
[]string{"Error closing file"},
[]string{err.Error()},
[]string{("Disk space might be full or the file might be corrupted."), "The user might not have sufficient permission."},
[]string{"Check for issues with file permissions or disk space and try again."},
)
}
40 changes: 40 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"strconv"
"strings"

"github.com/layer5io/meshkit/models/meshmodel/entity"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

// transforms the keys of a Map recursively with the given transform function
Expand Down Expand Up @@ -345,6 +347,25 @@ func MergeMaps(mergeInto, toMerge map[string]interface{}) map[string]interface{}
return mergeInto
}

func WriteYamlToFile[K any](outputPath string, data K) error {
byt, err := yaml.Marshal(data)
if err != nil {
// Use a different error code
return ErrMarshal(err)
}

file, err := os.Create(outputPath)
if err != nil {
return ErrCreateFile(err, outputPath)
}

_, err = file.Write(byt)
if err != nil {
return ErrWriteFile(err, outputPath)
}
return nil
}

func WriteJSONToFile[K any](outputPath string, data K) error {
byt, err := json.MarshalIndent(data, " ", " ")
if err != nil {
Expand Down Expand Up @@ -410,3 +431,22 @@ func IsSchemaEmpty(schema string) (valid bool) {
valid = true
return
}
func FindEntityType(content []byte) (entity.EntityType, error) {
var tempMap map[string]interface{}
if err := json.Unmarshal(content, &tempMap); err != nil {
return "", ErrUnmarshal(err)
}
if schemaVersion, ok := tempMap["schemaVersion"].(string); ok {
lastIndex := strings.LastIndex(schemaVersion, "/")
if lastIndex != -1 {
schemaVersion = schemaVersion[:lastIndex]
}
switch schemaVersion {
case "relationships.meshery.io":
return entity.RelationshipDefinition, nil
case "components.meshery.io":
return entity.ComponentDefinition, nil
}
}
return "", nil
}

0 comments on commit b971e59

Please sign in to comment.