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
This commit is contained in:
Julian Tölle 2023-04-12 10:43:19 +02:00 committed by GitHub
parent 6a3983c9cd
commit 3c2d4df8bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 94 additions and 8 deletions

View file

@ -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
=======

View file

@ -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:

View file

@ -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)

View file

@ -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:

View file

@ -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,

View file

@ -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()

View file

@ -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

View file

@ -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"

View file

@ -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