github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/securitygroup/create_security_group.go (about) 1 package securitygroup 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/flags" 7 . "code.cloudfoundry.org/cli/cf/i18n" 8 9 "code.cloudfoundry.org/cli/cf/api/securitygroups" 10 "code.cloudfoundry.org/cli/cf/commandregistry" 11 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 12 "code.cloudfoundry.org/cli/cf/errors" 13 "code.cloudfoundry.org/cli/cf/requirements" 14 "code.cloudfoundry.org/cli/cf/terminal" 15 "code.cloudfoundry.org/cli/cf/util/json" 16 ) 17 18 type CreateSecurityGroup struct { 19 ui terminal.UI 20 securityGroupRepo securitygroups.SecurityGroupRepo 21 configRepo coreconfig.Reader 22 } 23 24 func init() { 25 commandregistry.Register(&CreateSecurityGroup{}) 26 } 27 28 func (cmd *CreateSecurityGroup) MetaData() commandregistry.CommandMetadata { 29 primaryUsage := T("CF_NAME create-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE") 30 secondaryUsage := T(` The provided path can be an absolute or relative path to a file. The file should have 31 a single array with JSON objects inside describing the rules. The JSON Base Object is 32 omitted and only the square brackets and associated child object are required in the file. 33 34 Valid json file example: 35 [ 36 { 37 "protocol": "tcp", 38 "destination": "10.244.1.18", 39 "ports": "3306" 40 } 41 ]`) 42 43 return commandregistry.CommandMetadata{ 44 Name: "create-security-group", 45 Description: T("Create a security group"), 46 Usage: []string{ 47 primaryUsage, 48 "\n\n", 49 secondaryUsage, 50 }, 51 } 52 } 53 54 func (cmd *CreateSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 55 if len(fc.Args()) != 2 { 56 cmd.ui.Failed(T("Incorrect Usage. Requires SECURITY_GROUP and PATH_TO_JSON_RULES_FILE as arguments\n\n") + commandregistry.Commands.CommandUsage("create-security-group")) 57 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 58 } 59 60 reqs := []requirements.Requirement{ 61 requirementsFactory.NewLoginRequirement(), 62 } 63 64 return reqs, nil 65 } 66 67 func (cmd *CreateSecurityGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 68 cmd.ui = deps.UI 69 cmd.configRepo = deps.Config 70 cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository() 71 return cmd 72 } 73 74 func (cmd *CreateSecurityGroup) Execute(context flags.FlagContext) error { 75 name := context.Args()[0] 76 pathToJSONFile := context.Args()[1] 77 rules, err := json.ParseJSONArray(pathToJSONFile) 78 if err != nil { 79 return errors.New(T(`Incorrect json format: file: {{.JSONFile}} 80 81 Valid json file example: 82 [ 83 { 84 "protocol": "tcp", 85 "destination": "10.244.1.18", 86 "ports": "3306" 87 } 88 ]`, map[string]interface{}{"JSONFile": pathToJSONFile})) 89 } 90 91 cmd.ui.Say(T("Creating security group {{.security_group}} as {{.username}}", 92 map[string]interface{}{ 93 "security_group": terminal.EntityNameColor(name), 94 "username": terminal.EntityNameColor(cmd.configRepo.Username()), 95 })) 96 97 err = cmd.securityGroupRepo.Create(name, rules) 98 99 httpErr, ok := err.(errors.HTTPError) 100 if ok && httpErr.ErrorCode() == errors.SecurityGroupNameTaken { 101 cmd.ui.Ok() 102 cmd.ui.Warn(T("Security group {{.security_group}} {{.error_message}}", 103 map[string]interface{}{ 104 "security_group": terminal.EntityNameColor(name), 105 "error_message": terminal.WarningColor(T("already exists")), 106 })) 107 return nil 108 } 109 110 if err != nil { 111 return err 112 } 113 114 cmd.ui.Ok() 115 return nil 116 }