Skip to content

Commit

Permalink
feat(delete) delete broken destinations
Browse files Browse the repository at this point in the history
- Command `html5-delete -d` will not fail,
   if service instance with provided `app-host-id`
  does not exist, but delete destination and all
  other destinations having same value of
  `sap.cloud.service` property
- Update CHANGELOG.md
- Bump version to 1.4.3
- Close #32
  • Loading branch information
micellius committed Jun 3, 2020
1 parent fbef14e commit 84f37a5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.4.3] - 2020-06-03
### Changed
- Command `html5-delete -d` will not fail, if service instance with provided `app-host-id`
does not exist, but delete destination and all other destinations having same value of
`sap.cloud.service` property

## [1.4.2] - 2020-05-19
### Added
Expand Down
106 changes: 75 additions & 31 deletions commands/html5_delete_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *DeleteCommand) DeleteServiceInstancesContent(appHostGUIDs []string, app
// DeleteServiceInstances delete service instances by app-host-ids,
// including all dependent service keys
func (c *DeleteCommand) DeleteServiceInstances(appHostGUIDs []string, appHostNames []string, deleteDestinations bool) ExecutionStatus {
log.Tracef("Deleting service instances by app-host-ids: %v\n", appHostGUIDs)
log.Tracef("Deleting service instances by IDs: %v\n", appHostGUIDs)
var err error

// Get context
Expand Down Expand Up @@ -176,40 +176,18 @@ func (c *DeleteCommand) DeleteServiceInstances(appHostGUIDs []string, appHostNam
}
}

ui.Say("Deleting service instances with app-host-id %s in org %s / space %s as %s...",
msg := ""
if deleteDestinations {
msg = "and associated destinations "
}

ui.Say("Deleting service instances with IDs %s %sin org %s / space %s as %s...",
terminal.EntityNameColor(strings.Join(appHostGUIDs, ", ")),
msg,
terminal.EntityNameColor(context.Org),
terminal.EntityNameColor(context.Space),
terminal.EntityNameColor(context.Username))

// Delete service instances
for _, appHostGUID := range appHostGUIDs {
log.Tracef("Getting list of service keys for app-host-id %s\n", appHostGUID)
// Get service keys
serviceKeys, err := clients.GetServiceKeys(c.CliConnection, appHostGUID)
if err != nil {
ui.Failed("Could not get list of service keys for app-host-id %s: %+v\n", appHostGUID, err)
return Failure
}
// Delete dependent service keys
for _, serviceKey := range serviceKeys {
log.Tracef("Deleting service key %s (%s)\n", serviceKey.GUID, serviceKey.Name)
err = clients.DeleteServiceKey(c.CliConnection, serviceKey.GUID, maxRetryCount)
if err != nil {
ui.Failed("Could not delete service key %s: %+v\n", serviceKey.GUID, err)
return Failure
}
}
log.Tracef("Deleting service instance %s\n", appHostGUID)
// Delete service instance
err = clients.DeleteServiceInstance(c.CliConnection, appHostGUID, maxRetryCount)
if err != nil {
ui.Failed("Could not delete service instance %s: %+v\n", appHostGUID, err)
return Failure
}
log.Tracef("Service instance %s successfully deleted\n", appHostGUID)
}

// Delete destinatons if needed
if deleteDestinations {
// Create destination context
Expand All @@ -226,8 +204,13 @@ func (c *DeleteCommand) DeleteServiceInstances(appHostGUIDs []string, appHostNam
destinationContext.DestinationServiceInstanceKeyToken)

// Find relevant destinations and delete them
sapCloudServices := make([]string, 0)
for _, destination := range destinations {
if val, ok := destination.Properties["html5-apps-repo.app_host_id"]; ok {
val, ok := destination.Properties["html5-apps-repo.app_host_id"]
if !ok {
val, ok = destination.Properties["app_host_id"]
}
if ok {
val = strings.Trim(val, " ")
for _, appHostGUID := range appHostGUIDs {
if val == appHostGUID {
Expand All @@ -240,6 +223,33 @@ func (c *DeleteCommand) DeleteServiceInstances(appHostGUIDs []string, appHostNam
ui.Failed("Could not delete destination '%s': %+v\n", destination.Name, err)
return Failure
}
if sapCloudService, ok := destination.Properties["sap.cloud.service"]; ok {
log.Tracef("Adding sap.cloud.service '%s' to the deletion list\n", sapCloudService)
sapCloudServices = append(sapCloudServices, sapCloudService)
}
}
}
}
}

// Find destinations with same sap.cloud.service value as deleted destinations and delete them
if len(sapCloudServices) > 0 {
for _, destination := range destinations {
if val, ok := destination.Properties["sap.cloud.service"]; ok {
for _, sapCloudService := range sapCloudServices {
if val == sapCloudService {
log.Tracef("Deleting destination '%s' with sap.cloud.service '%s'\n", destination.Name, val)
err = clients.DeleteSubaccountDestination(
*destinationContext.DestinationServiceInstanceKey.Credentials.URI,
destinationContext.DestinationServiceInstanceKeyToken,
destination.Name)
if err != nil {
ui.Failed("Could not delete destination '%s' with sap.cloud.service '%s': %+v\n",
destination.Name, val, err)
return Failure
}
break
}
}
}
}
Expand All @@ -253,6 +263,40 @@ func (c *DeleteCommand) DeleteServiceInstances(appHostGUIDs []string, appHostNam
}
}

// Delete service instances
for _, appHostGUID := range appHostGUIDs {
log.Tracef("Getting list of service keys for app-host-id %s\n", appHostGUID)
// Get service keys
serviceKeys, err := clients.GetServiceKeys(c.CliConnection, appHostGUID)
if err != nil {
ui.Failed("Could not get list of service keys for app-host-id %s: %+v\n", appHostGUID, err)
return Failure
}
// Delete dependent service keys
for _, serviceKey := range serviceKeys {
log.Tracef("Deleting service key %s (%s)\n", serviceKey.GUID, serviceKey.Name)
err = clients.DeleteServiceKey(c.CliConnection, serviceKey.GUID, maxRetryCount)
if err != nil {
ui.Failed("Could not delete service key %s: %+v\n", serviceKey.GUID, err)
return Failure
}
}
log.Tracef("Deleting service instance %s\n", appHostGUID)
// Delete service instance
err = clients.DeleteServiceInstance(c.CliConnection, appHostGUID, maxRetryCount)
if err != nil {
if deleteDestinations {
log.Tracef("Service instance %s was not deleted (probably not found)\n", appHostGUID)
ui.Warn("[WARNING] Service instance with ID = %s was not deleted (probably not found)\n", appHostGUID)
} else {
ui.Failed("Could not delete service instance %s: %+v\n", appHostGUID, err)
return Failure
}
} else {

}
}

ui.Ok()
ui.Say("")

Expand Down
2 changes: 1 addition & 1 deletion html5_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// Version is the version of the CLI plugin.
var Version = "1.4.2"
var Version = "1.4.3"

// HTML5Plugin represents a cf CLI plugin for working with HTML5 Application Repository service
type HTML5Plugin struct{}
Expand Down

0 comments on commit 84f37a5

Please sign in to comment.