From ee28d661904ae74dc394c4971c7fe79761efeeab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Fri, 28 Oct 2022 17:02:59 +0200 Subject: [PATCH] instance: refactor to fix backups on create (#32) --- .../fragments/instance-feature-handling.yml | 2 + plugins/module_utils/vultr_v2.py | 10 +++- plugins/modules/instance.py | 49 +++++++++++++------ .../targets/instance/defaults/main.yml | 21 +++++++- .../targets/instance/tasks/present.yml | 15 ++++++ 5 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 changelogs/fragments/instance-feature-handling.yml diff --git a/changelogs/fragments/instance-feature-handling.yml b/changelogs/fragments/instance-feature-handling.yml new file mode 100644 index 0000000..c16f0fd --- /dev/null +++ b/changelogs/fragments/instance-feature-handling.yml @@ -0,0 +1,2 @@ +bugfixes: + - instance - Fixed the handling for activating/deactivating backups. diff --git a/plugins/module_utils/vultr_v2.py b/plugins/module_utils/vultr_v2.py index 0d1fcba..c381ee5 100644 --- a/plugins/module_utils/vultr_v2.py +++ b/plugins/module_utils/vultr_v2.py @@ -126,6 +126,12 @@ def __init__( def configure(self): pass + def transform_resource(self, resource): + """ + Transforms (optional) the resource dict queried from the API + """ + return resource + def api_query(self, path, method="GET", data=None): if method == "GET" and data: @@ -197,7 +203,7 @@ def query_filter_list_by_name( if get_details: return self.query_by_id(resource_id=found[key_id]) else: - return found + return self.transform_resource(found) elif fail_not_found: self.module.fail_json(msg="No Resource %s with %s found: %s" % (path, key_name, param_value)) @@ -221,7 +227,7 @@ def query_by_id(self, resource_id=None, path=None, result_key=None): resource = self.api_query(path="%s%s" % (path, "/" + resource_id if resource_id else resource_id)) if resource: - return resource[result_key] + return self.transform_resource(resource[result_key]) return dict() diff --git a/plugins/modules/instance.py b/plugins/modules/instance.py index df783b1..b5ddb87 100644 --- a/plugins/modules/instance.py +++ b/plugins/modules/instance.py @@ -341,6 +341,24 @@ returned: success type: str sample: I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczoKICAtIGh0b3AK + backups: + description: Whether backups are enabled or disabled. + returned: success + type: str + sample: enabled + version_added: "1.3.0" + ddos_protection: + description: Whether DDOS protections is enabled or not. + returned: success + type: bool + sample: true + version_added: "1.3.0" + enable_ipv6: + description: Whether IPv6 is enabled or not. + returned: success + type: bool + sample: true + version_added: "1.3.0" """ import base64 @@ -421,6 +439,17 @@ def get_user_data(self, resource): return str(res.get("user_data", dict()).get("data")) return "" + def transform_resource(self, resource): + if not resource: + return resource + + features = resource.get("features", list()) + resource["backups"] = "enabled" if "auto_backups" in features else "disabled" + resource["enable_ipv6"] = "ipv6" in features + resource["ddos_protection"] = "ddos_protection" in features + + return resource + def configure(self): if self.module.params["state"] != "absent": if self.module.params["startup_script"] is not None: @@ -445,6 +474,9 @@ def configure(self): # sshkey_id ist a list of ids self.module.params["sshkey_id"] = self.get_ssh_key_ids() + if self.module.params["backups"] is not None: + self.module.params["backups"] = "enabled" if self.module.params["backups"] else "disabled" + def handle_power_status(self, resource, state, action, power_status, force=False): if state == self.module.params["state"] and (resource["power_status"] != power_status or force): self.result["changed"] = True @@ -456,17 +488,6 @@ def handle_power_status(self, resource, state, action, power_status, force=False resource = self.wait_for_state(resource=resource, key="power_status", state=power_status) return resource - def update_feature(self, param_key, resource, feature=None): - features = resource.get("features", list()) - - if feature is not None: - feature = param_key - - feature_enabled = self.module.params[param_key] - if feature_enabled is not None: - if feature_enabled != feature in features: - self.resource_update_param_keys.append(feature) - def create(self): param_keys = ("os", "image", "app") if not any(self.module.params.get(x) is not None for x in param_keys): @@ -474,9 +495,6 @@ def create(self): return super(AnsibleVultrInstance, self).create() def update(self, resource): - self.update_feature(param_key="backups", resource=resource, feature="auto_backup") - self.update_feature(param_key="ddos_protection", resource=resource) - self.update_feature(param_key="enable_ipv6", resource=resource, feature="ipv6") user_data = self.get_user_data(resource=resource) resource["user_data"] = user_data.encode() return super(AnsibleVultrInstance, self).update(resource=resource) @@ -568,6 +586,9 @@ def main(): "plan", "tags", "firewall_group_id", + "enable_ipv6", + "ddos_protection", + "backups", "user_data", ], resource_key_name="label", diff --git a/tests/integration/targets/instance/defaults/main.yml b/tests/integration/targets/instance/defaults/main.yml index bbc2170..9fbaabe 100644 --- a/tests/integration/targets/instance/defaults/main.yml +++ b/tests/integration/targets/instance/defaults/main.yml @@ -19,8 +19,6 @@ vultr_instances: - vim plan: vc2-1c-1gb plan_update: vc2-1c-2gb - ddos_protection: true - ddos_protection_update: false ssh_keys: - "{{ vultr_instance_ssh_key_name }}" tags: @@ -31,15 +29,34 @@ vultr_instances: - four region: ams os: Debian 11 x64 (bullseye) + backups: true + backups_update: false + ddos_protection: true + ddos_protection_update: false + enable_ipv6: false + enable_ipv6_update: true - label: "{{ vultr_resource_prefix }}_app1" plan: vc2-1c-1gb plan_update: vc2-1c-2gb region: ams app: Docker on Ubuntu 20.04 x64 + backups: false + backups_update: false + ddos_protection: false + ddos_protection_update: false + enable_ipv6: false + enable_ipv6_update: false - label: "{{ vultr_resource_prefix }}_img1" plan: vc2-1c-1gb plan_update: vc2-1c-2gb region: ams image: Gitea on Ubuntu 20.04 + backups: false + backups_update: true + ddos_protection: false + ddos_protection_update: true + enable_ipv6: true + # API does not disable IPv6 once enabled. + enable_ipv6_update: true diff --git a/tests/integration/targets/instance/tasks/present.yml b/tests/integration/targets/instance/tasks/present.yml index 86721b6..7ebaa7b 100644 --- a/tests/integration/targets/instance/tasks/present.yml +++ b/tests/integration/targets/instance/tasks/present.yml @@ -49,6 +49,9 @@ - result is changed - result.vultr_instance.plan == instance.plan - result.vultr_instance.region == instance.region + - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'" + - result.vultr_instance.ddos_protection == instance.ddos_protection + - result.vultr_instance.enable_ipv6 == instance.enable_ipv6 - name: test create instance idempotence vultr.cloud.instance: @@ -73,6 +76,9 @@ - result is not changed - result.vultr_instance.plan == instance.plan - result.vultr_instance.region == instance.region + - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'" + - result.vultr_instance.ddos_protection == instance.ddos_protection + - result.vultr_instance.enable_ipv6 == instance.enable_ipv6 - name: test update instance in check mode vultr.cloud.instance: @@ -98,6 +104,9 @@ - result is changed - result.vultr_instance.plan == instance.plan - result.vultr_instance.region == instance.region + - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups else 'disabled' }}'" + - result.vultr_instance.ddos_protection == instance.ddos_protection + - result.vultr_instance.enable_ipv6 == instance.enable_ipv6 - name: test update instance vultr.cloud.instance: @@ -122,6 +131,9 @@ - result is changed - result.vultr_instance.plan == instance.plan_update - result.vultr_instance.region == instance.region + - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups_update else 'disabled' }}'" + - result.vultr_instance.ddos_protection == instance.ddos_protection_update + - result.vultr_instance.enable_ipv6 == instance.enable_ipv6_update - name: test update instance idempotence vultr.cloud.instance: @@ -146,3 +158,6 @@ - result is not changed - result.vultr_instance.plan == instance.plan_update - result.vultr_instance.region == instance.region + - "result.vultr_instance.backups == '{{ 'enabled' if instance.backups_update else 'disabled' }}'" + - result.vultr_instance.ddos_protection == instance.ddos_protection_update + - result.vultr_instance.enable_ipv6 == instance.enable_ipv6_update