Skip to content

Commit e645a83

Browse files
feat(cloud): add create_and_poll() and delete_and_poll() for floating ips
1 parent f6d4e0b commit e645a83

3 files changed

Lines changed: 204 additions & 20 deletions

File tree

examples/cloud/floating_ips.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ def main() -> None:
3333

3434
def create_floating_ip(*, client: Gcore) -> str:
3535
print("\n=== CREATE FLOATING IP ===")
36-
response = client.cloud.floating_ips.create(tags={"name": "gcore-go-example"})
37-
task = client.cloud.tasks.poll(task_id=response.tasks[0])
38-
if task.created_resources is None or task.created_resources.floatingips is None:
39-
raise RuntimeError("Task completed but created_resources or floatingips is missing")
40-
floating_ip_id: str = task.created_resources.floatingips[0]
41-
print(f"Created Floating IP: ID={floating_ip_id}")
36+
floating_ip = client.cloud.floating_ips.create_and_poll(tags={"name": "gcore-go-example"})
37+
if floating_ip.id is None:
38+
raise RuntimeError("Failed to create floating IP")
39+
print(f"Created Floating IP: ID={floating_ip.id}")
4240
print("========================")
43-
return floating_ip_id
41+
return floating_ip.id
4442

4543

4644
def list_floating_ips(*, client: Gcore) -> None:
@@ -81,9 +79,7 @@ def unassign_floating_ip(*, client: Gcore, floating_ip_id: str) -> None:
8179

8280
def delete_floating_ip(*, client: Gcore, floating_ip_id: str) -> None:
8381
print("\n=== DELETE FLOATING IP ===")
84-
response = client.cloud.floating_ips.delete(floating_ip_id=floating_ip_id)
85-
task_id = response.tasks[0]
86-
client.cloud.tasks.poll(task_id=task_id)
82+
client.cloud.floating_ips.delete_and_poll(floating_ip_id=floating_ip_id)
8783
print(f"Deleted floating IP: ID={floating_ip_id}")
8884
print("========================")
8985

examples/cloud/floating_ips_async.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ async def main() -> None:
3434

3535
async def create_floating_ip(*, client: AsyncGcore) -> str:
3636
print("\n=== CREATE FLOATING IP ===")
37-
response = await client.cloud.floating_ips.create(tags={"name": "gcore-go-example"})
38-
task = await client.cloud.tasks.poll(task_id=response.tasks[0])
39-
if task.created_resources is None or task.created_resources.floatingips is None:
40-
raise RuntimeError("Task completed but created_resources or floatingips is missing")
41-
floating_ip_id: str = task.created_resources.floatingips[0]
42-
print(f"Created floating IP: ID={floating_ip_id}")
37+
floating_ip = await client.cloud.floating_ips.create_and_poll(tags={"name": "gcore-go-example"})
38+
if floating_ip.id is None:
39+
raise RuntimeError("Failed to create floating IP")
40+
print(f"Created floating IP: ID={floating_ip.id}")
4341
print("========================")
44-
return floating_ip_id
42+
return floating_ip.id
4543

4644

4745
async def list_floating_ips(*, client: AsyncGcore) -> None:
@@ -81,9 +79,7 @@ async def unassign_floating_ip(*, client: AsyncGcore, floating_ip_id: str) -> No
8179

8280
async def delete_floating_ip(*, client: AsyncGcore, floating_ip_id: str) -> None:
8381
print("\n=== DELETE FLOATING IP ===")
84-
response = await client.cloud.floating_ips.delete(floating_ip_id=floating_ip_id)
85-
task_id = response.tasks[0]
86-
await client.cloud.tasks.poll(task_id=task_id)
82+
await client.cloud.floating_ips.delete_and_poll(floating_ip_id=floating_ip_id)
8783
print(f"Deleted floating IP: ID={floating_ip_id}")
8884
print("========================")
8985

src/gcore/resources/cloud/floating_ips.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,90 @@ def unassign(
356356
cast_to=FloatingIP,
357357
)
358358

