forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbind_running_security_group.go
84 lines (74 loc) · 2.94 KB
/
unbind_running_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
package securitygroup
import (
"strings"
"github.com/cloudfoundry/cli/cf/api/security_groups"
"github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running"
"github.com/cloudfoundry/cli/cf/command"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type unbindFromRunningGroup struct {
ui terminal.UI
configRepo core_config.Reader
securityGroupRepo security_groups.SecurityGroupRepo
runningGroupRepo running.RunningSecurityGroupsRepo
}
func NewUnbindFromRunningGroup(ui terminal.UI, configRepo core_config.Reader, securityGroupRepo security_groups.SecurityGroupRepo, runningGroupRepo running.RunningSecurityGroupsRepo) command.Command {
return &unbindFromRunningGroup{
ui: ui,
configRepo: configRepo,
securityGroupRepo: securityGroupRepo,
runningGroupRepo: runningGroupRepo,
}
}
func (cmd *unbindFromRunningGroup) Metadata() command_metadata.CommandMetadata {
primaryUsage := T("CF_NAME unbind-running-security-group SECURITY_GROUP")
tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
return command_metadata.CommandMetadata{
Name: "unbind-running-security-group",
Description: T("Unbind a security group from the set of security groups for running applications"),
Usage: strings.Join([]string{primaryUsage, tipUsage}, "\n\n"),
}
}
func (cmd *unbindFromRunningGroup) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
if len(context.Args()) != 1 {
cmd.ui.FailWithUsage(context)
}
return []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}, nil
}
func (cmd *unbindFromRunningGroup) Run(context *cli.Context) {
name := context.Args()[0]
securityGroup, err := cmd.securityGroupRepo.Read(name)
switch (err).(type) {
case nil:
case *errors.ModelNotFoundError:
cmd.ui.Ok()
cmd.ui.Warn(T("Security group {{.security_group}} {{.error_message}}",
map[string]interface{}{
"security_group": terminal.EntityNameColor(name),
"error_message": terminal.WarningColor(T("does not exist.")),
}))
return
default:
cmd.ui.Failed(err.Error())
}
cmd.ui.Say(T("Unbinding security group {{.security_group}} from defaults for running as {{.username}}",
map[string]interface{}{
"security_group": terminal.EntityNameColor(securityGroup.Name),
"username": terminal.EntityNameColor(cmd.configRepo.Username()),
}))
err = cmd.runningGroupRepo.UnbindFromRunningSet(securityGroup.Guid)
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."))
}