From 1d71f5de7f49d238e3578d558e27701adfad5b95 Mon Sep 17 00:00:00 2001 From: nithyatsu Date: Mon, 6 Jan 2025 15:57:19 -0800 Subject: [PATCH] add a test and refactor --- pkg/cli/clivalidation.go | 4 ++-- pkg/cli/cmd/resourcetype/create/create.go | 3 +-- pkg/cli/cmd/resourcetype/create/create_test.go | 6 +++--- .../create/testdata/missing-required-field.yaml | 6 ------ pkg/cli/cmd/resourcetype/create/testdata/valid.yaml | 8 ++++---- pkg/cli/manifest/registermanifest.go | 4 ++-- 6 files changed, 12 insertions(+), 19 deletions(-) delete mode 100644 pkg/cli/cmd/resourcetype/create/testdata/missing-required-field.yaml diff --git a/pkg/cli/clivalidation.go b/pkg/cli/clivalidation.go index 2c5fe77b0d..849725b7bf 100644 --- a/pkg/cli/clivalidation.go +++ b/pkg/cli/clivalidation.go @@ -465,7 +465,7 @@ func RequireResourceTypeNameArgs(cmd *cobra.Command, args []string) (string, err return "", err } if resourceType == "" { - return "", fmt.Errorf("resource type name is not provided or is empty ") + return "", fmt.Errorf("resource type name is not provided or is empty") } return resourceType, nil @@ -474,7 +474,7 @@ func RequireResourceTypeNameArgs(cmd *cobra.Command, args []string) (string, err // ReadResourceTypeNameArgs is used to get the resource type name that is supplied as the first argument func ReadResourceTypeNameArgs(cmd *cobra.Command, args []string) (string, error) { if len(args) < 1 { - return "", errors.New("no resource type name provided") + return "", errors.New("resource type name is provided") } return args[0], nil } diff --git a/pkg/cli/cmd/resourcetype/create/create.go b/pkg/cli/cmd/resourcetype/create/create.go index 792a96f4e6..39ecc16e3d 100644 --- a/pkg/cli/cmd/resourcetype/create/create.go +++ b/pkg/cli/cmd/resourcetype/create/create.go @@ -94,7 +94,6 @@ func NewRunner(factory framework.Factory) *Runner { // Validate runs validation for the `rad resourceprovider create` command. func (r *Runner) Validate(cmd *cobra.Command, args []string) error { - // Validate command line args and resourceTypeName, err := cli.RequireResourceTypeNameArgs(cmd, args) if err != nil { return err @@ -137,7 +136,7 @@ func (r *Runner) Run(ctx context.Context) error { } } - //response, err := r.UCPClientFactory.NewResourceProvidersClient().Get(ctx, "local", r.ResourceProvider.Name, nil) + // If resource provider does not exist or if there is any other error, first register the resource provider. response, err := r.UCPClientFactory.NewResourceTypesClient().Get(ctx, "local", r.ResourceProvider.Name, r.ResourceTypeName, nil) if err != nil { r.Output.LogInfo("Resource provider %q not found.", r.ResourceProvider.Name) diff --git a/pkg/cli/cmd/resourcetype/create/create_test.go b/pkg/cli/cmd/resourcetype/create/create_test.go index 79450e0c52..75eb5fe7ec 100644 --- a/pkg/cli/cmd/resourcetype/create/create_test.go +++ b/pkg/cli/cmd/resourcetype/create/create_test.go @@ -45,13 +45,13 @@ func Test_Validate(t *testing.T) { ConfigHolder: framework.ConfigHolder{Config: config}, }, { - Name: "Invalid: Error in manifest", - Input: []string{"testResources", "--from-file", "testdata/missing-required-field.yaml"}, + Name: "Invalid: resource type not present in manifest", + Input: []string{"myResources", "--from-file", "testdata/valid.yaml"}, ExpectedValid: false, ConfigHolder: framework.ConfigHolder{Config: config}, }, { - Name: "Invalid: missing arguments", + Name: "Invalid: missing resource type as argument", Input: []string{"--from-file", "testdata/valid.yaml"}, ExpectedValid: false, ConfigHolder: framework.ConfigHolder{Config: config}, diff --git a/pkg/cli/cmd/resourcetype/create/testdata/missing-required-field.yaml b/pkg/cli/cmd/resourcetype/create/testdata/missing-required-field.yaml deleted file mode 100644 index 15ab61b11d..0000000000 --- a/pkg/cli/cmd/resourcetype/create/testdata/missing-required-field.yaml +++ /dev/null @@ -1,6 +0,0 @@ -types: - testResources: - apiVersions: - '2025-01-01-preview': - schema: {} - capabilities: ["Recipes"] \ No newline at end of file diff --git a/pkg/cli/cmd/resourcetype/create/testdata/valid.yaml b/pkg/cli/cmd/resourcetype/create/testdata/valid.yaml index 49bc2b9116..220799cc23 100644 --- a/pkg/cli/cmd/resourcetype/create/testdata/valid.yaml +++ b/pkg/cli/cmd/resourcetype/create/testdata/valid.yaml @@ -2,11 +2,11 @@ name: CoolCompany.Resources types: testResources: apiVersions: - '2025-01-01-preview': + '2023-10-01-preview': schema: {} - capabilities: ["Recipes"] + capabilities: ["Recipes"] coolResources: apiVersions: - '2025-01-01-preview': + '2023-10-01-preview': schema: {} - capabilities: ["Recipes"] \ No newline at end of file + capabilities: ["Recipes"] \ No newline at end of file diff --git a/pkg/cli/manifest/registermanifest.go b/pkg/cli/manifest/registermanifest.go index df6ab830f5..da04cf13be 100644 --- a/pkg/cli/manifest/registermanifest.go +++ b/pkg/cli/manifest/registermanifest.go @@ -177,7 +177,7 @@ func RegisterType(ctx context.Context, clientFactory *v20231001preview.ClientFac // Check if the type exists in the manifest file resourceType, ok := resourceProvider.Types[typeName] if !ok { - return fmt.Errorf("type %s not found in manifest file %s", typeName, filePath) + return fmt.Errorf("Type %s not found in manifest file %s", typeName, filePath) } logIfEnabled(logger, "Creating resource type %s/%s", resourceProvider.Name, typeName) @@ -195,7 +195,7 @@ func RegisterType(ctx context.Context, clientFactory *v20231001preview.ClientFac return err } - // get the existing location resource and update it + // get the existing location resource and update it with new resource type. We have to revisit this code once schema is finalized and validated. locationResourceGetResponse, err := clientFactory.NewLocationsClient().Get(ctx, planeName, resourceProvider.Name, v1.LocationGlobal, nil) if err != nil { return err