Skip to content

Commit 9e8d30f

Browse files
authored
feat(instance): add create_server wrapper (scaleway#1299)
1 parent 855c526 commit 9e8d30f

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ test:
5858
poetry run pytest -v; \
5959
done
6060

61+
update-goldens:
62+
PYTHON_UPDATE_CASSETTES=true $(MAKE) test
63+
6164
publish: install-dependencies
6265
for lib in $(LIBRARIES); do \
6366
cd ${WORKDIR}/$$lib && \

scaleway/scaleway/instance/v1/custom_api.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from .api import InstanceV1API
1010
from .custom_marshalling import marshal_GetServerUserDataRequest
1111
from .custom_types import GetServerUserDataRequest, GetAllServerUserDataResponse
12+
from .types import (
13+
VolumeServerTemplate,
14+
BootType,
15+
CreateServerResponse,
16+
)
1217

1318
max_retry = 10
1419
interval = 0.01
@@ -146,3 +151,85 @@ def wait_instance_server(self, server_id: str, zone: ScwZone) -> GetServerRespon
146151
f"Server {server_id} in zone {zone} did not reach a stable state "
147152
f"after {max_retry} retries."
148153
)
154+
155+
def create_server(
156+
self,
157+
*,
158+
zone: Optional[ScwZone] = None,
159+
commercial_type: str,
160+
name: Optional[str] = None,
161+
dynamic_ip_required: Optional[bool] = None,
162+
routed_ip_enabled: Optional[bool] = None,
163+
image: Optional[str] = None,
164+
volumes: Optional[dict[str, VolumeServerTemplate]] = None,
165+
enable_ipv6: Optional[bool] = None,
166+
protected: bool = False,
167+
public_ip: Optional[str] = None,
168+
public_ips: Optional[list[str]] = None,
169+
boot_type: Optional[BootType] = None,
170+
organization: Optional[str] = None,
171+
project: Optional[str] = None,
172+
tags: Optional[list[str]] = None,
173+
security_group: Optional[str] = None,
174+
placement_group: Optional[str] = None,
175+
admin_password_encryption_ssh_key_id: Optional[str] = None,
176+
) -> CreateServerResponse:
177+
"""
178+
Create an Instance.
179+
Create a new Instance of the specified commercial type in the specified zone. Pay attention to the volumes parameter, which takes an object which can be used in different ways to achieve different behaviors.
180+
Get more information in the [Technical Information](#technical-information) section of the introduction.
181+
:param zone: Zone to target. If none is passed will use default zone from the config.
182+
:param commercial_type: Define the Instance commercial type (i.e. GP1-S).
183+
:param name: Instance name.
184+
:param dynamic_ip_required: By default, `dynamic_ip_required` is true, a dynamic ip is attached to the instance (if no flexible ip is already attached).
185+
:param routed_ip_enabled: If true, configure the Instance so it uses the new routed IP mode.
186+
:param image: Instance image ID or label.
187+
:param volumes: Volumes attached to the server.
188+
:param enable_ipv6: True if IPv6 is enabled on the server (deprecated and always `False` when `routed_ip_enabled` is `True`).
189+
:param protected: True to activate server protection option.
190+
:param public_ip: ID of the reserved IP to attach to the Instance.
191+
:param public_ips: A list of reserved IP IDs to attach to the Instance.
192+
:param boot_type: Boot type to use.
193+
:param organization: Instance Organization ID.
194+
One-Of ('project_identifier'): at most one of 'project', 'organization' could be set.
195+
:param project: Instance Project ID.
196+
One-Of ('project_identifier'): at most one of 'project', 'organization' could be set.
197+
:param tags: Instance tags.
198+
:param security_group: Security group ID.
199+
:param placement_group: Placement group ID if Instance must be part of a placement group.
200+
:param admin_password_encryption_ssh_key_id: The public_key value of this key is used to encrypt the admin password.
201+
:return: :class:`CreateServerResponse <CreateServerResponse>`
202+
203+
Usage:
204+
::
205+
206+
result = api.create_instance_server(
207+
commercial_type="example",
208+
protected=False,
209+
)
210+
"""
211+
212+
payload = {
213+
"zone": zone,
214+
"commercial_type": commercial_type,
215+
"name": name,
216+
"dynamic_ip_required": dynamic_ip_required,
217+
"routed_ip_enabled": routed_ip_enabled,
218+
"image": image,
219+
"volumes": volumes,
220+
"enable_ipv6": enable_ipv6,
221+
"protected": protected,
222+
"public_ip": public_ip,
223+
"public_ips": public_ips,
224+
"boot_type": boot_type,
225+
"organization": organization,
226+
"project": project,
227+
"tags": tags,
228+
"security_group": security_group,
229+
"placement_group": placement_group,
230+
"admin_password_encryption_ssh_key_id": admin_password_encryption_ssh_key_id,
231+
}
232+
233+
clean_payload = {k: v for k, v in payload.items() if v is not None}
234+
235+
return self._create_server(**clean_payload) # type: ignore[arg-type]

scaleway/scaleway/instance/v1/tests/test_custom_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def instance_api() -> InstanceUtilsV1API:
2626
@pytest.fixture(scope="module")
2727
@scw_vcr.use_cassette
2828
def server(instance_api: InstanceUtilsV1API) -> Generator[Server, Any, None]:
29-
instance = instance_api._create_server(
29+
instance = instance_api.create_server(
3030
commercial_type=commercial_type,
3131
zone=zone,
3232
name=server_name,

scaleway/scaleway/instance/v1/tests/test_instance_sdk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def instance_volume(
5151
)
5252
}
5353

54-
instance = instance_api._create_server(
54+
instance = instance_api.create_server(
5555
commercial_type=commercial_type,
5656
zone=zone,
5757
name=server_name,
@@ -131,7 +131,7 @@ def test_create_new_server(
131131
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
132132
) -> None:
133133
instance_api, _ = instance_block_api
134-
server_instance = instance_api._create_server(
134+
server_instance = instance_api.create_server(
135135
commercial_type=commercial_type,
136136
zone=zone,
137137
name=server_name_extra,

0 commit comments

Comments
 (0)