github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/enable_service_access_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/v7action"
     5  	"code.cloudfoundry.org/cli/command"
     6  	"code.cloudfoundry.org/cli/command/flag"
     7  )
     8  
     9  type EnableServiceAccessCommand struct {
    10  	BaseCommand
    11  
    12  	RequiredArgs    flag.Service `positional-args:"yes"`
    13  	ServiceBroker   string       `short:"b" description:"Enable access to a service offering from a particular service broker. Required when service offering name is ambiguous"`
    14  	Organization    string       `short:"o" description:"Enable access for a specified organization"`
    15  	ServicePlan     string       `short:"p" description:"Enable access to a specified service plan"`
    16  	usage           interface{}  `usage:"CF_NAME enable-service-access SERVICE [-b BROKER] [-p PLAN] [-o ORG]"`
    17  	relatedCommands interface{}  `related_commands:"disable-service-access, marketplace, service-access, service-brokers"`
    18  }
    19  
    20  func (cmd EnableServiceAccessCommand) Execute(args []string) error {
    21  	if err := cmd.SharedActor.CheckTarget(false, false); err != nil {
    22  		return err
    23  	}
    24  
    25  	if err := cmd.displayMessage(); err != nil {
    26  		return err
    27  	}
    28  
    29  	skipped, warnings, err := cmd.Actor.EnableServiceAccess(cmd.RequiredArgs.ServiceOffering, cmd.ServiceBroker, cmd.Organization, cmd.ServicePlan)
    30  	cmd.UI.DisplayWarnings(warnings)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	displaySkippedPlans(cmd.UI, "all", skipped)
    36  
    37  	cmd.UI.DisplayOK()
    38  
    39  	return nil
    40  }
    41  
    42  func (cmd EnableServiceAccessCommand) displayMessage() error {
    43  	user, err := cmd.Config.CurrentUser()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	setServiceAccessMessage{
    49  		Operation:       "Enabling",
    50  		ServiceOffering: cmd.RequiredArgs.ServiceOffering,
    51  		ServicePlan:     cmd.ServicePlan,
    52  		Organization:    cmd.Organization,
    53  		ServiceBroker:   cmd.ServiceBroker,
    54  		User:            user.Name,
    55  	}.displayMessage(cmd.UI)
    56  
    57  	return nil
    58  }
    59  
    60  type setServiceAccessMessage struct {
    61  	Operation, ServiceOffering, ServicePlan, Organization, ServiceBroker, User string
    62  }
    63  
    64  func (msg setServiceAccessMessage) displayMessage(ui command.UI) {
    65  	template := msg.Operation + " access to "
    66  
    67  	if msg.ServicePlan != "" {
    68  		template += "plan {{.ServicePlan}} "
    69  	} else {
    70  		template += "all plans "
    71  	}
    72  
    73  	template += "of service offering {{.ServiceOffering}} "
    74  
    75  	if msg.ServiceBroker != "" {
    76  		template += "from broker {{.ServiceBroker}} "
    77  	}
    78  
    79  	if msg.Organization != "" {
    80  		template += "for org {{.Organization}} "
    81  	} else {
    82  		template += "for all orgs "
    83  	}
    84  
    85  	template += "as {{.CurrentUser}}..."
    86  
    87  	ui.DisplayTextWithFlavor(template, map[string]interface{}{
    88  		"ServiceBroker":   msg.ServiceBroker,
    89  		"ServiceOffering": msg.ServiceOffering,
    90  		"ServicePlan":     msg.ServicePlan,
    91  		"Organization":    msg.Organization,
    92  		"CurrentUser":     msg.User,
    93  	})
    94  }
    95  
    96  func displaySkippedPlans(ui command.UI, visibility string, skipped v7action.SkippedPlans) {
    97  	if len(skipped) > 0 {
    98  		ui.DisplayNewline()
    99  
   100  		for _, plan := range skipped {
   101  			ui.DisplayTextWithFlavor(
   102  				"Did not update plan {{.ServicePlan}} as it already has visibility {{.Visibility}}.",
   103  				map[string]interface{}{
   104  					"ServicePlan": plan,
   105  					"Visibility":  visibility,
   106  				},
   107  			)
   108  		}
   109  
   110  		ui.DisplayNewline()
   111  	}
   112  }