Skip to content

Commit

Permalink
BUG/MEDIUM: defaults: deprecate PUT and use POST for creating a section
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuraga committed Feb 5, 2025
1 parent 3e96217 commit b8b2086
Show file tree
Hide file tree
Showing 13 changed files with 1,073 additions and 18 deletions.
1 change: 1 addition & 0 deletions configure_data_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler { //nolint:cyclop,m

// setup defaults configuration handlers
api.DefaultsCreateDefaultsSectionHandler = &handlers.CreateDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
api.DefaultsAddDefaultsSectionHandler = &handlers.AddDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
api.DefaultsDeleteDefaultsSectionHandler = &handlers.DeleteDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
api.DefaultsReplaceDefaultsSectionHandler = &handlers.ReplaceDefaultsSectionHandlerImpl{Client: client, ReloadAgent: ra}
api.DefaultsGetDefaultsSectionHandler = &handlers.GetDefaultsSectionHandlerImpl{Client: client}
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/defaults/create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ load '../../libs/haproxy_version'
load 'utils/_helpers'

@test "defaults: Create a named defaults configuration" {
resource_put "$_DEFAULTS_BASE_PATH" "data/post.json" ""
resource_post "$_DEFAULTS_BASE_PATH" "data/post.json" ""
assert_equal "$SC" 202

resource_get "$_DEFAULTS_BASE_PATH/created"
Expand All @@ -39,20 +39,20 @@ load 'utils/_helpers'
}

@test "defaults: Create a named defaults configuration that already exists" {
resource_put "$_DEFAULTS_BASE_PATH" "data/post_existing.json" ""
resource_post "$_DEFAULTS_BASE_PATH" "data/post_existing.json" ""
assert_equal "$SC" 409
}

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

resource_put "$_DEFAULTS_BASE_PATH" "data/post.json" ""
resource_post "$_DEFAULTS_BASE_PATH" "data/post.json" ""
assert_equal "$SC" 202

resource_get "$_DEFAULTS_BASE_PATH/created"
assert_equal "$SC" 200

resource_put "$_DEFAULTS_BASE_PATH" "data/post_with_from.json" ""
resource_post "$_DEFAULTS_BASE_PATH" "data/post_with_from.json" ""
assert_equal "$SC" 202

resource_get "$_DEFAULTS_BASE_PATH/created_with_from"
Expand Down
1 change: 1 addition & 0 deletions e2e/tests/defaults/data/put.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "unnamed_defaults_1",
"client_timeout": 25000,
"server_timeout": 25000,
"mode": "tcp"
Expand Down
21 changes: 10 additions & 11 deletions e2e/tests/defaults/replace.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ load '../../libs/version'
load 'utils/_helpers'


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

@test "defaults: Return a defaults configuration" {
resource_get "$_DEFAULTS_BASE_PATH/unnamed_defaults_1"
assert_equal "$SC" 200
assert_equal "$(get_json_path "$BODY" '.name')" "unnamed_defaults_1"
resource_get "$_DEFAULTS_BASE_PATH/unnamed_defaults_1" assert_equal "$SC" 200
assert_equal "$(get_json_path "$BODY" ".name")" "unnamed_defaults_1"
assert_equal "$(get_json_path "$BODY" ".mode")" "tcp"
assert_equal "$(get_json_path "$BODY" ".client_timeout")" 25000
assert_equal "$(get_json_path "$BODY" ".server_timeout")" 25000
}

@test "defaults: Return a named defaults configuration that does not exist" {
resource_get "$_DEFAULTS_BASE_PATH/nothing_to_see_here"
@test "defaults: Replace a defaults configuration that doesn't exist" {
resource_get "$_DEFAULTS_BASE_PATH/these_arent_the_droids_youre_looking_for"
assert_equal "$SC" 404
}
163 changes: 163 additions & 0 deletions embedded_spec.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/renameio v1.0.1
github.com/google/uuid v1.6.0
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68
github.com/jessevdk/go-flags v1.6.1
github.com/joho/godotenv v1.5.1
github.com/json-iterator/go v1.1.12
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/haproxytech/client-native/v5 v5.1.9 h1:D+JvMsRHCewn+r+FJpzf3QUu2yHwGuV+s6IuJlajxYE=
github.com/haproxytech/client-native/v5 v5.1.9/go.mod h1:rSJ7gT0vpZqNYdZQwzttLC4doOyoTL+B68F7cDjELNQ=
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f h1:tox7vjRpHED06IetsNZgv1sAR9pwYrLXR3HeQ7JKtOI=
github.com/haproxytech/client-native/v6 v6.0.8-0.20250203095557-389b6b56764f/go.mod h1:v1Gc4Lh3KGGjN3KnuiRFmm4D6eUXhbRvcjT6oEDUpI8=
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68 h1:JfTr2nOx9NpiPGGYqVA2jjgz2K0FPmhV6Bh3IF7rgBw=
github.com/haproxytech/client-native/v6 v6.0.8-0.20250205140139-57094e527e68/go.mod h1:v1Gc4Lh3KGGjN3KnuiRFmm4D6eUXhbRvcjT6oEDUpI8=
github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE=
github.com/haproxytech/go-logger v1.1.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
Expand Down
56 changes: 56 additions & 0 deletions handlers/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,62 @@ func (h *CreateDefaultsSectionHandlerImpl) createDefaultsSection(params defaults
return configuration.CreateDefaultsSection(params.Data, t, v)
}

type AddDefaultsSectionHandlerImpl struct {
Client client_native.HAProxyClient
ReloadAgent haproxy.IReloadAgent
}

func (h AddDefaultsSectionHandlerImpl) Handle(params defaults.AddDefaultsSectionParams, principal interface{}) middleware.Responder {
t := ""
v := int64(0)
if params.TransactionID != nil {
t = *params.TransactionID
}
if params.Version != nil {
v = *params.Version
}

if t != "" && *params.ForceReload {
msg := "Both force_reload and transaction specified, specify only one"
c := misc.ErrHTTPBadRequest
e := &models.Error{
Message: &msg,
Code: &c,
}
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
}

err := h.createDefaultsSection(params, t, v)
if err != nil {
e := misc.HandleError(err)
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
}
if params.TransactionID == nil {
if *params.ForceReload {
err := h.ReloadAgent.ForceReload()
if err != nil {
e := misc.HandleError(err)
return defaults.NewAddDefaultsSectionDefault(int(*e.Code)).WithPayload(e)
}
return defaults.NewAddDefaultsSectionCreated().WithPayload(params.Data)
}
rID := h.ReloadAgent.Reload()
return defaults.NewAddDefaultsSectionAccepted().WithReloadID(rID).WithPayload(params.Data)
}
return defaults.NewAddDefaultsSectionAccepted().WithPayload(params.Data)
}

func (h *AddDefaultsSectionHandlerImpl) createDefaultsSection(params defaults.AddDefaultsSectionParams, t string, v int64) error {
configuration, err := h.Client.Configuration()
if err != nil {
return err
}
if params.FullSection != nil && *params.FullSection {
return configuration.CreateStructuredDefaultsSection(params.Data, t, v)
}
return configuration.CreateDefaultsSection(params.Data, t, v)
}

// ReplaceDefaultsHandlerImpl implementation of the ReplaceDefaultsHandler interface
type ReplaceDefaultsSectionHandlerImpl struct {
Client client_native.HAProxyClient
Expand Down
12 changes: 12 additions & 0 deletions operations/data_plane_api.go

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

Loading

0 comments on commit b8b2086

Please sign in to comment.