Skip to content

Commit

Permalink
fix: resolve YAML refs immediately after rendering template
Browse files Browse the repository at this point in the history
When rendering an apiproxy or sharedflow, resolve the YAML refs
immediately after rendering the input template. This makes it
so that you don't need to manually copy the files in the $refs
  • Loading branch information
micovery committed Apr 26, 2024
1 parent 9f29149 commit da39abe
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/templates/graphql/apiproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ APIProxy:
DisplayName: {{ $.Values.api_name }}
Description: {{ $.Values.schema.Description }}
Policies:
#{{- os_copyfile "./policies.yaml" "./policies.yaml" | blank }}
$ref: ./policies.yaml#/
ProxyEndpoints:
- ProxyEndpoint:
Expand Down Expand Up @@ -56,4 +55,5 @@ TargetEndpoints:
Resources:
- Resource:
Type: graphql
Path: {{ os_writefile "./schema.graphql" $.Values.schema_string }}
#{{ os_writefile "./schema.graphql" $.Values.schema_string }}
Path: ./schema.graphql
1 change: 0 additions & 1 deletion examples/templates/grpc/apiproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ APIProxy:
DisplayName: {{ include "get_api_name" $.Values.proto }}
Description: API proxy generated from proto file
Policies:
#{{- os_copyfile "./policies.yaml" "./policies.yaml" | blank }}
$ref: ./policies.yaml#/
ProxyEndpoints:
- ProxyEndpoint:
Expand Down
7 changes: 4 additions & 3 deletions examples/templates/oas3/apiproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ APIProxy:
DisplayName: {{ $.Values.spec.info.title }}
Description: {{ $.Values.spec.info.description }}
Policies:
#{{- os_copyfile "./policies.yaml" "./policies.yaml" | blank }}
$ref: ./policies.yaml#/
ProxyEndpoints:
- ProxyEndpoint:
Expand Down Expand Up @@ -65,7 +64,9 @@ TargetEndpoints:
Resources:
- Resource:
Type: oas
Path: {{ os_writefile "./spec.yaml" $.Values.spec_string }}
#{{ os_writefile "./spec.yaml" $.Values.spec_string }}
Path: ./spec.yaml
- Resource:
Type: properties
Path: {{ os_copyfile "./test.properties" "./resources/test.properties" }}
#{{ os_copyfile "./test.properties" "./resources/test.properties" }}
Path: ./test.properties
44 changes: 44 additions & 0 deletions pkg/render/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package render

import (
"bytes"
"fmt"
"github.com/go-errors/errors"
"github.com/micovery/apigee-go-gen/pkg/apigee/v1"
"github.com/micovery/apigee-go-gen/pkg/flags"
"github.com/micovery/apigee-go-gen/pkg/utils"
"os"
"path/filepath"
)
Expand All @@ -41,6 +43,24 @@ func GenerateBundle(createModelFunc func(string) (v1.Model, error), cFlags *Comm
return errors.New(err)
}

templateFile := string(cFlags.TemplateFile)

//resolve YAML Refs immediately after rendering to avoid having copy files in $ref
rendered, err := os.ReadFile(string(cFlags.OutputFile))
if err != nil {
return errors.New(err)
}

rendered, err = ResolveYAML(rendered, templateFile)
if err != nil {
return err
}

err = os.WriteFile(string(cFlags.OutputFile), rendered, os.ModePerm)
if err != nil {
return errors.New(err)
}

// create apiproxy from rendered template
model, err := createModelFunc(string(cFlags.OutputFile))
if err != nil {
Expand Down Expand Up @@ -86,3 +106,27 @@ func CreateBundle(model v1.Model, output string, validate bool, dryRun string) (

return nil
}

func ResolveYAML(text []byte, filePath string) ([]byte, error) {
wd, err := os.Getwd()
if err != nil {
return nil, errors.New(err)
}
defer func() { utils.Must(os.Chdir(wd)) }()

err = os.Chdir(filepath.Dir(filePath))
if err != nil {
return nil, errors.New(err)
}

yaml, err := utils.Text2YAML(bytes.NewReader(text))
if err != nil {
return nil, err
}
newText, err := utils.YAML2Text(yaml, 2)
if err != nil {
return nil, err
}

return newText, nil
}

0 comments on commit da39abe

Please sign in to comment.