github.com/loafoe/cli@v7.1.0+incompatible/command/v7/create_security_group_command.go (about) 1 package v7 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 "code.cloudfoundry.org/cli/command/flag" 9 ) 10 11 type CreateSecurityGroupCommand struct { 12 BaseCommand 13 14 RequiredArgs flag.SecurityGroupArgs `positional-args:"yes"` 15 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 ]"` 16 relatedCommands interface{} `related_commands:"bind-running-security-group, bind-security-group, bind-staging-security-group, security-groups"` 17 } 18 19 func (cmd CreateSecurityGroupCommand) Execute(args []string) error { 20 err := cmd.SharedActor.CheckTarget(false, false) 21 if err != nil { 22 return err 23 } 24 25 user, err := cmd.Config.CurrentUser() 26 if err != nil { 27 return err 28 } 29 30 cmd.UI.DisplayTextWithFlavor("Creating security group {{.Name}} as {{.Username}}...", map[string]interface{}{ 31 "Name": cmd.RequiredArgs.SecurityGroup, 32 "Username": user.Name, 33 }) 34 cmd.UI.DisplayNewline() 35 36 warnings, err := cmd.Actor.CreateSecurityGroup(cmd.RequiredArgs.SecurityGroup, string(cmd.RequiredArgs.PathToJSONRules)) 37 cmd.UI.DisplayWarnings(warnings) 38 39 _, isSyntaxErr := err.(*json.SyntaxError) 40 _, isUnmarshalErr := err.(*json.UnmarshalTypeError) 41 if isSyntaxErr || isUnmarshalErr { 42 return actionerror.SecurityGroupJsonSyntaxError{Path: string(cmd.RequiredArgs.PathToJSONRules)} 43 } 44 45 if _, ok := err.(ccerror.SecurityGroupAlreadyExists); ok { 46 cmd.UI.DisplayWarning(err.Error()) 47 } else if err != nil { 48 return err 49 } 50 51 cmd.UI.DisplayOK() 52 return nil 53 }