Skip to content

Commit

Permalink
Implement GET /v3/service_plans/:guid/visibility
Browse files Browse the repository at this point in the history
fixes #3274

Co-authored-by: Georgi Sabev <[email protected]>
  • Loading branch information
danail-branekov and georgethebeatle committed Jul 31, 2024
1 parent e27b5c9 commit 2a79058
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 229 deletions.
83 changes: 83 additions & 0 deletions api/handlers/fake/cfservice_plan_repository.go

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

22 changes: 20 additions & 2 deletions api/handlers/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
)

const (
ServicePlansPath = "/v3/service_plans"
ServicePlansPath = "/v3/service_plans"
ServicePlanVisivilityPath = "/v3/service_plans/{guid}/visibility"
)

//counterfeiter:generate -o fake -fake-name CFServicePlanRepository . CFServicePlanRepository
type CFServicePlanRepository interface {
ListPlans(context.Context, authorization.Info, repositories.ListServicePlanMessage) ([]repositories.ServicePlanRecord, error)
GetPlanVisibility(context.Context, authorization.Info, string) (repositories.ServicePlanVisibilityRecord, error)
}

type ServicePlan struct {
Expand Down Expand Up @@ -53,18 +55,34 @@ func (h *ServicePlan) list(r *http.Request) (*routing.Response, error) {

servicePlanList, err := h.servicePlanRepo.ListPlans(r.Context(), authInfo, payload.ToMessage())
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Failed to list service plans")
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) getPlanVisibility(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-plan.get-visibility")

planGUID := routing.URLParam(r, "guid")
logger = logger.WithValues("guid", planGUID)

visibility, err := h.servicePlanRepo.GetPlanVisibility(r.Context(), authInfo, planGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to get plan visibility")
}

return routing.NewResponse(http.StatusOK).WithBody(presenter.ForServicePlanVisibility(visibility, h.serverURL)), 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},
{Method: "GET", Pattern: ServicePlanVisivilityPath, Handler: h.getPlanVisibility},
}
}
40 changes: 40 additions & 0 deletions api/handlers/service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.cloudfoundry.org/korifi/model"
. "code.cloudfoundry.org/korifi/tests/matchers"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -106,4 +107,43 @@ var _ = Describe("ServicePlan", func() {
})
})
})

Describe("GET /v3/service_plans/{guid}/visibility", func() {
BeforeEach(func() {
servicePlanRepo.GetPlanVisibilityReturns(repositories.ServicePlanVisibilityRecord{
Type: korifiv1alpha1.AdminServicePlanVisibilityType,
}, nil)
})

JustBeforeEach(func() {
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/service_plans/my-service-plan/visibility", nil)
Expect(err).NotTo(HaveOccurred())

routerBuilder.Build().ServeHTTP(rr, req)
})

It("returns the plan visibility", func() {
Expect(servicePlanRepo.GetPlanVisibilityCallCount()).To(Equal(1))
_, actualAuthInfo, actualPlanID := servicePlanRepo.GetPlanVisibilityArgsForCall(0)
Expect(actualPlanID).To(Equal("my-service-plan"))
Expect(actualAuthInfo).To(Equal(authInfo))

Expect(rr).To(HaveHTTPStatus(http.StatusOK))
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json"))

Expect(rr).To(HaveHTTPBody(SatisfyAll(
MatchJSONPath("$.type", korifiv1alpha1.AdminServicePlanVisibilityType),
)))
})

When("getting the visibility fails", func() {
BeforeEach(func() {
servicePlanRepo.GetPlanVisibilityReturns(repositories.ServicePlanVisibilityRecord{}, errors.New("visibility-err"))
})

It("returns an error", func() {
expectUnknownError()
})
})
})
})
2 changes: 1 addition & 1 deletion api/payloads/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (l *ServicePlanList) ToMessage() repositories.ListServicePlanMessage {
}

func (l *ServicePlanList) SupportedKeys() []string {
return []string{"service_offering_guids", "page", "per_page"}
return []string{"service_offering_guids", "page", "per_page", "include"}
}

func (l *ServicePlanList) IgnoredKeys() []*regexp.Regexp {
Expand Down
9 changes: 9 additions & 0 deletions api/presenter/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"

"code.cloudfoundry.org/korifi/api/repositories"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
)

type ServicePlanLinks struct {
Expand All @@ -29,3 +30,11 @@ func ForServicePlan(servicePlan repositories.ServicePlanRecord, baseURL url.URL)
},
}
}

type ServicePlanVisibilityResponse korifiv1alpha1.ServicePlanVisibility

func ForServicePlanVisibility(visibility repositories.ServicePlanVisibilityRecord, _ url.URL) ServicePlanVisibilityResponse {
return ServicePlanVisibilityResponse{
Type: visibility.Type,
}
}
Loading

0 comments on commit 2a79058

Please sign in to comment.