github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/space_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/actor/v3action"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    12  	"code.cloudfoundry.org/cli/command"
    13  	"code.cloudfoundry.org/cli/command/flag"
    14  	"code.cloudfoundry.org/cli/command/translatableerror"
    15  	"code.cloudfoundry.org/cli/command/v2/shared"
    16  	sharedV3 "code.cloudfoundry.org/cli/command/v3/shared"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  )
    19  
    20  //go:generate counterfeiter . SpaceActor
    21  
    22  type SpaceActor interface {
    23  	CloudControllerAPIVersion() string
    24  	GetSpaceByOrganizationAndName(orgGUID string, spaceName string) (v2action.Space, v2action.Warnings, error)
    25  	GetSpaceSummaryByOrganizationAndName(orgGUID string, spaceName string, includeStagingSecurityGroupsRules bool) (v2action.SpaceSummary, v2action.Warnings, error)
    26  }
    27  
    28  //go:generate counterfeiter . SpaceActorV3
    29  
    30  type SpaceActorV3 interface {
    31  	CloudControllerAPIVersion() string
    32  	GetEffectiveIsolationSegmentBySpace(spaceGUID string, orgDefaultIsolationSegmentGUID string) (v3action.IsolationSegment, v3action.Warnings, error)
    33  }
    34  
    35  type SpaceCommand struct {
    36  	RequiredArgs       flag.Space  `positional-args:"yes"`
    37  	GUID               bool        `long:"guid" description:"Retrieve and display the given space's guid.  All other output for the space is suppressed."`
    38  	SecurityGroupRules bool        `long:"security-group-rules" description:"Retrieve the rules for all the security groups associated with the space."`
    39  	usage              interface{} `usage:"CF_NAME space SPACE [--guid] [--security-group-rules]"`
    40  	relatedCommands    interface{} `related_commands:"set-space-isolation-segment, space-quota, space-users"`
    41  
    42  	UI          command.UI
    43  	Config      command.Config
    44  	SharedActor command.SharedActor
    45  	Actor       SpaceActor
    46  	ActorV3     SpaceActorV3
    47  }
    48  
    49  func (cmd *SpaceCommand) Setup(config command.Config, ui command.UI) error {
    50  	cmd.Config = config
    51  	cmd.UI = ui
    52  	cmd.SharedActor = sharedaction.NewActor(config)
    53  
    54  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    59  
    60  	ccClientV3, _, err := sharedV3.NewClients(config, ui, true)
    61  	if err != nil {
    62  		if _, ok := err.(translatableerror.V3APIDoesNotExistError); !ok {
    63  			return err
    64  		}
    65  	} else {
    66  		cmd.ActorV3 = v3action.NewActor(ccClientV3, config, nil, nil)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func (cmd SpaceCommand) Execute(args []string) error {
    73  	err := cmd.SharedActor.CheckTarget(true, false)
    74  
    75  	if err == nil {
    76  		if cmd.GUID {
    77  			err = cmd.displaySpaceGUID()
    78  		} else {
    79  			err = cmd.displaySpaceSummary(cmd.SecurityGroupRules)
    80  		}
    81  	}
    82  
    83  	return err
    84  }
    85  
    86  func (cmd SpaceCommand) displaySpaceGUID() error {
    87  	org, warnings, err := cmd.Actor.GetSpaceByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space)
    88  	cmd.UI.DisplayWarnings(warnings)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	cmd.UI.DisplayText(org.GUID)
    94  
    95  	return nil
    96  }
    97  
    98  func (cmd SpaceCommand) displaySpaceSummary(displaySecurityGroupRules bool) error {
    99  	user, err := cmd.Config.CurrentUser()
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	cmd.UI.DisplayTextWithFlavor("Getting info for space {{.TargetSpace}} in org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
   105  		"TargetSpace": cmd.RequiredArgs.Space,
   106  		"OrgName":     cmd.Config.TargetedOrganization().Name,
   107  		"CurrentUser": user.Name,
   108  	})
   109  	cmd.UI.DisplayNewline()
   110  
   111  	err = command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionLifecyleStagingV2)
   112  	includeStagingSecurityGroupsRules := err == nil
   113  
   114  	spaceSummary, warnings, err := cmd.Actor.GetSpaceSummaryByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space, includeStagingSecurityGroupsRules)
   115  	cmd.UI.DisplayWarnings(warnings)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	table := [][]string{
   121  		{cmd.UI.TranslateText("name:"), spaceSummary.Name},
   122  		{cmd.UI.TranslateText("org:"), spaceSummary.OrgName},
   123  		{cmd.UI.TranslateText("apps:"), strings.Join(spaceSummary.AppNames, ", ")},
   124  		{cmd.UI.TranslateText("services:"), strings.Join(spaceSummary.ServiceInstanceNames, ", ")},
   125  	}
   126  
   127  	isolationSegmentRow, err := cmd.isolationSegmentRow(spaceSummary)
   128  	if err != nil {
   129  		return err
   130  	}
   131  	if isolationSegmentRow != nil {
   132  		table = append(table, isolationSegmentRow)
   133  	}
   134  
   135  	table = append(table,
   136  		[]string{cmd.UI.TranslateText("space quota:"), spaceSummary.SpaceQuotaName})
   137  	table = append(table,
   138  		[]string{cmd.UI.TranslateText("running security groups:"), strings.Join(spaceSummary.RunningSecurityGroupNames, ", ")})
   139  	table = append(table,
   140  		[]string{cmd.UI.TranslateText("staging security groups:"), strings.Join(spaceSummary.StagingSecurityGroupNames, ", ")})
   141  
   142  	cmd.UI.DisplayKeyValueTable("", table, 3)
   143  
   144  	if displaySecurityGroupRules {
   145  		table := [][]string{
   146  			{
   147  				cmd.UI.TranslateText(""),
   148  				cmd.UI.TranslateText("security group"),
   149  				cmd.UI.TranslateText("destination"),
   150  				cmd.UI.TranslateText("ports"),
   151  				cmd.UI.TranslateText("protocol"),
   152  				cmd.UI.TranslateText("lifecycle"),
   153  				cmd.UI.TranslateText("description"),
   154  			},
   155  		}
   156  
   157  		currentGroupIndex := -1
   158  		var currentGroupName string
   159  		for _, securityGroupRule := range spaceSummary.SecurityGroupRules {
   160  			var currentGroupIndexString string
   161  
   162  			if securityGroupRule.Name != currentGroupName {
   163  				currentGroupIndex++
   164  				currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
   165  				currentGroupName = securityGroupRule.Name
   166  			}
   167  
   168  			table = append(table, []string{
   169  				currentGroupIndexString,
   170  				securityGroupRule.Name,
   171  				securityGroupRule.Destination,
   172  				securityGroupRule.Ports,
   173  				securityGroupRule.Protocol,
   174  				string(securityGroupRule.Lifecycle),
   175  				securityGroupRule.Description,
   176  			})
   177  		}
   178  
   179  		cmd.UI.DisplayNewline()
   180  		cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func (cmd SpaceCommand) isolationSegmentRow(spaceSummary v2action.SpaceSummary) ([]string, error) {
   187  	if cmd.ActorV3 == nil {
   188  		return nil, nil
   189  	}
   190  
   191  	apiCheck := command.MinimumAPIVersionCheck(cmd.ActorV3.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
   192  	if apiCheck != nil {
   193  		return nil, nil
   194  	}
   195  
   196  	isolationSegmentName := ""
   197  	isolationSegment, v3Warnings, err := cmd.ActorV3.GetEffectiveIsolationSegmentBySpace(
   198  		spaceSummary.GUID, spaceSummary.OrgDefaultIsolationSegmentGUID)
   199  	cmd.UI.DisplayWarnings(v3Warnings)
   200  	if err == nil {
   201  		isolationSegmentName = isolationSegment.Name
   202  	} else {
   203  		if _, ok := err.(actionerror.NoRelationshipError); !ok {
   204  			return nil, err
   205  		}
   206  	}
   207  
   208  	return []string{cmd.UI.TranslateText("isolation segment:"), isolationSegmentName}, nil
   209  }