From 3c2d4df8bb98787dee2a5680476f870a52c3c134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Wed, 12 Apr 2023 10:43:19 +0200 Subject: [PATCH] feat: add support for ARM APIs (#208) * feat: add architecture fields for image & server type * feat: filter images by architecture * feat: select right image by arch on server create & rebuild * feat(inventory): add architecture field to returned servers * docs: add changelog for arm features * chore: prepare v1.11.0 --- CHANGELOG.rst | 12 +++++++ changelogs/changelog.yaml | 12 +++++++ galaxy.yml | 2 +- plugins/inventory/hcloud.py | 1 + plugins/modules/hcloud_image_info.py | 23 +++++++++++++- plugins/modules/hcloud_server.py | 12 ++++--- plugins/modules/hcloud_server_type_info.py | 8 ++++- .../hcloud_image_info/defaults/main.yml | 1 + .../targets/hcloud_image_info/tasks/main.yml | 31 +++++++++++++++++++ 9 files changed, 94 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8640bfc..e9eff3c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,18 @@ Hetzner Cloud Ansible Collection Release Notes .. contents:: Topics +v1.11.0 +======= + +Minor Changes +------------- + +- hcloud_image_info - Add cpu architecture field to return value. +- hcloud_image_info - Allow filtering images by cpu architecture. +- hcloud_server - Select matching image for the cpu architecture of the server type on create & rebuild. +- hcloud_server_type_info - Add cpu architecture field to return value. +- inventory plugin - Add cpu architecture to server variables. + v1.10.1 ======= diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index a063848..0a4f123 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -85,6 +85,18 @@ releases: - server-fix-backups.yml - server-race-condition-pg-attach.yml release_date: '2023-04-03' + 1.11.0: + changes: + minor_changes: + - hcloud_image_info - Add cpu architecture field to return value. + - hcloud_image_info - Allow filtering images by cpu architecture. + - hcloud_server - Select matching image for the cpu architecture of the server + type on create & rebuild. + - hcloud_server_type_info - Add cpu architecture field to return value. + - inventory plugin - Add cpu architecture to server variables. + fragments: + - arm-features.yaml + release_date: '2023-04-11' 1.2.0: changes: minor_changes: diff --git a/galaxy.yml b/galaxy.yml index b94477c..8b41d97 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: hetzner name: hcloud -version: 1.10.1 +version: 1.11.0 readme: README.md authors: - Hetzner Cloud (github.com/hetznercloud) diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index f032eb8..390dedc 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -217,6 +217,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self.inventory.set_variable(server.name, "name", to_native(server.name)) self.inventory.set_variable(server.name, "status", to_native(server.status)) self.inventory.set_variable(server.name, "type", to_native(server.server_type.name)) + self.inventory.set_variable(server.name, "architecture", to_native(server.server_type.architecture)) # Network if server.public_net.ipv4: diff --git a/plugins/modules/hcloud_image_info.py b/plugins/modules/hcloud_image_info.py index de2e358..8acd884 100644 --- a/plugins/modules/hcloud_image_info.py +++ b/plugins/modules/hcloud_image_info.py @@ -38,10 +38,15 @@ options: type: str type: description: - - The label selector for the images you want to get. + - The type for the images you want to get. default: system choices: [ system, snapshot, backup ] type: str + architecture: + description: + - The architecture for the images you want to get. + type: str + choices: [ x86, arm ] extends_documentation_fragment: - hetzner.hcloud.hcloud @@ -98,6 +103,11 @@ hcloud_image_info: returned: always type: str sample: 18.04 + architecture: + description: Image is compatible with this architecture + returned: always + type: str + sample: x86 labels: description: User-defined labels (key-value pairs) returned: always @@ -127,6 +137,7 @@ class AnsibleHcloudImageInfo(Hcloud): "description": to_native(image.description), "os_flavor": to_native(image.os_flavor), "os_version": to_native(image.os_version), + "architecture": to_native(image.architecture), "labels": image.labels, }) return tmp @@ -137,6 +148,11 @@ class AnsibleHcloudImageInfo(Hcloud): self.hcloud_image_info = [self.client.images.get_by_id( self.module.params.get("id") )] + elif self.module.params.get("name") is not None and self.module.params.get("architecture") is not None: + self.hcloud_image_info = [self.client.images.get_by_name_and_architecture( + self.module.params.get("name"), + self.module.params.get("architecture") + )] elif self.module.params.get("name") is not None: self.hcloud_image_info = [self.client.images.get_by_name( self.module.params.get("name") @@ -151,6 +167,10 @@ class AnsibleHcloudImageInfo(Hcloud): if image_type: params["type"] = image_type + architecture = self.module.params.get("architecture") + if architecture: + params["architecture"] = architecture + self.hcloud_image_info = self.client.images.get_all(**params) except Exception as e: @@ -164,6 +184,7 @@ class AnsibleHcloudImageInfo(Hcloud): name={"type": "str"}, label_selector={"type": "str"}, type={"choices": ["system", "snapshot", "backup"], "default": "system", "type": "str"}, + architecture={"choices": ["x86", "arm"], "type": "str"}, **Hcloud.base_module_arguments() ), supports_check_mode=True, diff --git a/plugins/modules/hcloud_server.py b/plugins/modules/hcloud_server.py index 958b980..3a77da6 100644 --- a/plugins/modules/hcloud_server.py +++ b/plugins/modules/hcloud_server.py @@ -402,12 +402,14 @@ class AnsibleHcloudServer(Hcloud): required_params=["name", "server_type", "image"] ) + server_type = self._get_server_type() + params = { "name": self.module.params.get("name"), - "server_type": self._get_server_type(), + "server_type": server_type, "user_data": self.module.params.get("user_data"), "labels": self.module.params.get("labels"), - "image": self._get_image(), + "image": self._get_image(server_type), "placement_group": self._get_placement_group(), "public_net": ServerCreatePublicNetwork( enable_ipv4=self.module.params.get("enable_ipv4"), @@ -499,8 +501,8 @@ class AnsibleHcloudServer(Hcloud): self._mark_as_changed() self._get_server() - def _get_image(self): - image_resp = self.client.images.get_list(name=self.module.params.get("image"), include_deprecated=True) + def _get_image(self, server_type): + image_resp = self.client.images.get_list(name=self.module.params.get("image"), architecture=server_type.architecture, include_deprecated=True) images = getattr(image_resp, 'images') image = None if images is not None and len(images) > 0: @@ -828,7 +830,7 @@ class AnsibleHcloudServer(Hcloud): ) try: if not self.module.check_mode: - image = self._get_image() + image = self._get_image(self.hcloud_server.server_type) self.client.servers.rebuild(self.hcloud_server, image).wait_until_finished(1000) # When we rebuild the server progress takes some more time. self._mark_as_changed() diff --git a/plugins/modules/hcloud_server_type_info.py b/plugins/modules/hcloud_server_type_info.py index 8ef37c8..a84067c 100644 --- a/plugins/modules/hcloud_server_type_info.py +++ b/plugins/modules/hcloud_server_type_info.py @@ -93,6 +93,11 @@ hcloud_server_type_info: returned: always type: str sample: shared + architecture: + description: Architecture of cpu + returned: always + type: str + sample: x86 """ from ansible.module_utils.basic import AnsibleModule @@ -118,7 +123,8 @@ class AnsibleHcloudServerTypeInfo(Hcloud): "memory": server_type.memory, "disk": server_type.disk, "storage_type": to_native(server_type.storage_type), - "cpu_type": to_native(server_type.cpu_type) + "cpu_type": to_native(server_type.cpu_type), + "architecture": to_native(server_type.architecture) }) return tmp diff --git a/tests/integration/targets/hcloud_image_info/defaults/main.yml b/tests/integration/targets/hcloud_image_info/defaults/main.yml index ddca332..7c25d17 100644 --- a/tests/integration/targets/hcloud_image_info/defaults/main.yml +++ b/tests/integration/targets/hcloud_image_info/defaults/main.yml @@ -4,3 +4,4 @@ hcloud_prefix: "tests" hcloud_test_image_name: "always-there-snapshot" hcloud_test_image_id: 10164049 +hcloud_test_image_name_os: "ubuntu-22.04" diff --git a/tests/integration/targets/hcloud_image_info/tasks/main.yml b/tests/integration/targets/hcloud_image_info/tasks/main.yml index 503d5b7..16ed44a 100644 --- a/tests/integration/targets/hcloud_image_info/tasks/main.yml +++ b/tests/integration/targets/hcloud_image_info/tasks/main.yml @@ -60,3 +60,34 @@ assert: that: - result is failed + +- name: test gather hcloud image infos with name + hcloud_image_info: + name: "{{ hcloud_test_image_name_os }}" + register: hcloud_images +- name: verify test gather hcloud image infos with name + assert: + that: + - hcloud_images.hcloud_image_info | list | count == 1 + - hcloud_images.hcloud_image_info[0].architecture == "x86" + +- name: test gather hcloud image infos with name and architecture + hcloud_image_info: + name: "{{ hcloud_test_image_name_os }}" + architecture: arm + register: hcloud_images +- name: verify test gather hcloud image infos with name + assert: + that: + - hcloud_images.hcloud_image_info | list | count == 1 + - hcloud_images.hcloud_image_info[0].architecture == "arm" + +- name: test gather hcloud image infos with architecture + hcloud_image_info: + architecture: arm + register: hcloud_images +- name: verify test gather hcloud image infos with name + assert: + that: + - hcloud_images.hcloud_image_info | selectattr('architecture','equalto','x86') | list | count == 0 + - hcloud_images.hcloud_image_info | selectattr('architecture','equalto','arm') | list | count > 2