From fdb50621c09bfbd18543456bfbc5f0721af54592 Mon Sep 17 00:00:00 2001 From: Nithya Subramanian <98416062+nithyatsu@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:36:36 -0800 Subject: [PATCH] UDT update for json support (#8075) # Description yaml parser we use automatically supports parsing json, yaml being superset of json. Updated texts and test to capture a valid json input and a validation error for json input. ## Type of change - This pull request adds or changes features of Radius and has an approved issue (issue link required). - This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Radius (issue link optional). Fixes: https://dev.azure.com/azure-octo/Incubations/_workitems/edit/13650 ## Contributor checklist Please verify that the PR meets the following requirements, where applicable: - [ ] An overview of proposed schema changes is included in a linked GitHub issue. - [ ] A design document PR is created in the [design-notes repository](https://github.com/radius-project/design-notes/), if new APIs are being introduced. - [ ] If applicable, design document has been reviewed and approved by Radius maintainers/approvers. - [ ] A PR for the [samples repository](https://github.com/radius-project/samples) is created, if existing samples are affected by the changes in this PR. - [ ] A PR for the [documentation repository](https://github.com/radius-project/docs) is created, if the changes in this PR affect the documentation or any user facing updates are made. - [ ] A PR for the [recipes repository](https://github.com/radius-project/recipes) is created, if existing recipes are affected by the changes in this PR. --- pkg/cli/cmd/resourceprovider/create/create.go | 9 +++-- pkg/cli/manifest/manifest_test.go | 33 +++++++++++++++++-- .../testdata/missing-required-field.json | 12 +++++++ pkg/cli/manifest/testdata/valid.json | 13 ++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 pkg/cli/manifest/testdata/missing-required-field.json create mode 100644 pkg/cli/manifest/testdata/valid.json diff --git a/pkg/cli/cmd/resourceprovider/create/create.go b/pkg/cli/cmd/resourceprovider/create/create.go index da1e3cc91b..d34c282779 100644 --- a/pkg/cli/cmd/resourceprovider/create/create.go +++ b/pkg/cli/cmd/resourceprovider/create/create.go @@ -46,11 +46,14 @@ Resource providers are the entities that implement resource types such as 'Appli Creating a resource provider defines new resource types that can be used in applications. -Input can be passed in using a file or inline JSON as the second argument. Prefix the input with '@' to indicate a file path. +Input can be passed in using a JSON or YAML file using the --from-file option. `, Example: ` -# Create a resource provider +# Create a resource provider from YAML file rad resource-provider create --from-file /path/to/input.yaml + +# Create a resource provider from JSON file +rad resource-provider create --from-file /path/to/input.json `, Args: cobra.ExactArgs(0), RunE: framework.RunCommand(runner), @@ -60,7 +63,7 @@ rad resource-provider create --from-file /path/to/input.yaml commonflags.AddWorkspaceFlag(cmd) commonflags.AddFromFileFlagVar(cmd, &runner.ResourceProviderManifestFilePath) _ = cmd.MarkFlagRequired("from-file") - _ = cmd.MarkFlagFilename("from-file", "yaml") + _ = cmd.MarkFlagFilename("from-file", "yaml", "json") return cmd, runner } diff --git a/pkg/cli/manifest/manifest_test.go b/pkg/cli/manifest/manifest_test.go index 04e3c492c9..4ebdd300b4 100644 --- a/pkg/cli/manifest/manifest_test.go +++ b/pkg/cli/manifest/manifest_test.go @@ -22,7 +22,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestReadFile(t *testing.T) { +func TestReadFileYAML(t *testing.T) { expected := &ResourceProvider{ Name: "MyCompany.Resources", Types: map[string]*ResourceType{ @@ -49,16 +49,43 @@ func TestReadFile_InvalidYAML(t *testing.T) { require.Nil(t, result) } -func TestReadFile_DuplicateKey(t *testing.T) { +func TestReadFile_DuplicateKeyYAML(t *testing.T) { // Errors in the yaml library are non-exported, so it's hard to test the exact error. result, err := ReadFile("testdata/duplicate-key.yaml") require.Error(t, err) require.Nil(t, result) } -func TestReadFile_MissingRequiredField(t *testing.T) { +func TestReadFile_MissingRequiredFieldYAML(t *testing.T) { // Errors in the yaml library are non-exported, so it's hard to test the exact error. result, err := ReadFile("testdata/missing-required-field.yaml") require.Error(t, err) require.Nil(t, result) } + +func TestReadFileJSON(t *testing.T) { + expected := &ResourceProvider{ + Name: "MyCompany.Resources", + Types: map[string]*ResourceType{ + "testResources": { + APIVersions: map[string]*ResourceTypeAPIVersion{ + "2025-01-01-preview": { + Schema: map[string]any{}, + Capabilities: []string{"Recipes"}, + }, + }, + }, + }, + } + + result, err := ReadFile("testdata/valid.json") + require.NoError(t, err) + require.Equal(t, expected, result) +} + +func TestReadFile_MissingRequiredFieldJSON(t *testing.T) { + // Errors in the yaml library are non-exported, so it's hard to test the exact error. + result, err := ReadFile("testdata/missing-required-field.json") + require.Error(t, err) + require.Nil(t, result) +} diff --git a/pkg/cli/manifest/testdata/missing-required-field.json b/pkg/cli/manifest/testdata/missing-required-field.json new file mode 100644 index 0000000000..c6ef38a2f2 --- /dev/null +++ b/pkg/cli/manifest/testdata/missing-required-field.json @@ -0,0 +1,12 @@ +{ + "types": { + "testResources": { + "apiVersions": { + "2025-01-01-preview": { + "schema": {}, + "capabilities": ["Recipes"] + } + } + } + } +} diff --git a/pkg/cli/manifest/testdata/valid.json b/pkg/cli/manifest/testdata/valid.json new file mode 100644 index 0000000000..cd6e1e5705 --- /dev/null +++ b/pkg/cli/manifest/testdata/valid.json @@ -0,0 +1,13 @@ +{ + "name": "MyCompany.Resources", + "types": { + "testResources": { + "apiVersions": { + "2025-01-01-preview": { + "schema": {}, + "capabilities": ["Recipes"] + } + } + } + } +}