github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/create_service_broker.go (about)

     1  package servicebroker
     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/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type CreateServiceBroker struct {
    17  	ui                terminal.UI
    18  	config            coreconfig.Reader
    19  	serviceBrokerRepo api.ServiceBrokerRepository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&CreateServiceBroker{})
    24  }
    25  
    26  func (cmd *CreateServiceBroker) MetaData() commandregistry.CommandMetadata {
    27  	fs := make(map[string]flags.FlagSet)
    28  	fs["space-scoped"] = &flags.BoolFlag{Name: "space-scoped", Usage: T("Make the broker's service plans only visible within the targeted space")}
    29  
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "create-service-broker",
    32  		ShortName:   "csb",
    33  		Description: T("Create a service broker"),
    34  		Usage: []string{
    35  			T("CF_NAME create-service-broker SERVICE_BROKER USERNAME PASSWORD URL [--space-scoped]"),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *CreateServiceBroker) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 4 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_BROKER, USERNAME, PASSWORD, URL as arguments\n\n") + commandregistry.Commands.CommandUsage("create-service-broker"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 4)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  	}
    50  
    51  	if fc.IsSet("space-scoped") {
    52  		reqs = append(
    53  			reqs,
    54  			requirementsFactory.NewTargetedSpaceRequirement(),
    55  			requirementsFactory.NewMinAPIVersionRequirement("--space-scoped", cf.SpaceScopedMaximumAPIVersion),
    56  		)
    57  	}
    58  
    59  	return reqs, nil
    60  }
    61  
    62  func (cmd *CreateServiceBroker) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    63  	cmd.ui = deps.UI
    64  	cmd.config = deps.Config
    65  	cmd.serviceBrokerRepo = deps.RepoLocator.GetServiceBrokerRepository()
    66  	return cmd
    67  }
    68  
    69  func (cmd *CreateServiceBroker) Execute(c flags.FlagContext) error {
    70  	name := c.Args()[0]
    71  	username := c.Args()[1]
    72  	password := c.Args()[2]
    73  	url := c.Args()[3]
    74  
    75  	var err error
    76  	if c.Bool("space-scoped") {
    77  		cmd.ui.Say(T("Creating service broker {{.Name}} in org {{.Org}} / space {{.Space}} as {{.Username}}...",
    78  			map[string]interface{}{
    79  				"Name":     terminal.EntityNameColor(name),
    80  				"Org":      terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    81  				"Space":    terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    82  				"Username": terminal.EntityNameColor(cmd.config.Username())}))
    83  		err = cmd.serviceBrokerRepo.Create(name, url, username, password, cmd.config.SpaceFields().GUID)
    84  	} else {
    85  		cmd.ui.Say(T("Creating service broker {{.Name}} as {{.Username}}...",
    86  			map[string]interface{}{
    87  				"Name":     terminal.EntityNameColor(name),
    88  				"Username": terminal.EntityNameColor(cmd.config.Username())}))
    89  		err = cmd.serviceBrokerRepo.Create(name, url, username, password, "")
    90  	}
    91  
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	cmd.ui.Ok()
    97  	return err
    98  }