github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/service/purge_service_offering.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/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type PurgeServiceOffering struct {
    18  	ui          terminal.UI
    19  	serviceRepo api.ServiceRepository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&PurgeServiceOffering{})
    24  }
    25  
    26  func (cmd *PurgeServiceOffering) MetaData() commandregistry.CommandMetadata {
    27  	fs := make(map[string]flags.FlagSet)
    28  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    29  	fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Provider")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "purge-service-offering",
    33  		Description: T("Recursively remove a service and child objects from Cloud Foundry database without making requests to a service broker"),
    34  		Usage: []string{
    35  			T("CF_NAME purge-service-offering SERVICE [-p PROVIDER]"),
    36  			"\n\n",
    37  			scaryWarningMessage(),
    38  		},
    39  		Flags: fs,
    40  	}
    41  }
    42  
    43  func (cmd *PurgeServiceOffering) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    44  	if len(fc.Args()) != 1 {
    45  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("purge-service-offering"))
    46  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    47  	}
    48  
    49  	reqs := []requirements.Requirement{
    50  		requirementsFactory.NewLoginRequirement(),
    51  	}
    52  
    53  	if fc.IsSet("p") {
    54  		reqs = append(reqs, requirementsFactory.NewMaxAPIVersionRequirement("Option '-p'", cf.ServiceAuthTokenMaximumAPIVersion))
    55  	}
    56  
    57  	return reqs, nil
    58  }
    59  
    60  func (cmd *PurgeServiceOffering) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    61  	cmd.ui = deps.UI
    62  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    63  	return cmd
    64  }
    65  
    66  func scaryWarningMessage() string {
    67  	return T(`WARNING: This operation assumes that the service broker responsible for this service offering is no longer available, and all service instances have been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service will be removed from Cloud Foundry, including service instances and service bindings. No attempt will be made to contact the service broker; running this command without destroying the service broker will cause orphan service instances. After running this command you may want to run either delete-service-auth-token or delete-service-broker to complete the cleanup.`)
    68  }
    69  
    70  func (cmd *PurgeServiceOffering) Execute(c flags.FlagContext) error {
    71  	serviceName := c.Args()[0]
    72  
    73  	var offering models.ServiceOffering
    74  	if c.IsSet("p") {
    75  		var err error
    76  		offering, err = cmd.serviceRepo.FindServiceOfferingByLabelAndProvider(serviceName, c.String("p"))
    77  		if err != nil {
    78  			if _, ok := err.(*errors.ModelNotFoundError); ok {
    79  				cmd.ui.Warn(T("Service offering does not exist\nTIP: If you are trying to purge a v1 service offering, you must set the -p flag."))
    80  				return nil
    81  			}
    82  			return err
    83  		}
    84  	} else {
    85  		offerings, err := cmd.serviceRepo.FindServiceOfferingsByLabel(serviceName)
    86  		if err != nil {
    87  			if _, ok := err.(*errors.ModelNotFoundError); ok {
    88  				cmd.ui.Warn(T("Service offering does not exist\nTIP: If you are trying to purge a v1 service offering, you must set the -p flag."))
    89  				return nil
    90  			}
    91  			return err
    92  		}
    93  		offering = offerings[0]
    94  	}
    95  
    96  	confirmed := c.Bool("f")
    97  	if !confirmed {
    98  		cmd.ui.Warn(scaryWarningMessage())
    99  		confirmed = cmd.ui.Confirm(T("Really purge service offering {{.OfferingName}} from Cloud Foundry?",
   100  			map[string]interface{}{"OfferingName": serviceName},
   101  		))
   102  	}
   103  
   104  	if !confirmed {
   105  		return nil
   106  	}
   107  
   108  	cmd.ui.Say(T("Purging service {{.OfferingName}}...", map[string]interface{}{"OfferingName": serviceName}))
   109  
   110  	err := cmd.serviceRepo.PurgeServiceOffering(offering)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	cmd.ui.Ok()
   116  	return nil
   117  }