-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of resource providers and resource types registration
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 <[email protected]>
- Loading branch information
Showing
135 changed files
with
10,237 additions
and
401 deletions.
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
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.