github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/security_groups_command.go (about) 1 package v2 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 "code.cloudfoundry.org/cli/command/v2/shared" 12 "code.cloudfoundry.org/cli/util/ui" 13 ) 14 15 //go:generate counterfeiter . SecurityGroupsActor 16 17 type SecurityGroupsActor interface { 18 CloudControllerAPIVersion() string 19 GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging bool) ([]v2action.SecurityGroupWithOrganizationSpaceAndLifecycle, v2action.Warnings, error) 20 } 21 22 type SecurityGroupsCommand struct { 23 usage interface{} `usage:"CF_NAME security-groups"` 24 relatedCommands interface{} `related_commands:"bind-security-group, bind-running-security-group, bind-staging-security-group, security-group"` 25 26 SharedActor command.SharedActor 27 Config command.Config 28 UI command.UI 29 Actor SecurityGroupsActor 30 } 31 32 func (cmd *SecurityGroupsCommand) Setup(config command.Config, ui command.UI) error { 33 cmd.UI = ui 34 cmd.Config = config 35 cmd.SharedActor = sharedaction.NewActor(config) 36 37 ccClient, uaaClient, err := shared.NewClients(config, ui, true) 38 if err != nil { 39 return err 40 } 41 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 42 43 return nil 44 } 45 46 func (cmd SecurityGroupsCommand) Execute(args []string) error { 47 user, err := cmd.Config.CurrentUser() 48 if err != nil { 49 return err 50 } 51 52 err = cmd.SharedActor.CheckTarget(false, false) 53 if err != nil { 54 return err 55 } 56 57 includeStaging := true 58 59 err = command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionLifecyleStagingV2) 60 if err != nil { 61 switch err.(type) { 62 case translatableerror.MinimumAPIVersionNotMetError: 63 includeStaging = false 64 65 default: 66 return err 67 } 68 } 69 70 cmd.UI.DisplayTextWithFlavor("Getting security groups as {{.UserName}}...", 71 map[string]interface{}{"UserName": user.Name}) 72 73 secGroupOrgSpaces, warnings, err := cmd.Actor.GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging) 74 cmd.UI.DisplayWarnings(warnings) 75 if err != nil { 76 return err 77 } 78 79 cmd.UI.DisplayOK() 80 cmd.UI.DisplayNewline() 81 82 table := [][]string{ 83 { 84 cmd.UI.TranslateText(""), 85 cmd.UI.TranslateText("name"), 86 cmd.UI.TranslateText("organization"), 87 cmd.UI.TranslateText("space"), 88 cmd.UI.TranslateText("lifecycle"), 89 }, 90 } 91 92 currentGroupIndex := -1 93 var currentGroupName string 94 for _, secGroupOrgSpace := range secGroupOrgSpaces { 95 var currentGroupIndexString string 96 97 if secGroupOrgSpace.SecurityGroup.Name != currentGroupName { 98 currentGroupIndex++ 99 currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex) 100 currentGroupName = secGroupOrgSpace.SecurityGroup.Name 101 } 102 103 switch { 104 case secGroupOrgSpace.Organization.Name == "" && secGroupOrgSpace.Space.Name == "" && 105 (secGroupOrgSpace.SecurityGroup.RunningDefault || 106 secGroupOrgSpace.SecurityGroup.StagingDefault): 107 table = append(table, []string{ 108 currentGroupIndexString, 109 secGroupOrgSpace.SecurityGroup.Name, 110 cmd.UI.TranslateText("<all>"), 111 cmd.UI.TranslateText("<all>"), 112 string(secGroupOrgSpace.Lifecycle), 113 }) 114 default: 115 table = append(table, []string{ 116 currentGroupIndexString, 117 secGroupOrgSpace.SecurityGroup.Name, 118 secGroupOrgSpace.Organization.Name, 119 secGroupOrgSpace.Space.Name, 120 string(secGroupOrgSpace.Lifecycle), 121 }) 122 } 123 } 124 125 cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding) 126 127 return nil 128 }