Skip to content

Commit

Permalink
Initial implementation of user-defined-types (#7686)
Browse files Browse the repository at this point in the history
This change implements the skeleton of user-defined types. The changes
here enable the following:

- Users can author a resource of type
`System.Resources/resourceProviders` to create a user-defined-type.
- Users can use the UCP API to register and query `resourceProviders`.
- Users can use the UCP API to execute the full lifecycle of a
user-defined-type.

Right now the user-defined-type RP will use our default operation
(synchronous) controllers to implement the resource lifecycle. There is
no background processing.

The next step will include the ability to execute asynchronous
operations like recipes.

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).
- This pull request adds or changes features of Radius and has an
approved issue (issue link required).

Part of: #6688

**note: This change is going into a feature-branch where we can iterate
on the user-defined-type design before integrating it with main. The PR
is an FYI 😆.**

---------

Signed-off-by: ytimocin <ytimocin@microsoft.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: willdavsmith <willdavsmith@gmail.com>
Signed-off-by: Ryan Nowak <nowakra@gmail.com>
Co-authored-by: Yetkin Timocin <ytimocin@microsoft.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Will Smith <willdavsmith@gmail.com>
  • Loading branch information
4 people committed Jun 24, 2024
1 parent 95dce96 commit 8f8632c
Show file tree
Hide file tree
Showing 54 changed files with 3,454 additions and 115 deletions.
57 changes: 57 additions & 0 deletions pkg/armrpc/frontend/middleware/resourceidoverride.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 middleware

import (
"net/http"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/ucp/resources"
"github.com/radius-project/radius/pkg/ucp/ucplog"
)

// OverrideResourceIDMiddleware is a middleware that tweaks the resource ID of the request.
//
// This is useful for URLs that don't follow the usual ResourceID pattern. We still want these
// URLs to be handled by our data storage and telemetry systems in the same way.
//
// For example a request like:
//
// GET /planes/radius/local/providers -> ResourceID: /planes/radius/local/providers/System.Resources/resourceProviders
func OverrideResourceID(override func(req *http.Request) (resources.ID, error)) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
// This handler will get the resource ID and update the stored request to refer to it.
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
id, err := override(req)
if err != nil {
logger := ucplog.FromContextOrDiscard(req.Context())
logger.Error(err, "failed to override resource ID")
next.ServeHTTP(w, req)
return
}

// Update the request context with the new resource ID.
armCtx := v1.ARMRequestContextFromContext(req.Context())
if armCtx != nil {
armCtx.ResourceID = id
*req = *req.WithContext(v1.WithARMRequestContext(req.Context(), armCtx))
}

next.ServeHTTP(w, req)
})
}
}
54 changes: 54 additions & 0 deletions pkg/armrpc/frontend/middleware/resourceidoverride_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 middleware

import (
"context"
"net/http"
"net/http/httptest"
"testing"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/ucp/resources"
"github.com/stretchr/testify/require"
)

func Test_OverrideResourceID(t *testing.T) {
override := func(req *http.Request) (resources.ID, error) {
return resources.MustParse("/planes/radius/local"), nil
}

actualID := resources.ID{}
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
armCtx := v1.ARMRequestContextFromContext(req.Context())
actualID = armCtx.ResourceID
})

h := OverrideResourceID(override)(handler)

w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)

ctx := v1.WithARMRequestContext(context.Background(), &v1.ARMRequestContext{
ResourceID: resources.MustParse("/planes/radius/anotherone/"),
})
req = req.WithContext(ctx)

h.ServeHTTP(w, req)

require.Equal(t, resources.MustParse("/planes/radius/local"), actualID)
}
32 changes: 32 additions & 0 deletions pkg/ucp/api/v20231001preview/dynamicresource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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

// DynamicResource is used as the versioned resource model for dynamic resources.
//
// A dynamic resource is implemented internally to UCP, and uses a user-provided
// OpenAPI specification to define the resource schema. Since the resource is internal
// to UCP and dynamically generated, this struct is used to represent all dynamic resources.
type DynamicResource struct {
ID *string `json:"id"`
Name *string `json:"name"`
Type *string `json:"type"`
Location *string `json:"location"`
Tags map[string]*string `json:"tags,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
SystemData *SystemData `json:"systemData,omitempty"`
}
64 changes: 64 additions & 0 deletions pkg/ucp/api/v20231001preview/dynamicresource_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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"
)

func (d *DynamicResource) ConvertTo() (v1.DataModelInterface, error) {
dm := &datamodel.DynamicResource{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: to.String(d.ID),
Name: to.String(d.Name),
Type: to.String(d.Type),
Location: to.String(d.Location),
Tags: to.StringMap(d.Tags),
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
Properties: d.Properties,
}

return dm, nil
}

func (d *DynamicResource) ConvertFrom(src v1.DataModelInterface) error {
dm, ok := src.(*datamodel.DynamicResource)
if !ok {
return v1.ErrInvalidModelConversion
}

d.ID = &dm.ID
d.Name = &dm.Name
d.Type = &dm.Type
d.Location = &dm.Location
d.Tags = *to.StringMapPtr(dm.Tags)
d.SystemData = fromSystemDataModel(dm.SystemData)
d.Properties = dm.Properties
if d.Properties == nil {
d.Properties = map[string]any{}
}
d.Properties["provisioningState"] = fromProvisioningStateDataModel(dm.AsyncProvisioningState)

return nil
}
126 changes: 126 additions & 0 deletions pkg/ucp/api/v20231001preview/dynamicresource_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
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"

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"
"github.com/radius-project/radius/test/testutil"

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

func Test_DynamicResource_ConvertVersionedToDataModel(t *testing.T) {
conversionTests := []struct {
filename string
expected *datamodel.DynamicResource
err error
}{
{
filename: "dynamicresource-resource.json",
expected: &datamodel.DynamicResource{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: "/planes/radius/local/resourceGroups/test/providers/Applications.Test/testResources/testResource",
Name: "testResource",
Type: "Applications.Test/testResources",
Location: "global",
Tags: map[string]string{
"env": "dev",
},
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
Properties: map[string]any{
"message": "Hello, world!",
},
},
},
}

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

dm, err := r.ConvertTo()

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

func Test_DynamicResource_ConvertDataModelToVersioned(t *testing.T) {
conversionTests := []struct {
filename string
expected *DynamicResource
err error
}{
{
filename: "dynamicresource-datamodel.json",
expected: &DynamicResource{
ID: to.Ptr("/planes/radius/local/resourceGroups/test/providers/Applications.Test/testResources/testResource"),
Name: to.Ptr("testResource"),
Type: to.Ptr("Applications.Test/testResources"),
Location: to.Ptr("global"),
Tags: map[string]*string{
"env": to.Ptr("dev"),
},
Properties: map[string]any{
"provisioningState": fromProvisioningStateDataModel(v1.ProvisioningStateSucceeded),
"message": "Hello, world!",
},
},
},
}

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

resource := &DynamicResource{}
err = resource.ConvertFrom(dm)

// Avoid hardcoding the SystemData field in tests.
tt.expected.SystemData = fromSystemDataModel(dm.SystemData)

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, resource)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/ucp/api/v20231001preview/genericresource_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
ResourceType = "System.Resources/resources"
GenericResourceType = "System.Resources/resources"
)

// ConvertTo converts from the versioned GenericResource resource to version-agnostic datamodel.
Expand Down
Loading

0 comments on commit 8f8632c

Please sign in to comment.