From c26a7ffcf419ad3183b909b4750ff7de7c9ac556 Mon Sep 17 00:00:00 2001 From: Carl Henderson Date: Thu, 8 Feb 2024 16:18:25 +0000 Subject: [PATCH 1/2] Hyperdrive: Password should be nested in Origin --- .changelog/1501.txt | 3 +++ hyperdrive.go | 14 ++++---------- hyperdrive_test.go | 11 +++-------- 3 files changed, 10 insertions(+), 18 deletions(-) create mode 100644 .changelog/1501.txt diff --git a/.changelog/1501.txt b/.changelog/1501.txt new file mode 100644 index 00000000000..91b8d37a4b9 --- /dev/null +++ b/.changelog/1501.txt @@ -0,0 +1,3 @@ +```release-note:bug +hyperdrive: password should be nested in origin +``` diff --git a/hyperdrive.go b/hyperdrive.go index 346eb1ccb1a..266a53db29b 100644 --- a/hyperdrive.go +++ b/hyperdrive.go @@ -12,7 +12,6 @@ import ( var ( ErrMissingHyperdriveConfigID = errors.New("required hyperdrive config id is missing") ErrMissingHyperdriveConfigName = errors.New("required hyperdrive config name is missing") - ErrMissingHyperdriveConfigPassword = errors.New("required hyperdrive config password is missing") ) type HyperdriveConfig struct { @@ -24,6 +23,7 @@ type HyperdriveConfig struct { type HyperdriveConfigOrigin struct { Database string `json:"database,omitempty"` + Password string `json:"password"` Host string `json:"host,omitempty"` Port int `json:"port,omitempty"` Scheme string `json:"scheme,omitempty"` @@ -42,10 +42,9 @@ type HyperdriveConfigListResponse struct { } type CreateHyperdriveConfigParams struct { - Name string `json:"name"` - Password string `json:"password"` - Origin HyperdriveConfigOrigin `json:"origin"` - Caching HyperdriveConfigCaching `json:"caching,omitempty"` + Name string `json:"name"` + Origin HyperdriveConfigOrigin `json:"origin"` + Caching HyperdriveConfigCaching `json:"caching,omitempty"` } type HyperdriveConfigResponse struct { @@ -56,7 +55,6 @@ type HyperdriveConfigResponse struct { type UpdateHyperdriveConfigParams struct { HyperdriveID string `json:"-"` Name string `json:"name"` - Password string `json:"password"` Origin HyperdriveConfigOrigin `json:"origin"` Caching HyperdriveConfigCaching `json:"caching,omitempty"` } @@ -99,10 +97,6 @@ func (api *API) CreateHyperdriveConfig(ctx context.Context, rc *ResourceContaine return HyperdriveConfig{}, ErrMissingHyperdriveConfigName } - if params.Password == "" { - return HyperdriveConfig{}, ErrMissingHyperdriveConfigPassword - } - uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs", rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) diff --git a/hyperdrive_test.go b/hyperdrive_test.go index 633ce9cbbdc..c19378f1490 100644 --- a/hyperdrive_test.go +++ b/hyperdrive_test.go @@ -164,16 +164,11 @@ func TestHyperdriveConfig_Create(t *testing.T) { assert.Equal(t, ErrMissingHyperdriveConfigName, err) } - _, err = client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{Name: "example-hyperdrive"}) - if assert.Error(t, err) { - assert.Equal(t, ErrMissingHyperdriveConfigPassword, err) - } - result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{ - Name: "example-hyperdrive", - Password: "password", + Name: "example-hyperdrive", Origin: HyperdriveConfigOrigin{ Database: "postgres", + Password: "password", Host: "database.example.com", Port: 5432, Scheme: "postgres", @@ -264,9 +259,9 @@ func TestHyperdriveConfig_Update(t *testing.T) { result, err := client.UpdateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), UpdateHyperdriveConfigParams{ HyperdriveID: "6b7efc370ea34ded8327fa20698dfe3a", Name: "example-hyperdrive", - Password: "password", Origin: HyperdriveConfigOrigin{ Database: "postgres", + Password: "password", Host: "database.example.com", Port: 5432, Scheme: "postgres", From 79f2a0dd166b477698dcf76715136ce705350331 Mon Sep 17 00:00:00 2001 From: Emilio Assuncao Date: Thu, 8 Feb 2024 18:30:42 -0600 Subject: [PATCH 2/2] Hyperdrive: Enforce required on all string fields in HyperdriveConfigOrigin --- hyperdrive.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/hyperdrive.go b/hyperdrive.go index 266a53db29b..c1ddbc9a5f3 100644 --- a/hyperdrive.go +++ b/hyperdrive.go @@ -10,8 +10,13 @@ import ( ) var ( - ErrMissingHyperdriveConfigID = errors.New("required hyperdrive config id is missing") - ErrMissingHyperdriveConfigName = errors.New("required hyperdrive config name is missing") + ErrMissingHyperdriveConfigID = errors.New("required hyperdrive config id is missing") + ErrMissingHyperdriveConfigName = errors.New("required hyperdrive config name is missing") + ErrMissingHyperdriveConfigOriginDatabase = errors.New("required hyperdrive config origin database is missing") + ErrMissingHyperdriveConfigOriginPassword = errors.New("required hyperdrive config origin password is missing") + ErrMissingHyperdriveConfigOriginHost = errors.New("required hyperdrive config origin host is missing") + ErrMissingHyperdriveConfigOriginScheme = errors.New("required hyperdrive config origin scheme is missing") + ErrMissingHyperdriveConfigOriginUser = errors.New("required hyperdrive config origin user is missing") ) type HyperdriveConfig struct { @@ -172,6 +177,26 @@ func (api *API) UpdateHyperdriveConfig(ctx context.Context, rc *ResourceContaine return HyperdriveConfig{}, ErrMissingHyperdriveConfigID } + if params.Origin.Database == "" { + return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginDatabase + } + + if params.Origin.Password == "" { + return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginPassword + } + + if params.Origin.Host == "" { + return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginHost + } + + if params.Origin.Scheme == "" { + return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginScheme + } + + if params.Origin.User == "" { + return HyperdriveConfig{}, ErrMissingHyperdriveConfigOriginUser + } + uri := fmt.Sprintf("/accounts/%s/hyperdrive/configs/%s", rc.Identifier, params.HyperdriveID) res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)