Skip to content

Commit d537d4e

Browse files
feat(cloud): add create_and_poll() for subnets
* feat(cloud): add create_and_poll() for subnets * refactor(cloud): format subnets
1 parent ee59347 commit d537d4e

3 files changed

Lines changed: 138 additions & 16 deletions

File tree

examples/cloud/networks.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,16 @@ def update_network(*, client: Gcore, network_id: str) -> None:
7272

7373
def create_subnet(*, client: Gcore, network_id: str) -> str:
7474
print("\n=== CREATE SUBNET ===")
75-
response = client.cloud.networks.subnets.create(
75+
subnet = client.cloud.networks.subnets.create_and_poll(
7676
network_id=network_id,
7777
cidr="192.168.1.0/24",
7878
name="gcore-go-example",
7979
enable_dhcp=True,
8080
ip_version=4,
8181
)
82-
task_id = response.tasks[0]
83-
task = client.cloud.tasks.poll(task_id=task_id)
84-
if task.created_resources is None or task.created_resources.subnets is None:
85-
raise RuntimeError("Task completed but created_resources or subnets is missing")
86-
subnet_id: str = task.created_resources.subnets[0]
87-
print(f"Created subnet: ID={subnet_id}")
82+
print(f"Created subnet: ID={subnet.id}, CIDR={subnet.cidr}, name={subnet.name}")
8883
print("========================")
89-
return subnet_id
84+
return subnet.id or ""
9085

9186

9287
def list_subnets(*, client: Gcore, network_id: str) -> None:

examples/cloud/networks_async.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,16 @@ async def update_network(*, client: AsyncGcore, network_id: str) -> None:
7676

7777
async def create_subnet(*, client: AsyncGcore, network_id: str) -> str:
7878
print("\n=== CREATE SUBNET ===")
79-
response = await client.cloud.networks.subnets.create(
79+
subnet = await client.cloud.networks.subnets.create_and_poll(
8080
network_id=network_id,
8181
cidr="192.168.1.0/24",
8282
name="gcore-go-example",
8383
enable_dhcp=True,
8484
ip_version=4,
8585
)
86-
task_id = response.tasks[0]
87-
task = await client.cloud.tasks.poll(task_id=task_id)
88-
if task.created_resources is None or task.created_resources.subnets is None:
89-
raise RuntimeError("Task completed but created_resources or subnets is missing")
90-
subnet_id: str = task.created_resources.subnets[0]
91-
print(f"Created subnet: ID={subnet_id}")
86+
print(f"Created subnet: ID={subnet.id}, CIDR={subnet.cidr}, name={subnet.name}")
9287
print("========================")
93-
return subnet_id
88+
return subnet.id or ""
9489

9590

9691
async def list_subnets(*, client: AsyncGcore, network_id: str) -> None:

src/gcore/resources/cloud/networks/subnets.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,66 @@ def create(
148148
cast_to=TaskIDList,
149149
)
150150

151+
def create_and_poll(
152+
self,
153+
*,
154+
project_id: int | None = None,
155+
region_id: int | None = None,
156+
cidr: str,
157+
name: str,
158+
network_id: str,
159+
connect_to_network_router: bool | NotGiven = NOT_GIVEN,
160+
dns_nameservers: Optional[SequenceNotStr[str]] | NotGiven = NOT_GIVEN,
161+
enable_dhcp: bool | NotGiven = NOT_GIVEN,
162+
gateway_ip: Optional[str] | NotGiven = NOT_GIVEN,
163+
host_routes: Optional[Iterable[subnet_create_params.HostRoute]] | NotGiven = NOT_GIVEN,
164+
ip_version: IPVersion | NotGiven = NOT_GIVEN,
165+
router_id_to_connect: Optional[str] | NotGiven = NOT_GIVEN,
166+
tags: Dict[str, str] | NotGiven = NOT_GIVEN,
167+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
168+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
169+
# The extra values given here take precedence over values defined on the client or passed to this method.
170+
extra_headers: Headers | None = None,
171+
extra_query: Query | None = None,
172+
extra_body: Body | None = None,
173+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
174+
) -> Subnet:
175+
"""Create subnet and poll for the result."""
176+
response = self.create(
177+
project_id=project_id,
178+
region_id=region_id,
179+
cidr=cidr,
180+
name=name,
181+
network_id=network_id,
182+
connect_to_network_router=connect_to_network_router,
183+
dns_nameservers=dns_nameservers,
184+
enable_dhcp=enable_dhcp,
185+
gateway_ip=gateway_ip,
186+
host_routes=host_routes,
187+
ip_version=ip_version,
188+
router_id_to_connect=router_id_to_connect,
189+
tags=tags,
190+
extra_headers=extra_headers,
191+
extra_query=extra_query,
192+
extra_body=extra_body,
193+
timeout=timeout,
194+
)
195+
if not response.tasks or len(response.tasks) != 1:
196+
raise ValueError(f"Expected exactly one task to be created")
197+
task = self._client.cloud.tasks.poll(
198+
task_id=response.tasks[0],
199+
extra_headers=extra_headers,
200+
polling_interval_seconds=polling_interval_seconds,
201+
)
202+
if not task.created_resources or not task.created_resources.subnets or len(task.created_resources.subnets) != 1:
203+
raise ValueError(f"Expected exactly one resource to be created in a task")
204+
return self.get(
205+
subnet_id=task.created_resources.subnets[0],
206+
project_id=project_id,
207+
region_id=region_id,
208+
extra_headers=extra_headers,
209+
)
210+
151211
def update(
152212
self,
153213
subnet_id: str,
@@ -544,6 +604,66 @@ async def create(
544604
cast_to=TaskIDList,
545605
)
546606

607+
async def create_and_poll(
608+
self,
609+
*,
610+
project_id: int | None = None,
611+
region_id: int | None = None,
612+
cidr: str,
613+
name: str,
614+
network_id: str,
615+
connect_to_network_router: bool | NotGiven = NOT_GIVEN,
616+
dns_nameservers: Optional[SequenceNotStr[str]] | NotGiven = NOT_GIVEN,
617+
enable_dhcp: bool | NotGiven = NOT_GIVEN,
618+
gateway_ip: Optional[str] | NotGiven = NOT_GIVEN,
619+
host_routes: Optional[Iterable[subnet_create_params.HostRoute]] | NotGiven = NOT_GIVEN,
620+
ip_version: IPVersion | NotGiven = NOT_GIVEN,
621+
router_id_to_connect: Optional[str] | NotGiven = NOT_GIVEN,
622+
tags: Dict[str, str] | NotGiven = NOT_GIVEN,
623+
polling_interval_seconds: int | NotGiven = NOT_GIVEN,
624+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
625+
# The extra values given here take precedence over values defined on the client or passed to this method.
626+
extra_headers: Headers | None = None,
627+
extra_query: Query | None = None,
628+
extra_body: Body | None = None,
629+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
630+
) -> Subnet:
631+
"""Create subnet and poll for the result."""
632+
response = await self.create(
633+
project_id=project_id,
634+
region_id=region_id,
635+
cidr=cidr,
636+
name=name,
637+
network_id=network_id,
638+
connect_to_network_router=connect_to_network_router,
639+
dns_nameservers=dns_nameservers,
640+
enable_dhcp=enable_dhcp,
641+
gateway_ip=gateway_ip,
642+
host_routes=host_routes,
643+
ip_version=ip_version,
644+
router_id_to_connect=router_id_to_connect,
645+
tags=tags,
646+
extra_headers=extra_headers,
647+
extra_query=extra_query,
648+
extra_body=extra_body,
649+
timeout=timeout,
650+
)
651+
if not response.tasks or len(response.tasks) != 1:
652+
raise ValueError(f"Expected exactly one task to be created")
653+
task = await self._client.cloud.tasks.poll(
654+
task_id=response.tasks[0],
655+
extra_headers=extra_headers,
656+
polling_interval_seconds=polling_interval_seconds,
657+
)
658+
if not task.created_resources or not task.created_resources.subnets or len(task.created_resources.subnets) != 1:
659+
raise ValueError(f"Expected exactly one resource to be created in a task")
660+
return await self.get(
661+
subnet_id=task.created_resources.subnets[0],
662+
project_id=project_id,
663+
region_id=region_id,
664+
extra_headers=extra_headers,
665+
)
666+
547667
async def update(
548668
self,
549669
subnet_id: str,
@@ -828,6 +948,9 @@ def __init__(self, subnets: SubnetsResource) -> None:
828948
self.create = to_raw_response_wrapper(
829949
subnets.create,
830950
)
951+
self.create_and_poll = to_raw_response_wrapper(
952+
subnets.create_and_poll,
953+
)
831954
self.update = to_raw_response_wrapper(
832955
subnets.update,
833956
)
@@ -849,6 +972,9 @@ def __init__(self, subnets: AsyncSubnetsResource) -> None:
849972
self.create = async_to_raw_response_wrapper(
850973
subnets.create,
851974
)
975+
self.create_and_poll = async_to_raw_response_wrapper(
976+
subnets.create_and_poll,
977+
)
852978
self.update = async_to_raw_response_wrapper(
853979
subnets.update,
854980
)
@@ -870,6 +996,9 @@ def __init__(self, subnets: SubnetsResource) -> None:
870996
self.create = to_streamed_response_wrapper(
871997
subnets.create,
872998
)
999+
self.create_and_poll = to_streamed_response_wrapper(
1000+
subnets.create_and_poll,
1001+
)
8731002
self.update = to_streamed_response_wrapper(
8741003
subnets.update,
8751004
)
@@ -891,6 +1020,9 @@ def __init__(self, subnets: AsyncSubnetsResource) -> None:
8911020
self.create = async_to_streamed_response_wrapper(
8921021
subnets.create,
8931022
)
1023+
self.create_and_poll = async_to_streamed_response_wrapper(
1024+
subnets.create_and_poll,
1025+
)
8941026
self.update = async_to_streamed_response_wrapper(
8951027
subnets.update,
8961028
)

0 commit comments

Comments
 (0)