Skip to content

Commit

Permalink
Present included resources in includes resolver
Browse files Browse the repository at this point in the history
Included resources in the API response should be in the same format the
API would return them when getting them. This change makes the include
resolver delegate to a presenter that in turn delegates to the included
object presenter

This fixes service broker names not being visible in `cf marketplace`
  • Loading branch information
danail-branekov authored and georgethebeatle committed Aug 16, 2024
1 parent d3f479b commit 5282111
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 11 deletions.
112 changes: 112 additions & 0 deletions api/handlers/include/fake/resource_presenter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion api/handlers/include/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ type ResourceRelationshipRepository interface {
ListRelatedResources(context.Context, authorization.Info, string, []relationships.Resource) ([]relationships.Resource, error)
}

//counterfeiter:generate -o fake -fake-name ResourcePresenter . ResourcePresenter
type ResourcePresenter interface {
PresentResource(resource relationships.Resource) any
}

type IncludeResolver[S ~[]E, E relationships.Resource] struct {
relationshipsRepo ResourceRelationshipRepository
resourcePresenter ResourcePresenter
}

func NewIncludeResolver[S ~[]E, E relationships.Resource](
relationshipsRepo ResourceRelationshipRepository,
resourcePresenter ResourcePresenter,
) *IncludeResolver[S, E] {
return &IncludeResolver[S, E]{
relationshipsRepo: relationshipsRepo,
resourcePresenter: resourcePresenter,
}
}

