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