Skip to content

Commit

Permalink
remove hardcoding and handle error wehen schemas are not present
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <muzair.shaikh810@gmail.com>
  • Loading branch information
MUzairS15 committed Oct 3, 2024
1 parent a605654 commit 791faef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
2 changes: 2 additions & 0 deletions utils/component/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
ErrUpdateSchemaCode = "meshkit-11158"
)

var ErrNoSchemasFound = errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given openapi spec"}, []string{"The OpenAPI spec doesn't include \"components.schemas\" path"}, []string{"The spec doesn't have include any schema"}, []string{"Verify the spec has valid schema."})

// No reference usage found. Also check in adapters before deleting
func ErrCrdGenerate(err error) error {
return errors.New(ErrCrdGenerateCode, errors.Alert, []string{"Could not generate component with the given CRD"}, []string{err.Error()}, []string{""}, []string{"Verify CRD has valid schema."})
Expand Down
19 changes: 7 additions & 12 deletions utils/component/openapi_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package component

import (
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -15,7 +16,6 @@ import (
"gopkg.in/yaml.v3"

"github.com/meshery/schemas/models/v1beta1"
"github.com/meshery/schemas/models/v1beta1/category"
"github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/model"
)
Expand All @@ -25,6 +25,9 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
return nil, nil
}
resource, err := getResolvedManifest(resource)
if err != nil && errors.Is(err, ErrNoSchemasFound) {
return nil, nil
}
if err != nil {
return nil, err
}
Expand All @@ -36,7 +39,7 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo

parsedManifest := cuectx.BuildExpr(cueParsedManExpr)
definitions, err := utils.Lookup(parsedManifest, "components.schemas")

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,28 +104,20 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo

c := component.ComponentDefinition{
SchemaVersion: v1beta1.ComponentSchemaVersion,
Version: "v1.0.0",

Format: component.JSON,
Format: component.JSON,
Component: component.Component{
Kind: kind,
Version: apiVersion,
Schema: string(crd),
},
// Metadata: compMetadata,
DisplayName: manifests.FormatToReadableString(kind),
Model: model.ModelDefinition{
SchemaVersion: v1beta1.ModelSchemaVersion,
Version: "v1.0.0",

Model: model.Model{
Version: pkg.GetVersion(),
},
Name: pkg.GetName(),
DisplayName: manifests.FormatToReadableString(pkg.GetName()),
Category: category.CategoryDefinition{
Name: "Orchestration & Management",
},
Metadata: &model.ModelDefinition_Metadata{
AdditionalProperties: map[string]interface{}{
"source_uri": pkg.GetSourceURL(),
Expand Down Expand Up @@ -159,7 +154,7 @@ func getResolvedManifest(manifest string) (string, error) {
parsedManifest := cuectx.BuildExpr(cueParsedManExpr)
definitions, err := utils.Lookup(parsedManifest, "components.schemas")
if err != nil {
return "", err
return "", ErrNoSchemasFound
}
resol := manifests.ResolveOpenApiRefs{}
cache := make(map[string][]byte)
Expand Down

0 comments on commit 791faef

Please sign in to comment.