forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbind_security_group.go
109 lines (92 loc) · 3.53 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
package securitygroup
import (
"github.com/cloudfoundry/cli/cf/api"
"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_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type UnbindSecurityGroup struct {
ui terminal.UI
configRepo configuration.Reader
securityGroupRepo security_groups.SecurityGroupRepo
orgRepo api.OrganizationRepository
spaceRepo spaces.SpaceRepository
secBinder sgbinder.SecurityGroupSpaceBinder
}
func NewUnbindSecurityGroup(ui terminal.UI, configRepo configuration.Reader, securityGroupRepo security_groups.SecurityGroupRepo, orgRepo api.OrganizationRepository, spaceRepo spaces.SpaceRepository, secBinder sgbinder.SecurityGroupSpaceBinder) UnbindSecurityGroup {
return UnbindSecurityGroup{
ui: ui,
configRepo: configRepo,
securityGroupRepo: securityGroupRepo,
orgRepo: orgRepo,
spaceRepo: spaceRepo,
secBinder: secBinder,
}
}
func (cmd UnbindSecurityGroup) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "unbind-security-group",
Description: T("Unbind a security group from a space"),
Usage: T("CF_NAME unbind-security-group SECURITY_GROUP ORG SPACE"),
}
}
func (cmd UnbindSecurityGroup) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
argLength := len(context.Args())
if argLength == 0 || argLength == 2 || argLength >= 4 {
cmd.ui.FailWithUsage(context)
}
requirements := []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
return requirements, nil
}
func (cmd UnbindSecurityGroup) Run(context *cli.Context) {
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()
}
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
}