Skip to content

Commit 23510bb

Browse files
Added support for VPC DBaaS Integration (#560)
* Implemented support for VPC DBaaS Integration * Added unit tests
1 parent 7b7f647 commit 23510bb

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

linode_api4/groups/database.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from linode_api4.objects import (
1010
Database,
1111
DatabaseEngine,
12+
DatabasePrivateNetwork,
1213
DatabaseType,
1314
MySQLDatabase,
1415
PostgreSQLDatabase,
@@ -126,6 +127,7 @@ def mysql_create(
126127
engine,
127128
ltype,
128129
engine_config: Union[MySQLDatabaseConfigOptions, Dict[str, Any]] = None,
130+
private_network: Union[DatabasePrivateNetwork, Dict[str, Any]] = None,
129131
**kwargs,
130132
):
131133
"""
@@ -159,6 +161,8 @@ def mysql_create(
159161
:type ltype: str or Type
160162
:param engine_config: The configuration options for this MySQL cluster
161163
:type engine_config: Dict[str, Any] or MySQLDatabaseConfigOptions
164+
:param private_network: The private network settings to use for this cluster
165+
:type private_network: Dict[str, Any] or DatabasePrivateNetwork
162166
"""
163167

164168
params = {
@@ -167,6 +171,7 @@ def mysql_create(
167171
"engine": engine,
168172
"type": ltype,
169173
"engine_config": engine_config,
174+
"private_network": private_network,
170175
}
171176
params.update(kwargs)
172177

@@ -262,6 +267,7 @@ def postgresql_create(
262267
engine_config: Union[
263268
PostgreSQLDatabaseConfigOptions, Dict[str, Any]
264269
] = None,
270+
private_network: Union[DatabasePrivateNetwork, Dict[str, Any]] = None,
265271
**kwargs,
266272
):
267273
"""
@@ -295,6 +301,8 @@ def postgresql_create(
295301
:type ltype: str or Type
296302
:param engine_config: The configuration options for this PostgreSQL cluster
297303
:type engine_config: Dict[str, Any] or PostgreSQLDatabaseConfigOptions
304+
:param private_network: The private network settings to use for this cluster
305+
:type private_network: Dict[str, Any] or DatabasePrivateNetwork
298306
"""
299307

300308
params = {
@@ -303,6 +311,7 @@ def postgresql_create(
303311
"engine": engine,
304312
"type": ltype,
305313
"engine_config": engine_config,
314+
"private_network": private_network,
306315
}
307316
params.update(kwargs)
308317

linode_api4/objects/database.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ def invalidate(self):
7474
Base.invalidate(self)
7575

7676

77+
@dataclass
78+
class DatabasePrivateNetwork(JSONObject):
79+
"""
80+
DatabasePrivateNetwork is used to specify
81+
a Database Cluster's private network settings during its creation.
82+
"""
83+
84+
vpc_id: Optional[int] = None
85+
subnet_id: Optional[int] = None
86+
public_access: Optional[bool] = None
87+
88+
7789
@deprecated(
7890
reason="Backups are not supported for non-legacy database clusters."
7991
)
@@ -304,6 +316,9 @@ class MySQLDatabase(Base):
304316
"engine_config": Property(
305317
mutable=True, json_object=MySQLDatabaseConfigOptions
306318
),
319+
"private_network": Property(
320+
mutable=True, json_object=DatabasePrivateNetwork, nullable=True
321+
),
307322
}
308323

309324
@property
@@ -470,6 +485,9 @@ class PostgreSQLDatabase(Base):
470485
"engine_config": Property(
471486
mutable=True, json_object=PostgreSQLDatabaseConfigOptions
472487
),
488+
"private_network": Property(
489+
mutable=True, json_object=DatabasePrivateNetwork, nullable=True
490+
),
473491
}
474492

475493
@property
@@ -636,6 +654,9 @@ class Database(Base):
636654
"updated": Property(),
637655
"updates": Property(),
638656
"version": Property(),
657+
"private_network": Property(
658+
json_object=DatabasePrivateNetwork, nullable=True
659+
),
639660
}
640661

641662
@property

test/fixtures/databases_instances.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
"hour_of_day": 0,
2828
"week_of_month": null
2929
},
30-
"version": "8.0.26"
30+
"version": "8.0.26",
31+
"private_network": {
32+
"vpc_id": 1234,
33+
"subnet_id": 5678,
34+
"public_access": true
35+
}
3136
}
3237
],
3338
"page": 1,

test/fixtures/databases_mysql_instances.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
"tmp_table_size": 16777216,
6262
"wait_timeout": 28800
6363
}
64+
},
65+
"private_network": {
66+
"vpc_id": 1234,
67+
"subnet_id": 5678,
68+
"public_access": true
6469
}
6570
}
6671
],

test/fixtures/databases_postgresql_instances.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
},
8484
"shared_buffers_percentage": 41.5,
8585
"work_mem": 4
86+
},
87+
"private_network": {
88+
"vpc_id": 1234,
89+
"subnet_id": 5678,
90+
"public_access": true
8691
}
8792
}
8893
],

test/unit/groups/database_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def test_get_databases(self):
6161
self.assertEqual(dbs[0].region, "us-east")
6262
self.assertEqual(dbs[0].updates.duration, 3)
6363
self.assertEqual(dbs[0].version, "8.0.26")
64+
self.assertEqual(dbs[0].private_network.vpc_id, 1234)
65+
self.assertEqual(dbs[0].private_network.subnet_id, 5678)
66+
self.assertEqual(dbs[0].private_network.public_access, True)
6467

