github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/disable_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 . DisableServiceAccessActor
    13  
    14  type DisableServiceAccessActor interface {
    15  	DisablePlanForOrg(serviceName, servicePlanName, orgName, brokerName string) (v2action.Warnings, error)
    16  	DisableServiceForOrg(serviceName, orgName, brokerName string) (v2action.Warnings, error)
    17  	DisablePlanForAllOrgs(serviceName, servicePlanName, brokerName string) (v2action.Warnings, error)
    18  	DisableServiceForAllOrgs(serviceName, brokerName string) (v2action.Warnings, error)
    19  }
    20  
    21  type DisableServiceAccessCommand struct {
    22  	RequiredArgs    flag.Service `positional-args:"yes"`
    23  	ServiceBroker   string       `short:"b" hidden:"true" description:"[Experimental] Disable access to a service from a specific service broker"`
    24  	Organization    string       `short:"o" description:"Disable access for a specified organization"`
    25  	ServicePlan     string       `short:"p" description:"Disable access to a specified service plan"`
    26  	usage           interface{}  `usage:"CF_NAME disable-service-access SERVICE [-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       DisableServiceAccessActor
    32  	Config      command.Config
    33  }
    34  
    35  func (cmd *DisableServiceAccessCommand) 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.NewClients(config, ui, true)
    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 DisableServiceAccessCommand) 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  	serviceName := cmd.RequiredArgs.Service
    68  	servicePlanName := cmd.ServicePlan
    69  	orgName := cmd.Organization
    70  	serviceBrokerName := cmd.ServiceBroker
    71  	var warnings v2action.Warnings
    72  
    73  	cmd.UI.DisplayTextWithFlavor(disableMessages[disableServiceAccessOptions{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.DisablePlanForOrg(serviceName, servicePlanName, orgName, serviceBrokerName)
    84  	} else if orgName != "" {
    85  		warnings, err = cmd.Actor.DisableServiceForOrg(serviceName, orgName, serviceBrokerName)
    86  	} else if servicePlanName != "" {
    87  		warnings, err = cmd.Actor.DisablePlanForAllOrgs(serviceName, servicePlanName, serviceBrokerName)
    88  	} else {
    89  		warnings, err = cmd.Actor.DisableServiceForAllOrgs(serviceName, serviceBrokerName)
    90  	}
    91  
    92  	cmd.UI.DisplayWarnings(warnings)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	cmd.UI.DisplayOK()
    98  
    99  	return nil
   100  }
   101  
   102  type disableServiceAccessOptions struct {
   103  	Plan   bool
   104  	Org    bool
   105  	Broker bool
   106  }
   107  
   108  var disableMessages = map[disableServiceAccessOptions]string{
   109  	{Plan: true, Org: true, Broker: false}:   "Disabling access to plan {{.ServicePlan}} of service {{.Service}} for org {{.Organization}} as {{.User}}...",
   110  	{Plan: false, Org: true, Broker: false}:  "Disabling access to all plans of service {{.Service}} for the org {{.Organization}} as {{.User}}...",
   111  	{Plan: true, Org: false, Broker: false}:  "Disabling access of plan {{.ServicePlan}} for service {{.Service}} as {{.User}}...",
   112  	{Plan: false, Org: false, Broker: false}: "Disabling access to all plans of service {{.Service}} for all orgs as {{.User}}...",
   113  	{Plan: true, Org: true, Broker: true}:    "Disabling access to plan {{.ServicePlan}} of service {{.Service}} from broker {{.ServiceBroker}} for org {{.Organization}} as {{.User}}...",
   114  	{Plan: false, Org: true, Broker: true}:   "Disabling access to all plans of service {{.Service}} from broker {{.ServiceBroker}} for the org {{.Organization}} as {{.User}}...",
   115  	{Plan: true, Org: false, Broker: true}:   "Disabling access to plan {{.ServicePlan}} of service {{.Service}} from broker {{.ServiceBroker}} for all orgs as {{.User}}...",
   116  	{Plan: false, Org: false, Broker: true}:  "Disabling access to all plans of service {{.Service}} from broker {{.ServiceBroker}} for all orgs as {{.User}}...",
   117  }