Skip to content

Commit 788069d

Browse files
committed
MEDIUM: servers: add support for comments via Metadata field
1 parent ee1c078 commit 788069d

File tree

11 files changed

+91
-19
lines changed

11 files changed

+91
-19
lines changed

cmd/struct_equal_generator/generate.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ func (s {{.Data.Name}}) {{.Name}}(t {{.Data.Name}}, opts ...Options) {{- if eq $
103103
}
104104

105105
for k,v := range s.{{.Name}} {
106+
{{- if eq .Name "Metadata" }}
107+
if !reflect.DeepEqual(t.{{.Name}}[k], v) {
108+
{{- else }}
106109
if !t.{{.Name}}[k].Equal(v, opt) {
110+
{{- end }}
107111
{{- if eq $topLevel.Name "Equal" }}
108112
return false
109113
{{- else if eq $topLevel.Name "Diff" }}

configuration/misc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package configuration
22

33
import (
4+
"encoding/json"
5+
46
"github.com/haproxytech/client-native/v6/config-parser/parsers/http/actions"
57

68
"github.com/haproxytech/client-native/v6/misc"
@@ -36,3 +38,27 @@ func modelHdr2ActionHdr(hdrs []*models.ReturnHeader) []*actions.Hdr {
3638
}
3739
return headers
3840
}
41+
42+
func parseMetadata(comment string) map[string]interface{} {
43+
if comment == "" {
44+
return nil
45+
}
46+
metadata := make(map[string]interface{})
47+
err := json.Unmarshal([]byte(comment), &metadata)
48+
if err != nil {
49+
metadata["comment"] = comment
50+
return metadata
51+
}
52+
return metadata
53+
}
54+
55+
func serializeMetadata(metadata map[string]interface{}) (string, error) {
56+
if metadata == nil {
57+
return "", nil
58+
}
59+
b, err := json.Marshal(metadata)
60+
if err != nil {
61+
return "", err
62+
}
63+
return string(b), nil
64+
}

configuration/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ func ParseServer(ondiskServer types.Server) *models.Server {
511511
}
512512
s.Address = address
513513
s.Port = port
514+
s.Metadata = parseMetadata(ondiskServer.Comment)
514515
for _, p := range ondiskServer.Params {
515516
if v, ok := p.(*params.ServerOptionValue); ok {
516517
if v.Name == "id" {
@@ -861,6 +862,8 @@ func SerializeServer(s models.Server, opt *options.ConfigurationOptions) types.S
861862
} else {
862863
server.Address = misc.SanitizeIPv6Address(s.Address)
863864
}
865+
comment, _ := serializeMetadata(s.Metadata)
866+
server.Comment = comment
864867
server.Params = serializeServerParams(s.ServerParams, opt)
865868
if s.ID != nil {
866869
server.Params = append(server.Params, &params.ServerOptionValue{Name: "id", Value: strconv.FormatInt(*s.ID, 10)})

models/server.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/server_compare.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/server_compare_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/build/haproxy_spec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,6 +4193,9 @@ definitions:
41934193
id:
41944194
type: integer
41954195
x-nullable: true
4196+
metadata:
4197+
additionalProperties:
4198+
type: object
41964199
name:
41974200
pattern: ^[^\s]+$
41984201
type: string

specification/models/configuration/server.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ server:
2525
id:
2626
type: integer
2727
x-nullable: true
28+
metadata:
29+
additionalProperties:
30+
type: object
2831
additionalProperties: false
2932
example:
3033
name: www

test/configuration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ backend test
726726
external-check command /bin/false
727727
use-server webserv if TRUE
728728
use-server webserv2 unless TRUE
729-
server webserv 192.168.1.1:9200 maxconn 1000 ssl weight 10 inter 2s cookie BLAH slowstart 6000 proxy-v2-options authority,crc32c ws h1 pool-low-conn 128 id 1234 pool-purge-delay 10s tcp-ut 2s curves secp384r1 client-sigalgs ECDSA+SHA256:RSA+SHA256 sigalgs ECDSA+SHA256 log-bufsize 10 set-proxy-v2-tlv-fmt(0x20) %[fc_pp_tlv(0x20)]
730-
server webserv2 192.168.1.1:9300 maxconn 1000 ssl weight 10 inter 2s cookie BLAH slowstart 6000 proxy-v2-options authority,crc32c ws h1 pool-low-conn 128 hash-key akey pool-conn-name apoolconnname
729+
server webserv 192.168.1.1:9200 maxconn 1000 ssl weight 10 inter 2s cookie BLAH slowstart 6000 proxy-v2-options authority,crc32c ws h1 pool-low-conn 128 id 1234 pool-purge-delay 10s tcp-ut 2s curves secp384r1 client-sigalgs ECDSA+SHA256:RSA+SHA256 sigalgs ECDSA+SHA256 log-bufsize 10 set-proxy-v2-tlv-fmt(0x20) %[fc_pp_tlv(0x20)] # my comment
730+
server webserv2 192.168.1.1:9300 maxconn 1000 ssl weight 10 inter 2s cookie BLAH slowstart 6000 proxy-v2-options authority,crc32c ws h1 pool-low-conn 128 hash-key akey pool-conn-name apoolconnname # {"comment": "my structured comment", "id": "my_random_id_for_server"}
731731
http-request set-dst hdr(x-dst)
732732
http-request set-dst-port int(4000)
733733
http-check connect

test/expected/structured.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,10 @@
977977
"address": "192.168.1.1",
978978
"id": 1234,
979979
"name": "webserv",
980-
"port": 9200
980+
"port": 9200,
981+
"metadata": {
982+
"comment": "my comment"
983+
}
981984
},
982985
"webserv2": {
983986
"cookie": "BLAH",
@@ -996,7 +999,11 @@
996999
"name": "webserv2",
9971000
"port": 9300,
9981001
"pool_conn_name": "apoolconnname",
999-
"hash_key": "akey"
1002+
"hash_key": "akey",
1003+
"metadata": {
1004+
"comment": "my structured comment",
1005+
"id": "my_random_id_for_server"
1006+
}
10001007
}
10011008
}
10021009
},

0 commit comments

Comments
 (0)