-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3371 from /issues/3264-list-offerings
Implement GET /v3/service_offerings
- Loading branch information
Showing
53 changed files
with
2,150 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
*.test | ||
tests/assets/dorifi/dorifi | ||
tests/assets/multi-process/dorifi | ||
tests/assets/sample-broker/sample-broker |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"code.cloudfoundry.org/korifi/api/authorization" | ||
apierrors "code.cloudfoundry.org/korifi/api/errors" | ||
"code.cloudfoundry.org/korifi/api/presenter" | ||
"code.cloudfoundry.org/korifi/api/repositories" | ||
"code.cloudfoundry.org/korifi/api/routing" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
const ( | ||
ServiceOfferingsPath = "/v3/service_offerings" | ||
) | ||
|
||
//counterfeiter:generate -o fake -fake-name CFServiceOfferingRepository . CFServiceOfferingRepository | ||
type CFServiceOfferingRepository interface { | ||
ListOfferings(context.Context, authorization.Info) ([]repositories.ServiceOfferingResource, error) | ||
} | ||
|
||
type ServiceOffering struct { | ||
serverURL url.URL | ||
serviceOfferingRepo CFServiceOfferingRepository | ||
} | ||
|
||
func NewServiceOffering( | ||
serverURL url.URL, | ||
serviceOfferingRepo CFServiceOfferingRepository, | ||
) *ServiceOffering { | ||
return &ServiceOffering{ | ||
serverURL: serverURL, | ||
serviceOfferingRepo: serviceOfferingRepo, | ||
} | ||
} | ||
|
||
func (h *ServiceOffering) list(r *http.Request) (*routing.Response, error) { | ||
authInfo, _ := authorization.InfoFromContext(r.Context()) | ||
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-offering.list") | ||
|
||
serviceOfferingList, err := h.serviceOfferingRepo.ListOfferings(r.Context(), authInfo) | ||
if err != nil { | ||
return nil, apierrors.LogAndReturn(logger, err, "Failed to list service offerings") | ||
} | ||
|
||
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForList(presenter.ForServiceOffering, serviceOfferingList, h.serverURL, *r.URL)), nil | ||
} | ||
|
||
func (h *ServiceOffering) UnauthenticatedRoutes() []routing.Route { | ||
return nil | ||
} | ||
|
||
func (h *ServiceOffering) AuthenticatedRoutes() []routing.Route { | ||
return []routing.Route{ | ||
{Method: "GET", Pattern: ServiceOfferingsPath, Handler: h.list}, | ||
} | ||
} |
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,71 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"net/http" | ||
|
||
. "code.cloudfoundry.org/korifi/api/handlers" | ||
"code.cloudfoundry.org/korifi/api/handlers/fake" | ||
"code.cloudfoundry.org/korifi/api/repositories" | ||
"code.cloudfoundry.org/korifi/model" | ||
"code.cloudfoundry.org/korifi/model/services" | ||
. "code.cloudfoundry.org/korifi/tests/matchers" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ServiceOffering", func() { | ||
var serviceOfferingRepo *fake.CFServiceOfferingRepository | ||
|
||
BeforeEach(func() { | ||
serviceOfferingRepo = new(fake.CFServiceOfferingRepository) | ||
|
||
apiHandler := NewServiceOffering( | ||
*serverURL, | ||
serviceOfferingRepo, | ||
) | ||
routerBuilder.LoadRoutes(apiHandler) | ||
}) | ||
|
||
Describe("GET /v3/service_offerings", func() { | ||
BeforeEach(func() { | ||
serviceOfferingRepo.ListOfferingsReturns([]repositories.ServiceOfferingResource{{ | ||
ServiceOffering: services.ServiceOffering{}, | ||
CFResource: model.CFResource{ | ||
GUID: "offering-guid", | ||
}, | ||
Relationships: repositories.ServiceOfferingRelationships{ | ||
ServiceBroker: model.ToOneRelationship{ | ||
Data: model.Relationship{ | ||
GUID: "broker-guid", | ||
}, | ||
}, | ||
}, | ||
}}, nil) | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/service_offerings", nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
routerBuilder.Build().ServeHTTP(rr, req) | ||
}) | ||
|
||
It("lists the service offerings", func() { | ||
Expect(serviceOfferingRepo.ListOfferingsCallCount()).To(Equal(1)) | ||
_, actualAuthInfo := serviceOfferingRepo.ListOfferingsArgsForCall(0) | ||
Expect(actualAuthInfo).To(Equal(authInfo)) | ||
|
||
Expect(rr).Should(HaveHTTPStatus(http.StatusOK)) | ||
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json")) | ||
Expect(rr).To(HaveHTTPBody(SatisfyAll( | ||
MatchJSONPath("$.pagination.total_results", BeEquivalentTo(1)), | ||
MatchJSONPath("$.pagination.first.href", "https://api.example.org/v3/service_offerings"), | ||
MatchJSONPath("$.resources[0].guid", "offering-guid"), | ||
MatchJSONPath("$.resources[0].links.self.href", "https://api.example.org/v3/service_offerings/offering-guid"), | ||
MatchJSONPath("$.resources[0].links.service_plans.href", "https://api.example.org/v3/service_plans?service_offering_guids=offering-guid"), | ||
MatchJSONPath("$.resources[0].links.service_broker.href", "https://api.example.org/v3/service_brokers/broker-guid"), | ||
))) | ||
}) | ||
}) | ||
}) |
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,47 @@ | ||
package presenter | ||
|
||
import ( | ||
"net/url" | ||
|
||
"code.cloudfoundry.org/korifi/api/repositories" | ||
) | ||
|
||
const ( | ||
serviceOfferingsBase = "/v3/service_offerings" | ||
servicePlansBase = "/v3/service_plans" | ||
serviceBrokersBase = "/v3/service_brokers" | ||
) | ||
|
||
type ServiceOfferingLinks struct { | ||
Self Link `json:"self"` | ||
ServicePlans Link `json:"service_plans"` | ||
ServiceBroker Link `json:"service_broker"` | ||
} | ||
|
||
type ServiceOfferingResponse struct { | ||
repositories.ServiceOfferingResource | ||
Links ServiceOfferingLinks `json:"links"` | ||
} | ||
|
||
func ForServiceOffering(serviceOfferingResource repositories.ServiceOfferingResource, baseURL url.URL) ServiceOfferingResponse { | ||
return ServiceOfferingResponse{ | ||
ServiceOfferingResource: serviceOfferingResource, | ||
Links: ServiceOfferingLinks{ | ||
Self: Link{ | ||
HRef: buildURL(baseURL).appendPath(serviceOfferingsBase, serviceOfferingResource.GUID).build(), | ||
}, | ||
ServicePlans: Link{ | ||
HRef: buildURL(baseURL).appendPath(servicePlansBase).setQuery("service_offering_guids=" + serviceOfferingResource.GUID).build(), | ||
}, | ||
ServiceBroker: Link{ | ||
HRef: buildURL(baseURL).appendPath(serviceBrokersBase, serviceOfferingResource.Relationships.ServiceBroker.Data.GUID).build(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ForServiceOfferingList(serviceOfferingResourceList []repositories.ServiceOfferingResource, baseURL, requestURL url.URL) ListResponse[ServiceOfferingResponse] { | ||
return ForList(func(serviceOfferingResource repositories.ServiceOfferingResource, baseURL url.URL) ServiceOfferingResponse { | ||
return ForServiceOffering(serviceOfferingResource, baseURL) | ||
}, serviceOfferingResourceList, baseURL, requestURL) | ||
} |
Oops, something went wrong.