Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Get and GetCredentials for CFServiceInstances #3610

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions api/handlers/fake/cfservice_instance_repository.go

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

56 changes: 54 additions & 2 deletions api/handlers/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
)

const (
ServiceInstancesPath = "/v3/service_instances"
ServiceInstancePath = "/v3/service_instances/{guid}"
ServiceInstancesPath = "/v3/service_instances"
ServiceInstancePath = "/v3/service_instances/{guid}"
ServiceInstanceCredentialsPath = "/v3/service_instances/{guid}/credentials"
)

//counterfeiter:generate -o fake -fake-name CFServiceInstanceRepository . CFServiceInstanceRepository
Expand All @@ -32,6 +33,7 @@ type CFServiceInstanceRepository interface {
PatchServiceInstance(context.Context, authorization.Info, repositories.PatchServiceInstanceMessage) (repositories.ServiceInstanceRecord, error)
ListServiceInstances(context.Context, authorization.Info, repositories.ListServiceInstanceMessage) ([]repositories.ServiceInstanceRecord, error)
GetServiceInstance(context.Context, authorization.Info, string) (repositories.ServiceInstanceRecord, error)
GetServiceInstanceCredentials(context.Context, authorization.Info, string) (map[string]any, error)
DeleteServiceInstance(context.Context, authorization.Info, repositories.DeleteServiceInstanceMessage) (repositories.ServiceInstanceRecord, error)
}

Expand Down Expand Up @@ -62,6 +64,54 @@ func NewServiceInstance(
}
}

func (h *ServiceInstance) get(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-instance.get")

payload := new(payloads.ServiceInstanceGet)
err := h.requestValidator.DecodeAndValidateURLValues(r, payload)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Unable to decode request query parameters")
}

serviceInstanceGUID := routing.URLParam(r, "guid")

serviceInstance, err := h.serviceInstanceRepo.GetServiceInstance(r.Context(), authInfo, serviceInstanceGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get service instance", "GUID", serviceInstanceGUID)
}

includedResources, err := h.includeResolver.ResolveIncludes(r.Context(), authInfo, []repositories.ServiceInstanceRecord{serviceInstance}, payload.IncludeResourceRules)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to build included resources")
}

return routing.NewResponse(http.StatusOK).WithBody(presenter.ForServiceInstance(serviceInstance, h.serverURL, includedResources...)), nil
}

func (h *ServiceInstance) getCredentials(r *http.Request) (*routing.Response, error) {
danail-branekov marked this conversation as resolved.
Show resolved Hide resolved
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-instance.get-credentials")

serviceInstanceGUID := routing.URLParam(r, "guid")

serviceInstance, err := h.serviceInstanceRepo.GetServiceInstance(r.Context(), authInfo, serviceInstanceGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get service instance", "GUID", serviceInstanceGUID)
}

if serviceInstance.Type != korifiv1alpha1.UserProvidedType {
Copy link
Member

@danail-branekov danail-branekov Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to push this check to the repository (reasoning below). The repository looks up the service instance anyway, so it could return a not found error in case the service instance type is not upsi.

From API point of view, instance credentials are just another resource. We strive to keep api handlers simple and only take care to process the request parameters, delegate to a repository and present the result. Business logic on aspects of getting and processing resources should be "outsourced" to repositories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget about it, after a short discussion, having that decision in the handler is probably ok. Apologies for the noise

return nil, apierrors.NewNotFoundError(nil, repositories.ServiceInstanceResourceType)
}

credentials, err := h.serviceInstanceRepo.GetServiceInstanceCredentials(r.Context(), authInfo, serviceInstanceGUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to get service instance credentials")
}

return routing.NewResponse(http.StatusOK).WithBody(credentials), nil
}

//nolint:dupl
func (h *ServiceInstance) create(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
Expand Down Expand Up @@ -199,6 +249,8 @@ func (h *ServiceInstance) AuthenticatedRoutes() []routing.Route {
{Method: "POST", Pattern: ServiceInstancesPath, Handler: h.create},
{Method: "PATCH", Pattern: ServiceInstancePath, Handler: h.patch},
{Method: "GET", Pattern: ServiceInstancesPath, Handler: h.list},
{Method: "GET", Pattern: ServiceInstancePath, Handler: h.get},
{Method: "GET", Pattern: ServiceInstanceCredentialsPath, Handler: h.getCredentials},
{Method: "DELETE", Pattern: ServiceInstancePath, Handler: h.delete},
}
}
Loading
Loading