github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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.GetNewClientsAndConnectToCF(config, ui)
    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  
    68  	table := [][]string{
    69  		{
    70  			cmd.UI.TranslateText(""),
    71  			cmd.UI.TranslateText("name"),
    72  			cmd.UI.TranslateText("organization"),
    73  			cmd.UI.TranslateText("space"),
    74  			cmd.UI.TranslateText("lifecycle"),
    75  		},
    76  	}
    77  
    78  	currentGroupIndex := -1
    79  	var currentGroupName string
    80  	for _, secGroupOrgSpace := range secGroupOrgSpaces {
    81  		var currentGroupIndexString string
    82  
    83  		if secGroupOrgSpace.SecurityGroup.Name != currentGroupName {
    84  			currentGroupIndex++
    85  			currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
    86  			currentGroupName = secGroupOrgSpace.SecurityGroup.Name
    87  		}
    88  
    89  		switch {
    90  		case secGroupOrgSpace.Organization.Name == "" && secGroupOrgSpace.Space.Name == "" &&
    91  			(secGroupOrgSpace.SecurityGroup.RunningDefault ||
    92  				secGroupOrgSpace.SecurityGroup.StagingDefault):
    93  			table = append(table, []string{
    94  				currentGroupIndexString,
    95  				secGroupOrgSpace.SecurityGroup.Name,
    96  				cmd.UI.TranslateText("<all>"),
    97  				cmd.UI.TranslateText("<all>"),
    98  				string(secGroupOrgSpace.Lifecycle),
    99  			})
   100  		default:
   101  			table = append(table, []string{
   102  				currentGroupIndexString,
   103  				secGroupOrgSpace.SecurityGroup.Name,
   104  				secGroupOrgSpace.Organization.Name,
   105  				secGroupOrgSpace.Space.Name,
   106  				string(secGroupOrgSpace.Lifecycle),
   107  			})
   108  		}
   109  	}
   110  
   111  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   112  
   113  	return nil
   114  }