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_vol: Add support for OutpostArn param #597

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/597-ec2_vol-add-outpostarn-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_vol - add support for OutpostArn param (https://github.com/ansible-collections/amazon.aws/pull/597).
13 changes: 13 additions & 0 deletions plugins/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,16 @@ def normalize_ec2_vpc_dhcp_config(option_config):
config_data[option] = [val['Value'] for val in config_item['Values']]

return config_data


def is_outposts_arn(input_regex):
"""
Validates the provided regex pattern of outpost arn as per API specification document.

API Specification Document:
https://docs.aws.amazon.com/outposts/latest/APIReference/API_Outpost.html
"""
regex_pattern = r'^arn:aws([a-z-]+)?:outposts:[a-z\d-]+:\d{12}:outpost/op-[a-f0-9]{17}$'
if not re.match(regex_pattern, input_regex):
return False
return True
15 changes: 15 additions & 0 deletions plugins/modules/ec2_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@
- This parameter is supported with io1 and io2 volumes only.
type: bool
version_added: 2.0.0
outpost_arn:
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
description:
- The Amazon Resource Name (ARN) of the Outpost.
- If set, allows to create volume in an Outpost.
type: str
version_added: 3.1.0
author: "Lester Wade (@lwade)"
extends_documentation_fragment:
- amazon.aws.aws
Expand Down Expand Up @@ -262,6 +268,7 @@
from ..module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ..module_utils.ec2 import describe_ec2_tags
from ..module_utils.ec2 import ensure_ec2_tags
from ..module_utils.ec2 import is_outposts_arn
from ..module_utils.ec2 import AWSRetry
from ..module_utils.core import is_boto3_error_code

Expand Down Expand Up @@ -455,6 +462,7 @@ def create_volume(module, ec2_conn, zone):
snapshot = module.params.get('snapshot')
throughput = module.params.get('throughput')
multi_attach = module.params.get('multi_attach')
outpost_arn = module.params.get('outpost_arn')

volume = get_volume(module, ec2_conn)

Expand Down Expand Up @@ -488,6 +496,12 @@ def create_volume(module, ec2_conn, zone):
if multi_attach:
additional_params['MultiAttachEnabled'] = True

if outpost_arn:
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
if is_outposts_arn(outpost_arn):
additional_params['OutpostArn'] = outpost_arn
else:
module.fail_json('OutpostArn does not match the pattern specified in API specifications.')

create_vol_response = ec2_conn.create_volume(
aws_retry=True,
AvailabilityZone=zone,
Expand Down Expand Up @@ -700,6 +714,7 @@ def main():
tags=dict(default={}, type='dict'),
modify_volume=dict(default=False, type='bool'),
throughput=dict(type='int'),
outpost_arn=dict(type='str'),
purge_tags=dict(type='bool', default=False),
multi_attach=dict(type='bool'),
)
Expand Down
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ netaddr
awscli
# Used for comparing SSH Public keys to the Amazon fingerprints
pycrypto
# Used for unit testing using parametrize
parametrize
mandar242 marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions tests/unit/module_utils/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)

from plugins.module_utils.ec2 import is_outposts_arn
__metaclass__ = type

import unittest
from parametrize import parametrize
mandar242 marked this conversation as resolved.
Show resolved Hide resolved

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import map_complex_type
Expand Down Expand Up @@ -76,3 +79,20 @@ def test_ansible_dict_with_integer_to_boto3_filter_list(self):

converted_filters_int = ansible_dict_to_boto3_filter_list(filters)
self.assertEqual(converted_filters_int, filter_list_integer)

# ========================================================
# ec2.is_outposts_arn
# ========================================================

@parametrize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @pytest.mark.parametrize instead.

Copy link
Collaborator

@alinabuzachis alinabuzachis Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goneri @pytest.mark.parametrize cannot be used with unittest.TestCase methods, for this reason Mandar used parametrize. He is trying to fix this by placing the test method outside of the class.

"outpost_arn, result",
[
("arn:aws:outposts:us-east-1:123456789012:outpost/op-1234567890abcdef0", True),
("arn:aws:outposts:us-east-1:123456789012:outpost/op-1234567890abcdef0123", False),
("arn:aws:outpost:us-east-1: 123456789012:outpost/ op-1234567890abcdef0", False),
("ars:aws:outposts:us-east-1: 123456789012:outpost/ op-1234567890abcdef0", False),
("arn:was:outposts:us-east-1: 123456789012:outpost/ op-1234567890abcdef0", False),
]
)
def test_is_outposts_arn(self, outpost_arn, result):
self.assertEqual(is_outposts_arn(outpost_arn), result)
3 changes: 3 additions & 0 deletions tests/unit/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ boto3
boto

placebo

# Used for unit testing using parametrize
parametrize
mandar242 marked this conversation as resolved.
Show resolved Hide resolved