Skip to content

Commit

Permalink
updates to work with updates a.aws module_utils/ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Oct 17, 2024
1 parent 7474612 commit 29639e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
42 changes: 14 additions & 28 deletions plugins/modules/ec2_vpc_vgw.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@

from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import attach_vpc_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_vpc_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_vpc_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_vpn_gateways
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import attach_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpn_gateways
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpcs
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import detach_vpc_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import detach_vpn_gateway
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
Expand Down Expand Up @@ -250,15 +250,10 @@ def wait_for_status(


def attach_vgw(client, module: AnsibleAWSModule, vpn_gateway_id: str) -> Any:
params = dict()
vpc_id = module.params.get("vpc_id")
if vpc_id:
params["VpcId"] = vpc_id
if vpn_gateway_id:
params["VpnGatewayId"] = vpn_gateway_id

try:
response = attach_vpc_vpn_gateway(client, **params)
response = attach_vpn_gateway(client, vpc_id, vpn_gateway_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

Expand All @@ -271,16 +266,10 @@ def attach_vgw(client, module: AnsibleAWSModule, vpn_gateway_id: str) -> Any:


def detach_vgw(client, module: AnsibleAWSModule, vpn_gateway_id: str, vpc_id: Optional[str] = None) -> Any:
params = dict()
params["VpcId"] = module.params.get("vpc_id")

detach_params = {
"VpnGatewayId": vpn_gateway_id,
"VpcId": vpc_id if vpc_id else params["VpcId"],
}
vpc_id = vpc_id or module.params.get("vpc_id")

try:
response = detach_vpc_vpn_gateway(client, **detach_params)
response = detach_vpn_gateway(client, vpc_id, vpn_gateway_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

Expand All @@ -302,11 +291,11 @@ def create_vgw(client, module: AnsibleAWSModule) -> Any:
params["AmazonSideAsn"] = module.params.get("asn")

try:
response = create_vpc_vpn_gateway(client, **params)
get_waiter(client, "vpn_gateway_exists").wait(VpnGatewayIds=[response["VpnGateway"]["VpnGatewayId"]])
response = create_vpn_gateway(client, **params)
get_waiter(client, "vpn_gateway_exists").wait(VpnGatewayIds=[response["VpnGatewayId"]])
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(
e, msg=f"Failed to wait for Vpn Gateway {response['VpnGateway']['VpnGatewayId']} to be available"
e, msg=f"Failed to wait for Vpn Gateway {response['VpnGatewayId']} to be available"
)
except is_boto3_error_code("VpnGatewayLimitExceeded") as e:
module.fail_json_aws(e, msg="Too many VPN gateways exist in this account.")
Expand All @@ -318,11 +307,8 @@ def create_vgw(client, module: AnsibleAWSModule) -> Any:


def delete_vgw(client, module: AnsibleAWSModule, vpn_gateway_id: str) -> Optional[str]:
params = {}
if vpn_gateway_id:
params["VpnGatewayId"] = vpn_gateway_id
try:
delete_vpc_vpn_gateway(client, **params)
delete_vpn_gateway(client, vpn_gateway_id)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

Expand Down Expand Up @@ -360,7 +346,7 @@ def find_vgw(
if module.params.get("state") == "present":
params["Filters"].append({"Name": "state", "Values": ["pending", "available"]})
try:
response = describe_vpc_vpn_gateways(client, **params)
response = describe_vpn_gateways(client, **params)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

Expand Down Expand Up @@ -435,7 +421,7 @@ def ensure_vgw_present(client, module: AnsibleAWSModule) -> Tuple[bool, Dict[str
# create a new vgw
new_vgw = create_vgw(client, module)
changed = True
vpn_gateway_id = new_vgw["VpnGateway"]["VpnGatewayId"]
vpn_gateway_id = new_vgw["VpnGatewayId"]

# if a vpc-id was supplied, attempt to attach it to the vgw
if params["VpcId"]:
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/ec2_vpc_vgw_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_vpn_gateways
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpn_gateways
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list

Expand Down Expand Up @@ -154,7 +154,7 @@ def list_virtual_gateways(client, module):
params["VpnGatewayIds"] = vpn_gateway_ids

try:
all_virtual_gateways = describe_vpc_vpn_gateways(client, **params)
all_virtual_gateways = describe_vpn_gateways(client, **params)
except AnsibleEC2Error as e:
module.fail_json_aws_error(e)

Expand Down

0 comments on commit 29639e8

Please sign in to comment.