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