From 22a5562d40a75f1cbc49be4dfa1ef78df7e2d326 Mon Sep 17 00:00:00 2001 From: Daniel Vigueras Date: Wed, 20 Nov 2024 00:03:10 +0100 Subject: [PATCH] Add Rules field to DatabaseCreateRequest to support creating Databases with Firewall Rules (#758) Co-authored-by: Andrew Starr-Bochicchio --- databases.go | 30 +++++++---- databases_test.go | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 11 deletions(-) diff --git a/databases.go b/databases.go index e2a7d567..1217ef05 100644 --- a/databases.go +++ b/databases.go @@ -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. diff --git a/databases_test.go b/databases_test.go index 96d16eab..cf63a3c6 100644 --- a/databases_test.go +++ b/databases_test.go @@ -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:zt91mum075ofzyww@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, + }, + 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:zt91mum075ofzyww@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 + }, + "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" + } }`, }, }