Skip to content

Commit

Permalink
fix method delete and create as
Browse files Browse the repository at this point in the history
  • Loading branch information
Laure-di committed Feb 24, 2025
1 parent c83b436 commit bd058ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
11 changes: 11 additions & 0 deletions plugins/module_utils/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def scaleway_pop_client_params(module: AnsibleModule) -> None:
"access_key",
"secret_key",
"organization_id",
"project_id",
"api_url",
"api_allow_insecure",
"user_agent",
Expand All @@ -139,3 +140,13 @@ def scaleway_pop_waitable_resource_params(module: AnsibleModule) -> None:
for param in params:
if param in module.params:
module.params.pop(param)

def object_to_dict(obj):
if isinstance(obj, list):
return [object_to_dict(item) for item in obj]
elif isinstance(obj, dict):
return {key: object_to_dict(value) for key, value in obj.items()}
elif hasattr(obj, "__dict__"):
return {key: object_to_dict(value) for key, value in obj.__dict__.items()}
else:
return obj
27 changes: 16 additions & 11 deletions plugins/modules/scaleway_applesilicon_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
scaleway_get_client_from_module,
scaleway_pop_client_params,
scaleway_pop_waitable_resource_params,
object_to_dict,
)

try:
Expand All @@ -108,7 +109,6 @@
except ImportError:
HAS_SCALEWAY_SDK = False


def create(module: AnsibleModule, client: "Client") -> None:
api = ApplesiliconV1Alpha1API(client)

Expand All @@ -127,13 +127,14 @@ def create(module: AnsibleModule, client: "Client") -> None:
not_none_params = {
key: value for key, value in module.params.items() if value is not None
}
not_none_params["project_id"] = client.default_project_id
resource = api.create_server(**not_none_params)
resource = api.wait_for_server(
server_id=resource.id,
zone=resource.zone
)

module.exit_json(changed=True, data=resource.__dict__)
module.exit_json(changed=False, data=object_to_dict(resource))


def delete(module: AnsibleModule, client: "Client") -> None:
Expand All @@ -143,27 +144,31 @@ def delete(module: AnsibleModule, client: "Client") -> None:
name = module.params.pop("name", None)

if id is not None:
resource = api.get_server(server_id=id, region=module.params["region"])
resource = api.get_server(server_id=id, zone=module.params["zone"])
elif name is not None:
resources = api.list_servers_all(name=name, region=module.params["region"])
if len(resources) == 0:
resources = api.list_servers_all(zone=module.params["zone"])
final_resources = []
for resource in resources:
if resource.name == name:
final_resources.append(resource)
if len(final_resources) == 0:
module.exit_json(msg="No server found with name {name}")
elif len(resources) > 1:
elif len(final_resources) > 1:
module.exit_json(msg="More than one server found with name {name}")
else:
resource = resources[0]
resource = final_resources[0]
else:
module.fail_json(msg="id is required")

if module.check_mode:
module.exit_json(changed=True)

api.delete_server(server_id=resource.id, region=module.params["region"])
api.delete_server(server_id=resource.id, zone=module.params["zone"])

try:
api.wait_for_server(server_id=resource.id, region=module.params["region"])
api.wait_for_server(server_id=resource.id, zone=module.params["zone"])
except ScalewayException as e:
if e.status_code != 404:
if e.status_code != 403:
raise e

module.exit_json(
Expand Down Expand Up @@ -205,7 +210,7 @@ def main() -> None:
),
project_id=dict(
type="str",
required=False,
required=True,
),
enable_vpc=dict(
type="bool",
Expand Down

0 comments on commit bd058ed

Please sign in to comment.