Skip to content

Commit

Permalink
Add Rules field to DatabaseCreateRequest to support creating Database…
Browse files Browse the repository at this point in the history
…s with Firewall Rules (#758)

Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
dvigueras and andrewsomething authored Nov 19, 2024
1 parent 45ad288 commit 22a5562
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 11 deletions.
30 changes: 19 additions & 11 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,27 @@ type DatabaseBackupRestore struct {
BackupCreatedAt string `json:"backup_created_at,omitempty"`
}

// DatabaseCreateFirewallRule is a rule describing an inbound source to a database
type DatabaseCreateFirewallRule struct {
UUID string `json:"uuid"`
Type string `json:"type"`
Value string `json:"value"`
}

// DatabaseCreateRequest represents a request to create a database cluster
type DatabaseCreateRequest struct {
Name string `json:"name,omitempty"`
EngineSlug string `json:"engine,omitempty"`
Version string `json:"version,omitempty"`
SizeSlug string `json:"size,omitempty"`
Region string `json:"region,omitempty"`
NumNodes int `json:"num_nodes,omitempty"`
PrivateNetworkUUID string `json:"private_network_uuid"`
Tags []string `json:"tags,omitempty"`
BackupRestore *DatabaseBackupRestore `json:"backup_restore,omitempty"`
ProjectID string `json:"project_id"`
StorageSizeMib uint64 `json:"storage_size_mib,omitempty"`
Name string `json:"name,omitempty"`
EngineSlug string `json:"engine,omitempty"`
Version string `json:"version,omitempty"`
SizeSlug string `json:"size,omitempty"`
Region string `json:"region,omitempty"`
NumNodes int `json:"num_nodes,omitempty"`
PrivateNetworkUUID string `json:"private_network_uuid"`
Tags []string `json:"tags,omitempty"`
BackupRestore *DatabaseBackupRestore `json:"backup_restore,omitempty"`
ProjectID string `json:"project_id"`
StorageSizeMib uint64 `json:"storage_size_mib,omitempty"`
Rules []*DatabaseCreateFirewallRule `json:"rules"`
}

// DatabaseResizeRequest can be used to initiate a database resize operation.
Expand Down
132 changes: 132 additions & 0 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,138 @@ func TestDatabases_Create(t *testing.T) {
"project_id": "05d84f74-db8c-4de5-ae72-2fd4823fb1c8",
"storage_size_mib": 81920
}
}`,
},
{
title: "create with firewall rules",
createRequest: &DatabaseCreateRequest{
Name: "firewall-rules-test",
EngineSlug: "pg",
Version: "15",
Region: "nyc3",
SizeSlug: "db-s-2vcpu-4gb",
NumNodes: 2,
Tags: []string{"production", "staging"},
ProjectID: "05d84f74-db8c-4de5-ae72-2fd4823fb1c8",
Rules: []*DatabaseCreateFirewallRule{
{
UUID: "bc47473b-603e-49a8-b36a-810c2703f1d0",
Type: "ip_addr",
Value: "172.16.1.1",
},
{
UUID: "17d460b2-5879-4466-ac09-6c90c9a6d7e0",
Type: "tag",
Value: "production",
},
},
},
want: &Database{
ID: "8d91899c-0739-4a1a-acc5-deadbeefbb8f",
Name: "firewall-rules-test",
EngineSlug: "pg",
VersionSlug: "10",
Connection: &DatabaseConnection{
URI: "postgres://doadmin:[email protected]:25060/defaultdb?sslmode=require",
Database: "defaultdb",
Host: "dbtest-do-user-3342561-0.db.ondigitalocean.com",
Port: 25060,
User: "doadmin",
Password: "zt91mum075ofzyww",
SSL: true,
},
PrivateConnection: &DatabaseConnection{
URI: "postgres://doadmin:zt91mum075ofzyww@private-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
Database: "defaultdb",
Host: "dbtest-do-user-3342561-0.db.ondigitalocean.com",
Port: 25060,
User: "doadmin",
Password: "zt91mum075ofzyww",
SSL: true,
},
StandbyConnection: &DatabaseConnection{
URI: "postgres://doadmin:zt91mum075ofzyww@replica-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
Database: "defaultdb",
Host: "replica-dbtest-do-user-3342561-0.db.ondigitalocean.com",
Port: 25060,
User: "doadmin",
Password: "zt91mum075ofzyww",
SSL: true,
},
StandbyPrivateConnection: &DatabaseConnection{
URI: "postgres://doadmin:zt91mum075ofzyww@private-replica-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
Database: "defaultdb",
Host: "private-replica-dbtest-do-user-3342561-0.db.ondigitalocean.com",
Port: 25060,
User: "doadmin",
Password: "zt91mum075ofzyww",
SSL: true,
},
Users: nil,
DBNames: nil,
NumNodes: 2,
RegionSlug: "nyc3",
Status: "creating",
CreatedAt: time.Date(2019, 2, 26, 6, 12, 39, 0, time.UTC),
MaintenanceWindow: nil,
SizeSlug: "db-s-2vcpu-4gb",
Tags: []string{"production", "staging"},
ProjectID: "05d84f74-db8c-4de5-ae72-2fd4823fb1c8",
},
body: `
{
"database": {
"id": "8d91899c-0739-4a1a-acc5-deadbeefbb8f",
"name": "firewall-rules-test",
"engine": "pg",
"version": "10",
"connection": {
"uri": "postgres://doadmin:[email protected]:25060/defaultdb?sslmode=require",
"database": "defaultdb",
"host": "dbtest-do-user-3342561-0.db.ondigitalocean.com",
"port": 25060,
"user": "doadmin",
"password": "zt91mum075ofzyww",
"ssl": true
},
"private_connection": {
"uri": "postgres://doadmin:zt91mum075ofzyww@private-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
"database": "defaultdb",
"host": "dbtest-do-user-3342561-0.db.ondigitalocean.com",
"port": 25060,
"user": "doadmin",
"password": "zt91mum075ofzyww",
"ssl": true
},
"standby_connection": {
"uri": "postgres://doadmin:zt91mum075ofzyww@replica-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
"database": "defaultdb",
"host": "replica-dbtest-do-user-3342561-0.db.ondigitalocean.com",
"port": 25060,
"user": "doadmin",
"password": "zt91mum075ofzyww",
"ssl": true
},
"standby_private_connection": {
"uri": "postgres://doadmin:zt91mum075ofzyww@private-replica-dbtest-do-user-3342561-0.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
"database": "defaultdb",
"host": "private-replica-dbtest-do-user-3342561-0.db.ondigitalocean.com",
"port": 25060,
"user": "doadmin",
"password": "zt91mum075ofzyww",
"ssl": true
},
"users": null,
"db_names": null,
"num_nodes": 2,
"region": "nyc3",
"status": "creating",
"created_at": "2019-02-26T06:12:39Z",
"maintenance_window": null,
"size": "db-s-2vcpu-4gb",
"tags": ["production", "staging"],
"project_id": "05d84f74-db8c-4de5-ae72-2fd4823fb1c8"
}
}`,
},
}
Expand Down

0 comments on commit 22a5562

Please sign in to comment.