Skip to content

Commit bb3676b

Browse files
authored
feat(lbaas) inverse rule matcher (#236)
1 parent 999a465 commit bb3676b

7 files changed

Lines changed: 132 additions & 644 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Added
9+
- Managed Load Balancer inverse rule matcher
10+
811
## [6.3.0]
912

1013
### Added

upcloud/load_balancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ type LoadBalancerMatcher struct {
256256
Header *LoadBalancerMatcherStringWithArgument `json:"match_header,omitempty"`
257257
URLParam *LoadBalancerMatcherStringWithArgument `json:"match_url_param,omitempty"`
258258
NumMembersUp *LoadBalancerMatcherNumMembersUp `json:"match_num_members_up,omitempty"`
259+
Inverse *bool `json:"inverse,omitempty"`
259260
}
260261

261262
// LoadBalancerMatcherStringWithArgument represents 'string with argument' matcher

upcloud/load_balancer_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func TestMarshalLoadBalancer(t *testing.T) {
11+
t.Parallel()
1112
lbString := `
1213
[
1314
{
@@ -247,6 +248,7 @@ func TestMarshalLoadBalancer(t *testing.T) {
247248
}
248249

249250
func TestLoadBalancerPlan(t *testing.T) {
251+
t.Parallel()
250252
testJSON(t,
251253
&LoadBalancerPlan{},
252254
&LoadBalancerPlan{
@@ -265,6 +267,7 @@ func TestLoadBalancerPlan(t *testing.T) {
265267
}
266268

267269
func TestLoadBalancerFrontend(t *testing.T) {
270+
t.Parallel()
268271
testJSON(t,
269272
&LoadBalancerFrontend{},
270273
&LoadBalancerFrontend{
@@ -325,6 +328,7 @@ func TestLoadBalancerFrontend(t *testing.T) {
325328
}
326329

327330
func TestLoadBalancerFrontendProperties(t *testing.T) {
331+
t.Parallel()
328332
testJSON(t,
329333
&LoadBalancerFrontendProperties{},
330334
&LoadBalancerFrontendProperties{
@@ -366,6 +370,7 @@ func TestLoadBalancerFrontendProperties(t *testing.T) {
366370
}
367371

368372
func TestLoadBalancerRule(t *testing.T) {
373+
t.Parallel()
369374
testJSON(t,
370375
&LoadBalancerFrontendRule{},
371376
&LoadBalancerFrontendRule{
@@ -446,6 +451,7 @@ func TestLoadBalancerRule(t *testing.T) {
446451
}
447452

448453
func TestLoadBalancerFrontendTLSConfig(t *testing.T) {
454+
t.Parallel()
449455
testJSON(t,
450456
&LoadBalancerFrontendTLSConfig{},
451457
&LoadBalancerFrontendTLSConfig{
@@ -466,6 +472,7 @@ func TestLoadBalancerFrontendTLSConfig(t *testing.T) {
466472
}
467473

468474
func TestLoadBalancerBackend(t *testing.T) {
475+
t.Parallel()
469476
testJSON(t,
470477
&LoadBalancerBackend{},
471478
&LoadBalancerBackend{
@@ -494,6 +501,7 @@ func TestLoadBalancerBackend(t *testing.T) {
494501
}
495502

496503
func TestLoadBalancerBackendProperties(t *testing.T) {
504+
t.Parallel()
497505
testJSON(t,
498506
&LoadBalancerBackendProperties{},
499507
&LoadBalancerBackendProperties{
@@ -531,6 +539,7 @@ func TestLoadBalancerBackendProperties(t *testing.T) {
531539
}
532540

533541
func TestLoadBalancerBackendMember(t *testing.T) {
542+
t.Parallel()
534543
members := []LoadBalancerBackendMember{
535544
{
536545
Name: "member.example.com",
@@ -588,6 +597,7 @@ func TestLoadBalancerBackendMember(t *testing.T) {
588597
}
589598

590599
func TestLoadBalancerResolver(t *testing.T) {
600+
t.Parallel()
591601
testJSON(t,
592602
&LoadBalancerResolver{},
593603
&LoadBalancerResolver{
@@ -620,6 +630,7 @@ func TestLoadBalancerResolver(t *testing.T) {
620630
}
621631

622632
func TestLoadBalancerMatcherStringWithArgument(t *testing.T) {
633+
t.Parallel()
623634
tv := true
624635
testJSON(t,
625636
&LoadBalancerMatcher{},
@@ -647,6 +658,7 @@ func TestLoadBalancerMatcherStringWithArgument(t *testing.T) {
647658
}
648659

649660
func TestLoadBalancerMatcherHost(t *testing.T) {
661+
t.Parallel()
650662
testJSON(t,
651663
&LoadBalancerMatcher{},
652664
&LoadBalancerMatcher{
@@ -667,6 +679,7 @@ func TestLoadBalancerMatcherHost(t *testing.T) {
667679
}
668680

669681
func TestLoadBalancerMatcherNumMembersUp(t *testing.T) {
682+
t.Parallel()
670683
testJSON(t,
671684
&LoadBalancerMatcher{},
672685
&LoadBalancerMatcher{
@@ -691,6 +704,7 @@ func TestLoadBalancerMatcherNumMembersUp(t *testing.T) {
691704
}
692705

693706
func TestLoadBalancerMatcherHTTPMethod(t *testing.T) {
707+
t.Parallel()
694708
testJSON(t,
695709
&LoadBalancerMatcher{},
696710
&LoadBalancerMatcher{
@@ -711,6 +725,7 @@ func TestLoadBalancerMatcherHTTPMethod(t *testing.T) {
711725
}
712726

713727
func TestLoadBalancerMatcherInteger(t *testing.T) {
728+
t.Parallel()
714729
testJSON(t,
715730
&LoadBalancerMatcher{},
716731
&LoadBalancerMatcher{
@@ -733,6 +748,7 @@ func TestLoadBalancerMatcherInteger(t *testing.T) {
733748
}
734749

735750
func TestLoadBalancerMatcherIntegerRange(t *testing.T) {
751+
t.Parallel()
736752
testJSON(t,
737753
&LoadBalancerMatcher{},
738754
&LoadBalancerMatcher{
@@ -757,6 +773,7 @@ func TestLoadBalancerMatcherIntegerRange(t *testing.T) {
757773
}
758774

759775
func TestLoadBalancerMatcherString(t *testing.T) {
776+
t.Parallel()
760777
tv := true
761778
testJSON(t,
762779
&LoadBalancerMatcher{},
@@ -782,6 +799,7 @@ func TestLoadBalancerMatcherString(t *testing.T) {
782799
}
783800

784801
func TestLoadBalancerMatcherSourceIP(t *testing.T) {
802+
t.Parallel()
785803
testJSON(t,
786804
&LoadBalancerMatcher{},
787805
&LoadBalancerMatcher{
@@ -801,7 +819,31 @@ func TestLoadBalancerMatcherSourceIP(t *testing.T) {
801819
)
802820
}
803821

822+
func TestLoadBalancerMatcherSourceIP_Inverse(t *testing.T) {
823+
t.Parallel()
824+
testJSON(t,
825+
&LoadBalancerMatcher{},
826+
&LoadBalancerMatcher{
827+
Type: LoadBalancerMatcherTypeSrcIP,
828+
Inverse: BoolPtr(true),
829+
SrcIP: &LoadBalancerMatcherSourceIP{
830+
Value: "213.3.44.11",
831+
},
832+
},
833+
`
834+
{
835+
"type": "src_ip",
836+
"inverse": true,
837+
"match_src_ip": {
838+
"value": "213.3.44.11"
839+
}
840+
}
841+
`,
842+
)
843+
}
844+
804845
func TestLoadBalancerActionUseBackend(t *testing.T) {
846+
t.Parallel()
805847
testJSON(t,
806848
&LoadBalancerAction{},
807849
&LoadBalancerAction{
@@ -822,6 +864,7 @@ func TestLoadBalancerActionUseBackend(t *testing.T) {
822864
}
823865

824866
func TestLoadBalancerActionTCPReject(t *testing.T) {
867+
t.Parallel()
825868
testJSON(t,
826869
&LoadBalancerAction{},
827870
&LoadBalancerAction{
@@ -838,6 +881,7 @@ func TestLoadBalancerActionTCPReject(t *testing.T) {
838881
}
839882

840883
func TestLoadBalancerActionHTTPReturn(t *testing.T) {
884+
t.Parallel()
841885
testJSON(t,
842886
&LoadBalancerAction{},
843887
&LoadBalancerAction{
@@ -862,6 +906,7 @@ func TestLoadBalancerActionHTTPReturn(t *testing.T) {
862906
}
863907

864908
func TestLoadBalancerActionHTTPRedirect(t *testing.T) {
909+
t.Parallel()
865910
testJSON(t,
866911
&LoadBalancerAction{},
867912
&LoadBalancerAction{
@@ -882,6 +927,7 @@ func TestLoadBalancerActionHTTPRedirect(t *testing.T) {
882927
}
883928

884929
func TestLoadBalancerCertificateBundle(t *testing.T) {
930+
t.Parallel()
885931
testJSON(t,
886932
&LoadBalancerCertificateBundle{},
887933
&LoadBalancerCertificateBundle{
@@ -953,6 +999,7 @@ func TestLoadBalancerCertificateBundle(t *testing.T) {
953999
}
9541000

9551001
func TestLoadBalancerNetworks(t *testing.T) {
1002+
t.Parallel()
9561003
testJSON(t,
9571004
&LoadBalancer{},
9581005
&LoadBalancer{

upcloud/request/load_balancer_helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ func NewLoadBalancerSrcIPMatcher(IP string) upcloud.LoadBalancerMatcher {
205205
}
206206
}
207207

208+
// NewLoadBalancerInverseMatcher helper converts matcher to inverse matcher.
209+
//
210+
// Usage: inverseIPMatch := NewLoadBalancerInverseMatcher(NewLoadBalancerSrcIPMatcher("127.0.0.2"))
211+
func NewLoadBalancerInverseMatcher(m upcloud.LoadBalancerMatcher) upcloud.LoadBalancerMatcher {
212+
m.Inverse = upcloud.BoolPtr(true)
213+
return m
214+
}
215+
208216
func newLoadBalancerBackendMember(t upcloud.LoadBalancerBackendMemberType, name string, weight int, maxSessions int, enabled bool, IP string, port int) LoadBalancerBackendMember {
209217
return LoadBalancerBackendMember{
210218
Type: t,

upcloud/request/load_balancer_helpers_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
func TestMatcheresAndActionsHelper(t *testing.T) {
12+
t.Parallel()
1213
isTrue := true
1314

1415
expected := `
@@ -115,6 +116,13 @@ func TestMatcheresAndActionsHelper(t *testing.T) {
115116
"match_src_ip": {
116117
"value": "127.0.0.1"
117118
}
119+
},
120+
{
121+
"type": "src_ip",
122+
"inverse": true,
123+
"match_src_ip": {
124+
"value": "127.0.0.2"
125+
}
118126
}
119127
],
120128
"actions": [
@@ -169,6 +177,7 @@ func TestMatcheresAndActionsHelper(t *testing.T) {
169177
NewLoadBalancerBodySizeRangeMatcher(8000, 9000),
170178
NewLoadBalancerBodySizeMatcher(upcloud.LoadBalancerIntegerMatcherMethodEqual, 8000),
171179
NewLoadBalancerSrcIPMatcher("127.0.0.1"),
180+
NewLoadBalancerInverseMatcher(NewLoadBalancerSrcIPMatcher("127.0.0.2")),
172181
},
173182
Actions: []upcloud.LoadBalancerAction{
174183
NewLoadBalancerUseBackendAction("example-backend-2"),
@@ -184,6 +193,7 @@ func TestMatcheresAndActionsHelper(t *testing.T) {
184193
}
185194

186195
func TestNewLoadBalancerBackendMemberHelper(t *testing.T) {
196+
t.Parallel()
187197
expected := `
188198
[
189199
{
@@ -215,6 +225,7 @@ func TestNewLoadBalancerBackendMemberHelper(t *testing.T) {
215225
}
216226

217227
func TestCreateLoadBalancerCertificateBundleRequest(t *testing.T) {
228+
t.Parallel()
218229
expected := `
219230
[
220231
{

0 commit comments

Comments
 (0)