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