|
17 | 17 | Region, |
18 | 18 | ) |
19 | 19 | from linode_api4.objects.base import _flatten_request_body_recursive |
| 20 | +from linode_api4.objects.networking import ReservedIPAddress, ReservedIPType |
20 | 21 | from linode_api4.util import drop_null_keys |
21 | 22 |
|
22 | 23 |
|
@@ -328,29 +329,53 @@ def ips_assign(self, region, *assignments): |
328 | 329 | }, |
329 | 330 | ) |
330 | 331 |
|
331 | | - def ip_allocate(self, linode, public=True): |
| 332 | + def ip_allocate( |
| 333 | + self, linode=None, public=True, reserved=False, region=None |
| 334 | + ): |
332 | 335 | """ |
333 | | - Allocates an IP to a Instance you own. Additional IPs must be requested |
334 | | - by opening a support ticket first. |
| 336 | + Allocates an IP to a Instance you own, or reserves a new IP address. |
| 337 | +
|
| 338 | + When ``reserved`` is False (default), ``linode`` is required and an |
| 339 | + ephemeral IP is allocated and assigned to that Instance. |
| 340 | +
|
| 341 | + When ``reserved`` is True, either ``region`` or ``linode`` must be |
| 342 | + provided. Passing only ``region`` creates an unassigned reserved IP. |
| 343 | + Passing ``linode`` (with or without ``region``) creates a reserved IP |
| 344 | + in the Instance's region and assigns it to that Instance. |
335 | 345 |
|
336 | 346 | API Documentation: https://techdocs.akamai.com/linode-api/reference/post-allocate-ip |
337 | 347 |
|
338 | 348 | :param linode: The Instance to allocate the new IP for. |
339 | 349 | :type linode: Instance or int |
340 | 350 | :param public: If True, allocate a public IP address. Defaults to True. |
341 | 351 | :type public: bool |
| 352 | + :param reserved: If True, reserve the new IP address. |
| 353 | + NOTE: Reserved IP feature may not currently be available to all users. |
| 354 | + :type reserved: bool |
| 355 | + :param region: The region for the reserved IP (required when reserved=True and linode is not set). |
| 356 | + NOTE: Reserved IP feature may not currently be available to all users. |
| 357 | + :type region: str or Region |
342 | 358 |
|
343 | 359 | :returns: The new IPAddress. |
344 | 360 | :rtype: IPAddress |
345 | 361 | """ |
346 | | - result = self.client.post( |
347 | | - "/networking/ips/", |
348 | | - data={ |
349 | | - "linode_id": linode.id if isinstance(linode, Base) else linode, |
350 | | - "type": "ipv4", |
351 | | - "public": public, |
352 | | - }, |
353 | | - ) |
| 362 | + data = { |
| 363 | + "type": "ipv4", |
| 364 | + "public": public, |
| 365 | + } |
| 366 | + |
| 367 | + if linode is not None: |
| 368 | + data["linode_id"] = ( |
| 369 | + linode.id if isinstance(linode, Base) else linode |
| 370 | + ) |
| 371 | + |
| 372 | + if reserved: |
| 373 | + data["reserved"] = True |
| 374 | + |
| 375 | + if region is not None: |
| 376 | + data["region"] = region.id if isinstance(region, Base) else region |
| 377 | + |
| 378 | + result = self.client.post("/networking/ips/", data=data) |
354 | 379 |
|
355 | 380 | if not "address" in result: |
356 | 381 | raise UnexpectedResponseError( |
@@ -510,3 +535,71 @@ def delete_vlan(self, vlan, region): |
510 | 535 | return False |
511 | 536 |
|
512 | 537 | return True |
| 538 | + |
| 539 | + def reserved_ips(self, *filters): |
| 540 | + """ |
| 541 | + Returns a list of reserved IPv4 addresses on your account. |
| 542 | +
|
| 543 | + NOTE: Reserved IP feature may not currently be available to all users. |
| 544 | +
|
| 545 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-reserved-ips |
| 546 | +
|
| 547 | + :param filters: Any number of filters to apply to this query. |
| 548 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 549 | + for more details on filtering. |
| 550 | +
|
| 551 | + :returns: A list of reserved IP addresses on the account. |
| 552 | + :rtype: PaginatedList of ReservedIPAddress |
| 553 | + """ |
| 554 | + return self.client._get_and_filter(ReservedIPAddress, *filters) |
| 555 | + |
| 556 | + def reserved_ip_create(self, region, tags=None, **kwargs): |
| 557 | + """ |
| 558 | + Reserves a new IPv4 address in the given region. |
| 559 | +
|
| 560 | + NOTE: Reserved IP feature may not currently be available to all users. |
| 561 | +
|
| 562 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-reserve-ip |
| 563 | +
|
| 564 | + :param region: The region in which to reserve the IP. |
| 565 | + :type region: str or Region |
| 566 | + :param tags: Tags to apply to the reserved IP. |
| 567 | + :type tags: list of str |
| 568 | +
|
| 569 | + :returns: The new reserved IP address. |
| 570 | + :rtype: ReservedIPAddress |
| 571 | + """ |
| 572 | + params = { |
| 573 | + "region": region.id if isinstance(region, Region) else region, |
| 574 | + } |
| 575 | + if tags is not None: |
| 576 | + params["tags"] = tags |
| 577 | + params.update(kwargs) |
| 578 | + |
| 579 | + result = self.client.post("/networking/reserved/ips", data=params) |
| 580 | + |
| 581 | + if "address" not in result: |
| 582 | + raise UnexpectedResponseError( |
| 583 | + "Unexpected response when reserving IP address!", json=result |
| 584 | + ) |
| 585 | + |
| 586 | + return ReservedIPAddress(self.client, result["address"], result) |
| 587 | + |
| 588 | + def reserved_ip_types(self, *filters): |
| 589 | + """ |
| 590 | + Returns a list of reserved IP types with pricing information. |
| 591 | +
|
| 592 | + NOTE: Reserved IP feature may not currently be available to all users. |
| 593 | +
|
| 594 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-reserved-iptypes |
| 595 | +
|
| 596 | + :param filters: Any number of filters to apply to this query. |
| 597 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 598 | + for more details on filtering. |
| 599 | +
|
| 600 | + :returns: A list of reserved IP types. |
| 601 | + :rtype: PaginatedList of ReservedIPType |
| 602 | + """ |
| 603 | + return self.client._get_and_filter( |
| 604 | + ReservedIPType, *filters, endpoint="/networking/reserved/ips/types" |
| 605 | + ) |
0 commit comments