Skip to content

Commit

Permalink
fix: UpdateSecurityGroupRule can now remove DestPortFrom and DestPort…
Browse files Browse the repository at this point in the history
…To (#179)
  • Loading branch information
jerome-quere authored Sep 11, 2019
1 parent baafd7b commit 01104ee
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 78 deletions.
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

0 comments on commit 01104ee

Please sign in to comment.