Skip to content

Commit

Permalink
Adapt types.Resource to Resource153 (#35568)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingllama authored Dec 11, 2023
1 parent 7d31f5e commit cfabeb4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
64 changes: 64 additions & 0 deletions api/types/resource_153.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
)

// ResourceMetadata is the smallest interface that defines a Teleport resource.
type ResourceMetadata interface {
// GetMetadata returns the generic resource metadata.
GetMetadata() *headerv1.Metadata
}

// Resource153 is a resource that follows RFD 153.
//
// It exists as a weak guideline for fields that resource protos must provide
Expand Down Expand Up @@ -52,6 +58,64 @@ type Resource153 interface {
GetMetadata() *headerv1.Metadata
}

// LegacyToResource153 converts a legacy [Resource] into a [Resource153].
//
// Useful to handle old and new resources uniformly. If you can, consider
// further "downgrading" the Resource153 interface into the smallest subset that
// works for you (for example, [ResourceMetadata]).
func LegacyToResource153(r Resource) Resource153 {
return &legacyToResource153Adapter{inner: r}
}

type legacyToResource153Adapter struct {
inner Resource
}

// Unwrap is an escape hatch for Resource instances that are piped down into the
// codebase as a legacy Resource.
//
// Ideally you shouldn't depend on this.
func (r *legacyToResource153Adapter) Unwrap() Resource {
return r.inner
}

// MarshalJSON adds support for marshaling the wrapped resource (instead of
// marshaling the adapter itself).
func (r *legacyToResource153Adapter) MarshalJSON() ([]byte, error) {
return json.Marshal(r.inner)
}

func (r *legacyToResource153Adapter) GetKind() string {
return r.inner.GetKind()
}

func (r *legacyToResource153Adapter) GetMetadata() *headerv1.Metadata {
md := r.inner.GetMetadata()

var expires *timestamppb.Timestamp
if md.Expires != nil {
expires = timestamppb.New(*md.Expires)
}

return &headerv1.Metadata{
Name: md.Name,
Namespace: md.Namespace,
Description: md.Description,
Labels: md.Labels,
Expires: expires,
Id: md.ID,
Revision: md.Revision,
}
}

func (r *legacyToResource153Adapter) GetSubKind() string {
return r.inner.GetSubKind()
}

func (r *legacyToResource153Adapter) GetVersion() string {
return r.inner.GetVersion()
}

// Resource153ToLegacy transforms an RFD 153 style resource into a legacy
// [Resource] type.
//
Expand Down
36 changes: 36 additions & 0 deletions api/types/resource_153_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ import (
"github.com/gravitational/teleport/api/types"
)

func TestLegacyToResource153(t *testing.T) {
// user is an example of a legacy resource.
// Any other resource type would to.
user := &types.UserV2{
Kind: "user",
Metadata: types.Metadata{
Name: "llama",
},
Spec: types.UserSpecV2{
Roles: []string{"human", "camelidae"},
},
}

resource := types.LegacyToResource153(user)

// Unwrap gives the underlying resource back.
t.Run("unwrap", func(t *testing.T) {
unwrapped := resource.(interface{ Unwrap() types.Resource }).Unwrap()
if diff := cmp.Diff(user, unwrapped, protocmp.Transform()); diff != "" {
t.Errorf("Unwrap mismatch (-want +got)\n%s", diff)
}
})

// Marshaling as JSON marshals the underlying resource.
t.Run("marshal", func(t *testing.T) {
jsonBytes, err := json.Marshal(resource)
require.NoError(t, err, "Marshal")

user2 := &types.UserV2{}
require.NoError(t, json.Unmarshal(jsonBytes, user2), "Unmarshal")
if diff := cmp.Diff(user, user2, protocmp.Transform()); diff != "" {
t.Errorf("Marshal/Unmarshal mismatch (-want +got)\n%s", diff)
}
})
}

func TestResource153ToLegacy(t *testing.T) {
// bot is an example of an RFD 153 "compliant" resource.
// Any other resource type would do.
Expand Down

0 comments on commit cfabeb4

Please sign in to comment.