|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/cloudfoundry/cloud-service-broker/dbservice" |
| 10 | + "github.com/cloudfoundry/cloud-service-broker/internal/storage" |
| 11 | + "github.com/cloudfoundry/cloud-service-broker/utils" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + purgeCmd := &cobra.Command{ |
| 16 | + Use: "purge-binding", |
| 17 | + Short: "purge a service binding from the database", |
| 18 | + Long: `Lets you remove a service binding (or service key) from the Cloud Service Broker database. |
| 19 | +
|
| 20 | +It does not actually delete the service binding, it just removes all references from the database. |
| 21 | +This can be used to remove references to a service binding that has been manually removed, |
| 22 | +or to clean up a service binding that fails to delete. |
| 23 | +
|
| 24 | +If using Cloud Foundry, identify the GUID of the service instance: |
| 25 | +
|
| 26 | + cf service <name> --guid # Prints the service instance guid |
| 27 | +
|
| 28 | +Then identify the GUID of the service binding, or service key that you want to remove. |
| 29 | +You can see the service keys and bindings for a service instance by running: |
| 30 | +
|
| 31 | + cf curl /v3/service_credential_bindings?service_instance_guids=<service-instance-guid> |
| 32 | +
|
| 33 | +Remove the binding from Cloud Service broker: |
| 34 | +
|
| 35 | + cloud-service-broker purge <service-instance-guid> <service-binding-guid> |
| 36 | +
|
| 37 | +Then you can delete the binding from Cloud Foundry. Cloud Service Broker will confirm |
| 38 | +to Cloud Foundry that the service binding or key no longer exists, and it will be removed |
| 39 | +from the Cloud Foundry database |
| 40 | +
|
| 41 | + cf unbind-service <app-name> <service-instance-name> |
| 42 | +
|
| 43 | +Or |
| 44 | +
|
| 45 | + cf delete-service-key <service-instance-name> <service-key-name> |
| 46 | +`, |
| 47 | + Run: func(cmd *cobra.Command, args []string) { |
| 48 | + switch len(args) { |
| 49 | + case 0: |
| 50 | + log.Fatal("missing service instance GUID and service binding GUID") |
| 51 | + case 1: |
| 52 | + log.Fatal("missing service binding GUID") |
| 53 | + case 2: |
| 54 | + purgeServiceBinding(args[0], args[1]) |
| 55 | + default: |
| 56 | + log.Fatal("too many arguments") |
| 57 | + } |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + rootCmd.AddCommand(purgeCmd) |
| 62 | +} |
| 63 | + |
| 64 | +func purgeServiceBinding(serviceInstanceGUID, serviceBindingGUID string) { |
| 65 | + logger := utils.NewLogger("purge-service-binding") |
| 66 | + db := dbservice.New(logger) |
| 67 | + encryptor := setupDBEncryption(db, logger) |
| 68 | + store := storage.New(db, encryptor) |
| 69 | + |
| 70 | + bindings, err := store.GetServiceBindingIDsForServiceInstance(serviceInstanceGUID) |
| 71 | + if err != nil { |
| 72 | + log.Fatalf("error listing bindings: %s", err) |
| 73 | + } |
| 74 | + for _, bindingGUID := range bindings { |
| 75 | + if bindingGUID == serviceBindingGUID { |
| 76 | + if err := deleteServiceBindingFromStore(store, serviceInstanceGUID, serviceBindingGUID); err != nil { |
| 77 | + log.Fatalf("error deleting binding %q for service instance %q: %s", serviceBindingGUID, serviceInstanceGUID, err) |
| 78 | + } |
| 79 | + log.Printf("deleted binding %q for service instance %q from the Cloud Service Broker database", serviceBindingGUID, serviceInstanceGUID) |
| 80 | + return |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + log.Fatalf("could not find service binding %q for service instance %q", serviceBindingGUID, serviceInstanceGUID) |
| 85 | +} |
| 86 | + |
| 87 | +func deleteServiceBindingFromStore(store *storage.Storage, serviceInstanceGUID, serviceBindingGUID string) error { |
| 88 | + if err := store.DeleteServiceBindingCredentials(serviceBindingGUID, serviceInstanceGUID); err != nil { |
| 89 | + return fmt.Errorf("error deleting binding credentials for %q: %w", serviceBindingGUID, err) |
| 90 | + } |
| 91 | + if err := store.DeleteBindRequestDetails(serviceBindingGUID, serviceInstanceGUID); err != nil { |
| 92 | + return fmt.Errorf("error deleting binding request details for %q: %w", serviceBindingGUID, err) |
| 93 | + } |
| 94 | + if err := store.DeleteTerraformDeployment(fmt.Sprintf("tf:%s:%s", serviceInstanceGUID, serviceBindingGUID)); err != nil { |
| 95 | + return fmt.Errorf("error deleting binding terraform deployment for %q: %s", serviceBindingGUID, err) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
0 commit comments