github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/x_security-groups.go (about) 1 // Copyright (C) 2015 Scaleway. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE.md file. 4 5 package cli 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "io" 11 12 "github.com/scaleway/scaleway-cli/pkg/api" 13 ) 14 15 var cmdSecurityGroups = &Command{ 16 Exec: runSecurityGroups, 17 UsageLine: "_security-groups OPTIONS [ARGS]", 18 Description: "Interacts with security-groups", 19 Hidden: true, 20 Help: "Interacts with security-groups", 21 Examples: ` 22 23 SGID = SecurityGroupID 24 ACTION = "{\"action\":"string", \"direction\": \"string\", \"ip_range\": \"string\", \"protocol\": \"string\", \"dest_port_from\": \"int\"}" 25 26 $ scw _security-groups list-groups 27 $ scw _security-groups show-group SGID 28 $ scw _security-groups new-group --name=NAME --desc=DESC 29 $ scw _security-groups update-group SGID --name=NAME --desc=DESC 30 $ scw _security-groups delete-group SGID 31 32 $ scw _security-groups list-rules SGID 33 $ scw _security-groups show-rule SGID RULEID 34 $ scw _security-gruops delete-rule SGID RULEID 35 $ scw _security-groups new-rule SGID ACTION 36 $ scw _security-gruops update-rule SGID RULEID ACTION`, 37 } 38 39 // "show-rule" 40 41 func init() { 42 cmdSecurityGroups.Flag.BoolVar(&securityGroupsHelp, []string{"h", "-help"}, false, "Print usage") 43 cmdSecurityGroups.Flag.StringVar(&securityGroupsName, []string{"n", "-name"}, "", "SecurityGroup's name") 44 cmdSecurityGroups.Flag.StringVar(&securityGroupsDesc, []string{"d", "-description"}, "", "SecurityGroup's description") 45 subCmdSecurityGroup = map[string]func(cmd *Command, args []string) error{ 46 "list-groups": listSecurityGroup, 47 "new-group": newSecurityGroup, 48 "update-group": updateSecurityGroup, 49 "delete-group": deleteSecurityGroup, 50 "show-group": showSecurityGroup, 51 "list-rules": listSecurityGroupRule, 52 "new-rule": newSecurityGroupRule, 53 "update-rule": updateSecurityGroupRule, 54 "delete-rule": deleteSecurityGroupRule, 55 "show-rule": showSecurityGroupRule, 56 } 57 } 58 59 // Flags 60 var securityGroupsHelp bool // -h, --help flag 61 var securityGroupsName string // -n, --name flag 62 var securityGroupsDesc string // -d, --description flag 63 64 // SubCommand 65 var subCmdSecurityGroup map[string]func(cmd *Command, args []string) error 66 67 type rulesDefinition struct { 68 Action string `json:"action"` 69 Direction string `json:"direction"` 70 IPRange string `json:"ip_range"` 71 Protocol string `json:"protocol"` 72 DestPortFrom *int `json:"dest_port_from,omitempty"` 73 } 74 75 func printRawMode(out io.Writer, data interface{}) error { 76 js, err := json.MarshalIndent(data, "", " ") 77 if err != nil { 78 return fmt.Errorf("Unable to parse the data: %v", err) 79 } 80 fmt.Fprintf(out, "%s\n", string(js)) 81 return nil 82 } 83 84 func newSecurityGroup(cmd *Command, args []string) error { 85 fmt.Println(securityGroupsDesc) 86 fmt.Println(securityGroupsName) 87 if securityGroupsName == "" || securityGroupsDesc == "" { 88 return cmd.PrintShortUsage() 89 } 90 return cmd.API.PostSecurityGroup(api.ScalewayNewSecurityGroup{ 91 Organization: cmd.API.Organization, 92 Name: securityGroupsName, 93 Description: securityGroupsDesc, 94 }) 95 } 96 97 func updateSecurityGroup(cmd *Command, args []string) error { 98 fmt.Println(args) 99 if securityGroupsName == "" || securityGroupsDesc == "" || len(args) != 1 { 100 return cmd.PrintShortUsage() 101 } 102 return cmd.API.PutSecurityGroup(api.ScalewayUpdateSecurityGroup{ 103 Organization: cmd.API.Organization, 104 Name: securityGroupsName, 105 Description: securityGroupsDesc, 106 }, args[0]) 107 } 108 109 func deleteSecurityGroup(cmd *Command, args []string) error { 110 if len(args) != 1 { 111 return cmd.PrintShortUsage() 112 } 113 return cmd.API.DeleteSecurityGroup(args[0]) 114 } 115 116 func showSecurityGroup(cmd *Command, args []string) error { 117 if len(args) != 1 { 118 return cmd.PrintShortUsage() 119 } 120 securityGroups, err := cmd.API.GetASecurityGroup(args[0]) 121 if err != nil { 122 return err 123 } 124 return printRawMode(cmd.Streams().Stdout, *securityGroups) 125 } 126 127 func listSecurityGroup(cmd *Command, args []string) error { 128 securityGroups, err := cmd.API.GetSecurityGroups() 129 if err != nil { 130 return err 131 } 132 return printRawMode(cmd.Streams().Stdout, *securityGroups) 133 } 134 135 func listSecurityGroupRule(cmd *Command, args []string) error { 136 if len(args) != 1 { 137 return cmd.PrintShortUsage() 138 } 139 GetSecurityGroupRules, err := cmd.API.GetSecurityGroupRules(args[0]) 140 if err != nil { 141 return err 142 } 143 return printRawMode(cmd.Streams().Stdout, *GetSecurityGroupRules) 144 } 145 146 func newSecurityGroupRule(cmd *Command, args []string) error { 147 var rule rulesDefinition 148 var content api.ScalewayNewSecurityGroupRule 149 150 if len(args) != 2 { 151 return cmd.PrintShortUsage() 152 } 153 if err := json.Unmarshal([]byte(args[1]), &rule); err != nil { 154 return err 155 } 156 content.Action = rule.Action 157 content.Direction = rule.Direction 158 content.IPRange = rule.IPRange 159 content.Protocol = rule.Protocol 160 if rule.DestPortFrom != nil { 161 content.DestPortFrom = *rule.DestPortFrom 162 } 163 return cmd.API.PostSecurityGroupRule(args[0], content) 164 } 165 166 func updateSecurityGroupRule(cmd *Command, args []string) error { 167 var rule rulesDefinition 168 var content api.ScalewayNewSecurityGroupRule 169 170 if len(args) != 3 { 171 return cmd.PrintShortUsage() 172 } 173 if err := json.Unmarshal([]byte(args[2]), &rule); err != nil { 174 return err 175 } 176 content.Action = rule.Action 177 content.Direction = rule.Direction 178 content.IPRange = rule.IPRange 179 content.Protocol = rule.Protocol 180 if rule.DestPortFrom != nil { 181 content.DestPortFrom = *rule.DestPortFrom 182 } 183 return cmd.API.PutSecurityGroupRule(content, args[0], args[1]) 184 } 185 186 func showSecurityGroupRule(cmd *Command, args []string) error { 187 if len(args) != 2 { 188 return cmd.PrintShortUsage() 189 } 190 GroupRuleID, err := cmd.API.GetASecurityGroupRule(args[0], args[1]) 191 if err != nil { 192 return err 193 } 194 return printRawMode(cmd.Streams().Stdout, *GroupRuleID) 195 } 196 197 func deleteSecurityGroupRule(cmd *Command, args []string) error { 198 if len(args) != 2 { 199 return cmd.PrintShortUsage() 200 } 201 return cmd.API.DeleteSecurityGroupRule(args[0], args[1]) 202 } 203 204 func runSecurityGroups(cmd *Command, args []string) error { 205 if securityGroupsHelp || len(args) == 0 { 206 return cmd.PrintUsage() 207 } 208 cmd.Flag.Parse(args[1:]) 209 if function, ok := subCmdSecurityGroup[args[0]]; ok { 210 return function(cmd, cmd.Flag.Args()) 211 } 212 return fmt.Errorf("subcommand not found: %s", args[0]) 213 }