generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support generation of model from openapi schemas #582
Merged
MUzairS15
merged 13 commits into
meshery:master
from
MUzairS15:MUzairS15/generator/openapi
Sep 30, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bdfb934
support generation of model from openapi schemas
MUzairS15 996d126
openapi-gen
MUzairS15 099cdd3
update test
MUzairS15 66434f0
Merge branch 'master' into MUzairS15/generator/openapi
leecalcote 368373e
update
MUzairS15 c859ff0
Merge branch 'master' into MUzairS15/generator/openapi
MUzairS15 5ba263e
support generation of model from openapi schemas
MUzairS15 b8eebd9
temp
MUzairS15 e757428
fix import
MUzairS15 112a538
define new methods on package interface
MUzairS15 ad5c265
remove unneeded files
MUzairS15 45521cd
finalize changes
MUzairS15 98a1e99
finalize changes
MUzairS15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package encoding | ||
|
||
import ( | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func ToYaml(data []byte) ([]byte, error) { | ||
var out map[string]interface{} | ||
err := Unmarshal(data, &out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return yaml.Marshal(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package component | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/cuecontext" | ||
cueJson "cuelang.org/go/encoding/json" | ||
"github.com/layer5io/meshkit/generators/models" | ||
"github.com/layer5io/meshkit/utils" | ||
"github.com/layer5io/meshkit/utils/manifests" | ||
|
||
"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" | ||
) | ||
|
||
func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.ComponentDefinition, error) { | ||
if resource == "" { | ||
return nil, nil | ||
} | ||
resource, err := getResolvedManifest(resource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cuectx := cuecontext.New() | ||
cueParsedManExpr, err := cueJson.Extract("", []byte(resource)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parsedManifest := cuectx.BuildExpr(cueParsedManExpr) | ||
definitions, err := utils.Lookup(parsedManifest, "components.schemas") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fields, err := definitions.Fields() | ||
if err != nil { | ||
fmt.Printf("%v\n", err) | ||
return nil, err | ||
} | ||
components := make([]component.ComponentDefinition, 0) | ||
|
||
for fields.Next() { | ||
fieldVal := fields.Value() | ||
kindCue, err := utils.Lookup(fieldVal, `"x-kubernetes-group-version-kind"[0].kind`) | ||
if err != nil { | ||
continue | ||
} | ||
kind, err := kindCue.String() | ||
kind = strings.ToLower(kind) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
crd, err := fieldVal.MarshalJSON() | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
versionCue, err := utils.Lookup(fieldVal, `"x-kubernetes-group-version-kind"[0].version`) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
groupCue, err := utils.Lookup(fieldVal, `"x-kubernetes-group-version-kind"[0].group`) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
apiVersion, _ := versionCue.String() | ||
if g, _ := groupCue.String(); g != "" { | ||
apiVersion = g + "/" + apiVersion | ||
} | ||
modified := make(map[string]interface{}) //Remove the given fields which is either not required by End user (like status) or is prefilled by system (like apiVersion, kind and metadata) | ||
err = json.Unmarshal(crd, &modified) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
modifiedProps, err := UpdateProperties(fieldVal, cue.ParsePath("properties.spec"), apiVersion) | ||
if err == nil { | ||
modified = modifiedProps | ||
} | ||
|
||
DeleteFields(modified) | ||
crd, err = json.Marshal(modified) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
c := component.ComponentDefinition{ | ||
SchemaVersion: v1beta1.ComponentSchemaVersion, | ||
Version: "v1.0.0", | ||
|
||
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this still hardcoded? |
||
|
||
Model: model.Model{ | ||
Version: pkg.GetVersion(), | ||
}, | ||
Name: pkg.GetName(), | ||
DisplayName: manifests.FormatToReadableString(pkg.GetName()), | ||
Category: category.CategoryDefinition{ | ||
Name: "Orchestration & Management", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update |
||
}, | ||
Metadata: &model.ModelDefinition_Metadata{ | ||
AdditionalProperties: map[string]interface{}{ | ||
"source_uri": pkg.GetSourceURL(), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
components = append(components, c) | ||
} | ||
return components, nil | ||
|
||
} | ||
|
||
func getResolvedManifest(manifest string) (string, error) { | ||
var m map[string]interface{} | ||
|
||
err := yaml.Unmarshal([]byte(manifest), &m) | ||
if err != nil { | ||
return "", utils.ErrDecodeYaml(err) | ||
} | ||
|
||
byt, err := json.Marshal(m) | ||
if err != nil { | ||
return "", utils.ErrMarshal(err) | ||
} | ||
|
||
cuectx := cuecontext.New() | ||
cueParsedManExpr, err := cueJson.Extract("", byt) | ||
if err != nil { | ||
return "", ErrGetSchema(err) | ||
} | ||
|
||
parsedManifest := cuectx.BuildExpr(cueParsedManExpr) | ||
definitions, err := utils.Lookup(parsedManifest, "components.schemas") | ||
if err != nil { | ||
return "", err | ||
} | ||
resol := manifests.ResolveOpenApiRefs{} | ||
cache := make(map[string][]byte) | ||
resolved, err := resol.ResolveReferences(byt, definitions, cache) | ||
if err != nil { | ||
return "", err | ||
} | ||
manifest = string(resolved) | ||
return manifest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding appropriate?