forked from cloudfoundry/cli
/
create_security_group_command.go
53 lines (42 loc) · 2.05 KB
/
create_security_group_command.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
package v7
import (
"encoding/json"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/command/flag"
)
type CreateSecurityGroupCommand struct {
BaseCommand
RequiredArgs flag.SecurityGroupArgs `positional-args:"yes"`
usage interface{} `usage:"CF_NAME create-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE\n\n The provided path can be an absolute or relative path to a file. The file should have\n a single array with JSON objects inside describing the rules. The JSON Base Object is\n omitted and only the square brackets and associated child object are required in the file.\n\n Valid json file example:\n [\n {\n \"protocol\": \"tcp\",\n \"destination\": \"10.0.11.0/24\",\n \"ports\": \"80,443\",\n \"description\": \"Allow http and https traffic from ZoneA\"\n }\n ]"`
relatedCommands interface{} `related_commands:"bind-running-security-group, bind-security-group, bind-staging-security-group, security-groups"`
}
func (cmd CreateSecurityGroupCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
user, err := cmd.Actor.GetCurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Creating security group {{.Name}} as {{.Username}}...", map[string]interface{}{
"Name": cmd.RequiredArgs.SecurityGroup,
"Username": user.Name,
})
cmd.UI.DisplayNewline()
warnings, err := cmd.Actor.CreateSecurityGroup(cmd.RequiredArgs.SecurityGroup, string(cmd.RequiredArgs.PathToJSONRules))
cmd.UI.DisplayWarnings(warnings)
_, isSyntaxErr := err.(*json.SyntaxError)
_, isUnmarshalErr := err.(*json.UnmarshalTypeError)
if isSyntaxErr || isUnmarshalErr {
return actionerror.SecurityGroupJsonSyntaxError{Path: string(cmd.RequiredArgs.PathToJSONRules)}
}
if _, ok := err.(ccerror.SecurityGroupAlreadyExists); ok {
cmd.UI.DisplayWarning(err.Error())
} else if err != nil {
return err
}
cmd.UI.DisplayOK()
return nil
}