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

     1  package securitygroup
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/securitygroups"
     7  	"code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/running"
     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/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type bindToRunningGroup struct {
    17  	ui                terminal.UI
    18  	configRepo        coreconfig.Reader
    19  	securityGroupRepo securitygroups.SecurityGroupRepo
    20  	runningGroupRepo  running.SecurityGroupsRepo
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&bindToRunningGroup{})
    25  }
    26  
    27  func (cmd *bindToRunningGroup) MetaData() commandregistry.CommandMetadata {
    28  	primaryUsage := T("CF_NAME bind-running-security-group SECURITY_GROUP")
    29  	tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "bind-running-security-group",
    32  		Description: T("Bind a security group to the list of security groups to be used for running applications"),
    33  		Usage: []string{
    34  			primaryUsage,
    35  			"\n\n",
    36  			tipUsage,
    37  		},
    38  	}
    39  }
    40  
    41  func (cmd *bindToRunningGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("bind-running-security-group"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  	}
    50  	return reqs, nil
    51  }
    52  
    53  func (cmd *bindToRunningGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    54  	cmd.ui = deps.UI
    55  	cmd.configRepo = deps.Config
    56  	cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
    57  	cmd.runningGroupRepo = deps.RepoLocator.GetRunningSecurityGroupsRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *bindToRunningGroup) Execute(context flags.FlagContext) error {
    62  	name := context.Args()[0]
    63  
    64  	securityGroup, err := cmd.securityGroupRepo.Read(name)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	cmd.ui.Say(T("Binding security group {{.security_group}} to defaults for running as {{.username}}",
    70  		map[string]interface{}{
    71  			"security_group": terminal.EntityNameColor(securityGroup.Name),
    72  			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
    73  		}))
    74  
    75  	err = cmd.runningGroupRepo.BindToRunningSet(securityGroup.GUID)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	cmd.ui.Ok()
    81  	cmd.ui.Say("\n\n")
    82  	cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
    83  	return nil
    84  }