github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/servicebroker/create_service_broker.go (about)

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