Skip to content
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

feat(reader) handle _transform field #520

Merged
merged 7 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions file/kong_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
"type": "object"
},
"_transform": {
"type": "boolean"
},
"_workspace": {
"type": "string"
},
Expand Down
18 changes: 17 additions & 1 deletion file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/kong/deck/utils"
)

var (
// ErrorTransformFalseNotSupported indicates that no transform mode is not supported
ErrorTransformFalseNotSupported = fmt.Errorf("_transform: false is not supported")
// ErrorFilenameEmpty indicates that you must provide a filename
ErrorFilenameEmpty = fmt.Errorf("filename cannot be empty")
)

// RenderConfig contains necessary information to render a correct
// KongConfig from a file.
type RenderConfig struct {
Expand All @@ -26,19 +33,24 @@ type RenderConfig struct {
// or if there is any error during processing.
func GetContentFromFiles(filenames []string) (*Content, error) {
if len(filenames) == 0 {
return nil, fmt.Errorf("filename cannot be empty")
return nil, ErrorFilenameEmpty
}

return getContent(filenames)
}

// GetForKonnect processes the fileContent and renders a RawState and KonnectRawState
func GetForKonnect(fileContent *Content, opt RenderConfig) (*utils.KongRawState, *utils.KonnectRawState, error) {
var builder stateBuilder
// setup
builder.targetContent = fileContent
builder.currentState = opt.CurrentState
builder.kongVersion = opt.KongVersion

if fileContent.Transform != nil && !*fileContent.Transform {
return nil, nil, ErrorTransformFalseNotSupported
}

kongState, konnectState, err := builder.build()
if err != nil {
return nil, nil, fmt.Errorf("building state: %w", err)
Expand All @@ -58,6 +70,10 @@ func Get(fileContent *Content, opt RenderConfig, dumpConfig dump.Config) (*utils
builder.selectTags = dumpConfig.SelectorTags
}

if fileContent.Transform != nil && !*fileContent.Transform {
return nil, ErrorTransformFalseNotSupported
}

state, _, err := builder.build()
if err != nil {
return nil, fmt.Errorf("building state: %w", err)
Expand Down
38 changes: 38 additions & 0 deletions file/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"testing"

"github.com/kong/deck/dump"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -71,6 +72,43 @@ func TestReadKongStateFromStdinFailsToParseText(t *testing.T) {
assert.Nil(c)
}

func TestTransformNotFalse(t *testing.T) {
filenames := []string{"-"}
assert := assert.New(t)

tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())

_, err = tmpfile.WriteString("_transform: false\nservices:\n- host: test.com\n name: test service\n")
if err != nil {
panic(err)
}

if _, err := tmpfile.Seek(0, 0); err != nil {
panic(err)
}

oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }() // Restore original Stdin

os.Stdin = tmpfile

c, err := GetContentFromFiles(filenames)
rainest marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}
parsed, err := Get(c, RenderConfig{}, dump.Config{})
assert.Equal(err, ErrorTransformFalseNotSupported)
assert.Nil(parsed)

parsed, _, err = GetForKonnect(c, RenderConfig{})
assert.Equal(err, ErrorTransformFalseNotSupported)
assert.Nil(parsed)
}

func TestReadKongStateFromStdin(t *testing.T) {
filenames := []string{"-"}
assert := assert.New(t)
Expand Down
1 change: 1 addition & 0 deletions file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ func (s FServicePackage) sortKey() string {
// +k8s:deepcopy-gen=true
type Content struct {
FormatVersion string `json:"_format_version,omitempty" yaml:"_format_version,omitempty"`
Transform *bool `json:"_transform,omitempty" yaml:"_transform,omitempty"`
Info *Info `json:"_info,omitempty" yaml:"_info,omitempty"`
Workspace string `json:"_workspace,omitempty" yaml:"_workspace,omitempty"`

Expand Down
5 changes: 5 additions & 0 deletions file/zz_generated.deepcopy.go

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