github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/enable_service_access_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/command"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  )
    11  
    12  //go:generate counterfeiter . EnableServiceAccessActor
    13  
    14  type EnableServiceAccessActor interface {
    15  	EnablePlanForOrg(serviceName, servicePlanName, orgName, serviceBrokerName string) (v2action.Warnings, error)
    16  	EnableServiceForOrg(serviceName, orgName, serviceBrokerName string) (v2action.Warnings, error)
    17  	EnablePlanForAllOrgs(serviceName, servicePlanName, serviceBrokerName string) (v2action.Warnings, error)
    18  	EnableServiceForAllOrgs(serviceName, serviceBrokerName string) (v2action.Warnings, error)
    19  }
    20  
    21  type EnableServiceAccessCommand struct {
    22  	RequiredArgs    flag.Service `positional-args:"yes"`
    23  	ServiceBroker   string       `short:"b" description:"Enable access to a service from a particular service broker. Required when service name is ambiguous"`
    24  	Organization    string       `short:"o" description:"Enable access for a specified organization"`
    25  	ServicePlan     string       `short:"p" description:"Enable access to a specified service plan"`
    26  	usage           interface{}  `usage:"CF_NAME enable-service-access SERVICE [-b BROKER] [-p PLAN] [-o ORG]"`
    27  	relatedCommands interface{}  `related_commands:"marketplace, service-access, service-brokers"`
    28  
    29  	UI          command.UI
    30  	SharedActor command.SharedActor
    31  	Actor       EnableServiceAccessActor
    32  	Config      command.Config
    33  }
    34  
    35  func (cmd *EnableServiceAccessCommand) Setup(config command.Config, ui command.UI) error {
    36  	cmd.UI = ui
    37  	cmd.Config = config
    38  	cmd.SharedActor = sharedaction.NewActor(config)
    39  
    40  	ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    46  
    47  	return nil
    48  }
    49  
    50  func (cmd EnableServiceAccessCommand) Execute(args []string) error {
    51  	if len(args) > 0 {
    52  		return translatableerror.TooManyArgumentsError{
    53  			ExtraArgument: args[0],
    54  		}
    55  	}
    56  
    57  	err := cmd.SharedActor.CheckTarget(false, false)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	user, err := cmd.Config.CurrentUser()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	serviceBrokerName := cmd.ServiceBroker
    68  	serviceName := cmd.RequiredArgs.Service
    69  	servicePlanName := cmd.ServicePlan
    70  	orgName := cmd.Organization
    71  	var warnings v2action.Warnings
    72  
    73  	cmd.UI.DisplayTextWithFlavor(messages[enableServiceAccessOptions{servicePlanName != "", orgName != "", serviceBrokerName != ""}],
    74  		map[string]interface{}{
    75  			"ServicePlan":   servicePlanName,
    76  			"Service":       serviceName,
    77  			"ServiceBroker": serviceBrokerName,
    78  			"Organization":  orgName,
    79  			"User":          user.Name,
    80  		})
    81  
    82  	if servicePlanName != "" && orgName != "" {
    83  		warnings, err = cmd.Actor.EnablePlanForOrg(serviceName, servicePlanName, orgName, serviceBrokerName)
    84  	} else if orgName != "" {
    85  		warnings, err = cmd.Actor.EnableServiceForOrg(serviceName, orgName, serviceBrokerName)
    86  	} else if servicePlanName != "" {
    87  		warnings, err = cmd.Actor.EnablePlanForAllOrgs(serviceName, servicePlanName, serviceBrokerName)
    88  	} else {
    89  		warnings, err = cmd.Actor.EnableServiceForAllOrgs(serviceName, serviceBrokerName)
    90  	}
    91  
    92  	cmd.UI.DisplayWarnings(warnings)
    93  
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	cmd.UI.DisplayOK()
    99  
   100  	return nil
   101  }
   102  
   103  type enableServiceAccessOptions struct {
   104  	Plan   bool
   105  	Org    bool
   106  	Broker bool
   107  }
   108  
   109  var messages = map[enableServiceAccessOptions]string{
   110  	{Plan: true, Org: true, Broker: false}:   "Enabling access to plan {{.ServicePlan}} of service {{.Service}} for org {{.Organization}} as {{.User}}...",
   111  	{Plan: false, Org: true, Broker: false}:  "Enabling access to all plans of service {{.Service}} for the org {{.Organization}} as {{.User}}...",
   112  	{Plan: true, Org: false, Broker: false}:  "Enabling access of plan {{.ServicePlan}} for service {{.Service}} as {{.User}}...",
   113  	{Plan: false, Org: false, Broker: false}: "Enabling access to all plans of service {{.Service}} for all orgs as {{.User}}...",
   114  	{Plan: true, Org: true, Broker: true}:    "Enabling access to plan {{.ServicePlan}} of service {{.Service}} from broker {{.ServiceBroker}} for org {{.Organization}} as {{.User}}...",
   115  	{Plan: false, Org: true, Broker: true}:   "Enabling access to all plans of service {{.Service}} from broker {{.ServiceBroker}} for the org {{.Organization}} as {{.User}}...",
   116  	{Plan: true, Org: false, Broker: true}:   "Enabling access to plan {{.ServicePlan}} for service {{.Service}} from broker {{.ServiceBroker}} for all orgs as {{.User}}...",
   117  	{Plan: false, Org: false, Broker: true}:  "Enabling access to all plans of service {{.Service}} from broker {{.ServiceBroker}} for all orgs as {{.User}}...",
   118  }