forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbind_security_group.go
118 lines (99 loc) · 4.02 KB
/
unbind_security_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package securitygroup
import (
"strings"
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/security_groups"
sgbinder "github.com/cloudfoundry/cli/cf/api/security_groups/spaces"
"github.com/cloudfoundry/cli/cf/api/spaces"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
)
type UnbindSecurityGroup struct {
ui terminal.UI
configRepo core_config.Reader
securityGroupRepo security_groups.SecurityGroupRepo
orgRepo organizations.OrganizationRepository
spaceRepo spaces.SpaceRepository
secBinder sgbinder.SecurityGroupSpaceBinder
}
func init() {
command_registry.Register(&UnbindSecurityGroup{})
}
func (cmd *UnbindSecurityGroup) MetaData() command_registry.CommandMetadata {
primaryUsage := T("CF_NAME unbind-security-group SECURITY_GROUP ORG SPACE")
tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
return command_registry.CommandMetadata{
Name: "unbind-security-group",
Description: T("Unbind a security group from a space"),
Usage: strings.Join([]string{primaryUsage, tipUsage}, "\n\n"),
}
}
func (cmd *UnbindSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
argLength := len(fc.Args())
if argLength == 0 || argLength == 2 || argLength >= 4 {
cmd.ui.Failed(T("Incorrect Usage. Requires SECURITY_GROUP, ORG and SPACE as arguments\n\n") + command_registry.Commands.CommandUsage("unbind-security-group"))
}
requirements := []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
return requirements, nil
}
func (cmd *UnbindSecurityGroup) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.configRepo = deps.Config
cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
cmd.secBinder = deps.RepoLocator.GetSecurityGroupSpaceBinder()
return cmd
}
func (cmd *UnbindSecurityGroup) Execute(context flags.FlagContext) {
var spaceGuid string
secName := context.Args()[0]
if len(context.Args()) == 1 {
spaceGuid = cmd.configRepo.SpaceFields().Guid
spaceName := cmd.configRepo.SpaceFields().Name
orgName := cmd.configRepo.OrganizationFields().Name
cmd.flavorText(secName, orgName, spaceName)
} else {
orgName := context.Args()[1]
spaceName := context.Args()[2]
cmd.flavorText(secName, orgName, spaceName)
spaceGuid = cmd.lookupSpaceGuid(orgName, spaceName)
}
securityGroup, err := cmd.securityGroupRepo.Read(secName)
if err != nil {
cmd.ui.Failed(err.Error())
}
secGuid := securityGroup.Guid
err = cmd.secBinder.UnbindSpace(secGuid, spaceGuid)
if err != nil {
cmd.ui.Failed(err.Error())
}
cmd.ui.Ok()
cmd.ui.Say("\n\n")
cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
}
func (cmd UnbindSecurityGroup) flavorText(secName string, orgName string, spaceName string) {
cmd.ui.Say(T("Unbinding security group {{.security_group}} from {{.organization}}/{{.space}} as {{.username}}",
map[string]interface{}{
"security_group": terminal.EntityNameColor(secName),
"organization": terminal.EntityNameColor(orgName),
"space": terminal.EntityNameColor(spaceName),
"username": terminal.EntityNameColor(cmd.configRepo.Username()),
}))
}
func (cmd UnbindSecurityGroup) lookupSpaceGuid(orgName string, spaceName string) string {
organization, err := cmd.orgRepo.FindByName(orgName)
if err != nil {
cmd.ui.Failed(err.Error())
}
orgGuid := organization.Guid
space, err := cmd.spaceRepo.FindByNameInOrg(spaceName, orgGuid)
if err != nil {
cmd.ui.Failed(err.Error())
}
return space.Guid
}