359+
def create_and_poll(
360+
self,
361+
*,
362+
project_id: int | None = None,
363+
region_id: int | None = None,
364+
fixed_ip_address: Optional[str] | NotGiven = NOT_GIVEN,
365+
port_id: Optional[str] | NotGiven = NOT_GIVEN,
366+
tags: Dict[str, str] | NotGiven = NOT_GIVEN,
367+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
368+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
369+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
370+
# The extra values given here take precedence over values defined on the client or passed to this method.
371+
extra_headers: Headers | None = None,
372+
extra_query: Query | None = None,
373+
extra_body: Body | None = None,
374+
) -> FloatingIP:
375+
"""
376+
Create floating IP and poll for the result. Only the first task will be polled. If you need to poll more tasks, use the `tasks.poll` method.
377+
"""
378+
response = self.create(
379+
project_id=project_id,
380+
region_id=region_id,
381+
fixed_ip_address=fixed_ip_address,
382+
port_id=port_id,
383+
tags=tags,
384+
extra_headers=extra_headers,
385+
extra_query=extra_query,
386+
extra_body=extra_body,
387+
timeout=timeout,
388+
)
389+
if not response.tasks:
390+
raise ValueError("Expected at least one task to be created")
391+
task = self._client.cloud.tasks.poll(
392+
task_id=response.tasks[0],
393+
extra_headers=extra_headers,
394+
polling_interval_seconds=polling_interval_seconds,
395+
)
396+
if task.created_resources is None or task.created_resources.floatingips is None:
397+
raise ValueError("Task completed but created_resources or floatingips is missing")
398+
floating_ip_id = task.created_resources.floatingips[0]
399+
return self.get(
400+
floating_ip_id=floating_ip_id,
401+
project_id=project_id,
402+
region_id=region_id,
403+
extra_headers=extra_headers,
404+
extra_query=extra_query,
405+
extra_body=extra_body,
406+
timeout=timeout,
407+
)
408+
409+
def delete_and_poll(
410+
self,
411+
floating_ip_id: str,
412+
*,
413+
project_id: int | None = None,
414+
region_id: int | None = None,
415+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
416+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
417+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
418+
# The extra values given here take precedence over values defined on the client or passed to this method.
419+
extra_headers: Headers | None = None,
420+
extra_query: Query | None = None,
421+
extra_body: Body | None = None,
422+
) -> None:
423+
"""
424+
Delete floating IP and poll for the result. Only the first task will be polled. If you need to poll more tasks, use the `tasks.poll` method.
425+
"""
426+
response = self.delete(
427+
floating_ip_id=floating_ip_id,
428+
project_id=project_id,
429+
region_id=region_id,
430+
extra_headers=extra_headers,
431+
extra_query=extra_query,
432+
extra_body=extra_body,
433+
timeout=timeout,
434+
)
435+
if not response.tasks:
436+
raise ValueError("Expected at least one task to be created")
437+
self._client.cloud.tasks.poll(
438+
task_id=response.tasks[0],
439+
extra_headers=extra_headers,
440+
polling_interval_seconds=polling_interval_seconds,
441+
)
442+
359443

360444
class AsyncFloatingIPsResource(AsyncAPIResource):
361445
@cached_property
@@ -687,6 +771,90 @@ async def unassign(
687771
cast_to=FloatingIP,
688772
)
689773

