From 880c31b3cd7b8d281801ee6861029c490a244873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Tue, 5 Dec 2023 17:27:51 +0100 Subject: [PATCH] implement distinguish per region if available (#98) * implement distinguish per region if available * add changelog --- .../fragments/distinguish-per-region.yml | 3 ++ plugins/module_utils/common_instance.py | 5 ++- plugins/module_utils/vultr_v2.py | 17 +++++++- .../targets/instance/defaults/main.yml | 4 ++ .../integration/targets/vpc/defaults/main.yml | 1 + tests/integration/targets/vpc/tasks/tests.yml | 42 +++++++++++++++++++ 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/distinguish-per-region.yml diff --git a/changelogs/fragments/distinguish-per-region.yml b/changelogs/fragments/distinguish-per-region.yml new file mode 100644 index 0000000..acb60de --- /dev/null +++ b/changelogs/fragments/distinguish-per-region.yml @@ -0,0 +1,3 @@ +minor_changes: + - Implemented a feature to distinguish resources by region if available. + This allows to have identical name per region e.g. a VPC named ``default`` in each region. (https://github.com/vultr/ansible-collection-vultr/pull/98). diff --git a/plugins/module_utils/common_instance.py b/plugins/module_utils/common_instance.py index 9f2eaf7..8e6bcac 100644 --- a/plugins/module_utils/common_instance.py +++ b/plugins/module_utils/common_instance.py @@ -22,7 +22,7 @@ class AnsibleVultrCommonInstance(AnsibleVultr): "param": "vpc2s", "path": "/vpc2", "suffix": "2", - } + }, } def get_ssh_key_ids(self): @@ -61,6 +61,9 @@ def get_vpc_ids(self, api_version="v1"): vpc_ids = list() for vpc in vpcs: + if self.module.params["region"] != vpc["region"]: + continue + if vpc["description"] in vpc_names: vpc_ids.append(vpc["id"]) vpc_names.remove(vpc["description"]) diff --git a/plugins/module_utils/vultr_v2.py b/plugins/module_utils/vultr_v2.py index 21c1c85..82fcbf9 100644 --- a/plugins/module_utils/vultr_v2.py +++ b/plugins/module_utils/vultr_v2.py @@ -75,7 +75,6 @@ def __init__( resource_update_param_keys=None, resource_update_method="PATCH", ): - self.module = module self.namespace = namespace @@ -202,9 +201,23 @@ def query_filter_list_by_name( found = dict() for resource in self.query_list(path=path, result_key=result_key, query_params=query_params): if resource.get(key_name) == param_value: + # In case the resource has a region, distinguish between the region + # This allows to have identical identifiers (e.g. names) per region + region_param = self.module.params.get("region") + region_resource = resource.get("region") + if region_resource and region_param and (region_param != region_resource): + continue + if found: - self.module.fail_json(msg="More than one record with name=%s found. " "Use multiple=true if module supports it." % param_value) + if region_resource and not region_param: + msg = "More than one record with name=%s found. Use region to distinguish." % param_value + else: + msg = "More than one record with name=%s found. Use multiple=true if module supports it." % param_value + + self.module.fail_json(msg=msg) + found = resource + if found: if get_details: return self.query_by_id(resource_id=found[key_id], skip_transform=skip_transform) diff --git a/tests/integration/targets/instance/defaults/main.yml b/tests/integration/targets/instance/defaults/main.yml index 68e9e93..dd280e9 100644 --- a/tests/integration/targets/instance/defaults/main.yml +++ b/tests/integration/targets/instance/defaults/main.yml @@ -6,6 +6,10 @@ vultr_instance_ssh_key_name: "{{ vultr_resource_prefix }}_instance_sshkey" vultr_instance_ssh_key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAyWYItY+3w5b8PdGRoz0oY5mufqydW96naE+VM3JSvJFAUS08rAjQQpQ03ymoALeHQy6JVZbcgecxn6p0pAOINQdqufn4udPtOPCtMjNiPGpkSM9ah/6X5+kvyWMNrvlf+Ld4OOoszP5sAkgQzIbrFQAm41XknBUha0zkewZwfrVhain4pnDjV7wCcChId/Q/Gbi4xMtXkisznWcAJcueBs3EEZDKhJ5q0VeWSJEhYJDLFN1sOxF0AIUnMrOhfKQ/LjgREXPB6uCl899INUTXRNNjRpeMXyJ2wMMmOAbua2qEd1r13Bu1n+6A823Hzb33fyMXuqWnJwBJ4DCvMlGuEsfuOK+xk7DaBfLHbcM6fsPk0/4psTE6YLgC41remr6+u5ZWsY/faMtSnNPie8Z8Ov0DIYGdhbJjUXk1HomxRV9+ZfZ2Ob8iCwlaAQAyEUM6fs3Kxt8pBD8dx1HOkhsfBWPvuDr5y+kqE7H8/MuPDTc0QgH2pjUMpmw/XBwNDHshVEjrZvtICOjOLUJxcowLO1ivNYwPwowQxfisMy56LfYdjsOslBiqsrkAqvNGm1zu8wKHeqVN9w5l3yUELpvubfm9NKIvYcl6yWF36T0c5vE+g0DU/Jy4XpTj0hZG9QV2mRQcLJnd2pxQtJT7cPFtrn/+tgRxzjEtbDXummDV4sE= ansible@example.com" vutr_instance_vpcs: + - description: "{{ vultr_resource_prefix }}_instance_vpc_1" + v4_subnet: 192.168.24.0 + v4_subnet_mask: 24 + region: ewr - description: "{{ vultr_resource_prefix }}_instance_vpc_1" v4_subnet: 192.168.42.0 v4_subnet_mask: 24 diff --git a/tests/integration/targets/vpc/defaults/main.yml b/tests/integration/targets/vpc/defaults/main.yml index 83923ff..524dbd4 100644 --- a/tests/integration/targets/vpc/defaults/main.yml +++ b/tests/integration/targets/vpc/defaults/main.yml @@ -3,3 +3,4 @@ vultr_vpc_description: "{{ vultr_resource_prefix }}_vpc" vultr_vpc_v4_subnet: 192.168.42.0 vultr_vpc_v4_subnet_mask: 24 vultr_vpc_region: ewr +vultr_vpc_region_2: ams diff --git a/tests/integration/targets/vpc/tasks/tests.yml b/tests/integration/targets/vpc/tasks/tests.yml index 8ed2dfd..28793a7 100644 --- a/tests/integration/targets/vpc/tasks/tests.yml +++ b/tests/integration/targets/vpc/tasks/tests.yml @@ -72,9 +72,38 @@ - result.vultr_vpc.v4_subnet == vultr_vpc_v4_subnet - result.vultr_vpc.v4_subnet_mask == vultr_vpc_v4_subnet_mask +- name: test create vpc in diff region but same description + vultr.cloud.vpc: + description: "{{ vultr_vpc_description }}" + v4_subnet: "{{ vultr_vpc_v4_subnet }}" + v4_subnet_mask: "{{ vultr_vpc_v4_subnet_mask }}" + region: "{{ vultr_vpc_region_2 }}" + register: result +- name: verify test create vpc in diff region but same description + ansible.builtin.assert: + that: + - result is changed + - result.vultr_vpc.description == vultr_vpc_description + - result.vultr_vpc.region == vultr_vpc_region_2 + - result.vultr_vpc.v4_subnet == vultr_vpc_v4_subnet + - result.vultr_vpc.v4_subnet_mask == vultr_vpc_v4_subnet_mask + +- name: test destroy vpc having 2 vpcs with same description distinqush region + vultr.cloud.vpc: + description: "{{ vultr_vpc_description }}" + state: absent + register: result + ignore_errors: true + check_mode: true +- name: verify test destroy vpc having 2 vpcs with same description distinqush region + ansible.builtin.assert: + that: + - result is failed + - name: test destroy vpc in check mode vultr.cloud.vpc: description: "{{ vultr_vpc_description }}" + region: "{{ vultr_vpc_region }}" state: absent register: result check_mode: true @@ -90,6 +119,7 @@ - name: test destroy vpc vultr.cloud.vpc: description: "{{ vultr_vpc_description }}" + region: "{{ vultr_vpc_region }}" state: absent register: result - name: verify test destroy vpc @@ -104,9 +134,21 @@ - name: test destroy an existing vpc idempotence vultr.cloud.vpc: description: "{{ vultr_vpc_description }}" + region: "{{ vultr_vpc_region }}" state: absent register: result - name: verify test destroy an existing vpc idempotence ansible.builtin.assert: that: - result is not changed + +- name: cleanup vpc in diff region + vultr.cloud.vpc: + description: "{{ vultr_vpc_description }}" + region: "{{ vultr_vpc_region_2 }}" + state: absent + register: result +- name: verify test cleanup vpc in diff region + ansible.builtin.assert: + that: + - result is changed