6568
def test_database_instance(self):
6669
"""
@@ -1338,6 +1341,10 @@ def test_get_mysql_instances(self):
13381341
self.assertEqual(dbs[0].engine_config.mysql.tmp_table_size, 16777216)
13391342
self.assertEqual(dbs[0].engine_config.mysql.wait_timeout, 28800)
13401343

1344+
self.assertEqual(dbs[0].private_network.vpc_id, 1234)
1345+
self.assertEqual(dbs[0].private_network.subnet_id, 5678)
1346+
self.assertEqual(dbs[0].private_network.public_access, True)
1347+
13411348
def test_get_postgresql_instances(self):
13421349
"""
13431350
Test that postgresql instances can be retrieved properly
@@ -1452,3 +1459,7 @@ def test_get_postgresql_instances(self):
14521459
self.assertEqual(dbs[0].engine_config.pg.track_io_timing, "off")
14531460
self.assertEqual(dbs[0].engine_config.pg.wal_sender_timeout, 60000)
14541461
self.assertEqual(dbs[0].engine_config.pg.wal_writer_delay, 50)
1462+
1463+
self.assertEqual(dbs[0].private_network.vpc_id, 1234)
1464+
self.assertEqual(dbs[0].private_network.subnet_id, 5678)
1465+
self.assertEqual(dbs[0].private_network.public_access, True)

test/unit/objects/database_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from test.unit.base import ClientBaseCase
33

44
from linode_api4 import (
5+
DatabasePrivateNetwork,
56
MySQLDatabaseConfigMySQLOptions,
67
MySQLDatabaseConfigOptions,
78
PostgreSQLDatabase,
@@ -41,6 +42,11 @@ def test_create(self):
4142
),
4243
binlog_retention_period=200,
4344
),
45+
private_network=DatabasePrivateNetwork(
46+
vpc_id=1234,
47+
subnet_id=5678,
48+
public_access=True,
49+
),
4450
)
4551
except Exception as e:
4652
logger.warning(
@@ -61,6 +67,12 @@ def test_create(self):
6167
m.call_data["engine_config"]["binlog_retention_period"], 200
6268
)
6369

70+
self.assertEqual(m.call_data["private_network"]["vpc_id"], 1234)
71+
self.assertEqual(m.call_data["private_network"]["subnet_id"], 5678)
72+
self.assertEqual(
73+
m.call_data["private_network"]["public_access"], True
74+
)
75+
6476
def test_update(self):
6577
"""
6678
Test that the MySQL database can be updated
@@ -78,6 +90,11 @@ def test_update(self):
7890
mysql=MySQLDatabaseConfigMySQLOptions(connect_timeout=20),
7991
binlog_retention_period=200,
8092
)
93+
db.private_network = DatabasePrivateNetwork(
94+
vpc_id=1234,
95+
subnet_id=5678,
96+
public_access=True,
97+
)
8198

8299
db.save()
83100

@@ -93,6 +110,12 @@ def test_update(self):
93110
m.call_data["engine_config"]["binlog_retention_period"], 200
94111
)
95112

113+
self.assertEqual(m.call_data["private_network"]["vpc_id"], 1234)
114+
self.assertEqual(m.call_data["private_network"]["subnet_id"], 5678)
115+
self.assertEqual(
116+
m.call_data["private_network"]["public_access"], True
117+
)
118+
96119
def test_list_backups(self):
97120
"""
98121
Test that MySQL backups list properly
@@ -259,6 +282,11 @@ def test_create(self):
259282
),
260283
work_mem=4,
261284
),
285+
private_network=DatabasePrivateNetwork(
286+
vpc_id=1234,
287+
subnet_id=5678,
288+
public_access=True,
289+
),
262290
)
263291
except Exception:
264292
pass
@@ -302,6 +330,12 @@ def test_create(self):
302330
)
303331
self.assertEqual(m.call_data["engine_config"]["work_mem"], 4)
304332

333+
self.assertEqual(m.call_data["private_network"]["vpc_id"], 1234)
334+
self.assertEqual(m.call_data["private_network"]["subnet_id"], 5678)
335+
self.assertEqual(
336+
m.call_data["private_network"]["public_access"], True
337+
)
338+
305339
def test_update(self):
306340
"""
307341
Test that the PostgreSQL database can be updated
@@ -322,6 +356,12 @@ def test_update(self):
322356
work_mem=4,
323357
)
324358

359+
db.private_network = DatabasePrivateNetwork(
360+
vpc_id=1234,
361+
subnet_id=5678,
362+
public_access=True,
363+
)
364+
325365
db.save()
326366

327367
self.assertEqual(m.method, "put")
@@ -337,6 +377,12 @@ def test_update(self):
337377
)
338378
self.assertEqual(m.call_data["engine_config"]["work_mem"], 4)
339379

380+
self.assertEqual(m.call_data["private_network"]["vpc_id"], 1234)
381+
self.assertEqual(m.call_data["private_network"]["subnet_id"], 5678)
382+
self.assertEqual(
383+
m.call_data["private_network"]["public_access"], True
384+
)
385+
340386
def test_list_backups(self):
341387
"""
342388
Test that PostgreSQL backups list properly

0 commit comments

Comments
 (0)