774+
async def create_and_poll(
775+
self,
776+
*,
777+
project_id: int | None = None,
778+
region_id: int | None = None,
779+
fixed_ip_address: Optional[str] | NotGiven = NOT_GIVEN,
780+
port_id: Optional[str] | NotGiven = NOT_GIVEN,
781+
tags: Dict[str, str] | NotGiven = NOT_GIVEN,
782+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
783+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
784+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
785+
# The extra values given here take precedence over values defined on the client or passed to this method.
786+
extra_headers: Headers | None = None,
787+
extra_query: Query | None = None,
788+
extra_body: Body | None = None,
789+
) -> FloatingIP:
790+
"""
791+
Create floating IP and poll for the result. Only the first task will be polled. If you need to poll more tasks, use the `tasks.poll` method.
792+
"""
793+
response = await self.create(
794+
project_id=project_id,
795+
region_id=region_id,
796+
fixed_ip_address=fixed_ip_address,
797+
port_id=port_id,
798+
tags=tags,
799+
extra_headers=extra_headers,
800+
extra_query=extra_query,
801+
extra_body=extra_body,
802+
timeout=timeout,
803+
)
804+
if not response.tasks:
805+
raise ValueError("Expected at least one task to be created")
806+
task = await self._client.cloud.tasks.poll(
807+
task_id=response.tasks[0],
808+
extra_headers=extra_headers,
809+
polling_interval_seconds=polling_interval_seconds,
810+
)
811+
if task.created_resources is None or task.created_resources.floatingips is None:
812+
raise ValueError("Task completed but created_resources or floatingips is missing")
813+
floating_ip_id = task.created_resources.floatingips[0]
814+
return await self.get(
815+
floating_ip_id=floating_ip_id,
816+
project_id=project_id,
817+
region_id=region_id,
818+
extra_headers=extra_headers,
819+
extra_query=extra_query,
820+
extra_body=extra_body,
821+
timeout=timeout,
822+
)
823+
824+
async def delete_and_poll(
825+
self,
826+
floating_ip_id: str,
827+
*,
828+
project_id: int | None = None,
829+
region_id: int | None = None,
830+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
831+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
832+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
833+
# The extra values given here take precedence over values defined on the client or passed to this method.
834+
extra_headers: Headers | None = None,
835+
extra_query: Query | None = None,
836+
extra_body: Body | None = None,
837+
) -> None:
838+
"""
839+
Delete floating IP and poll for the result. Only the first task will be polled. If you need to poll more tasks, use the `tasks.poll` method.
840+
"""
841+
response = await self.delete(
842+
floating_ip_id=floating_ip_id,
843+
project_id=project_id,
844+
region_id=region_id,
845+
extra_headers=extra_headers,
846+
extra_query=extra_query,
847+
extra_body=extra_body,
848+
timeout=timeout,
849+
)
850+
if not response.tasks:
851+
raise ValueError("Expected at least one task to be created")
852+
await self._client.cloud.tasks.poll(
853+
task_id=response.tasks[0],
854+
extra_headers=extra_headers,
855+
polling_interval_seconds=polling_interval_seconds,
856+
)
857+
690858

691859
class FloatingIPsResourceWithRawResponse:
692860
def __init__(self, floating_ips: FloatingIPsResource) -> None:
@@ -710,6 +878,12 @@ def __init__(self, floating_ips: FloatingIPsResource) -> None:
710878
self.unassign = to_raw_response_wrapper(
711879
floating_ips.unassign,
712880
)
881+
self.create_and_poll = to_raw_response_wrapper(
882+
floating_ips.create_and_poll,
883+
)
884+
self.delete_and_poll = to_raw_response_wrapper(
885+
floating_ips.delete_and_poll,
886+
)
713887

714888

715889
class AsyncFloatingIPsResourceWithRawResponse:
@@ -734,6 +908,12 @@ def __init__(self, floating_ips: AsyncFloatingIPsResource) -> None:
734908
self.unassign = async_to_raw_response_wrapper(
735909
floating_ips.unassign,
736910
)
911+
self.create_and_poll = async_to_raw_response_wrapper(
912+
floating_ips.create_and_poll,
913+
)
914+
self.delete_and_poll = async_to_raw_response_wrapper(
915+
floating_ips.delete_and_poll,
916+
)
737917

738918

739919
class FloatingIPsResourceWithStreamingResponse:
@@ -758,6 +938,12 @@ def __init__(self, floating_ips: FloatingIPsResource) -> None:
758938
self.unassign = to_streamed_response_wrapper(
759939
floating_ips.unassign,
760940
)
941+
self.create_and_poll = to_streamed_response_wrapper(
942+
floating_ips.create_and_poll,
943+
)
944+
self.delete_and_poll = to_streamed_response_wrapper(
945+
floating_ips.delete_and_poll,
946+
)
761947

762948

763949
class AsyncFloatingIPsResourceWithStreamingResponse:
@@ -782,3 +968,9 @@ def __init__(self, floating_ips: AsyncFloatingIPsResource) -> None:
782968
self.unassign = async_to_streamed_response_wrapper(
783969
floating_ips.unassign,
784970
)
971+
self.create_and_poll = async_to_streamed_response_wrapper(
972+
floating_ips.create_and_poll,
973+
)
974+
self.delete_and_poll = async_to_streamed_response_wrapper(
975+
floating_ips.delete_and_poll,
976+
)

0 commit comments

Comments
 (0)