Skip to content

Commit 91dc446

Browse files
committed
feat(server): make --family optional to allow editing default firewall rules
1 parent d0faf5b commit 91dc446

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Add `--show-ip-addresses` flag to `server list` command to optionally include IP addresses in command output.
1111

12+
### Changed
13+
- Make `--family` parameter of `server firewall create` command optional to allow editing the default rules.
14+
1215
### Fixed
1316
- Complete shell input with uppercase letters (e.g., `Cap` to `CapitalizedName` will now work)
1417
- Display UUID of created template in `storage templatise` output.

internal/commands/serverfirewall/create.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func CreateCommand() commands.Command {
3838
BaseCommand: commands.New(
3939
"create",
4040
"Create a new firewall rule",
41+
"upctl server firewall create 00038afc-d526-4148-af0e-d2f1eeaded9b --direction in --action drop",
4142
"upctl server firewall create 00038afc-d526-4148-af0e-d2f1eeaded9b --direction in --action accept --family IPv4",
4243
"upctl server firewall create 00038afc-d526-4148-af0e-d2f1eeaded9b --direction in --action drop --family IPv4 --src-ipaddress-block 10.11.0.88/24",
4344
),
@@ -51,6 +52,13 @@ func (s *createCommand) MaximumExecutions() int {
5152

5253
// InitCommand implements Command.InitCommand
5354
func (s *createCommand) InitCommand() {
55+
s.Cobra().Long = `Create a new firewall rule
56+
57+
To edit the default rule of the firewall, set only --direction and --action
58+
parameters. This creates catch-all rule that will take effect when no other rule
59+
matches. Note that the default rule must be positioned after all other rules.
60+
Use --position parameter or create default rule after other rules.`
61+
5462
flagSet := &pflag.FlagSet{}
5563

5664
flagSet.StringVar(&s.direction, "direction", "", "Rule direction. Available: in / out")
@@ -80,11 +88,7 @@ func (s *createCommand) Execute(exec commands.Executor, arg string) (output.Outp
8088
return nil, fmt.Errorf("action is required")
8189
}
8290

83-
if s.family == "" {
84-
return nil, fmt.Errorf("family (IPv4/IPv6) is required")
85-
}
86-
87-
if s.family != "IPv4" && s.family != "IPv6" {
91+
if s.family != "" && s.family != "IPv4" && s.family != "IPv6" {
8892
return nil, fmt.Errorf("invalid family, use either IPv4 or IPv6")
8993
}
9094

@@ -133,6 +137,7 @@ func (s *createCommand) Execute(exec commands.Executor, arg string) (output.Outp
133137
msg := fmt.Sprintf("creating firewall rule for server %v", arg)
134138
logline := exec.NewLogEntry(msg)
135139
logline.StartedNow()
140+
136141
res, err := exec.Firewall().CreateFirewallRule(&request.CreateFirewallRuleRequest{
137142
ServerUUID: arg,
138143
FirewallRule: upcloud.FirewallRule{

internal/commands/serverfirewall/create_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ func TestCreateFirewallRuleCommand(t *testing.T) {
5252
error: "action is required",
5353
},
5454
{
55-
name: "Family is required",
55+
name: "FirewallRule, drop incoming by default",
5656
flags: []string{
5757
Server1.UUID,
5858
"--direction", "in",
59-
"--action", "accept",
59+
"--action", "drop",
60+
},
61+
arg: Server1.UUID,
62+
expectedReq: &request.CreateFirewallRuleRequest{
63+
FirewallRule: upcloud.FirewallRule{
64+
Direction: "in",
65+
Action: "drop",
66+
},
67+
ServerUUID: Server1.UUID,
6068
},
61-
arg: Server1.UUID,
62-
error: "family (IPv4/IPv6) is required",
6369
},
6470
{
6571
name: "FirewallRule, accept incoming IPv6",

0 commit comments

Comments
 (0)