Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UpdateSecurityGroupRule can now remove DestPortFrom and DestPortTo #179

Merged
merged 4 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions api/instance/v1/security_group_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ type UpdateSecurityGroupRuleRequest struct {
SecurityGroupID string `json:"-"`
SecurityGroupRuleID string `json:"-"`

Protocol *SecurityGroupRuleProtocol `json:"protocol,omitempty"`
Direction *SecurityGroupRuleDirection `json:"direction,omitempty"`
Action *SecurityGroupRuleAction `json:"action,omitempty"`
IPRange *string `json:"ip_range,omitempty"`
DestPortFrom *uint32 `json:"dest_port_from,omitempty"`
DestPortTo *uint32 `json:"dest_port_to,omitempty"`
Position *uint32 `json:"position,omitempty"`
Protocol *SecurityGroupRuleProtocol `json:"protocol"`
Direction *SecurityGroupRuleDirection `json:"direction"`
Action *SecurityGroupRuleAction `json:"action"`
IPRange *string `json:"ip_range"`
Position *uint32 `json:"position"`

// If set to 0, DestPortFrom will be removed.
// See SecurityGroupRule.DestPortFrom for more information
DestPortFrom *uint32 `json:"dest_port_from"`

// If set to 0, DestPortTo will be removed.
// See SecurityGroupRule.DestPortTo for more information
DestPortTo *uint32 `json:"dest_port_to"`
}

type UpdateSecurityGroupRuleResponse struct {
Expand Down Expand Up @@ -155,10 +161,18 @@ func (s *API) UpdateSecurityGroupRule(req *UpdateSecurityGroupRuleRequest, opts
setRequest.IPRange = *req.IPRange
}
if req.DestPortTo != nil {
setRequest.DestPortTo = req.DestPortTo
if *req.DestPortTo > 0 {
setRequest.DestPortTo = req.DestPortTo
} else {
setRequest.DestPortTo = nil
}
}
if req.DestPortFrom != nil {
setRequest.DestPortFrom = req.DestPortFrom
if *req.DestPortFrom > 0 {
setRequest.DestPortFrom = req.DestPortFrom
} else {
setRequest.DestPortFrom = nil
}
}
if req.DestPortFrom != nil && req.DestPortTo != nil && *req.DestPortFrom == *req.DestPortTo {
setRequest.DestPortTo = nil
Expand Down
21 changes: 21 additions & 0 deletions api/instance/v1/security_group_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,25 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
testhelpers.Equals(t, SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction)
})

t.Run("Remove ports", func(t *testing.T) {
group, rule, cleanUp := bootstrap(t)
defer cleanUp()

updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{
Zone: zone,
SecurityGroupID: group.ID,
SecurityGroupRuleID: rule.ID,
DestPortFrom: scw.Uint32Ptr(0),
DestPortTo: scw.Uint32Ptr(0),
})

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action)
testhelpers.Equals(t, "8.8.8.8", updateResponse.Rule.IPRange)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
testhelpers.Equals(t, SecurityGroupRuleProtocolTCP, updateResponse.Rule.Protocol)
testhelpers.Equals(t, SecurityGroupRuleDirectionInbound, updateResponse.Rule.Direction)
})

}
Loading