Skip to content

Commit

Permalink
UDT update for json support (#8075)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
nithyatsu authored Nov 25, 2024
1 parent 0aa2d6e commit fdb5062
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
9 changes: 6 additions & 3 deletions pkg/cli/cmd/resourceprovider/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}
Expand Down
33 changes: 30 additions & 3 deletions pkg/cli/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
12 changes: 12 additions & 0 deletions pkg/cli/manifest/testdata/missing-required-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"types": {
"testResources": {
"apiVersions": {
"2025-01-01-preview": {
"schema": {},
"capabilities": ["Recipes"]
}
}
}
}
}
13 changes: 13 additions & 0 deletions pkg/cli/manifest/testdata/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "MyCompany.Resources",
"types": {
"testResources": {
"apiVersions": {
"2025-01-01-preview": {
"schema": {},
"capabilities": ["Recipes"]
}
}
}
}
}

0 comments on commit fdb5062

Please sign in to comment.