-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #3265 Co-authored-by: Danail Branekov <danailster@gmail.com>
- Loading branch information
1 parent
e9a2f23
commit 6c60159
Showing
31 changed files
with
1,394 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// nolint:dupl | ||
package handlers | ||
|
||
import ( | ||
|
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,61 @@ | ||
// nolint:dupl | ||
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 ( | ||
ServicePlansPath = "/v3/service_plans" | ||
) | ||
|
||
//counterfeiter:generate -o fake -fake-name CFServicePlanRepository . CFServicePlanRepository | ||
type CFServicePlanRepository interface { | ||
ListPlans(context.Context, authorization.Info) ([]repositories.ServicePlanResource, error) | ||
} | ||
|
||
type ServicePlan struct { | ||
serverURL url.URL | ||
servicePlanRepo CFServicePlanRepository | ||
} | ||
|
||
func NewServicePlan( | ||
serverURL url.URL, | ||
servicePlanRepo CFServicePlanRepository, | ||
) *ServicePlan { | ||
return &ServicePlan{ | ||
serverURL: serverURL, | ||
servicePlanRepo: servicePlanRepo, | ||
} | ||
} | ||
|
||
func (h *ServicePlan) list(r *http.Request) (*routing.Response, error) { | ||
authInfo, _ := authorization.InfoFromContext(r.Context()) | ||
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-plan.list") | ||
|
||
servicePlanList, err := h.servicePlanRepo.ListPlans(r.Context(), authInfo) | ||
if err != nil { | ||
return nil, apierrors.LogAndReturn(logger, err, "Failed to list service plans") | ||
} | ||
|
||
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForList(presenter.ForServicePlan, servicePlanList, h.serverURL, *r.URL)), nil | ||
} | ||
|
||
func (h *ServicePlan) UnauthenticatedRoutes() []routing.Route { | ||
return nil | ||
} | ||
|
||
func (h *ServicePlan) AuthenticatedRoutes() []routing.Route { | ||
return []routing.Route{ | ||
{Method: "GET", Pattern: ServicePlansPath, 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,79 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"errors" | ||
"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/tests/matchers" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ServicePlan", func() { | ||
var servicePlanRepo *fake.CFServicePlanRepository | ||
|
||
BeforeEach(func() { | ||
servicePlanRepo = new(fake.CFServicePlanRepository) | ||
|
||
apiHandler := NewServicePlan( | ||
*serverURL, | ||
servicePlanRepo, | ||
) | ||
routerBuilder.LoadRoutes(apiHandler) | ||
}) | ||
|
||
Describe("GET /v3/service_plans", func() { | ||
BeforeEach(func() { | ||
servicePlanRepo.ListPlansReturns([]repositories.ServicePlanResource{{ | ||
CFResource: model.CFResource{ | ||
GUID: "plan-guid", | ||
}, | ||
Relationships: repositories.ServicePlanRelationships{ | ||
ServiceOffering: model.ToOneRelationship{ | ||
Data: model.Relationship{ | ||
GUID: "service-offering-guid", | ||
}, | ||
}, | ||
}, | ||
}}, nil) | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/service_plans", nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
routerBuilder.Build().ServeHTTP(rr, req) | ||
}) | ||
|
||
It("lists the service plans", func() { | ||
Expect(servicePlanRepo.ListPlansCallCount()).To(Equal(1)) | ||
_, actualAuthInfo := servicePlanRepo.ListPlansArgsForCall(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_plans"), | ||
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"), | ||
))) | ||
}) | ||
|
||
When("listing the plans fails", func() { | ||
BeforeEach(func() { | ||
servicePlanRepo.ListPlansReturns(nil, errors.New("list-err")) | ||
}) | ||
|
||
It("returns an error", func() { | ||
expectUnknownError() | ||
}) | ||
}) | ||
}) | ||
}) |
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
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,37 @@ | ||
package presenter | ||
|
||
import ( | ||
"net/url" | ||
|
||
"code.cloudfoundry.org/korifi/api/repositories" | ||
) | ||
|
||
type ServicePlanLinks struct { | ||
Self Link `json:"self"` | ||
ServiceOffering Link `json:"service_offering"` | ||
} | ||
|
||
type ServicePlanResponse struct { | ||
repositories.ServicePlanResource | ||
Links ServicePlanLinks `json:"links"` | ||
} | ||
|
||
func ForServicePlan(servicePlanResource repositories.ServicePlanResource, baseURL url.URL) ServicePlanResponse { | ||
return ServicePlanResponse{ | ||
ServicePlanResource: servicePlanResource, | ||
Links: ServicePlanLinks{ | ||
Self: Link{ | ||
HRef: buildURL(baseURL).appendPath(servicePlansBase, servicePlanResource.GUID).build(), | ||
}, | ||
ServiceOffering: Link{ | ||
HRef: buildURL(baseURL).appendPath(serviceOfferingsBase, servicePlanResource.Relationships.ServiceOffering.Data.GUID).build(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ForServicePlanList(servicePlanResourceList []repositories.ServicePlanResource, baseURL, requestURL url.URL) ListResponse[ServicePlanResponse] { | ||
return ForList(func(servicePlanResource repositories.ServicePlanResource, baseURL url.URL) ServicePlanResponse { | ||
return ForServicePlan(servicePlanResource, baseURL) | ||
}, servicePlanResourceList, baseURL, requestURL) | ||
} |
Oops, something went wrong.