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"] + } + } + } + } +}