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 Service Credential Bindings of type Key #3662

Open
wants to merge 4 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
62 changes: 45 additions & 17 deletions api/handlers/service_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,68 @@ func (h *ServiceBinding) create(r *http.Request) (*routing.Response, error) {
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
}

app, err := h.appRepo.GetApp(r.Context(), authInfo, payload.Relationships.App.Data.GUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get "+repositories.AppResourceType)
}

serviceInstance, err := h.serviceInstanceRepo.GetServiceInstance(r.Context(), authInfo, payload.Relationships.ServiceInstance.Data.GUID)
if err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get "+repositories.ServiceInstanceResourceType)
}

if app.SpaceGUID != serviceInstance.SpaceGUID {
ctx := logr.NewContext(r.Context(), logger.WithValues("service-instance", serviceInstance.GUID))

if payload.Type == korifiv1alpha1.CFServiceBindingTypeApp {
var app repositories.AppRecord
if app, err = h.appRepo.GetApp(ctx, authInfo, payload.Relationships.App.Data.GUID); err != nil {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "failed to get "+repositories.AppResourceType)
}

if app.SpaceGUID != serviceInstance.SpaceGUID {
return nil, apierrors.LogAndReturn(
logger,
apierrors.NewUnprocessableEntityError(nil, "The service instance and the app are in different spaces"),
"App and ServiceInstance in different spaces", "App GUID", app.GUID,
"ServiceInstance GUID", serviceInstance.GUID,
)
}
}

if serviceInstance.Type == korifiv1alpha1.UserProvidedType {
return h.createUserProvided(ctx, &payload, serviceInstance)
}

return h.createManaged(ctx, &payload, serviceInstance)
}

func (h *ServiceBinding) createUserProvided(ctx context.Context, payload *payloads.ServiceBindingCreate, serviceInstance repositories.ServiceInstanceRecord) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(ctx)
logger := logr.FromContextOrDiscard(ctx).WithName("handlers.service-binding.create-user-provided")

if payload.Type == korifiv1alpha1.CFServiceBindingTypeKey {
return nil, apierrors.LogAndReturn(
logger,
apierrors.NewUnprocessableEntityError(nil, "The service instance and the app are in different spaces"),
"App and ServiceInstance in different spaces", "App GUID", app.GUID,
"ServiceInstance GUID", serviceInstance.GUID,
apierrors.NewUnprocessableEntityError(nil, "Service credential bindings of type 'key' are not supported for user-provided service instances."),
"",
)
}

ctx := logr.NewContext(r.Context(), logger.WithValues("app", app.GUID, "service-instance", serviceInstance.GUID))

serviceBinding, err := h.serviceBindingRepo.CreateServiceBinding(ctx, authInfo, payload.ToMessage(app.SpaceGUID))
serviceBinding, err := h.serviceBindingRepo.CreateServiceBinding(ctx, authInfo, payload.ToMessage(serviceInstance.SpaceGUID))
if err != nil {
return nil, apierrors.LogAndReturn(logr.FromContextOrDiscard(ctx), err, "failed to create ServiceBinding")
}

if serviceInstance.Type == korifiv1alpha1.ManagedType {
return routing.NewResponse(http.StatusAccepted).
WithHeader("Location", presenter.JobURLForRedirects(serviceBinding.GUID, presenter.ManagedServiceBindingCreateOperation, h.serverURL)), nil
}

return routing.NewResponse(http.StatusCreated).WithBody(presenter.ForServiceBinding(serviceBinding, h.serverURL)), nil
}

func (h *ServiceBinding) createManaged(ctx context.Context, payload *payloads.ServiceBindingCreate, serviceInstance repositories.ServiceInstanceRecord) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(ctx)
logger := logr.FromContextOrDiscard(ctx).WithName("handlers.service-binding.create-managed")

serviceBinding, err := h.serviceBindingRepo.CreateServiceBinding(ctx, authInfo, payload.ToMessage(serviceInstance.SpaceGUID))
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to create ServiceBinding")
}
return routing.NewResponse(http.StatusAccepted).
WithHeader("Location", presenter.JobURLForRedirects(serviceBinding.GUID, presenter.ManagedServiceBindingCreateOperation, h.serverURL)), nil
}

func (h *ServiceBinding) delete(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.service-binding.delete")
Expand Down
Loading