Skip to content

Commit

Permalink
implement distinguish per region if available (#98)
Browse files Browse the repository at this point in the history
* implement distinguish per region if available

* add changelog
  • Loading branch information
resmo committed Dec 5, 2023
1 parent 55154e1 commit 880c31b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/distinguish-per-region.yml
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 4 additions & 1 deletion plugins/module_utils/common_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnsibleVultrCommonInstance(AnsibleVultr):
"param": "vpc2s",
"path": "/vpc2",
"suffix": "2",
}
},
}

def get_ssh_key_ids(self):
Expand Down Expand Up @@ -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"])
Expand Down
17 changes: 15 additions & 2 deletions plugins/module_utils/vultr_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def __init__(
resource_update_param_keys=None,
resource_update_method="PATCH",
):

self.module = module
self.namespace = namespace

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/targets/instance/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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= [email protected]"

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
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/vpc/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions tests/integration/targets/vpc/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 880c31b

Please sign in to comment.