Skip to content

Commit

Permalink
upd per discussion
Browse files Browse the repository at this point in the history
Signed-off-by: lakshmimsft <[email protected]>
  • Loading branch information
lakshmimsft committed Dec 18, 2024
1 parent 423706a commit 31c016b
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 1,436 deletions.
2 changes: 1 addition & 1 deletion cmd/ucpd/ucp-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ routing:
# This is the default downstream (dynamic-rp) for UDT implementations.
defaultDownstreamEndpoint: "http://localhost:8082"

manifests:
initialization:
# This is the directory location which contains manifests to be registered.
manifestDirectory: "deploy/manifest/built-in-providers"

Expand Down
2 changes: 1 addition & 1 deletion deploy/Chart/templates/ucp/configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ data:
routing:
defaultDownstreamEndpoint: "http://dynamic-rp.radius-sytem:8082"
manifests:
initialization:
manifestDirectory: "deploy/manifest/built-in-providers"
metricsProvider:
Expand Down
12 changes: 0 additions & 12 deletions deploy/manifest/built-in-providers/test1.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions deploy/manifest/built-in-providers/test2.yaml

This file was deleted.

59 changes: 39 additions & 20 deletions pkg/cli/cmd/resourceprovider/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ package create
import (
"context"

aztoken "github.com/radius-project/radius/pkg/azure/tokencredentials"
"github.com/radius-project/radius/pkg/cli"
"github.com/radius-project/radius/pkg/cli/cmd"
"github.com/radius-project/radius/pkg/cli/cmd/commonflags"
"github.com/radius-project/radius/pkg/cli/cmd/resourceprovider/common"
"github.com/radius-project/radius/pkg/cli/connections"
"github.com/radius-project/radius/pkg/cli/framework"
"github.com/radius-project/radius/pkg/cli/manifest"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
"github.com/radius-project/radius/pkg/ucp/ucpclient"
"github.com/radius-project/radius/pkg/sdk"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -69,22 +70,26 @@ rad resource-provider create --from-file /path/to/input.json

// Runner is the Runner implementation for the `rad resource-provider create` command.
type Runner struct {
ConnectionFactory connections.Factory
ConfigHolder *framework.ConfigHolder
Output output.Interface
Format string
Workspace *workspaces.Workspace
UCPClientFactory *v20231001preview.ClientFactory
ConfigHolder *framework.ConfigHolder
Output output.Interface
Format string
Workspace *workspaces.Workspace

ResourceProviderManifestFilePath string
ResourceProvider *manifest.ResourceProvider
Logger func(format string, args ...any)
}

// NewRunner creates an instance of the runner for the `rad resource-provider create` command.
func NewRunner(factory framework.Factory) *Runner {
output := factory.GetOutput()
return &Runner{
ConnectionFactory: factory.GetConnectionFactory(),
ConfigHolder: factory.GetConfigHolder(),
Output: factory.GetOutput(),
ConfigHolder: factory.GetConfigHolder(),
Output: output,
Logger: func(format string, args ...any) {
output.LogInfo(format, args...)
},
}
}

Expand All @@ -97,6 +102,13 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {
}
r.Workspace = workspace

if r.UCPClientFactory == nil {
err = r.setClientFactory(cmd.Context(), workspace)
if err != nil {
return err
}
}

format, err := cli.RequireOutput(cmd)
if err != nil {
return err
Expand All @@ -114,33 +126,40 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {
// Run runs the `rad resource-provider create` command.
func (r *Runner) Run(ctx context.Context) error {

connection, err := cmd.GetConnection(ctx, r.Workspace)
if err != nil {
// Proceed with registering manifests
if err := manifest.RegisterFile(ctx, r.UCPClientFactory, "local", r.ResourceProviderManifestFilePath, r.Logger); err != nil {
return err
}

ucpclient, err := ucpclient.NewUCPClient(connection)
response, err := r.UCPClientFactory.NewResourceProvidersClient().Get(ctx, "local", r.ResourceProvider.Name, nil)
if err != nil {
return err
}

// Proceed with registering manifests
if err := ucpclient.RegisterManifests(ctx, r.ResourceProviderManifestFilePath); err != nil {
return nil
// Add a blank line before printing the result.
r.Output.LogInfo("")

err = r.Output.WriteFormatted(r.Format, response, common.GetResourceProviderTableFormat())
if err != nil {
return err
}

response, err := ucpclient.GetResourceProvider(ctx, "local", r.ResourceProvider.Name)
return nil
}

func (r *Runner) setClientFactory(ctx context.Context, workspace *workspaces.Workspace) error {
connection, err := cmd.GetConnection(ctx, workspace)
if err != nil {
return err
}

// Add a blank line before printing the result.
r.Output.LogInfo("")
clientOptions := sdk.NewClientOptions(connection)

err = r.Output.WriteFormatted(r.Format, response, common.GetResourceProviderTableFormat())
clientFactory, err := v20231001preview.NewClientFactory(&aztoken.AnonymousCredential{}, clientOptions)
if err != nil {
return err
}

r.UCPClientFactory = clientFactory
return nil
}
144 changes: 53 additions & 91 deletions pkg/cli/cmd/resourceprovider/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ limitations under the License.
package create

import (
"bytes"
"context"
"fmt"
"testing"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/cmd/resourceprovider/common"
"github.com/radius-project/radius/pkg/cli/connections"
"github.com/radius-project/radius/pkg/cli/cmd/commonflags"
"github.com/radius-project/radius/pkg/cli/framework"
frmwk "github.com/radius-project/radius/pkg/cli/framework"
"github.com/radius-project/radius/pkg/cli/manifest"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
"github.com/radius-project/radius/test/radcli"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func Test_CommandValidation(t *testing.T) {
Expand All @@ -47,122 +45,86 @@ func Test_Validate(t *testing.T) {
Name: "Valid",
Input: []string{"--from-file", "testdata/valid.yaml"},
ExpectedValid: true,
ConfigHolder: framework.ConfigHolder{Config: config},
ConfigHolder: frmwk.ConfigHolder{Config: config},
},
{
Name: "Invalid: Error in manifest",
Input: []string{"--from-file", "testdata/missing-required-field.yaml"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{Config: config},
ConfigHolder: frmwk.ConfigHolder{Config: config},
},
{
Name: "Invalid: missing arguments",
Input: []string{},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{Config: config},
ConfigHolder: frmwk.ConfigHolder{Config: config},
},
{
Name: "Invalid: too many arguments",
Input: []string{"abcd", "--from-file", "testdata/valid.yaml"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{Config: config},
ConfigHolder: frmwk.ConfigHolder{Config: config},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)

radcli.SharedValidateValidation(t, func(framework frmwk.Factory) (*cobra.Command, framework.Runner) {
runner := NewRunner(framework)
clientFactory, err := manifest.NewTestClientFactory()
require.NoError(t, err)

runner.UCPClientFactory = clientFactory
runner.Output = &output.MockOutput{}
runner.ResourceProviderManifestFilePath = "testdata/valid.yaml"

cmd := &cobra.Command{
Use: "test",
Short: "Test command",
Args: cobra.ExactArgs(0),
RunE: frmwk.RunCommand(runner),
}
commonflags.AddOutputFlag(cmd)
commonflags.AddWorkspaceFlag(cmd)
commonflags.AddFromFileFlagVar(cmd, &runner.ResourceProviderManifestFilePath)
_ = cmd.MarkFlagRequired("from-file")
_ = cmd.MarkFlagFilename("from-file", "yaml", "json")
return cmd, runner
}, testcases)

}

func Test_Run(t *testing.T) {
t.Run("Success: resource provider created", func(t *testing.T) {
ctrl := gomock.NewController(t)

resourceProviderData, err := manifest.ReadFile("testdata/valid.yaml")
require.NoError(t, err)

expectedResourceProvider := v20231001preview.ResourceProviderResource{
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20231001preview.ResourceProviderProperties{},
}
expectedResourceType := v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{},
}
expectedAPIVersion := v20231001preview.APIVersionResource{
Properties: &v20231001preview.APIVersionProperties{},
}
expectedLocation := v20231001preview.LocationResource{
Properties: &v20231001preview.LocationProperties{
ResourceTypes: map[string]*v20231001preview.LocationResourceType{
"testResources": {
APIVersions: map[string]map[string]any{
"2025-01-01-preview": {},
},
},
},
},
}
expectedResourceType := "testResources"
expectedAPIVersion := "2025-01-01-preview"

appManagementClient := clients.NewMockApplicationsManagementClient(ctrl)
appManagementClient.EXPECT().
CreateOrUpdateResourceProvider(gomock.Any(), "local", "MyCompany.Resources", &expectedResourceProvider).
Return(expectedResourceProvider, nil).
Times(1)
appManagementClient.EXPECT().
CreateOrUpdateResourceType(gomock.Any(), "local", "MyCompany.Resources", "testResources", &expectedResourceType).
Return(expectedResourceType, nil).
Times(1)
appManagementClient.EXPECT().
CreateOrUpdateAPIVersion(gomock.Any(), "local", "MyCompany.Resources", "testResources", "2025-01-01-preview", &expectedAPIVersion).
Return(expectedAPIVersion, nil).
Times(1)
appManagementClient.EXPECT().
CreateOrUpdateLocation(gomock.Any(), "local", "MyCompany.Resources", v1.LocationGlobal, &expectedLocation).
Return(expectedLocation, nil).
Times(1)
appManagementClient.EXPECT().
GetResourceProvider(gomock.Any(), "local", "MyCompany.Resources").
Return(expectedResourceProvider, nil).
Times(1)

outputSink := &output.MockOutput{}
clientFactory, err := manifest.NewTestClientFactory()
require.NoError(t, err)

var logBuffer bytes.Buffer
logger := func(format string, args ...any) {
fmt.Fprintf(&logBuffer, format+"\n", args...)
}

runner := &Runner{
ConnectionFactory: &connections.MockFactory{ApplicationsManagementClient: appManagementClient},
Output: outputSink,
Workspace: &workspaces.Workspace{},
ResourceProvider: resourceProviderData,
Format: "table",
UCPClientFactory: clientFactory,
Output: &output.MockOutput{},
Workspace: &workspaces.Workspace{},
ResourceProvider: resourceProviderData,
Format: "table",
Logger: logger,
ResourceProviderManifestFilePath: "testdata/valid.yaml",
}

err = runner.Run(context.Background())
require.NoError(t, err)

expectedOutput := []any{
output.LogOutput{
Format: "Creating resource provider %s",
Params: []any{"MyCompany.Resources"},
},
output.LogOutput{
Format: "Creating resource type %s/%s",
Params: []any{"MyCompany.Resources", "testResources"},
},
output.LogOutput{
Format: "Creating API Version %s/%s@%s",
Params: []any{"MyCompany.Resources", "testResources", "2025-01-01-preview"},
},
output.LogOutput{
Format: "Creating location %s/%s",
Params: []any{"MyCompany.Resources", "global"},
},
output.LogOutput{
Format: "",
Params: nil,
},
output.FormattedOutput{
Format: "table",
Obj: expectedResourceProvider,
Options: common.GetResourceProviderTableFormat(),
},
}
require.Equal(t, expectedOutput, outputSink.Writes)
logOutput := logBuffer.String()
require.Contains(t, logOutput, fmt.Sprintf("Creating resource provider %s", resourceProviderData.Name))
require.Contains(t, logOutput, fmt.Sprintf("Creating resource type %s/%s", resourceProviderData.Name, expectedResourceType))
require.Contains(t, logOutput, fmt.Sprintf("Creating API Version %s/%s@%s", resourceProviderData.Name, expectedResourceType, expectedAPIVersion))
})

}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 31c016b

Please sign in to comment.