Skip to content

Commit

Permalink
MINOR: Add TCP session attribute attach-srv
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer committed Dec 4, 2023
1 parent 22edd10 commit 72b7eaf
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 49 deletions.
102 changes: 102 additions & 0 deletions parsers/tcp/actions/attach-srv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2023 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package actions

import (
"fmt"
"strings"

"github.com/haproxytech/config-parser/v5/common"
"github.com/haproxytech/config-parser/v5/types"
)

type AttachSrv struct {
Server string
Name common.Expression
Cond string
CondTest string
Comment string
}

// tcp-request session attach-srv <srv> [name <expr>] [{ if | unless } <condition>]

func (f *AttachSrv) Parse(parts []string, parserType types.ParserType, comment string) error {
if f.Comment != "" {
f.Comment = comment
}

n := len(parts)
if n < 4 {
return fmt.Errorf("not enough params")
}

f.Server = parts[3]

if n == 4 {
// Nothing more to parse.
return nil
}

i := 4

if parts[i] == "name" {
if n < 6 {
return fmt.Errorf("not enough params")
}
expr := common.Expression{}
err := expr.Parse([]string{parts[i+1]})
if err != nil {
return fmt.Errorf("invalid expression after '%s': %w", parts[i], err)
}
f.Name = expr
i += 2
if i == n {
return nil
}
}

_, condition := common.SplitRequest(parts[i:])
if len(condition) > 1 {
f.Cond = condition[0]
f.CondTest = strings.Join(condition[1:], " ")
} else {
return fmt.Errorf("invalid condition after '%s'", parts[i])
}

return nil
}

func (f *AttachSrv) String() string {
var result strings.Builder
result.WriteString("attach-srv ")
result.WriteString(f.Server)
if name := f.Name.String(); name != "" {
result.WriteString(" name ")
result.WriteString(name)
}
if f.Cond != "" {
result.WriteString(" ")
result.WriteString(f.Cond)
result.WriteString(" ")
result.WriteString(f.CondTest)
}
return result.String()
}

func (f *AttachSrv) GetComment() string {
return f.Comment
}
2 changes: 2 additions & 0 deletions parsers/tcp/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (f *Session) Parse(parts []string, comment string) error {
switch parts[2] {
case "accept":
err = f.ParseAction(&tcpActions.Accept{}, parts)
case "attach-srv":
err = f.ParseAction(&tcpActions.AttachSrv{}, parts)
case "reject":
err = f.ParseAction(&actions.Reject{}, parts)
case "silent-drop":
Expand Down
12 changes: 12 additions & 0 deletions tests/configs/haproxy_generated.cfg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/integration/backend_data_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/integration/backend_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/integration/frontend_data_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/integration/frontend_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 56 additions & 49 deletions tests/tcp-request_generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions types/types-other.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,13 @@ type TCPType interface {
//test:ok:tcp-request session unset-var(sess.dn)
//test:ok:tcp-request session silent-drop
//test:ok:tcp-request session silent-drop if !HTTP
//test:ok:tcp-request session attach-srv srv1
//test:ok:tcp-request session attach-srv srv1 name example.com
//test:ok:tcp-request session attach-srv srv1 name example.com if exceeds_limit
//test:fail:tcp-request session attach-srv
//test:fail:tcp-request session attach-srv srv1 name
//test:fail:tcp-request session attach-srv srv1 if
//test:fail:tcp-request session attach-srv srv1 name example.com unless
//test:ok:tcp-request content set-bandwidth-limit my-limit
//test:ok:tcp-request content set-bandwidth-limit my-limit limit 1m period 10s
//test:ok:tcp-request content set-bandwidth-limit my-limit period 10s
Expand Down

0 comments on commit 72b7eaf

Please sign in to comment.