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