Skip to content

Commit

Permalink
update per comments
Browse files Browse the repository at this point in the history
Signed-off-by: lakshmimsft <[email protected]>
  • Loading branch information
lakshmimsft committed Dec 20, 2024
1 parent 83e40ea commit 2588352
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 216 deletions.
3 changes: 1 addition & 2 deletions pkg/cli/cmd/resourceprovider/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

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/framework"
Expand Down Expand Up @@ -148,7 +147,7 @@ func (r *Runner) Run(ctx context.Context) error {
}

func (r *Runner) initializeClientFactory(ctx context.Context, workspace *workspaces.Workspace) error {
connection, err := cmd.GetConnection(ctx, workspace)
connection, err := workspace.Connect(ctx)
if err != nil {
return err
}
Expand Down
20 changes: 0 additions & 20 deletions pkg/cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ package cmd

import (
"context"
"errors"
"fmt"

"github.com/radius-project/radius/pkg/cli/aws"
"github.com/radius-project/radius/pkg/cli/azure"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clierrors"
"github.com/radius-project/radius/pkg/cli/workspaces"
corerp "github.com/radius-project/radius/pkg/corerp/api/v20231001preview"
"github.com/radius-project/radius/pkg/sdk"
"github.com/radius-project/radius/pkg/to"
)

Expand Down Expand Up @@ -97,20 +94,3 @@ func CheckIfRecipeExists(ctx context.Context, client clients.ApplicationsManagem

return envResource, recipeProperties, nil
}

// GetConnection from Workspace.
func GetConnection(ctx context.Context, workspace *workspaces.Workspace) (sdk.Connection, error) {
connection, err := workspace.Connect()
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

return connection, nil
}
38 changes: 4 additions & 34 deletions pkg/cli/connections/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package connections

import (
"context"
"errors"
"fmt"

aztoken "github.com/radius-project/radius/pkg/azure/tokencredentials"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clients_new/generated"
"github.com/radius-project/radius/pkg/cli/clierrors"
cli_credential "github.com/radius-project/radius/pkg/cli/credential"
"github.com/radius-project/radius/pkg/cli/deployment"
"github.com/radius-project/radius/pkg/cli/kubernetes"
Expand Down Expand Up @@ -55,18 +53,11 @@ type impl struct {
// CreateDeploymentClient connects to a workspace, tests the connection, creates a deployment client and an operations
// client, and returns them along with the resource group name. It returns an error if any of the steps fail.
func (i *impl) CreateDeploymentClient(ctx context.Context, workspace workspaces.Workspace) (clients.DeploymentClient, error) {
connection, err := workspace.Connect()
connection, err := workspace.Connect(ctx)
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

armClientOptions := sdk.NewClientOptions(connection)
dc, err := sdkclients.NewResourceDeploymentsClient(&sdkclients.Options{
Cred: &aztoken.AnonymousCredential{},
Expand Down Expand Up @@ -102,18 +93,11 @@ func (i *impl) CreateDeploymentClient(ctx context.Context, workspace workspaces.
// CreateDiagnosticsClient creates a DiagnosticsClient by connecting to a workspace, testing the connection, and creating
// clients for applications, containers, environments, and gateways. If an error occurs, it is returned.
func (i *impl) CreateDiagnosticsClient(ctx context.Context, workspace workspaces.Workspace) (clients.DiagnosticsClient, error) {
connection, err := workspace.Connect()
connection, err := workspace.Connect(ctx)
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

connectionConfig, err := workspace.ConnectionConfig()
if err != nil {
return nil, err
Expand Down Expand Up @@ -168,18 +152,11 @@ func (i *impl) CreateDiagnosticsClient(ctx context.Context, workspace workspaces
// CreateApplicationsManagementClient connects to the workspace, tests the connection, and returns a
// UCPApplicationsManagementClient if successful, or an error if unsuccessful.
func (*impl) CreateApplicationsManagementClient(ctx context.Context, workspace workspaces.Workspace) (clients.ApplicationsManagementClient, error) {
connection, err := workspace.Connect()
connection, err := workspace.Connect(ctx)
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

return &clients.UCPApplicationsManagementClient{
RootScope: workspace.Scope,
ClientOptions: sdk.NewClientOptions(connection),
Expand All @@ -192,18 +169,11 @@ func (*impl) CreateApplicationsManagementClient(ctx context.Context, workspace w
// CreateCredentialManagementClient establishes a connection to a workspace, tests the connection, creates Azure and AWS
// credential clients, and returns a UCPCredentialManagementClient. An error is returned if any of the steps fail.
func (*impl) CreateCredentialManagementClient(ctx context.Context, workspace workspaces.Workspace) (cli_credential.CredentialManagementClient, error) {
connection, err := workspace.Connect()
connection, err := workspace.Connect(ctx)
if err != nil {
return nil, err
}

err = sdk.TestConnection(ctx, connection)
if errors.Is(err, &sdk.ErrRadiusNotInstalled{}) {
return nil, clierrors.MessageWithCause(err, "Could not connect to Radius.")
} else if err != nil {
return nil, err
}

clientOptions := sdk.NewClientOptions(connection)

azureCredentialClient, err := v20231001preview.NewAzureCredentialsClient(&aztoken.AnonymousCredential{}, clientOptions)
Expand Down
140 changes: 0 additions & 140 deletions pkg/cli/manifest/registermanifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ package manifest
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"

armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
ucpfake "github.com/radius-project/radius/pkg/ucp/api/v20231001preview/fake"
)

// RegisterFile registers a manifest file
Expand Down Expand Up @@ -167,137 +161,3 @@ func logIfEnabled(logger func(format string, args ...any), format string, args .
logger(format, args...)
}
}

func NewTestClientFactory() (*v20231001preview.ClientFactory, error) {
// Create fake servers for each client
resourceProvidersServer := ucpfake.ResourceProvidersServer{
BeginCreateOrUpdate: func(
ctx context.Context,
planeName string,
resourceProviderName string,
resource v20231001preview.ResourceProviderResource,
options *v20231001preview.ResourceProvidersClientBeginCreateOrUpdateOptions,
) (resp azfake.PollerResponder[v20231001preview.ResourceProvidersClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) {
// Simulate successful creation
result := v20231001preview.ResourceProvidersClientCreateOrUpdateResponse{
ResourceProviderResource: resource,
}
resp.AddNonTerminalResponse(http.StatusCreated, nil)
resp.SetTerminalResponse(http.StatusOK, result, nil)

return
},
Get: func(
ctx context.Context,
planeName string,
resourceProviderName string,
options *v20231001preview.ResourceProvidersClientGetOptions, // Add this parameter
) (resp azfake.Responder[v20231001preview.ResourceProvidersClientGetResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ResourceProvidersClientGetResponse{
ResourceProviderResource: v20231001preview.ResourceProviderResource{
Name: to.Ptr(resourceProviderName),
},
}
resp.SetResponse(http.StatusOK, response, nil)
return
},
}

// Create other fake servers similarly
resourceTypesServer := ucpfake.ResourceTypesServer{
BeginCreateOrUpdate: func(
ctx context.Context,
planeName string,
resourceProviderName string,
resourceTypeName string,
resource v20231001preview.ResourceTypeResource,
options *v20231001preview.ResourceTypesClientBeginCreateOrUpdateOptions,
) (resp azfake.PollerResponder[v20231001preview.ResourceTypesClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) {
result := v20231001preview.ResourceTypesClientCreateOrUpdateResponse{
ResourceTypeResource: resource,
}

resp.AddNonTerminalResponse(http.StatusCreated, nil)
resp.SetTerminalResponse(http.StatusOK, result, nil)

return
},
Get: func(
ctx context.Context,
planeName string,
resourceProviderName string,
resourceTypeName string,
options *v20231001preview.ResourceTypesClientGetOptions,
) (resp azfake.Responder[v20231001preview.ResourceTypesClientGetResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ResourceTypesClientGetResponse{
ResourceTypeResource: v20231001preview.ResourceTypeResource{
Name: to.Ptr(resourceTypeName),
},
}
resp.SetResponse(http.StatusOK, response, nil)
return
},
}

apiVersionsServer := ucpfake.APIVersionsServer{
BeginCreateOrUpdate: func(
ctx context.Context,
planeName string,
resourceProviderName string,
resourceTypeName string,
apiVersionName string, // Added missing parameter
resource v20231001preview.APIVersionResource,
options *v20231001preview.APIVersionsClientBeginCreateOrUpdateOptions,
) (resp azfake.PollerResponder[v20231001preview.APIVersionsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) {
// Simulate successful creation
result := v20231001preview.APIVersionsClientCreateOrUpdateResponse{
APIVersionResource: resource,
}
resp.AddNonTerminalResponse(http.StatusCreated, nil)
resp.SetTerminalResponse(http.StatusOK, result, nil)
return
},
}

locationsServer := ucpfake.LocationsServer{
BeginCreateOrUpdate: func(
ctx context.Context,
planeName string,
resourceProviderName string,
locationName string,
resource v20231001preview.LocationResource,
options *v20231001preview.LocationsClientBeginCreateOrUpdateOptions,
) (resp azfake.PollerResponder[v20231001preview.LocationsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) {
// Simulate successful creation
result := v20231001preview.LocationsClientCreateOrUpdateResponse{
LocationResource: resource,
}
resp.AddNonTerminalResponse(http.StatusCreated, nil)
resp.SetTerminalResponse(http.StatusOK, result, nil)

return
},
}

serverFactory := ucpfake.ServerFactory{
ResourceProvidersServer: resourceProvidersServer,
ResourceTypesServer: resourceTypesServer,
APIVersionsServer: apiVersionsServer,
LocationsServer: locationsServer,
}

serverFactoryTransport := ucpfake.NewServerFactoryTransport(&serverFactory)

clientOptions := &armpolicy.ClientOptions{
ClientOptions: policy.ClientOptions{
Transport: serverFactoryTransport,
},
}

clientFactory, err := v20231001preview.NewClientFactory(&azfake.TokenCredential{}, clientOptions)
if err != nil {
return nil, err
}

return clientFactory, err
}
Loading

0 comments on commit 2588352

Please sign in to comment.