-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(update-collection-v3): add logs metadata migration
- Loading branch information
Mikołaj Świątek
committed
Nov 24, 2022
1 parent
8a0a113
commit 34c3f98
Showing
7 changed files
with
138 additions
and
0 deletions.
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
56 changes: 56 additions & 0 deletions
56
src/go/cmd/update-collection-v3/migrations/logs/logs_test.go
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,56 @@ | ||
package logs | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type TestCase struct { | ||
inputYaml string | ||
outputYaml string | ||
err error | ||
description string | ||
} | ||
|
||
func runYamlTest(t *testing.T, testCase TestCase) { | ||
actualOutput, err := Migrate(testCase.inputYaml) | ||
if testCase.err == nil { | ||
require.NoError(t, err, testCase.description) | ||
} else { | ||
require.Equal(t, err, testCase.err, testCase.description) | ||
} | ||
require.Equal(t, strings.Trim(testCase.outputYaml, "\n "), strings.Trim(actualOutput, "\n "), testCase.description) | ||
} | ||
|
||
func Test_LogsMetadataConfig(t *testing.T) { | ||
testCases := []TestCase{ | ||
{ | ||
inputYaml: `{}`, | ||
outputYaml: `{}`, | ||
description: "No changes to config", | ||
}, | ||
{ | ||
inputYaml: ` | ||
metadata: | ||
logs: | ||
config: | ||
key: value | ||
`, | ||
outputYaml: ` | ||
metadata: | ||
logs: | ||
config: | ||
merge: | ||
key: value | ||
`, | ||
description: "Custom config moved to config.merge", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.description, func(t *testing.T) { | ||
runYamlTest(t, testCase) | ||
}) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/go/cmd/update-collection-v3/migrations/logs/migrate.go
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,40 @@ | ||
package logs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Migrate(yamlV2 string) (yamlV3 string, err error) { | ||
valuesV2, err := parseValues(yamlV2) | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing input yaml: %v", err) | ||
} | ||
|
||
valuesV3, err := migrate(&valuesV2) | ||
if err != nil { | ||
return "", fmt.Errorf("error migrating: %v", err) | ||
} | ||
|
||
buffer := bytes.Buffer{} | ||
encoder := yaml.NewEncoder(&buffer) | ||
encoder.SetIndent(2) | ||
err = encoder.Encode(valuesV3) | ||
return buffer.String(), err | ||
} | ||
|
||
func parseValues(yamlV2 string) (ValuesV2, error) { | ||
var valuesV2 ValuesV2 | ||
err := yaml.Unmarshal([]byte(yamlV2), &valuesV2) | ||
return valuesV2, err | ||
} | ||
|
||
func migrate(valuesV2 *ValuesV2) (ValuesV3, error) { | ||
valuesV3 := ValuesV3{ | ||
Rest: valuesV2.Rest, | ||
} | ||
valuesV3.Metadata.Logs.Config.Merge = valuesV2.Metadata.Logs.Config | ||
return valuesV3, nil | ||
} |
14 changes: 14 additions & 0 deletions
14
src/go/cmd/update-collection-v3/migrations/logs/valuesv2.go
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,14 @@ | ||
package logs | ||
|
||
type ValuesV2 struct { | ||
Metadata struct { | ||
Logs LogsMetadataV2 `yaml:"logs,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} `yaml:"metadata,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type LogsMetadataV2 struct { | ||
Config map[string]interface{} `yaml:"config,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} |
17 changes: 17 additions & 0 deletions
17
src/go/cmd/update-collection-v3/migrations/logs/valuesv3.go
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,17 @@ | ||
package logs | ||
|
||
type ValuesV3 struct { | ||
Metadata struct { | ||
Logs LogsMetadataV3 `yaml:"logs,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} `yaml:"metadata,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
type LogsMetadataV3 struct { | ||
Config struct { | ||
Merge map[string]interface{} `yaml:"merge,omitempty"` | ||
Override map[string]interface{} `yaml:"override,omitempty"` | ||
} `yaml:"config,omitempty"` | ||
Rest map[string]interface{} `yaml:",inline"` | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
metadata: | ||
logs: | ||
config: | ||
merge: | ||
key: value | ||
sumologic: | ||
accessId: xxx | ||
accessKey: yyy | ||
|