Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_vpc_nat_gateway show fails if EIP doesn't exist #1604

Merged
Next Next commit
changes to the module
  • Loading branch information
taehopark32 committed Jun 6, 2023
commit 16fc659064d3ff253aad1ecdd8dfa8e5d7890cff
29 changes: 26 additions & 3 deletions plugins/modules/ec2_vpc_nat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ def pre_create(
wait=False,
client_token=None,
connectivity_type="public",
default_create=False,
):
"""Create an Amazon NAT Gateway.
Args:
Expand Down Expand Up @@ -744,10 +745,29 @@ def pre_create(
elif eip_address or allocation_id:
if eip_address and not allocation_id:
allocation_id, msg = get_eip_allocation_id_by_address(client, module, eip_address)
if not allocation_id:
# if not allocation_id:
# changed = False
# return changed, msg, dict()a
if not allocation_id and not default_create:
changed = False
return changed, msg, dict()

module.fail_json(msg=msg)
elif not allocation_id and default_create:
eip_address = None
return pre_create(
client,
module,
subnet_id,
tags,
purge_tags,
allocation_id,
eip_address,
if_exist_do_not_create,
wait,
client_token,
connectivity_type,
default_create,
)

existing_gateways, allocation_id_exists = gateway_in_subnet_exists(client, module, subnet_id, allocation_id)

if len(existing_gateways) > 0 and (allocation_id_exists or if_exist_do_not_create):
Expand Down Expand Up @@ -869,6 +889,7 @@ def main():
client_token=dict(type="str", no_log=False),
tags=dict(required=False, type="dict", aliases=["resource_tags"]),
purge_tags=dict(default=True, type="bool"),
default_create=dict(type="str"),
)

module = AnsibleAWSModule(
Expand All @@ -890,6 +911,7 @@ def main():
if_exist_do_not_create = module.params.get("if_exist_do_not_create")
tags = module.params.get("tags")
purge_tags = module.params.get("purge_tags")
default_create = module.params.get("default_create")

try:
client = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff())
Expand All @@ -912,6 +934,7 @@ def main():
wait,
client_token,
connectivity_type,
default_create,
)
else:
changed, msg, results = remove(client, module, nat_gateway_id, wait, release_eip, connectivity_type)
Expand Down