Skip to content

Commit

Permalink
Merge pull request #2729 from OilyLime/update-hyperdrive-model
Browse files Browse the repository at this point in the history
Update Hyperdrive Config Origin model to include new fields
  • Loading branch information
jacobbednarz authored Jul 15, 2024
2 parents d5733c2 + 76d4cf3 commit e5e6bf4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .changelog/2729.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-notes:enhancement
hyperdrive: Add support for Hyperdrive Over Access attributes
```
34 changes: 21 additions & 13 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"`
Password string `json:"password"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Scheme string `json:"scheme,omitempty"`
User string `json:"user,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"`
}

type HyperdriveConfigOriginWithSecrets struct {
HyperdriveConfigOrigin
Password string `json:"password"`
AccessClientSecret string `json:"access_client_secret,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 HyperdriveConfigOriginWithSecrets `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 HyperdriveConfigOriginWithSecrets `json:"origin"`
Caching HyperdriveConfigCaching `json:"caching,omitempty"`
}

type ListHyperdriveConfigParams struct{}
Expand Down
128 changes: 110 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 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access"
testAccessClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)

func testHyperdriveConfig() HyperdriveConfig {
func testHyperdriveHostAndPortConfig() HyperdriveConfig {
return HyperdriveConfig{
ID: testHyperdriveConfigId,
Name: testHyperdriveConfigName,
Expand All @@ -33,6 +35,25 @@ 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,
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
MaxAge: 30,
StaleWhileRevalidate: 15,
},
}
}

func TestHyperdriveConfig_List(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -72,7 +93,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 +140,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 +187,15 @@ func TestHyperdriveConfig_Create(t *testing.T) {

result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{
Name: "example-hyperdrive",
Origin: HyperdriveConfigOrigin{
Database: "postgres",
Origin: HyperdriveConfigOriginWithSecrets{
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 @@ -182,7 +205,74 @@ func TestHyperdriveConfig_Create(t *testing.T) {
})

if assert.NoError(t, err) {
assert.Equal(t, testHyperdriveConfig(), result)
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": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access",
"access_client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}`)
})

_, 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: HyperdriveConfigOriginWithSecrets{
HyperdriveConfigOrigin: HyperdriveConfigOrigin{
Database: "postgres",
Host: "database.example.com",
Scheme: "postgres",
User: "postgres",
AccessClientID: testAccessClientID,
},
Password: "password",
AccessClientSecret: testAccessClientSecret,
},
Caching: HyperdriveConfigCaching{
Disabled: BoolPtr(false),
MaxAge: 30,
StaleWhileRevalidate: 15,
},
})

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

Expand Down Expand Up @@ -259,13 +349,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: HyperdriveConfigOriginWithSecrets{
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 +367,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 e5e6bf4

Please sign in to comment.