github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/update_service_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/sharedaction"
     5  	"code.cloudfoundry.org/cli/actor/v2action"
     6  	"code.cloudfoundry.org/cli/actor/v2action/composite"
     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 . UpdateServiceActor
    14  
    15  type UpdateServiceActor interface {
    16  	GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (v2action.ServiceInstance, v2action.Warnings, error)
    17  	UpgradeServiceInstance(serviceInstanceGUID, servicePlanGUID string) (v2action.Warnings, error)
    18  }
    19  
    20  type textData map[string]interface{}
    21  
    22  type UpdateServiceCommand struct {
    23  	RequiredArgs     flag.ServiceInstance `positional-args:"yes"`
    24  	ParametersAsJSON flag.Path            `short:"c" description:"Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file. For a list of supported configuration parameters, see documentation for the particular service offering."`
    25  	Plan             string               `short:"p" description:"Change service plan for a service instance"`
    26  	Tags             string               `short:"t" description:"User provided tags"`
    27  	usage            interface{}          `usage:"CF_NAME update-service SERVICE_INSTANCE [-p NEW_PLAN] [-c PARAMETERS_AS_JSON] [-t TAGS]\n\n   Optionally provide service-specific configuration parameters in a valid JSON object in-line.\n   CF_NAME update-service -c '{\"name\":\"value\",\"name\":\"value\"}'\n\n   Optionally provide a file containing service-specific configuration parameters in a valid JSON object. \n   The path to the parameters file can be an absolute or relative path to a file.\n   CF_NAME update-service -c PATH_TO_FILE\n\n   Example of valid JSON object:\n   {\n      \"cluster_nodes\": {\n         \"count\": 5,\n         \"memory_mb\": 1024\n      }\n   }\n\n   Optionally provide a list of comma-delimited tags that will be written to the VCAP_SERVICES environment variable for any bound applications.\n\nEXAMPLES:\n   CF_NAME update-service mydb -p gold\n   CF_NAME update-service mydb -c '{\"ram_gb\":4}'\n   CF_NAME update-service mydb -c ~/workspace/tmp/instance_config.json\n   CF_NAME update-service mydb -t \"list, of, tags\""`
    28  	relatedCommands  interface{}          `related_commands:"rename-service, services, update-user-provided-service"`
    29  	Upgrade          bool                 `long:"upgrade" hidden:"true"`
    30  
    31  	UI          command.UI
    32  	Actor       UpdateServiceActor
    33  	SharedActor command.SharedActor
    34  	Config      command.Config
    35  }
    36  
    37  func (cmd *UpdateServiceCommand) Setup(config command.Config, ui command.UI) error {
    38  	cmd.UI = ui
    39  	cmd.Config = config
    40  
    41  	cmd.SharedActor = sharedaction.NewActor(config)
    42  
    43  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	baseActor := v2action.NewActor(ccClient, uaaClient, config)
    49  	cmd.Actor = &composite.UpdateServiceInstanceCompositeActor{
    50  		GetServiceInstanceActor:                   baseActor,
    51  		GetServicePlanActor:                       baseActor,
    52  		UpdateServiceInstanceMaintenanceInfoActor: baseActor,
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func (cmd *UpdateServiceCommand) Execute(args []string) error {
    59  	if !cmd.Upgrade {
    60  		return translatableerror.UnrefactoredCommandError{}
    61  	}
    62  
    63  	if len(args) > 0 {
    64  		return translatableerror.TooManyArgumentsError{
    65  			ExtraArgument: args[0],
    66  		}
    67  	}
    68  
    69  	if err := cmd.validateArgumentCombination(); err != nil {
    70  		return err
    71  	}
    72  
    73  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    74  		return err
    75  	}
    76  
    77  	instance, warnings, err := cmd.Actor.GetServiceInstanceByNameAndSpace(cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID)
    78  	cmd.UI.DisplayWarnings(warnings)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	proceed, err := cmd.promptForUpgrade()
    84  
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	if !proceed {
    90  		cmd.UI.DisplayText("Update cancelled")
    91  		return nil
    92  	}
    93  
    94  	return cmd.performUpgrade(instance)
    95  }
    96  
    97  func (cmd *UpdateServiceCommand) promptForUpgrade() (bool, error) {
    98  	var serviceName = textData{"ServiceName": cmd.RequiredArgs.ServiceInstance}
    99  
   100  	cmd.UI.DisplayText("This command is in EXPERIMENTAL stage and may change without notice.")
   101  	cmd.UI.DisplayTextWithFlavor("You are about to update {{.ServiceName}}.", serviceName)
   102  	cmd.UI.DisplayText("Warning: This operation may be long running and will block further operations on the service until complete.")
   103  
   104  	return cmd.UI.DisplayBoolPrompt(false, "Really update service {{.ServiceName}}?", serviceName)
   105  }
   106  
   107  func (cmd *UpdateServiceCommand) performUpgrade(instance v2action.ServiceInstance) error {
   108  	warnings, err := cmd.Actor.UpgradeServiceInstance(instance.GUID, instance.ServicePlanGUID)
   109  	cmd.UI.DisplayWarnings(warnings)
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	cmd.UI.DisplayOK()
   115  	return nil
   116  }
   117  
   118  func (cmd *UpdateServiceCommand) validateArgumentCombination() error {
   119  	if cmd.Tags != "" || cmd.ParametersAsJSON != "" || cmd.Plan != "" {
   120  		return translatableerror.ArgumentCombinationError{
   121  			Args: []string{"--upgrade", "-t", "-c", "-p"},
   122  		}
   123  	}
   124  
   125  	return nil
   126  }