-
Notifications
You must be signed in to change notification settings - Fork 93
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
rynowak
wants to merge
1
commit into
radius-project:main
Choose a base branch
from
rynowak:udt-registration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
112
pkg/ucp/api/v20231001preview/apiversion_conversion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
.tsp
, converters, and datamodel. That will give you an idea of the API surface being added.