Skip to content

Commit

Permalink
Fail cleanly if no subnets are provided on creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Sep 18, 2021
1 parent e6804ce commit 8db0ada
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugins/modules/elasticache_subnet_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
subnets:
description:
- List of subnet IDs that make up the ElastiCache subnet group.
- At least one subnet must be provided when creating an ElastiCache subnet group.
type: list
elements: str
author:
Expand Down Expand Up @@ -140,14 +141,15 @@ def get_subnet_group(name):

def create_subnet_group(name, description, subnets):

if not subnets:
module.fail_json(msg='At least one subnet must be provided when creating a subnet group')

if module.check_mode:
return True

try:
if not description:
description = name
if not subnets:
subnets = []
client.create_cache_subnet_group(
aws_retry=True,
CacheSubnetGroupName=name,
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/targets/elasticache_subnet_group/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@
security_token: '{{ security_token | default(omit) }}'
region: '{{ aws_region }}'
block:

# ============================================================

- name: Create Subnet Group with no subnets - check_mode
elasticache_subnet_group:
state: present
name: '{{ group_name }}'
check_mode: True
register: create_group
ignore_errors: True

- name: Check result - Create Subnet Group with no subnets - check_mode
assert:
that:
- create_group is failed
# Check we caught the issue before trying to create
- '"CreateCacheSubnetGroup" not in create_group.resource_actions'
# Check that we don't refer to the boto3 parameter
- '"SubnetIds" not in create_group.msg'
# Loosely check the message
- '"subnet" in create_group.msg'
- '"At least" in create_group.msg'

- name: Create Subnet Group with no subnets
elasticache_subnet_group:
state: present
name: '{{ group_name }}'
register: create_group
ignore_errors: True

- name: Check result - Create Subnet Group with no subnets
assert:
that:
- create_group is failed
# Check we caught the issue before trying to create
- '"CreateCacheSubnetGroup" not in create_group.resource_actions'
# Check that we don't refer to the boto3 parameter
- '"SubnetIds" not in create_group.msg'
# Loosely check the message
- '"subnet" in create_group.msg'
- '"At least" in create_group.msg'

# ============================================================
# Setup infra needed for tests
- name: create a VPC
Expand Down Expand Up @@ -55,6 +97,7 @@
subnet_id_b: '{{ vpc_subnet_create.results[1].subnet.id }}'
subnet_id_c: '{{ vpc_subnet_create.results[2].subnet.id }}'
subnet_id_d: '{{ vpc_subnet_create.results[3].subnet.id }}'

# ============================================================

- name: Create Subnet Group - check_mode
Expand Down

0 comments on commit 8db0ada

Please sign in to comment.