github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/purge_service_instance.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type PurgeServiceInstance struct {
    17  	ui          terminal.UI
    18  	serviceRepo api.ServiceRepository
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&PurgeServiceInstance{})
    23  }
    24  
    25  func (cmd *PurgeServiceInstance) MetaData() commandregistry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    28  
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "purge-service-instance",
    31  		Description: T("Recursively remove a service instance and child objects from Cloud Foundry database without making requests to a service broker"),
    32  		Usage: []string{
    33  			T("CF_NAME purge-service-instance SERVICE_INSTANCE"),
    34  			"\n\n",
    35  			cmd.scaryWarningMessage(),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *PurgeServiceInstance) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("purge-service-instance"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  		requirementsFactory.NewMinAPIVersionRequirement("purge-service-instance", cf.RoutePathMinimumAPIVersion),
    50  	}
    51  
    52  	return reqs, nil
    53  }
    54  
    55  func (cmd *PurgeServiceInstance) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    56  	cmd.ui = deps.UI
    57  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *PurgeServiceInstance) scaryWarningMessage() string {
    62  	return T(`WARNING: This operation assumes that the service broker responsible for this service instance is no longer available or is not responding with a 200 or 410, and the service instance has been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service instance will be removed from Cloud Foundry, including service bindings and service keys.`)
    63  }
    64  
    65  func (cmd *PurgeServiceInstance) Execute(c flags.FlagContext) error {
    66  	instanceName := c.Args()[0]
    67  
    68  	instance, err := cmd.serviceRepo.FindInstanceByName(instanceName)
    69  	if err != nil {
    70  		if _, ok := err.(*errors.ModelNotFoundError); ok {
    71  			cmd.ui.Warn(T("Service instance {{.InstanceName}} not found", map[string]interface{}{"InstanceName": instanceName}))
    72  			return nil
    73  		}
    74  
    75  		return err
    76  	}
    77  
    78  	force := c.Bool("f")
    79  	if !force {
    80  		cmd.ui.Warn(cmd.scaryWarningMessage())
    81  		confirmed := cmd.ui.Confirm(T("Really purge service instance {{.InstanceName}} from Cloud Foundry?",
    82  			map[string]interface{}{"InstanceName": instanceName},
    83  		))
    84  
    85  		if !confirmed {
    86  			return nil
    87  		}
    88  	}
    89  
    90  	cmd.ui.Say(T("Purging service {{.InstanceName}}...", map[string]interface{}{"InstanceName": instanceName}))
    91  	err = cmd.serviceRepo.PurgeServiceInstance(instance)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	cmd.ui.Ok()
    97  	return nil
    98  }