github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/security_groups_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  	"code.cloudfoundry.org/cli/util/ui"
    11  )
    12  
    13  //go:generate counterfeiter . SecurityGroupsActor
    14  
    15  type SecurityGroupsActor interface {
    16  	CloudControllerAPIVersion() string
    17  	GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging bool) ([]v2action.SecurityGroupWithOrganizationSpaceAndLifecycle, v2action.Warnings, error)
    18  }
    19  
    20  type SecurityGroupsCommand struct {
    21  	usage           interface{} `usage:"CF_NAME security-groups"`
    22  	relatedCommands interface{} `related_commands:"bind-security-group, bind-running-security-group, bind-staging-security-group, security-group"`
    23  
    24  	SharedActor command.SharedActor
    25  	Config      command.Config
    26  	UI          command.UI
    27  	Actor       SecurityGroupsActor
    28  }
    29  
    30  func (cmd *SecurityGroupsCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.UI = ui
    32  	cmd.Config = config
    33  	cmd.SharedActor = sharedaction.NewActor(config)
    34  
    35  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd SecurityGroupsCommand) Execute(args []string) error {
    45  	user, err := cmd.Config.CurrentUser()
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = cmd.SharedActor.CheckTarget(false, false)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	includeStaging := true
    56  
    57  	cmd.UI.DisplayTextWithFlavor("Getting security groups as {{.UserName}}...",
    58  		map[string]interface{}{"UserName": user.Name})
    59  
    60  	secGroupOrgSpaces, warnings, err := cmd.Actor.GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging)
    61  	cmd.UI.DisplayWarnings(warnings)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	cmd.UI.DisplayOK()
    67  	cmd.UI.DisplayNewline()
    68  
    69  	table := [][]string{
    70  		{
    71  			cmd.UI.TranslateText(""),
    72  			cmd.UI.TranslateText("name"),
    73  			cmd.UI.TranslateText("organization"),
    74  			cmd.UI.TranslateText("space"),
    75  			cmd.UI.TranslateText("lifecycle"),
    76  		},
    77  	}
    78  
    79  	currentGroupIndex := -1
    80  	var currentGroupName string
    81  	for _, secGroupOrgSpace := range secGroupOrgSpaces {
    82  		var currentGroupIndexString string
    83  
    84  		if secGroupOrgSpace.SecurityGroup.Name != currentGroupName {
    85  			currentGroupIndex++
    86  			currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
    87  			currentGroupName = secGroupOrgSpace.SecurityGroup.Name
    88  		}
    89  
    90  		switch {
    91  		case secGroupOrgSpace.Organization.Name == "" && secGroupOrgSpace.Space.Name == "" &&
    92  			(secGroupOrgSpace.SecurityGroup.RunningDefault ||
    93  				secGroupOrgSpace.SecurityGroup.StagingDefault):
    94  			table = append(table, []string{
    95  				currentGroupIndexString,
    96  				secGroupOrgSpace.SecurityGroup.Name,
    97  				cmd.UI.TranslateText("<all>"),
    98  				cmd.UI.TranslateText("<all>"),
    99  				string(secGroupOrgSpace.Lifecycle),
   100  			})
   101  		default:
   102  			table = append(table, []string{
   103  				currentGroupIndexString,
   104  				secGroupOrgSpace.SecurityGroup.Name,
   105  				secGroupOrgSpace.Organization.Name,
   106  				secGroupOrgSpace.Space.Name,
   107  				string(secGroupOrgSpace.Lifecycle),
   108  			})
   109  		}
   110  	}
   111  
   112  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   113  
   114  	return nil
   115  }