Skip to content

Commit

Permalink
Update hyperdrive config origin model to include new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlemilio committed Jul 9, 2024
1 parent f5d3137 commit 8a77381
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 30 deletions.
32 changes: 20 additions & 12 deletions hyperdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ type HyperdriveConfig struct {
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}

type HyperdriveOriginType string

type HyperdriveConfigOrigin struct {
Database string `json:"database,omitempty"`
Database string `json:"database,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Scheme string `json:"scheme,omitempty"`
User string `json:"user,omitempty"`
AccessClientID string `json:"access_client_id,omitempty"`
AccessClientSecret string `json:"access_client_secret,omitempty"`
}

type HyperdriveConfigOriginWithPassword struct {
HyperdriveConfigOrigin
Password string `json:"password"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Scheme string `json:"scheme,omitempty"`
User string `json:"user,omitempty"`
}

type HyperdriveConfigCaching struct {
Expand All @@ -47,9 +55,9 @@ type HyperdriveConfigListResponse struct {
}

type CreateHyperdriveConfigParams struct {
Name string `json:"name"`
Origin HyperdriveConfigOrigin `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
Name string `json:"name"`
Origin HyperdriveConfigOriginWithPassword `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}

type HyperdriveConfigResponse struct {
Expand All @@ -58,10 +66,10 @@ type HyperdriveConfigResponse struct {
}

type UpdateHyperdriveConfigParams struct {
HyperdriveID string `json:"-"`
Name string `json:"name"`
Origin HyperdriveConfigOrigin `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
HyperdriveID string `json:"-"`
Name string `json:"name"`
Origin HyperdriveConfigOriginWithPassword `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}

type ListHyperdriveConfigParams struct{}
Expand Down
129 changes: 111 additions & 18 deletions hyperdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
const (
testHyperdriveConfigId = "6b7efc370ea34ded8327fa20698dfe3a"
testHyperdriveConfigName = "example-hyperdrive"
testAccessClientID = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a.access"
testAccessClientSecret = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a"
)

func testHyperdriveConfig() HyperdriveConfig {
func testHyperdriveHostAndPortConfig() HyperdriveConfig {
return HyperdriveConfig{
ID: testHyperdriveConfigId,
Name: testHyperdriveConfigName,
Expand All @@ -33,6 +35,26 @@ func testHyperdriveConfig() HyperdriveConfig {
}
}

func testHyperdriveCFAccessConfig() HyperdriveConfig {
return HyperdriveConfig{
ID: testHyperdriveConfigId,
Name: testHyperdriveConfigName,
Origin: HyperdriveConfigOrigin{
Database: "postgres",
Host: "database.example.com",
Scheme: "postgres",
User: "postgres",
AccessClientID: testAccessClientID,
AccessClientSecret: testAccessClientSecret,
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
MaxAge: 30,
StaleWhileRevalidate: 15,
},
}
}

func TestHyperdriveConfig_List(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -72,7 +94,7 @@ func TestHyperdriveConfig_List(t *testing.T) {
result, err := client.ListHyperdriveConfigs(context.Background(), AccountIdentifier(testAccountID), ListHyperdriveConfigParams{})
if assert.NoError(t, err) {
assert.Equal(t, 1, len(result))
assert.Equal(t, testHyperdriveConfig(), result[0])
assert.Equal(t, testHyperdriveHostAndPortConfig(), result[0])
}
}

Expand Down Expand Up @@ -119,11 +141,11 @@ func TestHyperdriveConfig_Get(t *testing.T) {

result, err := client.GetHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), testHyperdriveConfigId)
if assert.NoError(t, err) {
assert.Equal(t, testHyperdriveConfig(), result)
assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
}
}

func TestHyperdriveConfig_Create(t *testing.T) {
func TestHyperdriveConfig_CreateHostAndPort(t *testing.T) {
setup()
defer teardown()

Expand Down Expand Up @@ -166,13 +188,82 @@ func TestHyperdriveConfig_Create(t *testing.T) {

result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{
Name: "example-hyperdrive",
Origin: HyperdriveConfigOrigin{
Database: "postgres",
Origin: HyperdriveConfigOriginWithPassword{
HyperdriveConfigOrigin: HyperdriveConfigOrigin{
Database: "postgres",
Host: "database.example.com",
Port: 5432,
Scheme: "postgres",
User: "postgres",
},
Password: "password",
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
MaxAge: 30,
StaleWhileRevalidate: 15,
},
})

if assert.NoError(t, err) {
assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
}
}

func TestHyperdriveConfig_CreateCFAccess(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/accounts/%s/hyperdrive/configs", testAccountID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "6b7efc370ea34ded8327fa20698dfe3a",
"caching": {
"disabled": false,
"max_age": 30,
"stale_while_revalidate": 15
},
"name": "example-hyperdrive",
"origin": {
"database": "postgres",
"host": "database.example.com",
"scheme": "postgres",
"user": "postgres",
"access_client_id": "a539ed18b8871f690c99d428f11fb785.access",
"access_client_secret": "732c2bbf71917e027c075212a72e07800e6989cc32223e4834999e12bda9703e"
}
}
}`)
})

_, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(""), CreateHyperdriveConfigParams{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}

_, err = client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingHyperdriveConfigName, err)
}

result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{
Name: "example-hyperdrive",
Origin: HyperdriveConfigOriginWithPassword{
HyperdriveConfigOrigin: HyperdriveConfigOrigin{
Database: "postgres",
Host: "database.example.com",
Scheme: "postgres",
User: "postgres",
AccessClientID: testAccessClientID,
AccessClientSecret: testAccessClientSecret,
},
Password: "password",
Host: "database.example.com",
Port: 5432,
Scheme: "postgres",
User: "postgres",
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
Expand All @@ -182,7 +273,7 @@ func TestHyperdriveConfig_Create(t *testing.T) {
})

if assert.NoError(t, err) {
assert.Equal(t, testHyperdriveConfig(), result)
assert.Equal(t, testHyperdriveCFAccessConfig(), result)
}
}

Expand Down Expand Up @@ -259,13 +350,15 @@ func TestHyperdriveConfig_Update(t *testing.T) {
result, err := client.UpdateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), UpdateHyperdriveConfigParams{
HyperdriveID: "6b7efc370ea34ded8327fa20698dfe3a",
Name: "example-hyperdrive",
Origin: HyperdriveConfigOrigin{
Database: "postgres",
Origin: HyperdriveConfigOriginWithPassword{
HyperdriveConfigOrigin: HyperdriveConfigOrigin{
Database: "postgres",
Host: "database.example.com",
Port: 5432,
Scheme: "postgres",
User: "postgres",
},
Password: "password",
Host: "database.example.com",
Port: 5432,
Scheme: "postgres",
User: "postgres",
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
Expand All @@ -275,6 +368,6 @@ func TestHyperdriveConfig_Update(t *testing.T) {
})

if assert.NoError(t, err) {
assert.Equal(t, testHyperdriveConfig(), result)
assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
}
}

0 comments on commit 8a77381

Please sign in to comment.