github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/update_service_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
    12  )
    13  
    14  type UpdateServiceCommand struct {
    15  	BaseCommand
    16  
    17  	RequiredArgs flag.ServiceInstance          `positional-args:"yes"`
    18  	Parameters   flag.JSONOrFileWithValidation `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."`
    19  	Plan         string                        `short:"p" description:"Change service plan for a service instance"`
    20  	Tags         flag.Tags                     `short:"t" description:"User provided tags"`
    21  	Wait         bool                          `short:"w" long:"wait" description:"Wait for the operation to complete"`
    22  	Upgrade      bool                          `long:"upgrade" hidden:"true"`
    23  
    24  	relatedCommands interface{} `related_commands:"rename-service, services, update-user-provided-service"`
    25  }
    26  
    27  func (cmd UpdateServiceCommand) Execute(args []string) error {
    28  	if cmd.Upgrade {
    29  		return fmt.Errorf(
    30  			`Upgrading is no longer supported via updates, please run "cf upgrade-service %s" instead.`,
    31  			cmd.RequiredArgs.ServiceInstance,
    32  		)
    33  	}
    34  
    35  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    36  		return err
    37  	}
    38  
    39  	if err := cmd.displayIntro(); err != nil {
    40  		return err
    41  	}
    42  
    43  	if cmd.noFlagsProvided() {
    44  		cmd.UI.DisplayText("No flags specified. No changes were made.")
    45  		cmd.UI.DisplayOK()
    46  		return nil
    47  	}
    48  
    49  	stream, warnings, err := cmd.Actor.UpdateManagedServiceInstance(
    50  		v7action.UpdateManagedServiceInstanceParams{
    51  			ServiceInstanceName: string(cmd.RequiredArgs.ServiceInstance),
    52  			ServicePlanName:     cmd.Plan,
    53  			SpaceGUID:           cmd.Config.TargetedSpace().GUID,
    54  			Tags:                types.OptionalStringSlice(cmd.Tags),
    55  			Parameters:          types.OptionalObject(cmd.Parameters),
    56  		},
    57  	)
    58  	cmd.UI.DisplayWarnings(warnings)
    59  	switch err.(type) {
    60  	case nil:
    61  	case actionerror.ServiceInstanceUpdateIsNoop:
    62  		cmd.UI.DisplayText("No changes were made.")
    63  		cmd.UI.DisplayOK()
    64  		return nil
    65  	default:
    66  		return err
    67  	}
    68  
    69  	complete, err := shared.WaitForResult(stream, cmd.UI, cmd.Wait)
    70  	switch {
    71  	case err != nil:
    72  		return err
    73  	case complete:
    74  		cmd.UI.DisplayTextWithFlavor("Update of service instance {{.ServiceInstance}} complete.", cmd.serviceInstanceName())
    75  	default:
    76  		cmd.UI.DisplayTextWithFlavor("Update in progress. Use 'cf services' or 'cf service {{.ServiceInstance}}' to check operation status.", cmd.serviceInstanceName())
    77  	}
    78  
    79  	cmd.UI.DisplayOK()
    80  	return nil
    81  }
    82  
    83  func (cmd UpdateServiceCommand) Usage() string {
    84  	return strings.TrimSpace(`
    85  CF_NAME update-service SERVICE_INSTANCE [-p NEW_PLAN] [-c PARAMETERS_AS_JSON] [-t TAGS]
    86  
    87  Optionally provide service-specific configuration parameters in a valid JSON object in-line:
    88  CF_NAME update-service SERVICE_INSTANCE -c '{"name":"value","name":"value"}'
    89  
    90  Optionally provide a file containing service-specific configuration parameters in a valid JSON object.
    91  The path to the parameters file can be an absolute or relative path to a file:
    92  CF_NAME update-service SERVICE_INSTANCE -c PATH_TO_FILE
    93  
    94  Example of valid JSON object:
    95  {
    96     "cluster_nodes": {
    97        "count": 5,
    98        "memory_mb": 1024
    99     }
   100  }
   101  
   102  Optionally provide a list of comma-delimited tags that will be written to the VCAP_SERVICES environment variable for any bound applications.
   103  `,
   104  	)
   105  }
   106  
   107  func (cmd UpdateServiceCommand) Examples() string {
   108  	return strings.TrimSpace(`
   109  CF_NAME update-service mydb -p gold
   110  CF_NAME update-service mydb -c '{"ram_gb":4}'
   111  CF_NAME update-service mydb -c ~/workspace/tmp/instance_config.json
   112  CF_NAME update-service mydb -t "list, of, tags"
   113  `,
   114  	)
   115  }
   116  
   117  func (cmd UpdateServiceCommand) displayIntro() error {
   118  	user, err := cmd.Actor.GetCurrentUser()
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	cmd.UI.DisplayTextWithFlavor(
   124  		"Updating service instance {{.ServiceInstanceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
   125  		map[string]interface{}{
   126  			"ServiceInstanceName": cmd.RequiredArgs.ServiceInstance,
   127  			"OrgName":             cmd.Config.TargetedOrganization().Name,
   128  			"SpaceName":           cmd.Config.TargetedSpace().Name,
   129  			"Username":            user.Name,
   130  		},
   131  	)
   132  	cmd.UI.DisplayNewline()
   133  
   134  	return nil
   135  }
   136  
   137  func (cmd UpdateServiceCommand) noFlagsProvided() bool {
   138  	return !cmd.Tags.IsSet && !cmd.Parameters.IsSet && cmd.Plan == ""
   139  }
   140  
   141  func (cmd UpdateServiceCommand) serviceInstanceName() map[string]interface{} {
   142  	return map[string]interface{}{
   143  		"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
   144  	}
   145  }