Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of resource providers and resource types registration #7967

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pkg/armrpc/frontend/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ func HandlerForController(controller ctrl.Controller, operationType v1.Operation
}
}

// CreateHandler creates an http.Handler for the given resource type and operation method.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers, here's a suggested tour:

  • Start with the .tsp, converters, and datamodel. That will give you an idea of the API surface being added.
  • Next look at the frontend changes. This is where I implemented support for child-resources.
  • Next look at the routing changes in UCP. It was impossible to implement the correct behavior using the current design of routing. UCP's requirements are more complex.
  • Last take a look at the integration tests + backend conrollers.

func CreateHandler(ctx context.Context, resourceType string, operationMethod v1.OperationMethod, opts ctrl.Options, factory ControllerFactoryFunc) (http.HandlerFunc, error) {
storageClient, err := opts.DataProvider.GetStorageClient(ctx, resourceType)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to extract this logic into its own function to use with UCP routing.

if err != nil {
return nil, err
}

opts.StorageClient = storageClient
opts.ResourceType = resourceType

ctrl, err := factory(opts)
if err != nil {
return nil, err
}

handler := HandlerForController(ctrl, v1.OperationType{Type: resourceType, Method: operationMethod})
return handler, nil
}

// RegisterHandler registers a handler for the given resource type and method. This function should only
// be used for controllers that process a single resource type.
func RegisterHandler(ctx context.Context, opts HandlerOptions, ctrlOpts ctrl.Options) error {
Expand Down
65 changes: 65 additions & 0 deletions pkg/ucp/api/v20231001preview/apiversion_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 The Radius Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
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 v20231001preview

import (
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/datamodel"
)

// ConvertTo converts from the versioned APIVersionResource resource to version-agnostic datamodel.
func (src *APIVersionResource) ConvertTo() (v1.DataModelInterface, error) {
dst := &datamodel.APIVersion{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: to.String(src.ID),
Name: to.String(src.Name),
Type: datamodel.APIVersionResourceType,

// NOTE: this is a child resource. It does not have a location, systemData, or tags.
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
}

dst.Properties = datamodel.APIVersionProperties{}

return dst, nil
}

// ConvertFrom converts from version-agnostic datamodel to the versioned APIVersionResource resource.
func (dst *APIVersionResource) ConvertFrom(src v1.DataModelInterface) error {
dm, ok := src.(*datamodel.APIVersion)
if !ok {
return v1.ErrInvalidModelConversion
}

dst.ID = to.Ptr(dm.ID)
dst.Name = to.Ptr(dm.Name)
dst.Type = to.Ptr(dm.Type)

// NOTE: this is a child resource. It does not have a location, systemData, or tags.

dst.Properties = &APIVersionProperties{
ProvisioningState: to.Ptr(ProvisioningState(dm.InternalMetadata.AsyncProvisioningState)),
}

return nil
}
112 changes: 112 additions & 0 deletions pkg/ucp/api/v20231001preview/apiversion_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright 2023 The Radius Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
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 v20231001preview

import (
"encoding/json"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/ucp/datamodel"
"github.com/radius-project/radius/test/testutil"

"github.com/stretchr/testify/require"
)

func Test_APIVersion_VersionedToDataModel(t *testing.T) {
conversionTests := []struct {
filename string
expected *datamodel.APIVersion
err error
}{
{
filename: "apiversion_resource.json",
expected: &datamodel.APIVersion{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: "/planes/radius/local/providers/System.Resources/resourceProviders/Applications.Test/resourceTypes/testResources/apiVersions/2025-01-01",
Name: "2025-01-01",
Type: datamodel.APIVersionResourceType,
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
Properties: datamodel.APIVersionProperties{},
},
},
}

for _, tt := range conversionTests {
t.Run(tt.filename, func(t *testing.T) {
rawPayload := testutil.ReadFixture(tt.filename)
versioned := &APIVersionResource{}
err := json.Unmarshal(rawPayload, versioned)
require.NoError(t, err)

dm, err := versioned.ConvertTo()

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, dm)
}
})
}
}

