Skip to content

Commit b8b2086

Browse files
committed
BUG/MEDIUM: defaults: deprecate PUT and use POST for creating a section
1 parent 3e96217 commit b8b2086

13 files changed

+1073
-18
lines changed

configure_data_plane.go

+1
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler { //nolint:cyclop,m
776776

777777
// setup defaults configuration handlers
778778
api.DefaultsCreateDefaultsSectionHandler = &handlers.CreateDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
779+
api.DefaultsAddDefaultsSectionHandler = &handlers.AddDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
779780
api.DefaultsDeleteDefaultsSectionHandler = &handlers.DeleteDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
780781
api.DefaultsReplaceDefaultsSectionHandler = &handlers.ReplaceDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
781782
api.DefaultsGetDefaultsSectionHandler = &handlers.GetDefaultsSectionHandlerImpl{Client: client}

e2e/tests/defaults/create.bats

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load '../../libs/haproxy_version'
2626
load 'utils/_helpers'
2727

2828
@test "defaults: Create a named defaults configuration" {
29-
resource_put "$_DEFAULTS_BASE_PATH" "data/post.json" ""
29+
resource_post "$_DEFAULTS_BASE_PATH" "data/post.json" ""
3030
assert_equal "$SC" 202
3131

3232
resource_get "$_DEFAULTS_BASE_PATH/created"
@@ -39,20 +39,20 @@ load 'utils/_helpers'
3939
}
4040

4141
@test "defaults: Create a named defaults configuration that already exists" {
42-
resource_put "$_DEFAULTS_BASE_PATH" "data/post_existing.json" ""
42+
resource_post "$_DEFAULTS_BASE_PATH" "data/post_existing.json" ""
4343
assert_equal "$SC" 409
4444
}
4545

4646
@test "defaults: Create a named defaults configuration with from" {
4747
haproxy_version_ge $_ERR_SUPPORTED_HAPROXY_VERSION || skip "requires HAProxy $_ERR_SUPPORTED_HAPROXY_VERSION+"
4848

49-
resource_put "$_DEFAULTS_BASE_PATH" "data/post.json" ""
49+
resource_post "$_DEFAULTS_BASE_PATH" "data/post.json" ""
5050
assert_equal "$SC" 202
5151

5252
resource_get "$_DEFAULTS_BASE_PATH/created"
5353
assert_equal "$SC" 200
5454

55-
resource_put "$_DEFAULTS_BASE_PATH" "data/post_with_from.json" ""
55+
resource_post "$_DEFAULTS_BASE_PATH" "data/post_with_from.json" ""
5656
assert_equal "$SC" 202
5757

5858
resource_get "$_DEFAULTS_BASE_PATH/created_with_from"

e2e/tests/defaults/data/put.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "unnamed_defaults_1",
23
"client_timeout": 25000,
34
"server_timeout": 25000,
45
"mode": "tcp"

e2e/tests/defaults/replace.bats

+10-11
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ load '../../libs/version'
2424
load 'utils/_helpers'
2525

2626

27-
@test "defaults: Return a list of defaults configurations" {
28-
resource_get "$_DEFAULTS_BASE_PATH"
29-
assert_equal "$SC" 200
30-
assert_equal "$(get_json_path "$BODY" '.[0].name')" "unnamed_defaults_1"
31-
}
27+
@test "backends: Replace a defaults" {
28+
resource_put "$_DEFAULTS_BASE_PATH/unnamed_defaults_1" "data/put.json" "force_reload=true"
29+
assert_equal "$SC" 200
3230

33-
@test "defaults: Return a defaults configuration" {
34-
resource_get "$_DEFAULTS_BASE_PATH/unnamed_defaults_1"
35-
assert_equal "$SC" 200
36-
assert_equal "$(get_json_path "$BODY" '.name')" "unnamed_defaults_1"
31+
resource_get "$_DEFAULTS_BASE_PATH/unnamed_defaults_1" assert_equal "$SC" 200
32+
assert_equal "$(get_json_path "$BODY" ".name")" "unnamed_defaults_1"
33+
assert_equal "$(get_json_path "$BODY" ".mode")" "tcp"
34+
assert_equal "$(get_json_path "$BODY" ".client_timeout")" 25000
35+
assert_equal "$(get_json_path "$BODY" ".server_timeout")" 25000
3736
}
3837

39-
@test "defaults: Return a named defaults configuration that does not exist" {
40-
resource_get "$_DEFAULTS_BASE_PATH/nothing_to_see_here"
38+
@test "defaults: Replace a defaults configuration that doesn't exist" {
39+
resource_get "$_DEFAULTS_BASE_PATH/these_arent_the_droids_youre_looking_for"
4140
assert_equal "$SC" 404
4241
}

embedded_spec.go

+163
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/google/go-cmp v0.6.0
2525
github.com/google/renameio v1.0.1
2626
github.com/google/uuid v1.6.0
27-
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f
27+
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68
2828
github.com/jessevdk/go-flags v1.6.1
2929
github.com/joho/godotenv v1.5.1
3030
github.com/json-iterator/go v1.1.12

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
101101
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
102102
github.com/haproxytech/client-native/v5 v5.1.9 h1:D+JvMsRHCewn+r+FJpzf3QUu2yHwGuV+s6IuJlajxYE=
103103
github.com/haproxytech/client-native/v5 v5.1.9/go.mod h1:rSJ7gT0vpZqNYdZQwzttLC4doOyoTL+B68F7cDjELNQ=
104-
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f h1:tox7vjRpHED06IetsNZgv1sAR9pwYrLXR3HeQ7JKtOI=
105-
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f/go.mod h1:v1Gc4Lh3KGGjN3KnuiRFmm4D6eUXhbRvcjT6oEDUpI8=
104+
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68 h1:JfTr2nOx9NpiPGGYqVA2jjgz2K0FPmhV6Bh3IF7rgBw=
105+
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68/go.mod h1:v1Gc4Lh3KGGjN3KnuiRFmm4D6eUXhbRvcjT6oEDUpI8=
106106
github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE=
107107
github.com/haproxytech/go-logger v1.1.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM=
108108
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=

handlers/defaults.go

+56
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,62 @@ func (h *CreateDefaultsSectionHandlerImpl) createDefaultsSection(params defaults
236236
return configuration.CreateDefaultsSection(params.Data, t, v)
237237
}
238238

239+
type AddDefaultsSectionHandlerImpl struct {
240+
Client client_native.HAProxyClient
241+
ReloadAgent haproxy.IReloadAgent
242+
}
243+
244+
func (h AddDefaultsSectionHandlerImpl) Handle(params defaults.AddDefaultsSectionParams, principal interface{}) middleware.Responder {
245+
t := ""
246+
v := int64(0)
247+
if params.TransactionID != nil {
248+
t = *params.TransactionID
249+
}
250+
if params.Version != nil {
251+
v = *params.Version
252+
}
253+
254+
if t != "" && *params.ForceReload {
255+
msg := "Both force_reload and transaction specified, specify only one"
256+
c := misc.ErrHTTPBadRequest
257+
e := &models.Error{
258+
Message: &msg,
259+
Code: &c,
260+
}
261+
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
262+
}
263+
264+
err := h.createDefaultsSection(params, t, v)
265+
if err != nil {
266+
e := misc.HandleError(err)
267+
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
268+
}
269+
if params.TransactionID == nil {
270+
if *params.ForceReload {
271+
err := h.ReloadAgent.ForceReload()
272+
if err != nil {
273+
e := misc.HandleError(err)
274+
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
275+
}
276+
return defaults.NewAddDefaultsSectionCreated().WithPayload(params.Data)
277+
}
278+
rID := h.ReloadAgent.Reload()
279+
return defaults.NewAddDefaultsSectionAccepted().WithReloadID(rID).WithPayload(params.Data)
280+
}
281+
return defaults.NewAddDefaultsSectionAccepted().WithPayload(params.Data)
282+
}
283+
284+
func (h *AddDefaultsSectionHandlerImpl) createDefaultsSection(params defaults.AddDefaultsSectionParams, t string, v int64) error {
285+
configuration, err := h.Client.Configuration()
286+
if err != nil {
287+
return err
288+
}
289+
if params.FullSection != nil && *params.FullSection {
290+
return configuration.CreateStructuredDefaultsSection(params.Data, t, v)
291+
}
292+
return configuration.CreateDefaultsSection(params.Data, t, v)
293+
}
294+
239295
// ReplaceDefaultsHandlerImpl implementation of the ReplaceDefaultsHandler interface
240296
type ReplaceDefaultsSectionHandlerImpl struct {
241297
Client client_native.HAProxyClient

operations/data_plane_api.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)