Skip to content

Commit

Permalink
Implementation of resource providers and resource types registration
Browse files Browse the repository at this point in the history
This change implements the API functionality for registering resource providers and related child-types through the Radius API. This is the first step towards making the Radius API surface extensible. For this first step there is no consumer of this data or these APIs. Subsequent changes will consume the data and implement the user-facing functionality.

What's possible in this commit:

- CRUDL operations on resource provider and related types like:
  - resource type
  - api version (child of resource type)
  - location

Additionally, this change implements the resource provider "summary" API. The summary API provides a combined view of the most useful data from each resource provider and its children.

---

For reviewers there are a few important notes:

- Routing in UCP is complex, and error prone to work on. I abandoned the existing code and rewrote it in a form that's more native to go-chi. This was a significant improvement and made it much easier to debug the code. UCP has good integration tests so I'm not very concerned about regressions.
- This is our first use-case for child resources in Radius. I used the async controllers to implement cascading deletion.
- The "summary" API uses cached data. The async controllers for resource provider and other types update the cache. This is our first use of this pattern, but it felt right to me.

Signed-off-by: Ryan Nowak <nowakra@gmail.com>
  • Loading branch information
rynowak committed Sep 26, 2024
1 parent 2df79ec commit e4547fb
Show file tree
Hide file tree
Showing 82 changed files with 8,363 additions and 300 deletions.
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.
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)
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

0 comments on commit e4547fb

Please sign in to comment.