Skip to content

Commit 30151c5

Browse files
committed
refactor(ufw):
- fixed update rules
1 parent 76fc58e commit 30151c5

2 files changed

Lines changed: 12 additions & 48 deletions

File tree

internal/cmd/ufw/rules/update/update.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ import (
2525
const (
2626
instanceIdArg = "INSTANCE_ID"
2727

28-
sourceIpFlag = "sourceIp"
29-
directionFlag = "direction"
30-
descriptionFlag = "description"
31-
etherTypeFlag = "etherType"
32-
portRangeFlag = "portRange"
33-
protocolFlag = "protocol"
28+
sourceIpFlag = "sourceIp"
3429
)
3530

3631
type inputModel struct {
@@ -107,11 +102,6 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
107102

108103
func configureFlags(cmd *cobra.Command) {
109104
cmd.Flags().StringP(sourceIpFlag, "s", "", "The IP (CIDR) to which the rule applies (e.g. 192.168.0.1/32)")
110-
cmd.Flags().StringP(directionFlag, "d", "", "Direction (the direction of the traffic, typically ingress or egress, for security rules type)")
111-
cmd.Flags().StringP(descriptionFlag, "D", "", "Description")
112-
cmd.Flags().StringP(etherTypeFlag, "e", "", "Specifies the bound of the rule (for security rules type)")
113-
cmd.Flags().StringP(portRangeFlag, "r", "", "Port range (the Port range to which the rule applies, for security rules type)")
114-
cmd.Flags().String(protocolFlag, "", "The network protocol (e.g. TCP, UDP, ICMP, for security rules type)")
115105

116106
err := flags.MarkFlagsRequired(cmd, sourceIpFlag)
117107
cobra.CheckErr(err)
@@ -133,12 +123,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
133123
GlobalFlagModel: globalFlags,
134124
InstanceId: instanceId,
135125

136-
SourceIp: flags.FlagToStringPointer(p, cmd, sourceIpFlag),
137-
Direction: flags.FlagToStringPointer(p, cmd, directionFlag),
138-
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
139-
EtherType: flags.FlagToStringPointer(p, cmd, etherTypeFlag),
140-
PortRange: flags.FlagToStringPointer(p, cmd, portRangeFlag),
141-
Protocol: flags.FlagToStringPointer(p, cmd, protocolFlag),
126+
SourceIp: flags.FlagToStringPointer(p, cmd, sourceIpFlag),
142127
}
143128

144129
p.DebugInputModel(model)
@@ -148,14 +133,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
148133
func buildRequest(ctx context.Context, model *inputModel, apiClient *ufw.APIClient) ufw.ApiUpdateRuleRequest {
149134
req := apiClient.DefaultAPI.UpdateRule(ctx, model.ProjectId, model.Region, model.InstanceId)
150135

151-
// TODO - add logic for field checking: existing ACLs, correct product, type, instanceID maybe
152-
153136
req = req.UpdateRulePayload(ufw.UpdateRulePayload{
154-
SourceIP: *model.SourceIp,
155-
Direction: model.Direction,
156-
EtherType: model.EtherType,
157-
PortRange: model.PortRange,
158-
Protocol: model.Protocol,
137+
SourceIP: *model.SourceIp,
159138
})
160139

161140
return req

internal/cmd/ufw/rules/update/update_test.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
4343
globalflags.ProjectIdFlag: testProjectId,
4444
globalflags.RegionFlag: testRegion,
4545
sourceIpFlag: testSourceIp,
46-
directionFlag: "ingress",
47-
descriptionFlag: "example-description",
48-
etherTypeFlag: "IPv4",
49-
portRangeFlag: "80-443",
50-
protocolFlag: "TCP",
5146
}
5247
for _, mod := range mods {
5348
mod(flagValues)
@@ -62,13 +57,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
6257
Region: testRegion,
6358
Verbosity: globalflags.VerbosityDefault,
6459
},
65-
RuleRefId: "", // Left blank because parseInput in the source file currently does not populate it
66-
SourceIp: new(testSourceIp),
67-
Direction: new("ingress"),
68-
Description: new("example-description"),
69-
EtherType: new("IPv4"),
70-
PortRange: new("80-443"),
71-
Protocol: new("TCP"),
60+
InstanceId: testRuleRefId,
61+
SourceIp: new(testSourceIp),
7262
}
7363
for _, mod := range mods {
7464
mod(model)
@@ -79,11 +69,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
7969
func fixtureRequest(mods ...func(request *ufw.ApiUpdateRuleRequest)) ufw.ApiUpdateRuleRequest {
8070
request := testClient.DefaultAPI.UpdateRule(testCtx, testProjectId, testRegion, testRuleRefId)
8171
request = request.UpdateRulePayload(ufw.UpdateRulePayload{
82-
SourceIP: testSourceIp,
83-
Direction: new("ingress"),
84-
EtherType: new("IPv4"),
85-
PortRange: new("80-443"),
86-
Protocol: new("TCP"),
72+
SourceIP: testSourceIp,
8773
})
8874
for _, mod := range mods {
8975
mod(&request)
@@ -133,7 +119,8 @@ func TestParseInput(t *testing.T) {
133119
Region: testRegion,
134120
Verbosity: globalflags.VerbosityDefault,
135121
},
136-
SourceIp: new(testSourceIp),
122+
InstanceId: testRuleRefId,
123+
SourceIp: new(testSourceIp),
137124
},
138125
},
139126
{
@@ -192,10 +179,8 @@ func TestBuildRequest(t *testing.T) {
192179
expectedRequest ufw.ApiUpdateRuleRequest
193180
}{
194181
{
195-
description: "base",
196-
model: fixtureInputModel(func(model *inputModel) {
197-
model.RuleRefId = testRuleRefId // Inject the ID that parseInput currently skips
198-
}),
182+
description: "base",
183+
model: fixtureInputModel(),
199184
expectedRequest: fixtureRequest(),
200185
},
201186
{
@@ -206,8 +191,8 @@ func TestBuildRequest(t *testing.T) {
206191
Region: testRegion,
207192
Verbosity: globalflags.VerbosityDefault,
208193
},
209-
RuleRefId: testRuleRefId,
210-
SourceIp: new(testSourceIp),
194+
InstanceId: testRuleRefId,
195+
SourceIp: new(testSourceIp),
211196
},
212197
expectedRequest: testClient.DefaultAPI.UpdateRule(testCtx, testProjectId, testRegion, testRuleRefId).
213198
UpdateRulePayload(ufw.UpdateRulePayload{

0 commit comments

Comments
 (0)