Skip to content

Commit

Permalink
Merge pull request #440 from xingzhou/service_key_cascade
Browse files Browse the repository at this point in the history
implement the story of delete service instance that has keys
  • Loading branch information
jberkhahn committed Jun 5, 2015
2 parents 1ab7cb9 + 8878f20 commit 83b11c0
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cf/api/resources/service_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ServiceInstanceEntity struct {
Name string
DashboardUrl string `json:"dashboard_url"`
ServiceBindings []ServiceBindingResource `json:"service_bindings"`
ServiceKeys []ServiceKeyResource `json:"service_keys"`
ServicePlan ServicePlanResource `json:"service_plan"`
LastOperation LastOperation `json:"last_operation"`
}
Expand All @@ -47,5 +48,10 @@ func (resource ServiceInstanceResource) ToModel() (instance models.ServiceInstan
for _, bindingResource := range resource.Entity.ServiceBindings {
instance.ServiceBindings = append(instance.ServiceBindings, bindingResource.ToFields())
}

instance.ServiceKeys = []models.ServiceKeyFields{}
for _, keyResource := range resource.Entity.ServiceKeys {
instance.ServiceKeys = append(instance.ServiceKeys, keyResource.ToFields())
}
return
}
4 changes: 2 additions & 2 deletions cf/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func (repo CloudControllerServiceRepository) RenameService(instance models.Servi
}

func (repo CloudControllerServiceRepository) DeleteService(instance models.ServiceInstance) (apiErr error) {
if len(instance.ServiceBindings) > 0 {
return errors.New("Cannot delete service instance, apps are still bound to it")
if len(instance.ServiceBindings) > 0 || len(instance.ServiceKeys) > 0 {
return errors.NewServiceAssociationError()
}
path := fmt.Sprintf("/v2/service_instances/%s?%s", instance.Guid, "accepts_incomplete=true")
return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
Expand Down
28 changes: 26 additions & 2 deletions cf/api/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ var _ = Describe("Services Repo", func() {
})

Describe("DeleteService", func() {
It("it deletes the service when no apps are bound", func() {
It("deletes the service when no apps and keys are bound", func() {
setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{
Method: "DELETE",
Path: "/v2/service_instances/my-service-instance-guid?accepts_incomplete=true&async=true",
Expand Down Expand Up @@ -467,7 +467,31 @@ var _ = Describe("Services Repo", func() {
}

err := repo.DeleteService(serviceInstance)
Expect(err.Error()).To(Equal("Cannot delete service instance, apps are still bound to it"))
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&errors.ServiceAssociationError{}))
})

It("doesn't delete the service when keys are bound", func() {
setupTestServer()

serviceInstance := models.ServiceInstance{}
serviceInstance.Guid = "my-service-instance-guid"
serviceInstance.ServiceKeys = []models.ServiceKeyFields{
{
Name: "fake-service-key-1",
Url: "/v2/service_keys/service-key-1-guid",
Guid: "service-key-1-guid",
},
{
Name: "fake-service-key-2",
Url: "/v2/service_keys/service-key-2-guid",
Guid: "service-key-2-guid",
},
}

err := repo.DeleteService(serviceInstance)
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&errors.ServiceAssociationError{}))
})
})

Expand Down
16 changes: 16 additions & 0 deletions cf/errors/service_association_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package errors

import (
. "github.com/cloudfoundry/cli/cf/i18n"
)

type ServiceAssociationError struct {
}

func NewServiceAssociationError() error {
return &ServiceAssociationError{}
}

func (err *ServiceAssociationError) Error() string {
return T("Cannot delete service instance, service keys and bindings must first be deleted")
}
5 changes: 5 additions & 0 deletions cf/i18n/resources/de_DE.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/en_US.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/es_ES.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instancias",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/fr_FR.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Impossible de supprimer instance de service , clés de service et fixations doivent d'abord être supprimées",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/it_IT.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/ja_JA.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/pt_BR.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instâncias",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/zh_Hans.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} 乘以 {{.InstanceCount}}实例数",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "无法删除服务实例,需要先删除服务密钥和绑定",
"modified": false
}
]
5 changes: 5 additions & 0 deletions cf/i18n/resources/zh_Hant.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -5303,5 +5303,10 @@
"id": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"translation": "{{.Usage}} {{.FormattedMemory}} x {{.InstanceCount}} instances",
"modified": false
},
{
"id": "Cannot delete service instance, service keys and bindings must first be deleted",
"translation": "Cannot delete service instance, service keys and bindings must first be deleted",
"modified": false
}
]
1 change: 1 addition & 0 deletions cf/models/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ServiceInstanceFields struct {
type ServiceInstance struct {
ServiceInstanceFields
ServiceBindings []ServiceBindingFields
ServiceKeys []ServiceKeyFields
ServicePlan ServicePlanFields
ServiceOffering ServiceOfferingFields
}
Expand Down

0 comments on commit 83b11c0

Please sign in to comment.