Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danail-branekov committed Aug 9, 2024
1 parent 0d33f43 commit a5e6c78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
14 changes: 10 additions & 4 deletions api/handlers/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"net/http"
"net/url"
"slices"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
Expand Down Expand Up @@ -66,12 +67,17 @@ func (h *ServicePlan) list(r *http.Request) (*routing.Response, error) {
return nil, apierrors.LogAndReturn(logger, err, "failed to list service plans")
}

includedOfferings, err := h.getIncludedServiceOfferings(r.Context(), authInfo, servicePlanList, h.serverURL)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to get included service offerings")
includedResources := []model.IncludedResource{}

if slices.Contains(payload.IncludeResources, "service_offering") {
includedOfferings, err := h.getIncludedServiceOfferings(r.Context(), authInfo, servicePlanList, h.serverURL)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to get included service offerings")
}
includedResources = append(includedResources, includedOfferings...)
}

return routing.NewResponse(http.StatusOK).WithBody(presenter.ForList(presenter.ForServicePlan, servicePlanList, h.serverURL, *r.URL, includedOfferings...)), nil
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForList(presenter.ForServicePlan, servicePlanList, h.serverURL, *r.URL, includedResources...)), nil
}

func (h *ServicePlan) getIncludedServiceOfferings(
Expand Down
19 changes: 10 additions & 9 deletions api/handlers/service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ var _ = Describe("ServicePlan", func() {
},
ServiceOfferingGUID: "service-offering-guid",
}}, nil)

serviceOfferingRepo.ListOfferingsReturns([]repositories.ServiceOfferingRecord{{
ServiceOffering: services.ServiceOffering{
Name: "service-offering-name",
},
CFResource: model.CFResource{
GUID: "service-offering-guid",
},
}}, nil)
})

JustBeforeEach(func() {
Expand All @@ -69,6 +78,7 @@ var _ = Describe("ServicePlan", func() {
MatchJSONPath("$.resources[0].guid", "plan-guid"),
MatchJSONPath("$.resources[0].links.self.href", "https://api.example.org/v3/service_plans/plan-guid"),
MatchJSONPath("$.resources[0].links.service_offering.href", "https://api.example.org/v3/service_offerings/service-offering-guid"),
Not(ContainSubstring("included")),
)))
})

Expand All @@ -88,15 +98,6 @@ var _ = Describe("ServicePlan", func() {

Describe("include service_offering", func() {
BeforeEach(func() {
serviceOfferingRepo.ListOfferingsReturns([]repositories.ServiceOfferingRecord{{
ServiceOffering: services.ServiceOffering{
Name: "service-offering-name",
},
CFResource: model.CFResource{
GUID: "service-offering-guid",
},
}}, nil)

requestValidator.DecodeAndValidateURLValuesStub = decodeAndValidateURLValuesStub(&payloads.ServicePlanList{
IncludeResources: []string{"service_offering"},
})
Expand Down

0 comments on commit a5e6c78

Please sign in to comment.