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

     1  package v6
     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/v6/shared"
    16  	sharedV3 "code.cloudfoundry.org/cli/command/v6/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) (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.NewV3BasedClients(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  	spaceSummary, warnings, err := cmd.Actor.GetSpaceSummaryByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space)
   112  	cmd.UI.DisplayWarnings(warnings)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	table := [][]string{
   118  		{cmd.UI.TranslateText("name:"), spaceSummary.Name},
   119  		{cmd.UI.TranslateText("org:"), spaceSummary.OrgName},
   120  		{cmd.UI.TranslateText("apps:"), strings.Join(spaceSummary.AppNames, ", ")},
   121  		{cmd.UI.TranslateText("services:"), strings.Join(spaceSummary.ServiceInstanceNames, ", ")},
   122  	}
   123  
   124  	isolationSegmentRow, err := cmd.isolationSegmentRow(spaceSummary)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	if isolationSegmentRow != nil {
   129  		table = append(table, isolationSegmentRow)
   130  	}
   131  
   132  	table = append(table,
   133  		[]string{cmd.UI.TranslateText("space quota:"), spaceSummary.SpaceQuotaName})
   134  	table = append(table,
   135  		[]string{cmd.UI.TranslateText("running security groups:"), strings.Join(spaceSummary.RunningSecurityGroupNames, ", ")})
   136  	table = append(table,
   137  		[]string{cmd.UI.TranslateText("staging security groups:"), strings.Join(spaceSummary.StagingSecurityGroupNames, ", ")})
   138  
   139  	cmd.UI.DisplayKeyValueTable("", table, 3)
   140  
   141  	if displaySecurityGroupRules {
   142  		table := [][]string{
   143  			{
   144  				cmd.UI.TranslateText(""),
   145  				cmd.UI.TranslateText("security group"),
   146  				cmd.UI.TranslateText("destination"),
   147  				cmd.UI.TranslateText("ports"),
   148  				cmd.UI.TranslateText("protocol"),
   149  				cmd.UI.TranslateText("lifecycle"),
   150  				cmd.UI.TranslateText("description"),
   151  			},
   152  		}
   153  
   154  		currentGroupIndex := -1
   155  		var currentGroupName string
   156  		for _, securityGroupRule := range spaceSummary.SecurityGroupRules {
   157  			var currentGroupIndexString string
   158  
   159  			if securityGroupRule.Name != currentGroupName {
   160  				currentGroupIndex++
   161  				currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
   162  				currentGroupName = securityGroupRule.Name
   163  			}
   164  
   165  			table = append(table, []string{
   166  				currentGroupIndexString,
   167  				securityGroupRule.Name,
   168  				securityGroupRule.Destination,
   169  				securityGroupRule.Ports,
   170  				securityGroupRule.Protocol,
   171  				string(securityGroupRule.Lifecycle),
   172  				securityGroupRule.Description,
   173  			})
   174  		}
   175  
   176  		cmd.UI.DisplayNewline()
   177  		cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   178  	}
   179  
   180  	return nil
   181  }
   182  
   183  func (cmd SpaceCommand) isolationSegmentRow(spaceSummary v2action.SpaceSummary) ([]string, error) {
   184  	if cmd.ActorV3 == nil {
   185  		return nil, nil
   186  	}
   187  
   188  	apiCheck := command.MinimumCCAPIVersionCheck(cmd.ActorV3.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3)
   189  	if apiCheck != nil {
   190  		return nil, nil
   191  	}
   192  
   193  	isolationSegmentName := ""
   194  	isolationSegment, v3Warnings, err := cmd.ActorV3.GetEffectiveIsolationSegmentBySpace(
   195  		spaceSummary.GUID, spaceSummary.OrgDefaultIsolationSegmentGUID)
   196  	cmd.UI.DisplayWarnings(v3Warnings)
   197  	if err == nil {
   198  		isolationSegmentName = isolationSegment.Name
   199  	} else {
   200  		if _, ok := err.(actionerror.NoRelationshipError); !ok {
   201  			return nil, err
   202  		}
   203  	}
   204  
   205  	return []string{cmd.UI.TranslateText("isolation segment:"), isolationSegmentName}, nil
   206  }