From 32d67b2eda3821a9a0eacbeb9c49fe1393bebca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Wed, 21 Apr 2021 10:58:32 +0200 Subject: [PATCH] Fix compatibility of hcloud_server module with python version lower than python 3.6 --- plugins/modules/hcloud_server.py | 22 +++++++++---------- .../targets/hcloud_server/tasks/main.yml | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/plugins/modules/hcloud_server.py b/plugins/modules/hcloud_server.py index 85eb583..ff5a464 100644 --- a/plugins/modules/hcloud_server.py +++ b/plugins/modules/hcloud_server.py @@ -8,8 +8,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -from dateutil import relativedelta - DOCUMENTATION = ''' --- module: hcloud_server @@ -262,6 +260,7 @@ hcloud_server: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud +from datetime import timedelta try: from hcloud.volumes.domain import Volume @@ -401,20 +400,19 @@ class AnsibleHcloudServer(Hcloud): else: try: image = self.client.images.get_by_id(self.module.params.get("image")) - except: - self.module.fail_json(msg=f"Image {self.module.params.get('image')} was not found") + except Exception: + self.module.fail_json(msg="Image %s was not found" % self.module.params.get('image')) if image.deprecated is not None: - available_until = image.deprecated + relativedelta.relativedelta(months=3) + available_until = image.deprecated + timedelta(days=90) if self.module.params.get("allow_deprecated_image"): self.module.warn( - f"You try to use a deprecated image. The image {image.name} will " - f"continue to be available until {available_until.strftime('%Y-%m-%d')}." - ) + "You try to use a deprecated image. The image %s will continue to be available until %s.") \ + % (image.name, available_until.strftime('%Y-%m-%d')) else: - self.module.fail_json(msg=f"You try to use a deprecated image. The image {image.name} will " - f"continue to be available until {available_until.strftime('%Y-%m-%d')}. " - f"If you want to use this image use allow_deprecated_image=yes." - ) + self.module.fail_json( + msg=("You try to use a deprecated image. The image %s will continue to be available until %s." + + " If you want to use this image use allow_deprecated_image=yes." + ) % (image.name, available_until.strftime('%Y-%m-%d'))) return image def _update_server(self): diff --git a/tests/integration/targets/hcloud_server/tasks/main.yml b/tests/integration/targets/hcloud_server/tasks/main.yml index 73acf79..783dede 100644 --- a/tests/integration/targets/hcloud_server/tasks/main.yml +++ b/tests/integration/targets/hcloud_server/tasks/main.yml @@ -48,7 +48,7 @@ assert: that: - result is failed - - 'result.msg == "You try to use a deprecated image. The image ubuntu-16.04 will continue to be available until 2021-06-24. If you want to use this image use allow_deprecated_image=yes."' + - 'result.msg == "You try to use a deprecated image. The image ubuntu-16.04 will continue to be available until 2021-06-22. If you want to use this image use allow_deprecated_image=yes."' - name: test create server with check mode hcloud_server: