Skip to content

Commit

Permalink
Add ToMessage to payload and return Service Instance Record on Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
klapkov committed Nov 6, 2024
1 parent 273919d commit bef485a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
33 changes: 19 additions & 14 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.

12 changes: 2 additions & 10 deletions api/handlers/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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)
DeleteServiceInstance(context.Context, authorization.Info, repositories.DeleteServiceInstanceMessage) error
DeleteServiceInstance(context.Context, authorization.Info, repositories.DeleteServiceInstanceMessage) (repositories.ServiceInstanceRecord, error)
}

type ServiceInstance struct {
Expand Down Expand Up @@ -178,15 +178,7 @@ func (h *ServiceInstance) delete(r *http.Request) (*routing.Response, error) {
return nil, apierrors.LogAndReturn(logger, err, "Unable to decode request query parameters")
}

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")
}

err = h.serviceInstanceRepo.DeleteServiceInstance(r.Context(), authInfo, repositories.DeleteServiceInstanceMessage{
GUID: serviceInstanceGUID,
Purge: payload.Purge,
})
serviceInstance, err := h.serviceInstanceRepo.DeleteServiceInstance(r.Context(), authInfo, payload.ToMessage(serviceInstanceGUID))
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "error when deleting service instance", "guid", serviceInstanceGUID)
}
Expand Down
2 changes: 1 addition & 1 deletion api/handlers/service_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ var _ = Describe("ServiceInstance", func() {

When("deleting the service instance fails", func() {
BeforeEach(func() {
serviceInstanceRepo.DeleteServiceInstanceReturns(errors.New("boom"))
serviceInstanceRepo.DeleteServiceInstanceReturns(repositories.ServiceInstanceRecord{}, errors.New("boom"))
})

It("returns 500 Internal Server Error", func() {
Expand Down
7 changes: 7 additions & 0 deletions api/payloads/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ type ServiceInstanceDelete struct {
Purge bool `json:"purge"`
}

func (d *ServiceInstanceDelete) ToMessage(guid string) repositories.DeleteServiceInstanceMessage {
return repositories.DeleteServiceInstanceMessage{
GUID: guid,
Purge: d.Purge,
}
}

func (d *ServiceInstanceDelete) SupportedKeys() []string {
return []string{
"purge",
Expand Down
14 changes: 7 additions & 7 deletions api/repositories/service_instance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,15 @@ func (r *ServiceInstanceRepo) GetServiceInstance(ctx context.Context, authInfo a
return cfServiceInstanceToRecord(*serviceInstance), nil
}

func (r *ServiceInstanceRepo) DeleteServiceInstance(ctx context.Context, authInfo authorization.Info, message DeleteServiceInstanceMessage) error {
func (r *ServiceInstanceRepo) DeleteServiceInstance(ctx context.Context, authInfo authorization.Info, message DeleteServiceInstanceMessage) (ServiceInstanceRecord, error) {
userClient, err := r.userClientFactory.BuildClient(authInfo)
if err != nil {
return fmt.Errorf("failed to build user client: %w", err)
return ServiceInstanceRecord{}, fmt.Errorf("failed to build user client: %w", err)
}

namespace, err := r.namespaceRetriever.NamespaceFor(ctx, message.GUID, ServiceInstanceResourceType)
if err != nil {
return fmt.Errorf("failed to get namespace for service instance: %w", err)
return ServiceInstanceRecord{}, fmt.Errorf("failed to get namespace for service instance: %w", err)
}

serviceInstance := &korifiv1alpha1.CFServiceInstance{
Expand All @@ -475,23 +475,23 @@ func (r *ServiceInstanceRepo) DeleteServiceInstance(ctx context.Context, authInf
}

if err := userClient.Get(ctx, client.ObjectKeyFromObject(serviceInstance), serviceInstance); err != nil {
return fmt.Errorf("failed to get service instance: %w", apierrors.FromK8sError(err, ServiceInstanceResourceType))
return ServiceInstanceRecord{}, fmt.Errorf("failed to get service instance: %w", apierrors.FromK8sError(err, ServiceInstanceResourceType))
}

if message.Purge {
err := k8s.PatchResource(ctx, userClient, serviceInstance, func() {
controllerutil.RemoveFinalizer(serviceInstance, korifiv1alpha1.CFManagedServiceInstanceFinalizerName)
})
if err != nil {
return fmt.Errorf("failed to remove finalizer for service instance: %s, %w", message.GUID, apierrors.FromK8sError(err, ServiceInstanceResourceType))
return ServiceInstanceRecord{}, fmt.Errorf("failed to remove finalizer for service instance: %s, %w", message.GUID, apierrors.FromK8sError(err, ServiceInstanceResourceType))
}
}

if err := userClient.Delete(ctx, serviceInstance); err != nil {
return fmt.Errorf("failed to delete service instance: %w", apierrors.FromK8sError(err, ServiceInstanceResourceType))
return ServiceInstanceRecord{}, fmt.Errorf("failed to delete service instance: %w", apierrors.FromK8sError(err, ServiceInstanceResourceType))
}

return nil
return cfServiceInstanceToRecord(*serviceInstance), nil
}

func (r ServiceInstanceRecord) GetResourceType() string {
Expand Down
4 changes: 2 additions & 2 deletions api/repositories/service_instance_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
})

JustBeforeEach(func() {
deleteErr = serviceInstanceRepo.DeleteServiceInstance(ctx, authInfo, deleteMessage)
_, deleteErr = serviceInstanceRepo.DeleteServiceInstance(ctx, authInfo, deleteMessage)
})

When("the user has permissions to delete service instances", func() {
Expand Down Expand Up @@ -1046,7 +1046,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
})

JustBeforeEach(func() {
deleteErr = serviceInstanceRepo.DeleteServiceInstance(ctx, authInfo, deleteMessage)
_, deleteErr = serviceInstanceRepo.DeleteServiceInstance(ctx, authInfo, deleteMessage)
})

It("purges the service instance", func() {
Expand Down

0 comments on commit bef485a

Please sign in to comment.