github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/serviceaccess/enable_service_access.go (about)

     1  package serviceaccess
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors"
     7  	"code.cloudfoundry.org/cli/cf/api/authentication"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/i18n"
    15  )
    16  
    17  type EnableServiceAccess struct {
    18  	ui             terminal.UI
    19  	config         coreconfig.Reader
    20  	actor          actors.ServicePlanActor
    21  	tokenRefresher authentication.TokenRefresher
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&EnableServiceAccess{})
    26  }
    27  
    28  func (cmd *EnableServiceAccess) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Enable access to a specified service plan")}
    31  	fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Enable access for a specified organization")}
    32  
    33  	return commandregistry.CommandMetadata{
    34  		Name:        "enable-service-access",
    35  		Description: T("Enable access to a service or service plan for one or all orgs"),
    36  		Usage: []string{
    37  			"CF_NAME enable-service-access SERVICE [-p PLAN] [-o ORG]",
    38  		},
    39  		Flags: fs,
    40  	}
    41  }
    42  
    43  func (cmd *EnableServiceAccess) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    44  	if len(fc.Args()) != 1 {
    45  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("enable-service-access"))
    46  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    47  	}
    48  
    49  	reqs := []requirements.Requirement{
    50  		requirementsFactory.NewLoginRequirement(),
    51  	}
    52  
    53  	return reqs, nil
    54  }
    55  
    56  func (cmd *EnableServiceAccess) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    57  	cmd.ui = deps.UI
    58  	cmd.config = deps.Config
    59  	cmd.actor = deps.ServicePlanHandler
    60  	cmd.tokenRefresher = deps.RepoLocator.GetAuthenticationRepository()
    61  	return cmd
    62  }
    63  
    64  func (cmd *EnableServiceAccess) Execute(c flags.FlagContext) error {
    65  	_, err := cmd.tokenRefresher.RefreshAuthToken()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	serviceName := c.Args()[0]
    71  	planName := c.String("p")
    72  	orgName := c.String("o")
    73  
    74  	if planName != "" && orgName != "" {
    75  		err = cmd.enablePlanAndOrgForService(serviceName, planName, orgName)
    76  	} else if planName != "" {
    77  		err = cmd.enablePlanForService(serviceName, planName)
    78  	} else if orgName != "" {
    79  		err = cmd.enableAllPlansForSingleOrgForService(serviceName, orgName)
    80  	} else {
    81  		err = cmd.enableAllPlansForService(serviceName)
    82  	}
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	cmd.ui.Ok()
    88  	return nil
    89  }
    90  
    91  func (cmd *EnableServiceAccess) enablePlanAndOrgForService(serviceName string, planName string, orgName string) error {
    92  	cmd.ui.Say(
    93  		T("Enabling access to plan {{.PlanName}} of service {{.ServiceName}} for org {{.OrgName}} as {{.Username}}...",
    94  			map[string]interface{}{
    95  				"PlanName":    terminal.EntityNameColor(planName),
    96  				"ServiceName": terminal.EntityNameColor(serviceName),
    97  				"OrgName":     terminal.EntityNameColor(orgName),
    98  				"Username":    terminal.EntityNameColor(cmd.config.Username()),
    99  			}))
   100  	return cmd.actor.UpdatePlanAndOrgForService(serviceName, planName, orgName, true)
   101  }
   102  
   103  func (cmd *EnableServiceAccess) enablePlanForService(serviceName string, planName string) error {
   104  	cmd.ui.Say(T("Enabling access of plan {{.PlanName}} for service {{.ServiceName}} as {{.Username}}...",
   105  		map[string]interface{}{
   106  			"PlanName":    terminal.EntityNameColor(planName),
   107  			"ServiceName": terminal.EntityNameColor(serviceName),
   108  			"Username":    terminal.EntityNameColor(cmd.config.Username()),
   109  		}))
   110  	return cmd.actor.UpdateSinglePlanForService(serviceName, planName, true)
   111  }
   112  
   113  func (cmd *EnableServiceAccess) enableAllPlansForService(serviceName string) error {
   114  	cmd.ui.Say(T("Enabling access to all plans of service {{.ServiceName}} for all orgs as {{.Username}}...",
   115  		map[string]interface{}{
   116  			"ServiceName": terminal.EntityNameColor(serviceName),
   117  			"Username":    terminal.EntityNameColor(cmd.config.Username()),
   118  		}))
   119  	return cmd.actor.UpdateAllPlansForService(serviceName, true)
   120  }
   121  
   122  func (cmd *EnableServiceAccess) enableAllPlansForSingleOrgForService(serviceName string, orgName string) error {
   123  	cmd.ui.Say(T("Enabling access to all plans of service {{.ServiceName}} for the org {{.OrgName}} as {{.Username}}...",
   124  		map[string]interface{}{
   125  			"ServiceName": terminal.EntityNameColor(serviceName),
   126  			"OrgName":     terminal.EntityNameColor(orgName),
   127  			"Username":    terminal.EntityNameColor(cmd.config.Username()),
   128  		}))
   129  	return cmd.actor.UpdateOrgForService(serviceName, orgName, true)
   130  }