Skip to content

Commit

Permalink
Merge pull request #3610 from klapkov/get_service_is_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
danail-branekov authored Jan 6, 2025
2 parents cf08b4a + cd1c0be commit 9a45998
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 40 deletions.
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) {
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 {
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

0 comments on commit 9a45998

Please sign in to comment.