-
Notifications
You must be signed in to change notification settings - Fork 85
TPT-4278: python-sdk: Implement support for Reserved IP for IPv4 #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: proj/reserved-ips
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import sys | ||
| import os | ||
|
|
||
| # Ensure the repo root is on sys.path so that `from test.unit.base import ...` | ||
| # works regardless of which directory pytest is invoked from. | ||
| sys.path.insert(0, os.path.dirname(__file__)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| Region, | ||
| ) | ||
| from linode_api4.objects.base import _flatten_request_body_recursive | ||
| from linode_api4.objects.networking import ReservedIPAddress, ReservedIPType | ||
| from linode_api4.util import drop_null_keys | ||
|
|
||
|
|
||
|
|
@@ -328,29 +329,53 @@ def ips_assign(self, region, *assignments): | |
| }, | ||
| ) | ||
|
|
||
| def ip_allocate(self, linode, public=True): | ||
| def ip_allocate( | ||
| self, linode=None, public=True, reserved=False, region=None | ||
| ): | ||
| """ | ||
| Allocates an IP to a Instance you own. Additional IPs must be requested | ||
| by opening a support ticket first. | ||
| Allocates an IP to a Instance you own, or reserves a new IP address. | ||
|
|
||
| When ``reserved`` is False (default), ``linode`` is required and an | ||
| ephemeral IP is allocated and assigned to that Instance. | ||
|
|
||
| When ``reserved`` is True, either ``region`` or ``linode`` must be | ||
| provided. Passing only ``region`` creates an unassigned reserved IP. | ||
| Passing ``linode`` (with or without ``region``) creates a reserved IP | ||
| in the Instance's region and assigns it to that Instance. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/post-allocate-ip | ||
|
|
||
| :param linode: The Instance to allocate the new IP for. | ||
| :type linode: Instance or int | ||
| :param public: If True, allocate a public IP address. Defaults to True. | ||
| :type public: bool | ||
| :param reserved: If True, reserve the new IP address. | ||
| NOTE: Reserved IP feature may not currently be available to all users. | ||
| :type reserved: bool | ||
| :param region: The region for the reserved IP (required when reserved=True and linode is not set). | ||
| NOTE: Reserved IP feature may not currently be available to all users. | ||
| :type region: str or Region | ||
|
|
||
| :returns: The new IPAddress. | ||
| :rtype: IPAddress | ||
| """ | ||
| result = self.client.post( | ||
| "/networking/ips/", | ||
| data={ | ||
| "linode_id": linode.id if isinstance(linode, Base) else linode, | ||
| "type": "ipv4", | ||
| "public": public, | ||
| }, | ||
| ) | ||
| data = { | ||
| "type": "ipv4", | ||
| "public": public, | ||
| } | ||
|
|
||
|
Comment on lines
+362
to
+366
|
||
| if linode is not None: | ||
| data["linode_id"] = ( | ||
| linode.id if isinstance(linode, Base) else linode | ||
| ) | ||
|
|
||
| if reserved: | ||
| data["reserved"] = True | ||
|
|
||
| if region is not None: | ||
| data["region"] = region.id if isinstance(region, Base) else region | ||
|
|
||
| result = self.client.post("/networking/ips/", data=data) | ||
|
|
||
| if not "address" in result: | ||
| raise UnexpectedResponseError( | ||
|
|
@@ -510,3 +535,71 @@ def delete_vlan(self, vlan, region): | |
| return False | ||
|
|
||
| return True | ||
|
|
||
| def reserved_ips(self, *filters): | ||
| """ | ||
| Returns a list of reserved IPv4 addresses on your account. | ||
|
|
||
| NOTE: Reserved IP feature may not currently be available to all users. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/get-reserved-ips | ||
|
|
||
| :param filters: Any number of filters to apply to this query. | ||
| See :doc:`Filtering Collections</linode_api4/objects/filtering>` | ||
| for more details on filtering. | ||
|
|
||
| :returns: A list of reserved IP addresses on the account. | ||
| :rtype: PaginatedList of ReservedIPAddress | ||
| """ | ||
| return self.client._get_and_filter(ReservedIPAddress, *filters) | ||
|
|
||
| def reserved_ip_create(self, region, tags=None, **kwargs): | ||
| """ | ||
| Reserves a new IPv4 address in the given region. | ||
|
|
||
| NOTE: Reserved IP feature may not currently be available to all users. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/post-reserve-ip | ||
|
|
||
| :param region: The region in which to reserve the IP. | ||
| :type region: str or Region | ||
| :param tags: Tags to apply to the reserved IP. | ||
| :type tags: list of str | ||
|
|
||
| :returns: The new reserved IP address. | ||
| :rtype: ReservedIPAddress | ||
| """ | ||
| params = { | ||
| "region": region.id if isinstance(region, Region) else region, | ||
| } | ||
| if tags is not None: | ||
| params["tags"] = tags | ||
| params.update(kwargs) | ||
|
|
||
| result = self.client.post("/networking/reserved/ips", data=params) | ||
|
|
||
| if "address" not in result: | ||
| raise UnexpectedResponseError( | ||
| "Unexpected response when reserving IP address!", json=result | ||
| ) | ||
|
|
||
| return ReservedIPAddress(self.client, result["address"], result) | ||
|
|
||
| def reserved_ip_types(self, *filters): | ||
| """ | ||
| Returns a list of reserved IP types with pricing information. | ||
|
|
||
| NOTE: Reserved IP feature may not currently be available to all users. | ||
|
|
||
| API Documentation: https://techdocs.akamai.com/linode-api/reference/get-reserved-iptypes | ||
|
|
||
| :param filters: Any number of filters to apply to this query. | ||
| See :doc:`Filtering Collections</linode_api4/objects/filtering>` | ||
| for more details on filtering. | ||
|
|
||
| :returns: A list of reserved IP types. | ||
| :rtype: PaginatedList of ReservedIPType | ||
| """ | ||
| return self.client._get_and_filter( | ||
| ReservedIPType, *filters, endpoint="/networking/reserved/ips/types" | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ def create( | |
| domains=None, | ||
| nodebalancers=None, | ||
| volumes=None, | ||
| reserved_ipv4_addresses=None, | ||
| entities=[], | ||
| ): | ||
|
Comment on lines
36
to
37
|
||
| """ | ||
|
|
@@ -61,6 +62,9 @@ def create( | |
| :param volumes: A list of Volumes to apply this Tag to upon | ||
| creation | ||
| :type volumes: list of Volumes or list of int | ||
| :param reserved_ipv4_addresses: A list of reserved IPv4 addresses to apply | ||
| this Tag to upon creation. | ||
| :type reserved_ipv4_addresses: list of str | ||
|
|
||
| :returns: The new Tag | ||
| :rtype: Tag | ||
|
|
@@ -103,6 +107,7 @@ def create( | |
| "nodebalancers": nodebalancer_ids or None, | ||
| "domains": domain_ids or None, | ||
| "volumes": volume_ids or None, | ||
| "reserved_ipv4_addresses": reserved_ipv4_addresses or None, | ||
| } | ||
|
|
||
| result = self.client.post("/tags", data=params) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [pytest] | ||
| testpaths = test | ||
| markers = | ||
| smoke: mark a test as a smoke test | ||
| flaky: mark a test as a flaky test for rerun | ||
| python_files = *_test.py test_*.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we restrict the type for the parameters? We prefer not implementing generic types for python-sdk.
Same for the rest functions in this PR