github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/unbind_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/errors"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type unbindFromRunningGroup struct {
    18  	ui                terminal.UI
    19  	configRepo        coreconfig.Reader
    20  	securityGroupRepo securitygroups.SecurityGroupRepo
    21  	runningGroupRepo  running.SecurityGroupsRepo
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&unbindFromRunningGroup{})
    26  }
    27  
    28  func (cmd *unbindFromRunningGroup) MetaData() commandregistry.CommandMetadata {
    29  	primaryUsage := T("CF_NAME unbind-running-security-group SECURITY_GROUP")
    30  	tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "unbind-running-security-group",
    33  		Description: T("Unbind a security group from the set of security groups for running applications"),
    34  		Usage: []string{
    35  			primaryUsage,
    36  			"\n\n",
    37  			tipUsage,
    38  		},
    39  	}
    40  }
    41  
    42  func (cmd *unbindFromRunningGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	if len(fc.Args()) != 1 {
    44  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("unbind-running-security-group"))
    45  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    46  	}
    47  
    48  	reqs := []requirements.Requirement{
    49  		requirementsFactory.NewLoginRequirement(),
    50  	}
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *unbindFromRunningGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.configRepo = deps.Config
    57  	cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
    58  	cmd.runningGroupRepo = deps.RepoLocator.GetRunningSecurityGroupsRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *unbindFromRunningGroup) Execute(context flags.FlagContext) error {
    63  	name := context.Args()[0]
    64  
    65  	securityGroup, err := cmd.securityGroupRepo.Read(name)
    66  	switch (err).(type) {
    67  	case nil:
    68  	case *errors.ModelNotFoundError:
    69  		cmd.ui.Ok()
    70  		cmd.ui.Warn(T("Security group {{.security_group}} {{.error_message}}",
    71  			map[string]interface{}{
    72  				"security_group": terminal.EntityNameColor(name),
    73  				"error_message":  terminal.WarningColor(T("does not exist.")),
    74  			}))
    75  		return nil
    76  	default:
    77  		return err
    78  	}
    79  
    80  	cmd.ui.Say(T("Unbinding security group {{.security_group}} from defaults for running as {{.username}}",
    81  		map[string]interface{}{
    82  			"security_group": terminal.EntityNameColor(securityGroup.Name),
    83  			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
    84  		}))
    85  	err = cmd.runningGroupRepo.UnbindFromRunningSet(securityGroup.GUID)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	cmd.ui.Ok()
    90  	cmd.ui.Say("\n\n")
    91  	cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
    92  	return nil
    93  }