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