|
1 | 1 | from linode_api4.errors import UnexpectedResponseError
|
2 | 2 | from linode_api4.groups import Group
|
3 | 3 | from linode_api4.objects import (
|
4 |
| - Base, |
5 | 4 | Database,
|
6 | 5 | DatabaseEngine,
|
7 | 6 | DatabaseType,
|
8 | 7 | MySQLDatabase,
|
9 | 8 | PostgreSQLDatabase,
|
| 9 | + drop_null_keys, |
10 | 10 | )
|
| 11 | +from linode_api4.objects.base import _flatten_request_body_recursive |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class DatabaseGroup(Group):
|
@@ -126,13 +127,71 @@ def mysql_create(self, label, region, engine, ltype, **kwargs):
|
126 | 127 |
|
127 | 128 | params = {
|
128 | 129 | "label": label,
|
129 |
| - "region": region.id if issubclass(type(region), Base) else region, |
130 |
| - "engine": engine.id if issubclass(type(engine), Base) else engine, |
131 |
| - "type": ltype.id if issubclass(type(ltype), Base) else ltype, |
| 130 | + "region": region, |
| 131 | + "engine": engine, |
| 132 | + "type": ltype, |
132 | 133 | }
|
133 | 134 | params.update(kwargs)
|
134 | 135 |
|
135 |
| - result = self.client.post("/databases/mysql/instances", data=params) |
| 136 | + result = self.client.post( |
| 137 | + "/databases/mysql/instances", |
| 138 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
| 139 | + ) |
| 140 | + |
| 141 | + if "id" not in result: |
| 142 | + raise UnexpectedResponseError( |
| 143 | + "Unexpected response when creating MySQL Database", json=result |
| 144 | + ) |
| 145 | + |
| 146 | + d = MySQLDatabase(self.client, result["id"], result) |
| 147 | + return d |
| 148 | + |
| 149 | + def mysql_fork(self, source, restore_time, **kwargs): |
| 150 | + """ |
| 151 | + Forks an :any:`MySQLDatabase` on this account with |
| 152 | + the given restore_time. label, region, engine, and ltype are optional. |
| 153 | + For example:: |
| 154 | +
|
| 155 | + client = LinodeClient(TOKEN) |
| 156 | +
|
| 157 | + db_to_fork = client.database.mysql_instances()[0] |
| 158 | +
|
| 159 | + new_fork = client.database.mysql_fork( |
| 160 | + db_to_fork.id, |
| 161 | + db_to_fork.updated, |
| 162 | + label="new-fresh-label" |
| 163 | + ) |
| 164 | +
|
| 165 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-mysql-instances |
| 166 | +
|
| 167 | + :param source: The id of the source database |
| 168 | + :type source: int |
| 169 | + :param restore_time: The timestamp for the fork |
| 170 | + :type restore_time: datetime |
| 171 | + :param label: The name for this cluster |
| 172 | + :type label: str |
| 173 | + :param region: The region to deploy this cluster in |
| 174 | + :type region: str | Region |
| 175 | + :param engine: The engine to deploy this cluster with |
| 176 | + :type engine: str | Engine |
| 177 | + :param ltype: The Linode Type to use for this cluster |
| 178 | + :type ltype: str | Type |
| 179 | + """ |
| 180 | + |
| 181 | + params = { |
| 182 | + "fork": { |
| 183 | + "source": source, |
| 184 | + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), |
| 185 | + } |
| 186 | + } |
| 187 | + if "ltype" in kwargs: |
| 188 | + params["type"] = kwargs["ltype"] |
| 189 | + params.update(kwargs) |
| 190 | + |
| 191 | + result = self.client.post( |
| 192 | + "/databases/mysql/instances", |
| 193 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
| 194 | + ) |
136 | 195 |
|
137 | 196 | if "id" not in result:
|
138 | 197 | raise UnexpectedResponseError(
|
@@ -191,14 +250,71 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs):
|
191 | 250 |
|
192 | 251 | params = {
|
193 | 252 | "label": label,
|
194 |
| - "region": region.id if issubclass(type(region), Base) else region, |
195 |
| - "engine": engine.id if issubclass(type(engine), Base) else engine, |
196 |
| - "type": ltype.id if issubclass(type(ltype), Base) else ltype, |
| 253 | + "region": region, |
| 254 | + "engine": engine, |
| 255 | + "type": ltype, |
| 256 | + } |
| 257 | + params.update(kwargs) |
| 258 | + |
| 259 | + result = self.client.post( |
| 260 | + "/databases/postgresql/instances", |
| 261 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
| 262 | + ) |
| 263 | + |
| 264 | + if "id" not in result: |
| 265 | + raise UnexpectedResponseError( |
| 266 | + "Unexpected response when creating PostgreSQL Database", |
| 267 | + json=result, |
| 268 | + ) |
| 269 | + |
| 270 | + d = PostgreSQLDatabase(self.client, result["id"], result) |
| 271 | + return d |
| 272 | + |
| 273 | + def postgresql_fork(self, source, restore_time, **kwargs): |
| 274 | + """ |
| 275 | + Forks an :any:`PostgreSQLDatabase` on this account with |
| 276 | + the given restore_time. label, region, engine, and ltype are optional. |
| 277 | + For example:: |
| 278 | +
|
| 279 | + client = LinodeClient(TOKEN) |
| 280 | +
|
| 281 | + db_to_fork = client.database.postgresql_instances()[0] |
| 282 | +
|
| 283 | + new_fork = client.database.postgresql_fork( |
| 284 | + db_to_fork.id, |
| 285 | + db_to_fork.updated, |
| 286 | + label="new-fresh-label" |
| 287 | + ) |
| 288 | +
|
| 289 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-postgresql-instances |
| 290 | +
|
| 291 | + :param source: The id of the source database |
| 292 | + :type source: int |
| 293 | + :param restore_time: The timestamp for the fork |
| 294 | + :type restore_time: datetime |
| 295 | + :param label: The name for this cluster |
| 296 | + :type label: str |
| 297 | + :param region: The region to deploy this cluster in |
| 298 | + :type region: str | Region |
| 299 | + :param engine: The engine to deploy this cluster with |
| 300 | + :type engine: str | Engine |
| 301 | + :param ltype: The Linode Type to use for this cluster |
| 302 | + :type ltype: str | Type |
| 303 | + """ |
| 304 | + |
| 305 | + params = { |
| 306 | + "fork": { |
| 307 | + "source": source, |
| 308 | + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), |
| 309 | + } |
197 | 310 | }
|
| 311 | + if "ltype" in kwargs: |
| 312 | + params["type"] = kwargs["ltype"] |
198 | 313 | params.update(kwargs)
|
199 | 314 |
|
200 | 315 | result = self.client.post(
|
201 |
| - "/databases/postgresql/instances", data=params |
| 316 | + "/databases/postgresql/instances", |
| 317 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
202 | 318 | )
|
203 | 319 |
|
204 | 320 | if "id" not in result:
|
|
0 commit comments