Expand Down Expand Up @@ -76,7 +84,7 @@ func (h *IncludeResolver[S, E]) resolveInclude(
includedResources = slices.Collect(it.Map(itx.FromSlice(resources), func(r relationships.Resource) model.IncludedResource {
return model.IncludedResource{
Type: plural(relatedResourceType),
Resource: r,
Resource: h.resourcePresenter.PresentResource(r),
}
}))
}
Expand Down
42 changes: 33 additions & 9 deletions api/handlers/include/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (t Bar) Relationships() map[string]model.ToOneRelationship {
var _ = Describe("ResolveIncludes", func() {
var (
relationshipsRepo *fake.ResourceRelationshipRepository
resourcePresenter *fake.ResourcePresenter
resolver *include.IncludeResolver[[]Foo, Foo]

inputResources []Foo
Expand All @@ -52,8 +53,13 @@ var _ = Describe("ResolveIncludes", func() {
result = []model.IncludedResource{}

relationshipsRepo = new(fake.ResourceRelationshipRepository)
resourcePresenter = new(fake.ResourcePresenter)
resourcePresenter.PresentResourceReturns(map[string]string{
"presented-field1": "present1",
"presented-field2": "present2",
})

resolver = include.NewIncludeResolver[[]Foo](relationshipsRepo)
resolver = include.NewIncludeResolver[[]Foo](relationshipsRepo, resourcePresenter)
})

JustBeforeEach(func() {
Expand Down Expand Up @@ -83,14 +89,23 @@ var _ = Describe("ResolveIncludes", func() {
}, nil)
})

It("includes the related object", func() {
It("presents the related object", func() {
Expect(resourcePresenter.PresentResourceCallCount()).To(Equal(1))
actualResource := resourcePresenter.PresentResourceArgsForCall(0)
Expect(actualResource).To(Equal(Bar{
Name: "bar",
GUID: "bar-guid",
}))
})

It("includes the presented object", func() {
Expect(resolveErr).NotTo(HaveOccurred())
Expect(result).To(ConsistOf(
model.IncludedResource{
Type: "bars",
Resource: map[string]any{
"name": "bar",
"guid": "bar-guid",
"presented-field1": "present1",
"presented-field2": "present2",
},
},
))
Expand All @@ -100,7 +115,7 @@ var _ = Describe("ResolveIncludes", func() {
BeforeEach(func() {
rules = []params.IncludeResourceRule{{
RelationshipPath: []string{"bar"},
Fields: []string{"name"},
Fields: []string{"presented-field2"},
}}
})

Expand All @@ -110,7 +125,7 @@ var _ = Describe("ResolveIncludes", func() {
model.IncludedResource{
Type: "bars",
Resource: map[string]any{
"name": "bar",
"presented-field2": "present2",
},
},
))
Expand Down Expand Up @@ -178,6 +193,15 @@ var _ = Describe("ResolveIncludes", func() {
}
return nil, nil
}

resourcePresenter.PresentResourceReturnsOnCall(0, map[string]string{
"guid": "presented-foo-guid",
"name": "presented-foo-name",
})
resourcePresenter.PresentResourceReturnsOnCall(1, map[string]string{
"guid": "presented-bar-guid",
"name": "presented-bar-name",
})
})

It("resolves all rules", func() {
Expand All @@ -186,14 +210,14 @@ var _ = Describe("ResolveIncludes", func() {
model.IncludedResource{
Type: "foos",
Resource: map[string]any{
"name": "foo",
"guid": "foo-guid",
"guid": "presented-foo-guid",
"name": "presented-foo-name",
},
},
model.IncludedResource{
Type: "bars",
Resource: map[string]any{
"name": "bar",
"name": "presented-bar-name",
},
},
))
Expand Down
2 changes: 1 addition & 1 deletion api/handlers/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewServicePlan(
serverURL: serverURL,
requestValidator: requestValidator,
servicePlanRepo: servicePlanRepo,
includeResolver: include.NewIncludeResolver[[]repositories.ServicePlanRecord](relationshipRepo),
includeResolver: include.NewIncludeResolver[[]repositories.ServicePlanRecord](relationshipRepo, presenter.NewResource(serverURL)),
}
}

Expand Down
31 changes: 31 additions & 0 deletions api/presenter/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package presenter

import (
"net/url"

"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/api/repositories/relationships"
)

type Resource struct {
serverURL url.URL
}

func NewResource(serverURL url.URL) *Resource {
return &Resource{
serverURL: serverURL,
}
}

func (r *Resource) PresentResource(resource relationships.Resource) any {
switch res := resource.(type) {
case repositories.ServiceBrokerRecord:
return ForServiceBroker(res, r.serverURL)
case repositories.ServiceOfferingRecord:
return ForServiceOffering(res, r.serverURL)
case repositories.ServicePlanRecord:
return ForServicePlan(res, r.serverURL)
default:
return resource
}
}
73 changes: 73 additions & 0 deletions api/presenter/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package presenter_test

import (
"net/url"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"code.cloudfoundry.org/korifi/api/presenter"
"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/api/repositories/relationships"
"code.cloudfoundry.org/korifi/model"
)

type unsupportedResource struct{}

func (r *unsupportedResource) Relationships() map[string]model.ToOneRelationship {
return nil
}

var _ = Describe("Resource", func() {
var (
resourcePresenter *presenter.Resource
resource relationships.Resource
presentedResource any
)

BeforeEach(func() {
url, err := url.Parse("https://api.example.org")
Expect(err).NotTo(HaveOccurred())

resource = &unsupportedResource{}
resourcePresenter = presenter.NewResource(*url)
})

JustBeforeEach(func() {
presentedResource = resourcePresenter.PresentResource(resource)
})

It("returns the original resource", func() {
Expect(presentedResource).To(BeAssignableToTypeOf(&unsupportedResource{}))
})

When("the resource is a service broker", func() {
BeforeEach(func() {
resource = repositories.ServiceBrokerRecord{}
})

It("returns presented broker", func() {
Expect(presentedResource).To(BeAssignableToTypeOf(presenter.ServiceBrokerResponse{}))
})
})

When("the resource is a service offering", func() {
BeforeEach(func() {
resource = repositories.ServiceOfferingRecord{}
})

It("returns presented broker", func() {
Expect(presentedResource).To(BeAssignableToTypeOf(presenter.ServiceOfferingResponse{}))
})
})

When("the resource is a service plan", func() {
BeforeEach(func() {
resource = repositories.ServicePlanRecord{}
})

It("returns presented broker", func() {
Expect(presentedResource).To(BeAssignableToTypeOf(presenter.ServicePlanResponse{}))
})
})
})

0 comments on commit 5282111

Please sign in to comment.