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

     1  package v7
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccerror"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
    11  )
    12  
    13  type CreateServiceCommand struct {
    14  	BaseCommand
    15  
    16  	RequiredArgs     flag.CreateServiceArgs        `positional-args:"yes"`
    17  	ServiceBroker    string                        `short:"b" description:"Create a service instance from a particular broker. Required when service offering name is ambiguous"`
    18  	ParametersAsJSON 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  	Tags             flag.Tags                     `short:"t" description:"User provided tags"`
    20  	Wait             bool                          `short:"w" long:"wait" description:"Wait for the operation to complete"`
    21  	relatedCommands  interface{}                   `related_commands:"bind-service, create-user-provided-service, marketplace, services"`
    22  }
    23  
    24  func (cmd CreateServiceCommand) Usage() string {
    25  	return `
    26  CF_NAME create-service SERVICE_OFFERING PLAN SERVICE_INSTANCE [-b SERVICE_BROKER] [-c PARAMETERS_AS_JSON] [-t TAGS]
    27  
    28  Optionally provide service-specific configuration parameters in a valid JSON object in-line:
    29  
    30  CF_NAME create-service SERVICE_OFFERING PLAN SERVICE_INSTANCE -c '{"name":"value","name":"value"}'
    31  
    32  Optionally provide a file containing service-specific configuration parameters in a valid JSON object.
    33  The path to the parameters file can be an absolute or relative path to a file:
    34  
    35  CF_NAME create-service SERVICE_OFFERING PLAN SERVICE_INSTANCE -c PATH_TO_FILE
    36  
    37  Example of valid JSON object:
    38  {
    39    "cluster_nodes": {
    40      "count": 5,
    41      "memory_mb": 1024
    42    }
    43  }
    44  
    45  TIP:
    46  	Use 'CF_NAME create-user-provided-service' to make user-provided service instances available to CF apps
    47  
    48  EXAMPLES:
    49  	Linux/Mac:
    50  	   CF_NAME create-service db-service silver mydb -c '{"ram_gb":4}'
    51  
    52  	Windows Command Line:
    53  	   CF_NAME create-service db-service silver mydb -c "{\"ram_gb\":4}"
    54  
    55  	Windows PowerShell:
    56  	   CF_NAME create-service db-service silver mydb -c '{\"ram_gb\":4}'
    57  
    58  	   CF_NAME create-service db-service silver mydb -c ~/workspace/tmp/instance_config.json
    59  
    60  	   CF_NAME create-service db-service silver mydb -t "list, of, tags"
    61  `
    62  }
    63  
    64  func (cmd CreateServiceCommand) Execute(args []string) error {
    65  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    66  		return err
    67  	}
    68  
    69  	cmd.RequiredArgs.ServiceInstance = strings.TrimSpace(cmd.RequiredArgs.ServiceInstance)
    70  
    71  	if err := cmd.displayCreatingMessage(); err != nil {
    72  		return err
    73  	}
    74  
    75  	stream, warnings, err := cmd.Actor.CreateManagedServiceInstance(
    76  		v7action.CreateManagedServiceInstanceParams{
    77  			ServiceOfferingName: cmd.RequiredArgs.ServiceOffering,
    78  			ServicePlanName:     cmd.RequiredArgs.ServicePlan,
    79  			ServiceInstanceName: cmd.RequiredArgs.ServiceInstance,
    80  			ServiceBrokerName:   cmd.ServiceBroker,
    81  			SpaceGUID:           cmd.Config.TargetedSpace().GUID,
    82  			Tags:                types.OptionalStringSlice(cmd.Tags),
    83  			Parameters:          types.OptionalObject(cmd.ParametersAsJSON),
    84  		},
    85  	)
    86  	cmd.UI.DisplayWarnings(warnings)
    87  	switch err.(type) {
    88  	case nil:
    89  	case ccerror.ServiceInstanceNameTakenError:
    90  		cmd.UI.DisplayOK()
    91  		cmd.UI.DisplayTextWithFlavor("Service instance {{.ServiceInstanceName}} already exists", cmd.serviceInstanceName())
    92  		return nil
    93  	default:
    94  		return err
    95  	}
    96  
    97  	cmd.UI.DisplayNewline()
    98  	complete, err := shared.WaitForResult(stream, cmd.UI, cmd.Wait)
    99  	switch {
   100  	case err != nil:
   101  		return err
   102  	case complete:
   103  		cmd.UI.DisplayTextWithFlavor("Service instance {{.ServiceInstanceName}} created.", cmd.serviceInstanceName())
   104  	default:
   105  		cmd.UI.DisplayTextWithFlavor("Create in progress. Use 'cf services' or 'cf service {{.ServiceInstanceName}}' to check operation status.", cmd.serviceInstanceName())
   106  	}
   107  
   108  	cmd.UI.DisplayOK()
   109  	return nil
   110  }
   111  
   112  func (cmd CreateServiceCommand) displayCreatingMessage() error {
   113  	user, err := cmd.Actor.GetCurrentUser()
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	cmd.UI.DisplayTextWithFlavor("Creating service instance {{.ServiceInstance}} in org {{.Org}} / space {{.Space}} as {{.User}}...",
   119  		map[string]interface{}{
   120  			"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
   121  			"Org":             cmd.Config.TargetedOrganization().Name,
   122  			"Space":           cmd.Config.TargetedSpace().Name,
   123  			"User":            user.Name,
   124  		},
   125  	)
   126  
   127  	return nil
   128  }
   129  
   130  func (cmd CreateServiceCommand) serviceInstanceName() map[string]interface{} {
   131  	return map[string]interface{}{
   132  		"ServiceInstanceName": cmd.RequiredArgs.ServiceInstance,
   133  	}
   134  }