From 423706a3b9c2e1b3b2ca72ab4ba2c376750f3770 Mon Sep 17 00:00:00 2001 From: lakshmimsft Date: Mon, 16 Dec 2024 09:16:47 -0800 Subject: [PATCH] still draft Signed-off-by: lakshmimsft --- cmd/ucpd/cmd/root.go | 27 -- pkg/cli/cmd/resourceprovider/create/create.go | 61 +--- pkg/cli/cmd/utils.go | 20 ++ pkg/cli/manifest/registermanifest.go | 151 ++++++++ pkg/cli/manifest/registermanifest_test.go | 323 ++++++++++++++++++ pkg/cli/manifest/testdata2/test1.yaml | 12 + pkg/cli/manifest/testdata2/test2.yaml | 12 + .../v20231001preview/locations_fake_test.go | 149 -------- .../resourceproviders_fakes_test.go | 159 --------- .../resourcetypes_fake_test.go | 148 -------- pkg/ucp/manifestservice/service.go | 32 +- ...st.go => locations_fake_test_notneeded.go} | 0 pkg/ucp/ucpclient/registermanifests.go | 3 +- pkg/ucp/ucpclient/registermanifests_test.go | 96 +----- ...resourceproviders_fakes_test_notneeded.go} | 0 ...o => resourcetypes_fake_test_notneeded.go} | 0 pkg/ucp/ucpclient/ucpclient.go | 26 -- 17 files changed, 545 insertions(+), 674 deletions(-) create mode 100644 pkg/cli/manifest/registermanifest.go create mode 100644 pkg/cli/manifest/registermanifest_test.go create mode 100644 pkg/cli/manifest/testdata2/test1.yaml create mode 100644 pkg/cli/manifest/testdata2/test2.yaml delete mode 100644 pkg/ucp/api/v20231001preview/locations_fake_test.go delete mode 100644 pkg/ucp/api/v20231001preview/resourceproviders_fakes_test.go delete mode 100644 pkg/ucp/api/v20231001preview/resourcetypes_fake_test.go rename pkg/ucp/ucpclient/{locations_fake_test.go => locations_fake_test_notneeded.go} (100%) rename pkg/ucp/ucpclient/{resourceproviders_fakes_test.go => resourceproviders_fakes_test_notneeded.go} (100%) rename pkg/ucp/ucpclient/{resourcetypes_fake_test.go => resourcetypes_fake_test_notneeded.go} (100%) diff --git a/cmd/ucpd/cmd/root.go b/cmd/ucpd/cmd/root.go index b19e064fbd..c6672ea03f 100644 --- a/cmd/ucpd/cmd/root.go +++ b/cmd/ucpd/cmd/root.go @@ -66,33 +66,6 @@ var rootCmd = &cobra.Command{ if err != nil { return err } - /* - // Discuss if there is a better way to check if the server is listening.. - // Start RegisterManifests in a goroutine after 15 seconds - go func() { - time.Sleep(15 * time.Second) - // Register manifests - manifestDir := options.Config.Manifests.ManifestDirectory - if _, err := os.Stat(manifestDir); os.IsNotExist(err) { - logger.Error(err, "Manifest directory does not exist", "directory", manifestDir) - return - } else if err != nil { - logger.Error(err, "Error checking manifest directory", "directory", manifestDir) - return - } - - ucpclient, err := ucpclient.NewUCPClient(options.UCPConnection) - if err != nil { - logger.Error(err, "Failed to create UCP client") - } - - if err := ucpclient.RegisterManifests(cmd.Context(), manifestDir); err != nil { - logger.Error(err, "Failed to register manifests") - } else { - logger.Info("Successfully registered manifests", "directory", manifestDir) - } - }() - */ ctx := logr.NewContext(cmd.Context(), logger) return hosting.RunWithInterrupts(ctx, host) diff --git a/pkg/cli/cmd/resourceprovider/create/create.go b/pkg/cli/cmd/resourceprovider/create/create.go index b5c55b775f..771a409af3 100644 --- a/pkg/cli/cmd/resourceprovider/create/create.go +++ b/pkg/cli/cmd/resourceprovider/create/create.go @@ -19,8 +19,8 @@ package create import ( "context" - v1 "github.com/radius-project/radius/pkg/armrpc/api/v1" "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" @@ -28,8 +28,7 @@ import ( "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/pkg/ucp/ucpclient" "github.com/spf13/cobra" ) @@ -114,65 +113,23 @@ 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 { - client, err := r.ConnectionFactory.CreateApplicationsManagementClient(ctx, *r.Workspace) + + connection, err := cmd.GetConnection(ctx, r.Workspace) if err != nil { return err } - r.Output.LogInfo("Creating resource provider %s", r.ResourceProvider.Name) - _, err = client.CreateOrUpdateResourceProvider(ctx, "local", r.ResourceProvider.Name, &v20231001preview.ResourceProviderResource{ - Location: to.Ptr(v1.LocationGlobal), - Properties: &v20231001preview.ResourceProviderProperties{}, - }) + ucpclient, err := ucpclient.NewUCPClient(connection) if err != nil { return err } - // The location resource contains references to all of the resource types and API versions that the resource provider supports. - // We're instantiating the struct here so we can update it as we loop. - locationResource := v20231001preview.LocationResource{ - Properties: &v20231001preview.LocationProperties{ - ResourceTypes: map[string]*v20231001preview.LocationResourceType{}, - }, - } - - for resourceTypeName, resourceType := range r.ResourceProvider.Types { - r.Output.LogInfo("Creating resource type %s/%s", r.ResourceProvider.Name, resourceTypeName) - _, err := client.CreateOrUpdateResourceType(ctx, "local", r.ResourceProvider.Name, resourceTypeName, &v20231001preview.ResourceTypeResource{ - Properties: &v20231001preview.ResourceTypeProperties{ - DefaultAPIVersion: resourceType.DefaultAPIVersion, - }, - }) - if err != nil { - return err - } - - locationResourceType := &v20231001preview.LocationResourceType{ - APIVersions: map[string]map[string]any{}, - } - - for apiVersionName := range resourceType.APIVersions { - r.Output.LogInfo("Creating API Version %s/%s@%s", r.ResourceProvider.Name, resourceTypeName, apiVersionName) - _, err := client.CreateOrUpdateAPIVersion(ctx, "local", r.ResourceProvider.Name, resourceTypeName, apiVersionName, &v20231001preview.APIVersionResource{ - Properties: &v20231001preview.APIVersionProperties{}, - }) - if err != nil { - return err - } - - locationResourceType.APIVersions[apiVersionName] = map[string]any{} - } - - locationResource.Properties.ResourceTypes[resourceTypeName] = locationResourceType - } - - r.Output.LogInfo("Creating location %s/%s", r.ResourceProvider.Name, v1.LocationGlobal) - _, err = client.CreateOrUpdateLocation(ctx, "local", r.ResourceProvider.Name, v1.LocationGlobal, &locationResource) - if err != nil { - return err + // Proceed with registering manifests + if err := ucpclient.RegisterManifests(ctx, r.ResourceProviderManifestFilePath); err != nil { + return nil } - response, err := client.GetResourceProvider(ctx, "local", r.ResourceProvider.Name) + response, err := ucpclient.GetResourceProvider(ctx, "local", r.ResourceProvider.Name) if err != nil { return err } diff --git a/pkg/cli/cmd/utils.go b/pkg/cli/cmd/utils.go index e13b1243a2..58541acd4f 100644 --- a/pkg/cli/cmd/utils.go +++ b/pkg/cli/cmd/utils.go @@ -18,13 +18,16 @@ 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" ) @@ -94,3 +97,20 @@ 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 +} diff --git a/pkg/cli/manifest/registermanifest.go b/pkg/cli/manifest/registermanifest.go new file mode 100644 index 0000000000..8fff48924d --- /dev/null +++ b/pkg/cli/manifest/registermanifest.go @@ -0,0 +1,151 @@ +/* +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package manifest + +import ( + "context" + "fmt" + "os" + "path/filepath" + + 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" +) + +// RegisterFile registers a manifest file +func RegisterFile(ctx context.Context, clientFactory v20231001preview.ClientFactory, planeName string, filePath string, logger func(string)) error { + // Check for valid file path + if filePath == "" { + return fmt.Errorf("invalid manifest file path") + } + + // Read the manifest file + resourceProvider, err := ReadFile(filePath) + if err != nil { + return err + } + + resourceProviderPoller, err := clientFactory.NewResourceProvidersClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, v20231001preview.ResourceProviderResource{ + Location: to.Ptr(v1.LocationGlobal), + Properties: &v20231001preview.ResourceProviderProperties{}, + }, nil) + if err != nil { + return err + } + + _, err = resourceProviderPoller.PollUntilDone(ctx, nil) + if err != nil { + return err + } + + // The location resource contains references to all of the resource types and API versions that the resource provider supports. + // We're instantiating the struct here so we can update it as we loop. + locationResource := v20231001preview.LocationResource{ + Properties: &v20231001preview.LocationProperties{ + ResourceTypes: map[string]*v20231001preview.LocationResourceType{}, + }, + } + + for resourceTypeName, resourceType := range resourceProvider.Types { + resourceTypePoller, err := clientFactory.NewResourceTypesClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, resourceTypeName, v20231001preview.ResourceTypeResource{ + Properties: &v20231001preview.ResourceTypeProperties{ + DefaultAPIVersion: resourceType.DefaultAPIVersion, + }, + }, nil) + if err != nil { + return err + } + + _, err = resourceTypePoller.PollUntilDone(ctx, nil) + if err != nil { + return err + } + + locationResourceType := &v20231001preview.LocationResourceType{ + APIVersions: map[string]map[string]any{}, + } + + for apiVersionName := range resourceType.APIVersions { + apiVersionsPoller, err := clientFactory.NewAPIVersionsClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, resourceTypeName, apiVersionName, v20231001preview.APIVersionResource{ + Properties: &v20231001preview.APIVersionProperties{}, + }, nil) + if err != nil { + return err + } + + _, err = apiVersionsPoller.PollUntilDone(ctx, nil) + if err != nil { + return err + } + + locationResourceType.APIVersions[apiVersionName] = map[string]any{} + } + + locationResource.Properties.ResourceTypes[resourceTypeName] = locationResourceType + } + + locationPoller, err := clientFactory.NewLocationsClient().BeginCreateOrUpdate(ctx, planeName, resourceProvider.Name, v1.LocationGlobal, locationResource, nil) + if err != nil { + return err + } + + _, err = locationPoller.PollUntilDone(ctx, nil) + if err != nil { + return err + } + + _, err = clientFactory.NewResourceProvidersClient().Get(ctx, planeName, resourceProvider.Name, nil) + if err != nil { + return err + } + + return nil +} + +// RegisterDirectory registers all manifest files in a directory +func RegisterDirectory(ctx context.Context, clientFactory v20231001preview.ClientFactory, planeName string, directoryPath string, logger func(string)) error { + // Check for valid directory path + if directoryPath == "" { + return fmt.Errorf("invalid manifest directory") + } + + info, err := os.Stat(directoryPath) + if err != nil { + return fmt.Errorf("failed to access manifest path %s: %w", directoryPath, err) + } + + if !info.IsDir() { + return fmt.Errorf("manifest path %s is not a directory", directoryPath) + } + + // List all files in the manifestDirectory + files, err := os.ReadDir(directoryPath) + if err != nil { + return err + } + + // Iterate over each file in the directory + for _, fileInfo := range files { + if fileInfo.IsDir() { + continue // Skip directories - TBD: check if want to include subdirectories + } + filePath := filepath.Join(directoryPath, fileInfo.Name()) + + err = RegisterFile(ctx, clientFactory, planeName, filePath, logger) + if err != nil { + return fmt.Errorf("failed to register manifest file %s: %w", filePath, err) + } + } + + return nil +} diff --git a/pkg/cli/manifest/registermanifest_test.go b/pkg/cli/manifest/registermanifest_test.go new file mode 100644 index 0000000000..334398e152 --- /dev/null +++ b/pkg/cli/manifest/registermanifest_test.go @@ -0,0 +1,323 @@ +/* +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package manifest + +import ( + "context" + "net/http" + "testing" + + // armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy" + 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" + "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" + "github.com/stretchr/testify/require" +) + +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, + ) (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 +} + +func TestRegisterDirectory(t *testing.T) { + tests := []struct { + name string + clientFactory func() (*v20231001preview.ClientFactory, error) + planeName string + directoryPath string + expectError bool + expectedErrorMessage string + expectedResourceProvider string + }{ + { + name: "Success", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + directoryPath: "testdata2", + expectError: false, + expectedErrorMessage: "", + expectedResourceProvider: "MyCompany2.CompanyName2", + }, + { + name: "EmptyDirectoryPath", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + directoryPath: "", + expectError: true, + expectedErrorMessage: "invalid manifest directory", + expectedResourceProvider: "", + }, + { + name: "InvalidDirectoryPath", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + directoryPath: "#^$/invalid", + expectError: true, + expectedErrorMessage: "failed to access manifest path #^$/invalid: stat #^$/invalid: no such file or directory", + expectedResourceProvider: "", + }, + { + name: "FilePathInsteadOfDirectory", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + directoryPath: "testdata/valid.yaml", + expectError: true, + expectedErrorMessage: "manifest path testdata/valid.yaml is not a directory path", + expectedResourceProvider: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + clientFactory, err := tt.clientFactory() + require.NoError(t, err, "Failed to create client factory") + logger := func(message string) {} + + err = RegisterDirectory(context.Background(), *clientFactory, tt.planeName, tt.directoryPath, logger) + if tt.expectError { + require.Error(t, err, "Expected an error but got none") + require.Contains(t, err.Error(), tt.expectedErrorMessage, "Error message does not match") + } else { + require.NoError(t, err, "Did not expect an error but got one") + + // Verify the expected resource provider exists + if tt.expectedResourceProvider != "" { + rp, err := clientFactory.NewResourceProvidersClient().Get(context.Background(), tt.planeName, tt.expectedResourceProvider, nil) + require.NoError(t, err, "Failed to retrieve the expected resource provider") + require.Equal(t, to.Ptr(tt.expectedResourceProvider), rp.Name, "Resource provider name does not match") + } + } + }) + } +} + +func TestRegisterFile(t *testing.T) { + tests := []struct { + name string + clientFactory func() (*v20231001preview.ClientFactory, error) + planeName string + filePath string + expectError bool + expectedErrorMessage string + expectedResourceProvider string + }{ + { + name: "Success", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + filePath: "testdata2", + expectError: false, + expectedErrorMessage: "", + expectedResourceProvider: "MyCompany2.CompanyName2", + }, + { + name: "EmptyDirectoryPath", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + filePath: "", + expectError: true, + expectedErrorMessage: "invalid manifest directory", + expectedResourceProvider: "", + }, + { + name: "InvalidDirectoryPath", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + filePath: "#^$/invalid", + expectError: true, + expectedErrorMessage: "failed to access manifest path #^$/invalid: stat #^$/invalid: no such file or directory", + expectedResourceProvider: "", + }, + { + name: "FilePathInsteadOfDirectory", + clientFactory: func() (*v20231001preview.ClientFactory, error) { + return NewTestClientFactory() + }, + planeName: "local", + filePath: "testdata/valid.yaml", + expectError: true, + expectedErrorMessage: "manifest path testdata/valid.yaml is not a directory path", + expectedResourceProvider: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + clientFactory, err := tt.clientFactory() + require.NoError(t, err, "Failed to create client factory") + logger := func(message string) {} + + err = RegisterDirectory(context.Background(), *clientFactory, tt.planeName, tt.filePath, logger) + if tt.expectError { + require.Error(t, err, "Expected an error but got none") + require.Contains(t, err.Error(), tt.expectedErrorMessage, "Error message does not match") + } else { + require.NoError(t, err, "Did not expect an error but got one") + + // Verify the expected resource provider exists + if tt.expectedResourceProvider != "" { + rp, err := clientFactory.NewResourceProvidersClient().Get(context.Background(), tt.planeName, tt.expectedResourceProvider, nil) + require.NoError(t, err, "Failed to retrieve the expected resource provider") + require.Equal(t, to.Ptr(tt.expectedResourceProvider), rp.Name, "Resource provider name does not match") + } + } + }) + } +} diff --git a/pkg/cli/manifest/testdata2/test1.yaml b/pkg/cli/manifest/testdata2/test1.yaml new file mode 100644 index 0000000000..b76f369047 --- /dev/null +++ b/pkg/cli/manifest/testdata2/test1.yaml @@ -0,0 +1,12 @@ +name: MyCompany.CompanyName +types: + testResource1: + apiVersions: + "2025-01-01-preview": + schema: {} + capabilities: [] + testResource2: + apiVersions: + "2025-01-01-preview": + schema: {} + capabilities: [] diff --git a/pkg/cli/manifest/testdata2/test2.yaml b/pkg/cli/manifest/testdata2/test2.yaml new file mode 100644 index 0000000000..3ef2b3ffcc --- /dev/null +++ b/pkg/cli/manifest/testdata2/test2.yaml @@ -0,0 +1,12 @@ +name: MyCompany2.CompanyName2 +types: + testResource3: + apiVersions: + "2025-01-01-preview": + schema: {} + capabilities: ["Recipes"] + testResource4: + apiVersions: + "2025-01-01-preview": + schema: {} + capabilities: ["Recipes"] diff --git a/pkg/ucp/api/v20231001preview/locations_fake_test.go b/pkg/ucp/api/v20231001preview/locations_fake_test.go deleted file mode 100644 index df5426505c..0000000000 --- a/pkg/ucp/api/v20231001preview/locations_fake_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package v20231001preview_test - -import ( - "context" - "net/http" - "testing" - - 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" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" - "github.com/stretchr/testify/require" - - v20231001preview "github.com/radius-project/radius/pkg/ucp/api/v20231001preview" - "github.com/radius-project/radius/pkg/ucp/api/v20231001preview/fake" -) - -func TestLocationsServer(t *testing.T) { - // Initialize the fake LocationsServer with method implementations. - srv := &fake.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) { - // Create the response object. - result := v20231001preview.LocationsClientCreateOrUpdateResponse{ - LocationResource: resource, - } - - // Create a PollerResponder to simulate a long-running operation (LRO). - resp.AddNonTerminalResponse(http.StatusCreated, nil) - resp.SetTerminalResponse(http.StatusOK, result, nil) - - return - }, - Get: func( - ctx context.Context, - planeName string, - resourceProviderName string, - locationName string, - options *v20231001preview.LocationsClientGetOptions, - ) (resp azfake.Responder[v20231001preview.LocationsClientGetResponse], errResp azfake.ErrorResponder) { - response := v20231001preview.LocationsClientGetResponse{ - LocationResource: v20231001preview.LocationResource{ - Name: to.Ptr(locationName), - }, - } - resp.SetResponse(http.StatusOK, response, nil) - return - }, - BeginDelete: func( - ctx context.Context, - planeName string, - resourceProviderName string, - locationName string, - options *v20231001preview.LocationsClientBeginDeleteOptions, - ) (resp azfake.PollerResponder[v20231001preview.LocationsClientDeleteResponse], errResp azfake.ErrorResponder) { - // Create the response object. - result := v20231001preview.LocationsClientDeleteResponse{} - - resp.AddNonTerminalResponse(http.StatusNoContent, nil) - resp.SetTerminalResponse(http.StatusOK, result, nil) - return - }, - NewListPager: func( - planeName string, - resourceProviderName string, - options *v20231001preview.LocationsClientListOptions, - ) (resp azfake.PagerResponder[v20231001preview.LocationsClientListResponse]) { - // Simulate paging with two pages of results. - page1 := v20231001preview.LocationsClientListResponse{ - LocationResourceListResult: v20231001preview.LocationResourceListResult{ - Value: []*v20231001preview.LocationResource{ - {Name: to.Ptr("location1")}, - {Name: to.Ptr("location2")}, - }, - NextLink: to.Ptr("nextPageLink"), - }, - } - page2 := v20231001preview.LocationsClientListResponse{ - LocationResourceListResult: v20231001preview.LocationResourceListResult{ - Value: []*v20231001preview.LocationResource{ - {Name: to.Ptr("location3")}, - }, - }, - } - resp.AddPage(http.StatusOK, page1, nil) - resp.AddPage(http.StatusOK, page2, nil) - return - }, - } - - // Create a fake transport using the LocationsServer. - transport := fake.NewLocationsServerTransport(srv) - - // Set up client options with the fake transport. - clientOptions := &armpolicy.ClientOptions{ - ClientOptions: policy.ClientOptions{ - Transport: transport, - }, - } - - // Create the LocationsClient with the fake transport and mock credential. - client, err := v20231001preview.NewLocationsClient(&azfake.TokenCredential{}, clientOptions) - require.NoError(t, err) - - ctx := context.Background() - planeName := "testPlane" - resourceProviderName := "testResourceProvider" - locationName := "testLocation" - resource := v20231001preview.LocationResource{ - Name: to.Ptr(locationName), - } - - // Call BeginCreateOrUpdate and poll until completion. - pollerResp, err := client.BeginCreateOrUpdate(ctx, planeName, resourceProviderName, locationName, resource, nil) - require.NoError(t, err) - finalResp, err := pollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - require.Equal(t, locationName, *finalResp.Name) - - // Call Get. - getResp, err := client.Get(ctx, planeName, resourceProviderName, locationName, nil) - require.NoError(t, err) - require.Equal(t, locationName, *getResp.Name) - - // Call BeginDelete and poll until completion. - deletePollerResp, err := client.BeginDelete(ctx, planeName, resourceProviderName, locationName, nil) - require.NoError(t, err) - _, err = deletePollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - - // Call NewListPager. - pager := client.NewListPager(planeName, resourceProviderName, nil) - var locations []*v20231001preview.LocationResource - for pager.More() { - page, err := pager.NextPage(ctx) - require.NoError(t, err) - locations = append(locations, page.Value...) - } - require.Len(t, locations, 3) - require.Equal(t, "location1", *locations[0].Name) - require.Equal(t, "location2", *locations[1].Name) - require.Equal(t, "location3", *locations[2].Name) -} diff --git a/pkg/ucp/api/v20231001preview/resourceproviders_fakes_test.go b/pkg/ucp/api/v20231001preview/resourceproviders_fakes_test.go deleted file mode 100644 index 3cc5f0d063..0000000000 --- a/pkg/ucp/api/v20231001preview/resourceproviders_fakes_test.go +++ /dev/null @@ -1,159 +0,0 @@ -package v20231001preview_test - -import ( - "context" - "net/http" - "testing" - - 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" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" - "github.com/stretchr/testify/require" - - v20231001preview "github.com/radius-project/radius/pkg/ucp/api/v20231001preview" - "github.com/radius-project/radius/pkg/ucp/api/v20231001preview/fake" -) - -func TestResourceProvidersServer(t *testing.T) { - // Initialize the fake ResourceProvidersServer with method implementations. - srv := &fake.ResourceProvidersServer{ - BeginCreateOrUpdate: func( - ctx context.Context, - planeName string, - resourceProviderName string, - resource v20231001preview.ResourceProviderResource, - options *v20231001preview.ResourceProvidersClientBeginCreateOrUpdateOptions, - ) (resp azfake.PollerResponder[v20231001preview.ResourceProvidersClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) { - 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, - ) (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 - }, - BeginDelete: func( - ctx context.Context, - planeName string, - resourceProviderName string, - options *v20231001preview.ResourceProvidersClientBeginDeleteOptions, - ) (resp azfake.PollerResponder[v20231001preview.ResourceProvidersClientDeleteResponse], errResp azfake.ErrorResponder) { - // Simulate a delete operation with a final response. - result := v20231001preview.ResourceProvidersClientDeleteResponse{} - - // resp.AddNonTerminalResponse(http.StatusCreated, nil) - resp.SetTerminalResponse(http.StatusOK, result, nil) - return - }, - // Updated NewListPager method - NewListPager: func( - planeName string, - options *v20231001preview.ResourceProvidersClientListOptions, - ) (resp azfake.PagerResponder[v20231001preview.ResourceProvidersClientListResponse]) { - // Simulate paging with two pages of results. - page1 := v20231001preview.ResourceProvidersClientListResponse{ - ResourceProviderResourceListResult: v20231001preview.ResourceProviderResourceListResult{ - Value: []*v20231001preview.ResourceProviderResource{ - {Name: to.Ptr("resourceProvider1")}, - {Name: to.Ptr("resourceProvider2")}, - }, - NextLink: to.Ptr("nextPageLink"), - }, - } - page2 := v20231001preview.ResourceProvidersClientListResponse{ - ResourceProviderResourceListResult: v20231001preview.ResourceProviderResourceListResult{ - Value: []*v20231001preview.ResourceProviderResource{ - {Name: to.Ptr("resourceProvider3")}, - }, - }, - } - resp.AddPage(http.StatusOK, page1, nil) - resp.AddPage(http.StatusOK, page2, nil) - return - }, - GetProviderSummary: func( - ctx context.Context, - planeName string, - resourceProviderName string, - options *v20231001preview.ResourceProvidersClientGetProviderSummaryOptions, - ) (resp azfake.Responder[v20231001preview.ResourceProvidersClientGetProviderSummaryResponse], errResp azfake.ErrorResponder) { - response := v20231001preview.ResourceProvidersClientGetProviderSummaryResponse{ - ResourceProviderSummary: v20231001preview.ResourceProviderSummary{ - Name: to.Ptr(resourceProviderName), - }, - } - resp.SetResponse(http.StatusOK, response, nil) - return - }, - } - - // Create a fake transport using the ResourceProvidersServer. - transport := fake.NewResourceProvidersServerTransport(srv) - clientOptions := &armpolicy.ClientOptions{ClientOptions: policy.ClientOptions{ - Transport: transport, - }} - - // Create the ResourceProvidersClient with the fake transport. - client, err := v20231001preview.NewResourceProvidersClient(&azfake.TokenCredential{}, clientOptions) - require.NoError(t, err) - - ctx := context.Background() - planeName := "local" - resourceProviderName := "testResourceProvider" - resource := v20231001preview.ResourceProviderResource{ - Name: to.Ptr(resourceProviderName), - } - - // Call BeginCreateOrUpdate and poll until completion. - pollerResp, err := client.BeginCreateOrUpdate(ctx, planeName, resourceProviderName, resource, nil) - require.NoError(t, err) - _, err = pollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - - // Call Get. - getResp, err := client.Get(ctx, planeName, resourceProviderName, nil) - require.NoError(t, err) - require.Equal(t, resourceProviderName, *getResp.Name) - - // Call BeginDelete and poll until completion. - deletePollerResp, err := client.BeginDelete(ctx, planeName, resourceProviderName, nil) - require.NoError(t, err) - _, err = deletePollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - - // Call NewListPager. - pager := client.NewListPager(planeName, nil) - var resources []*v20231001preview.ResourceProviderResource - - for pager.More() { - page, err := pager.NextPage(ctx) - require.NoError(t, err) - resources = append(resources, page.Value...) - } - - require.Len(t, resources, 3) - require.Equal(t, "resourceProvider1", *resources[0].Name) - require.Equal(t, "resourceProvider2", *resources[1].Name) - require.Equal(t, "resourceProvider3", *resources[2].Name) - - // Call GetProviderSummary. - summaryResp, err := client.GetProviderSummary(ctx, planeName, resourceProviderName, nil) - require.NoError(t, err) - require.Equal(t, resourceProviderName, *summaryResp.Name) -} diff --git a/pkg/ucp/api/v20231001preview/resourcetypes_fake_test.go b/pkg/ucp/api/v20231001preview/resourcetypes_fake_test.go deleted file mode 100644 index ff69d41eae..0000000000 --- a/pkg/ucp/api/v20231001preview/resourcetypes_fake_test.go +++ /dev/null @@ -1,148 +0,0 @@ -package v20231001preview_test - -import ( - "context" - "net/http" - "testing" - - 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" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" - "github.com/stretchr/testify/require" - - v20231001preview "github.com/radius-project/radius/pkg/ucp/api/v20231001preview" - "github.com/radius-project/radius/pkg/ucp/api/v20231001preview/fake" -) - -func TestResourceTypesServer(t *testing.T) { - // Initialize the fake ResourceTypesServer with method implementations. - srv := &fake.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, - } - - // Create a PollerResponder to simulate a long-running operation (LRO) - 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 - }, - BeginDelete: func( - ctx context.Context, - planeName string, - resourceProviderName string, - resourceTypeName string, - options *v20231001preview.ResourceTypesClientBeginDeleteOptions, - ) (resp azfake.PollerResponder[v20231001preview.ResourceTypesClientDeleteResponse], errResp azfake.ErrorResponder) { - // Create the response object - result := v20231001preview.ResourceTypesClientDeleteResponse{} - // Create a PollerResponder to simulate a long-running operation (LRO) - resp.AddNonTerminalResponse(http.StatusAccepted, nil) - resp.SetTerminalResponse(http.StatusNoContent, result, nil) - - return - }, - NewListPager: func( - planeName string, - resourceProviderName string, - options *v20231001preview.ResourceTypesClientListOptions, - ) (resp azfake.PagerResponder[v20231001preview.ResourceTypesClientListResponse]) { - // Simulate paging with two pages of results. - page1 := v20231001preview.ResourceTypesClientListResponse{ - ResourceTypeResourceListResult: v20231001preview.ResourceTypeResourceListResult{ - Value: []*v20231001preview.ResourceTypeResource{ - {Name: to.Ptr("resourceType1")}, - {Name: to.Ptr("resourceType2")}, - }, - NextLink: to.Ptr("nextPageLink"), - }, - } - page2 := v20231001preview.ResourceTypesClientListResponse{ - ResourceTypeResourceListResult: v20231001preview.ResourceTypeResourceListResult{ - Value: []*v20231001preview.ResourceTypeResource{ - {Name: to.Ptr("resourceType3")}, - }, - }, - } - resp.AddPage(http.StatusOK, page1, nil) - resp.AddPage(http.StatusOK, page2, nil) - return - }, - } - - // Create a fake transport using the ResourceTypesServer. - transport := fake.NewResourceTypesServerTransport(srv) - - // Set up client options with the fake transport. - clientOptions := &armpolicy.ClientOptions{ - ClientOptions: policy.ClientOptions{ - Transport: transport, - }, - } - - // Create the ResourceTypesClient with the fake transport and mock credential. - client, err := v20231001preview.NewResourceTypesClient(&azfake.TokenCredential{}, clientOptions) - require.NoError(t, err) - - ctx := context.Background() - planeName := "local" - resourceProviderName := "testResourceProvider" - resourceTypeName := "testResourceType" - resource := v20231001preview.ResourceTypeResource{ - Name: to.Ptr(resourceTypeName), - } - - // Call BeginCreateOrUpdate and poll until completion. - pollerResp, err := client.BeginCreateOrUpdate(ctx, planeName, resourceProviderName, resourceTypeName, resource, nil) - require.NoError(t, err) - finalResp, err := pollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - require.Equal(t, resourceTypeName, *finalResp.Name) - - // Call Get. - getResp, err := client.Get(ctx, planeName, resourceProviderName, resourceTypeName, nil) - require.NoError(t, err) - require.Equal(t, resourceTypeName, *getResp.Name) - - // Call BeginDelete and poll until completion. - deletePollerResp, err := client.BeginDelete(ctx, planeName, resourceProviderName, resourceTypeName, nil) - require.NoError(t, err) - _, err = deletePollerResp.PollUntilDone(ctx, nil) - require.NoError(t, err) - - // Call NewListPager. - pager := client.NewListPager(planeName, resourceProviderName, nil) - var resources []*v20231001preview.ResourceTypeResource - for pager.More() { - page, err := pager.NextPage(ctx) - require.NoError(t, err) - resources = append(resources, page.Value...) - } - require.Len(t, resources, 3) - require.Equal(t, "resourceType1", *resources[0].Name) - require.Equal(t, "resourceType2", *resources[1].Name) - require.Equal(t, "resourceType3", *resources[2].Name) -} diff --git a/pkg/ucp/manifestservice/service.go b/pkg/ucp/manifestservice/service.go index d38f092408..da34017ce9 100644 --- a/pkg/ucp/manifestservice/service.go +++ b/pkg/ucp/manifestservice/service.go @@ -21,14 +21,14 @@ import ( "fmt" "net" "os" - "os/signal" - "syscall" "time" + aztoken "github.com/radius-project/radius/pkg/azure/tokencredentials" + "github.com/radius-project/radius/pkg/cli/manifest" "github.com/radius-project/radius/pkg/sdk" + "github.com/radius-project/radius/pkg/ucp/api/v20231001preview" "github.com/radius-project/radius/pkg/ucp/hosting" ucpoptions "github.com/radius-project/radius/pkg/ucp/hostoptions" - "github.com/radius-project/radius/pkg/ucp/ucpclient" "github.com/radius-project/radius/pkg/ucp/ucplog" ) @@ -80,12 +80,6 @@ func waitForServer(ctx context.Context, host, port string, retryInterval time.Du func (w *Service) Run(ctx context.Context) error { logger := ucplog.FromContextOrDiscard(ctx) - // Set up signal handling for graceful shutdown - stopChan := make(chan os.Signal, 1) - signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM) - - // Start the manifest registration in a goroutine - //go func() { // Define connection parameters hostName := "localhost" //w.ucpConnection.Endpoint()/split to get host? // Replace with actual method port := "9000" // extract from endpoint Replace with actual method @@ -99,28 +93,32 @@ func (w *Service) Run(ctx context.Context) error { // Server is up, proceed to register manifests manifestDir := w.options.Manifests.ManifestDirectory - if _, err := os.Stat(manifestDir); os.IsNotExist(err) { - logger.Error(err, "Manifest directory does not exist", "directory", manifestDir) + if manifestDir == "" { + logger.Info("No manifest directory specified") return nil + } + + if _, err := os.Stat(manifestDir); os.IsNotExist(err) { + return fmt.Errorf("manifest directory does not exist: %w", err) } else if err != nil { - logger.Error(err, "Error checking manifest directory", "directory", manifestDir) - return nil + return fmt.Errorf("error checking manifest directory: %w", err) } - ucpclient, err := ucpclient.NewUCPClient(w.ucpConnection) + clientOptions := sdk.NewClientOptions(w.ucpConnection) + + clientFactory, err := v20231001preview.NewClientFactory(&aztoken.AnonymousCredential{}, clientOptions) if err != nil { - logger.Error(err, "Failed to create UCP client") + logger.Error(err, "Failed to create client factory") return nil } // Proceed with registering manifests - if err := ucpclient.RegisterManifests(ctx, manifestDir); err != nil { + if err := manifest.RegisterDirectory(ctx, *clientFactory, "local", manifestDir, nil); err != nil { logger.Error(err, "Failed to register manifests") return nil } logger.Info("Successfully registered manifests", "directory", manifestDir) - //}() return nil } diff --git a/pkg/ucp/ucpclient/locations_fake_test.go b/pkg/ucp/ucpclient/locations_fake_test_notneeded.go similarity index 100% rename from pkg/ucp/ucpclient/locations_fake_test.go rename to pkg/ucp/ucpclient/locations_fake_test_notneeded.go diff --git a/pkg/ucp/ucpclient/registermanifests.go b/pkg/ucp/ucpclient/registermanifests.go index e86f60fefe..cc0b447fe5 100644 --- a/pkg/ucp/ucpclient/registermanifests.go +++ b/pkg/ucp/ucpclient/registermanifests.go @@ -83,9 +83,10 @@ func (u *UCPClient) RegisterManifests(ctx context.Context, manifestDirectory str // Iterate over each file in the directory for _, fileInfo := range files { if fileInfo.IsDir() { - continue // Skip directories - check if want to include subdirectories + continue // Skip directories - TBD: check if want to include subdirectories } filePath := filepath.Join(manifestDirectory, fileInfo.Name()) + // Read the manifest file resourceProvider, err := manifest.ReadFile(filePath) if err != nil { diff --git a/pkg/ucp/ucpclient/registermanifests_test.go b/pkg/ucp/ucpclient/registermanifests_test.go index d27a23a754..ab4beb540a 100644 --- a/pkg/ucp/ucpclient/registermanifests_test.go +++ b/pkg/ucp/ucpclient/registermanifests_test.go @@ -9,52 +9,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package ucpclient - -/* -import ( - "context" - "testing" - - "github.com/radius-project/radius/pkg/ucp/hostoptions" - "github.com/radius-project/radius/pkg/ucp/server" - "github.com/stretchr/testify/require" -) - -func TestRegisterManifests(t *testing.T) { - ctx := context.Background() - - // Setup temporary manifest directory and files - manifestDir := "testdata" - - // Create sample manifest files in manifestDir - // ... - options := &server.Options{ - Config: &hostoptions.UCPConfig{ - Manifests: hostoptions.ManifestConfig{ - ManifestDirectory: manifestDir, - }, - }, - } - - // Setup fake UCP client with fake servers - fakeUCPClient, err := NewFakeUCPClient() - require.NoError(t, err) - - -} -*/ -/* -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ +package ucpclient import ( "context" @@ -79,20 +35,6 @@ type FakeUCPServer struct { LocationsServer *ucpfake.LocationsServer } -/* -type MultiTransport struct { - transports map[string]http.RoundTripper -} - -func (mt *MultiTransport) RoundTrip(req *http.Request) (*http.Response, error) { - for prefix, transport := range mt.transports { - if strings.HasPrefix(req.URL.Path, prefix) { - return transport.RoundTrip(req) - } - } - return nil, fmt.Errorf("no transport found for request path: %s", req.URL.Path) -}*/ - func NewFakeUCPServer() (*FakeUCPServer, error) { // Create fake servers for each client @@ -229,42 +171,6 @@ func NewTestUCPClient(server *FakeUCPServer) (*UCPClient, error) { apiVersionsTransport := ucpfake.NewAPIVersionsServerTransport(server.APIVersionsServer) locationsTransport := ucpfake.NewLocationsServerTransport(server.LocationsServer) - /* - // Create the ServerFactory - serverFactory := &ucpfake.ServerFactory{} - - // Register your fake servers with the ServerFactory - serverFactory.ResourceProvidersServer = *resourceProvidersServer - serverFactory.ResourceTypesServer = *resourceTypesServer - serverFactory.APIVersionsServer = *apiVersionsServer - serverFactory.LocationsServer = *locationsServer - //locTransporter := ucpfake.ServerFactoryTransport(locationsServer) - - serverFactoryTransport := ucpfake.NewServerFactoryTransport(serverFactory) - */ - - //clientOptions := &policy.ClientOptions{Transport: serverFactoryTransport} - - // Assuming you have a UCPConnection or can use a fake one for testing - //ucpConnection, err := sdk.NewDirectConnection("") // Replace with a valid implementation - /* - // Create the server.Options instance - serverOptions := &server.Options{ - UCPConnection: ucpConnection, - } - - httpClient:= http.Client{ - Transport: serverFactoryTransport, - } - - ucpConnection.Client() = httpClient - */ - // Instantiate UCPClientImpl with the unified ClientOptions - //ucpClient, err := NewUCPClient(ucpConnection) - /*if err != nil { - return nil, fmt.Errorf("failed to create UCP client: %w", err) - }*/ - // Configure client options with respective transports resourceProvidersOptions := &armpolicy.ClientOptions{ ClientOptions: policy.ClientOptions{ diff --git a/pkg/ucp/ucpclient/resourceproviders_fakes_test.go b/pkg/ucp/ucpclient/resourceproviders_fakes_test_notneeded.go similarity index 100% rename from pkg/ucp/ucpclient/resourceproviders_fakes_test.go rename to pkg/ucp/ucpclient/resourceproviders_fakes_test_notneeded.go diff --git a/pkg/ucp/ucpclient/resourcetypes_fake_test.go b/pkg/ucp/ucpclient/resourcetypes_fake_test_notneeded.go similarity index 100% rename from pkg/ucp/ucpclient/resourcetypes_fake_test.go rename to pkg/ucp/ucpclient/resourcetypes_fake_test_notneeded.go diff --git a/pkg/ucp/ucpclient/ucpclient.go b/pkg/ucp/ucpclient/ucpclient.go index 03de4be03e..0e1efa2e8d 100644 --- a/pkg/ucp/ucpclient/ucpclient.go +++ b/pkg/ucp/ucpclient/ucpclient.go @@ -18,30 +18,6 @@ import ( ucpv20231001 "github.com/radius-project/radius/pkg/ucp/api/v20231001preview" ) -/* -// UCPClient is a client for interacting with the UCP API. -type UCPClient interface { - // CreateOrUpdateResourceProvider creates or updates a resource provider in the configured scope. - CreateOrUpdateResourceProvider(ctx context.Context, planeName string, providerNamespace string, resource *ucpv20231001.ResourceProviderResource) (ucpv20231001.ResourceProviderResource, error) - - // CreateOrUpdateResourceType creates or updates a resource type in the configured scope. - GetResourceProvider(ctx context.Context, planeName string, resourceProviderName string) (ucpv20231001.ResourceProviderResource, error) - - // CreateOrUpdateResourceType creates or updates a resource type in the configured scope. - CreateOrUpdateResourceType(ctx context.Context, planeName string, providerNamespace string, resourceTypeName string, resource *ucpv20231001.ResourceTypeResource) (ucpv20231001.ResourceTypeResource, error) - - // CreateOrUpdateAPIVersion creates or updates an API version in the configured scope. - CreateOrUpdateAPIVersion(ctx context.Context, planeName string, resourceProviderName string, resourceTypeName string, apiVersionName string, resource *ucpv20231001.APIVersionResource) (ucpv20231001.APIVersionResource, error) - - // CreateOrUpdateLocation creates or updates a resource provider location in the configured scope. - CreateOrUpdateLocation(ctx context.Context, planeName string, resourceProviderName string, locationName string, resource *ucpv20231001.LocationResource) (ucpv20231001.LocationResource, error) - - RegisterManifests(ctx context.Context) error - - // ... Add other methods as needed ... -} -*/ - const planeName = "local" // UCPClient holds instances of each specific client. @@ -53,8 +29,6 @@ type UCPClient struct { // Add other clients as needed } -//var _ UCPClient = (*UCPClientFactory)(nil) - // CreateOrUpdateResourceProvider creates or updates a resource provider in the configured scope. func (u *UCPClient) CreateOrUpdateResourceProvider(ctx context.Context, planeName string, resourceProviderName string, resource *ucpv20231001.ResourceProviderResource) (ucpv20231001.ResourceProviderResource, error) { if u.ResourceProvidersClient == nil {