Skip to content

Commit

Permalink
pass TagSpecifications only if tags are set (not {} and not None)
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Nov 26, 2023
1 parent 5b8f7e9 commit 1cce528
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1843-ec2_vpc_subnet.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
minor_changes:
- ec2_vpc_subnet - use ``ResourceTags`` to set initial tags upon creation (https://github.com/ansible-collections/amazon.aws/issues/1843)
- ec2_vpc_subnet - the default value for ``tags`` has been changed from ``{}`` to ``None``, to remove tags from a subnet an empty map must
be explicitly passed to the module (https://github.com/ansible-collections/amazon.aws/pull/1876).
4 changes: 2 additions & 2 deletions plugins/modules/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def allocate_address(
)

params = {"Domain": domain}
if tags is not None:
if tags:
params["TagSpecifications"] = boto3_tag_specifications(tags, types="elastic-ip")

try:
Expand Down Expand Up @@ -538,7 +538,7 @@ def allocate_address_from_pool(
if public_ipv4_pool is not None:
params["PublicIpv4Pool"] = public_ipv4_pool

if tags is not None:
if tags:
params["TagSpecifications"] = boto3_tag_specifications(tags, types="elastic-ip")

try:
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/ec2_vpc_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def ensure_igw_present(self, igw_id, vpc_id, tags, purge_tags, force_attach, det

try:
create_params = {}
if tags is not None:
if tags:
create_params["TagSpecifications"] = boto3_tag_specifications(tags, types="internet-gateway")
response = self._connection.create_internet_gateway(aws_retry=True, **create_params)

Expand Down
16 changes: 7 additions & 9 deletions plugins/modules/ec2_vpc_subnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@
- Ignored unless I(wait=True).
default: 300
type: int
tags:
default: {}
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
Expand Down Expand Up @@ -212,7 +210,6 @@
except ImportError:
pass # caught by AnsibleAWSModule

from ansible.module_utils._text import to_text
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.arn import is_outpost_arn
Expand Down Expand Up @@ -280,7 +277,7 @@ def create_subnet(conn, module, vpc_id, cidr, tags, ipv6_cidr=None, outpost_arn=
if az:
params["AvailabilityZone"] = az

if tags is not None:
if tags:
params["TagSpecifications"] = boto3_tag_specifications(tags, types="subnet")

if outpost_arn:
Expand Down Expand Up @@ -316,6 +313,9 @@ def ensure_tags(conn, module, subnet, tags, purge_tags, start_time):
retry_codes=["InvalidSubnetID.NotFound"],
)

if not changed:
return changed

if module.params["wait"] and not module.check_mode:
# Wait for tags to be updated
filters = [{"Name": f"tag:{k}", "Values": [v]} for k, v in tags.items()]
Expand Down Expand Up @@ -483,10 +483,8 @@ def ensure_subnet_present(conn, module):
)
changed = True

if module.params["tags"] != subnet["tags"]:
stringified_tags_dict = dict((to_text(k), to_text(v)) for k, v in module.params["tags"].items())
if ensure_tags(conn, module, subnet, stringified_tags_dict, module.params["purge_tags"], start_time):
changed = True
if ensure_tags(conn, module, subnet, module.params["tags"], module.params["purge_tags"], start_time):
changed = True

subnet = get_matching_subnet(conn, module, module.params["vpc_id"], module.params["cidr"])
if not module.check_mode and module.params["wait"]:
Expand Down Expand Up @@ -554,7 +552,7 @@ def main():
ipv6_cidr=dict(default="", required=False),
outpost_arn=dict(default="", type="str", required=False),
state=dict(default="present", choices=["present", "absent"]),
tags=dict(default={}, required=False, type="dict", aliases=["resource_tags"]),
tags=dict(required=False, type="dict", aliases=["resource_tags"]),
vpc_id=dict(required=True),
map_public=dict(default=False, required=False, type="bool"),
assign_instances_ipv6=dict(default=False, required=False, type="bool"),
Expand Down

0 comments on commit 1cce528

Please sign in to comment.