func Test_APIVersion_DataModelToVersioned(t *testing.T) {
conversionTests := []struct {
filename string
expected *APIVersionResource
err error
}{
{
filename: "apiversion_datamodel.json",
expected: &APIVersionResource{
ID: to.Ptr("/planes/radius/local/providers/System.Resources/resourceProviders/Applications.Test/resourceTypes/testResources/apiVersions/2025-01-01"),
Type: to.Ptr(datamodel.APIVersionResourceType),
Name: to.Ptr("2025-01-01"),
Properties: &APIVersionProperties{
ProvisioningState: to.Ptr(ProvisioningStateSucceeded),
},
},
},
}

for _, tt := range conversionTests {
t.Run(tt.filename, func(t *testing.T) {
rawPayload := testutil.ReadFixture(tt.filename)
data := &datamodel.APIVersion{}
err := json.Unmarshal(rawPayload, data)
require.NoError(t, err)

versioned := &APIVersionResource{}

err = versioned.ConvertFrom(data)

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, versioned)
}
})
}
}
116 changes: 116 additions & 0 deletions pkg/ucp/api/v20231001preview/location_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2023 The Radius Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
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 v20231001preview

import (
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/pkg/ucp/datamodel"
)

// ConvertTo converts from the versioned LocationResource resource to version-agnostic datamodel.
func (src *LocationResource) ConvertTo() (v1.DataModelInterface, error) {
dst := &datamodel.Location{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: to.String(src.ID),
Name: to.String(src.Name),
Type: datamodel.LocationResourceType,

// NOTE: this is a child resource. It does not have a location, systemData, or tags.
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
}

dst.Properties = datamodel.LocationProperties{
Address: src.Properties.Address,
ResourceTypes: map[string]datamodel.LocationResourceTypeConfiguration{},
}

for name, value := range src.Properties.ResourceTypes {
dst.Properties.ResourceTypes[name] = toLocationResourceTypeDatamodel(value)
}

return dst, nil
}

// ConvertFrom converts from version-agnostic datamodel to the versioned LocationResource resource.
func (dst *LocationResource) ConvertFrom(src v1.DataModelInterface) error {
dm, ok := src.(*datamodel.Location)
if !ok {
return v1.ErrInvalidModelConversion
}

dst.ID = to.Ptr(dm.ID)
dst.Name = to.Ptr(dm.Name)
dst.Type = to.Ptr(datamodel.LocationResourceType)

// NOTE: this is a child resource. It does not have a location, systemData, or tags.

dst.Properties = &LocationProperties{
ProvisioningState: to.Ptr(ProvisioningState(dm.InternalMetadata.AsyncProvisioningState)),
Address: dm.Properties.Address,
ResourceTypes: map[string]*LocationResourceType{},
}

for name, value := range dm.Properties.ResourceTypes {
dst.Properties.ResourceTypes[name] = fromLocationResourceTypeDatamodel(value)
}

return nil
}

func toLocationResourceTypeDatamodel(src *LocationResourceType) datamodel.LocationResourceTypeConfiguration {
dst := datamodel.LocationResourceTypeConfiguration{
APIVersions: map[string]datamodel.LocationAPIVersionConfiguration{},
}

for name, value := range src.APIVersions {
dst.APIVersions[name] = toLocationAPIVersionDatamodel(value)
}

return dst
}

func toLocationAPIVersionDatamodel(_ map[string]any) datamodel.LocationAPIVersionConfiguration {
dst := datamodel.LocationAPIVersionConfiguration{
// Empty for now.
}
return dst
}

func fromLocationResourceTypeDatamodel(src datamodel.LocationResourceTypeConfiguration) *LocationResourceType {
dst := &LocationResourceType{
APIVersions: map[string]map[string]any{},
}

for name, value := range src.APIVersions {
dst.APIVersions[name] = fromLocationAPIVersionDatamodel(value)
}

return dst
}

func fromLocationAPIVersionDatamodel(src datamodel.LocationAPIVersionConfiguration) map[string]any {
dst := map[string]any{
// Empty for now.
}
return dst
}
Loading
Loading