github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/purge_service_offering_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/translatableerror"
    10  	"code.cloudfoundry.org/cli/command/v6/shared"
    11  )
    12  
    13  //go:generate counterfeiter . PurgeServiceOfferingActor
    14  
    15  type PurgeServiceOfferingActor interface {
    16  	PurgeServiceOffering(service v2action.Service) (v2action.Warnings, error)
    17  	GetServiceByNameAndBrokerName(serviceName, brokerName string) (v2action.Service, v2action.Warnings, error)
    18  }
    19  
    20  type PurgeServiceOfferingCommand struct {
    21  	RequiredArgs    flag.Service `positional-args:"yes"`
    22  	ServiceBroker   string       `short:"b" description:"Purge a service from a particular service broker. Required when service name is ambiguous"`
    23  	Force           bool         `short:"f" description:"Force deletion without confirmation"`
    24  	Provider        string       `short:"p" description:"Provider"`
    25  	usage           interface{}  `usage:"CF_NAME purge-service-offering SERVICE [-b BROKER] [-p PROVIDER] [-f]\n\nWARNING: 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."`
    26  	relatedCommands interface{}  `related_commands:"marketplace, purge-service-instance, service-brokers"`
    27  
    28  	UI          command.UI
    29  	SharedActor command.SharedActor
    30  	Actor       PurgeServiceOfferingActor
    31  	Config      command.Config
    32  }
    33  
    34  func (cmd *PurgeServiceOfferingCommand) Setup(config command.Config, ui command.UI) error {
    35  	cmd.Config = config
    36  	cmd.UI = ui
    37  	cmd.SharedActor = sharedaction.NewActor(config)
    38  
    39  	ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    45  
    46  	return nil
    47  }
    48  
    49  func (cmd *PurgeServiceOfferingCommand) Execute(args []string) error {
    50  	if len(args) > 0 {
    51  		return translatableerror.TooManyArgumentsError{
    52  			ExtraArgument: args[0],
    53  		}
    54  	}
    55  
    56  	if err := cmd.SharedActor.CheckTarget(false, false); err != nil {
    57  		return err
    58  	}
    59  
    60  	if cmd.Provider != "" {
    61  		return translatableerror.FlagNoLongerSupportedError{Flag: "-p"}
    62  	}
    63  
    64  	service, warnings, err := cmd.Actor.GetServiceByNameAndBrokerName(cmd.RequiredArgs.Service, cmd.ServiceBroker)
    65  	if err != nil {
    66  		cmd.UI.DisplayWarnings(warnings)
    67  
    68  		switch err.(type) {
    69  		case actionerror.ServiceNotFoundError:
    70  			cmd.UI.DisplayText("Service offering '{{.ServiceOffering}}' not found", map[string]interface{}{
    71  				"ServiceOffering": cmd.RequiredArgs.Service,
    72  			})
    73  			cmd.UI.DisplayOK()
    74  			return nil
    75  		default:
    76  			return err
    77  		}
    78  	}
    79  
    80  	cmd.UI.DisplayText("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.\n")
    81  
    82  	if !cmd.Force {
    83  		var promptMessage string
    84  		if cmd.ServiceBroker != "" {
    85  			promptMessage = "Really purge service offering {{.ServiceOffering}} from broker {{.ServiceBroker}} from Cloud Foundry?"
    86  		} else {
    87  			promptMessage = "Really purge service offering {{.ServiceOffering}} from Cloud Foundry?"
    88  		}
    89  
    90  		purgeServiceOffering, promptErr := cmd.UI.DisplayBoolPrompt(false, promptMessage, map[string]interface{}{
    91  			"ServiceOffering": cmd.RequiredArgs.Service,
    92  			"ServiceBroker":   cmd.ServiceBroker,
    93  		})
    94  		if promptErr != nil {
    95  			return promptErr
    96  		}
    97  
    98  		if !purgeServiceOffering {
    99  			cmd.UI.DisplayText("Purge service offering cancelled")
   100  			return nil
   101  		}
   102  	}
   103  
   104  	cmd.UI.DisplayText("Purging service {{.ServiceOffering}}...", map[string]interface{}{
   105  		"ServiceOffering": cmd.RequiredArgs.Service,
   106  	})
   107  
   108  	purgeWarnings, err := cmd.Actor.PurgeServiceOffering(service)
   109  	allWarnings := append(warnings, purgeWarnings...)
   110  	cmd.UI.DisplayWarnings(allWarnings)
   111  
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	cmd.UI.DisplayOK()
   117  
   118  	return nil
   119  }