Skip to content

Commit

Permalink
Add documentation and support for check_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Sep 23, 2021
1 parent aac8190 commit 451bef7
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugins/modules/redshift_subnet_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ def get_subnet_group(name):
cluster_subnet_groups=camel_dict_to_snake_dict(groups),
)

# No support for managing tags yet, but make sure that we don't need to
# change the return value structure after it's been available in a release.
tags = boto3_tag_list_to_ansible_dict(groups[0]['Tags'])

subnet_group = camel_dict_to_snake_dict(groups[0])

subnet_group['tags'] = tags
Expand All @@ -143,6 +146,9 @@ 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
Expand Down Expand Up @@ -170,6 +176,9 @@ def update_subnet_group(subnet_group, name, description, subnets):
if not update_params:
return False

if module.check_mode:
return True

# Description is optional, SubnetIds is not
if 'SubnetIds' not in update_params:
update_params['SubnetIds'] = subnet_group['subnet_ids']
Expand All @@ -187,6 +196,10 @@ def update_subnet_group(subnet_group, name, description, subnets):


def delete_subnet_group(name):

if module.check_mode:
return True

try:
client.delete_cluster_subnet_group(
aws_retry=True,
Expand Down Expand Up @@ -214,6 +227,7 @@ def main():

module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True,
)

state = module.params.get('state')
Expand Down
214 changes: 214 additions & 0 deletions tests/integration/targets/redshift_subnet_group/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@

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

- name: Create Subnet Group with no subnets - check_mode
redshift_subnet_group:
state: present
name: '{{ group_name }}'
register: create_group
ignore_errors: True
check_mode: 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
- '"CreateClusterSubnetGroup" 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
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -81,6 +101,23 @@

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

- name: Create Subnet Group - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
group_description: '{{ description_default }}'
group_subnets:
- '{{ subnet_id_a }}'
- '{{ subnet_id_b }}'
register: create_group
check_mode: True

- name: Check result - Create Subnet Group - check_mode
assert:
that:
- create_group is successful
- create_group is changed

- name: Create Subnet Group
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -113,6 +150,23 @@
- subnet_id_d not in create_group.cluster_subnet_group.subnet_ids
- create_group.cluster_subnet_group.vpc_id == vpc_id

- name: Create Subnet Group - idempotency - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
group_description: '{{ description_default }}'
group_subnets:
- '{{ subnet_id_a }}'
- '{{ subnet_id_b }}'
register: create_group
check_mode: True

- name: Check result - Create Subnet Group - idempotency - check_mode
assert:
that:
- create_group is successful
- create_group is not changed

- name: Create Subnet Group - idempotency
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -147,6 +201,24 @@

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

- name: Update Subnet Group Description - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
group_description: '{{ description_updated }}'
## No longer mandatory
# group_subnets:
# - '{{ subnet_id_a }}'
# - '{{ subnet_id_b }}'
register: update_description
check_mode: True

- name: Check result - Update Subnet Group Description - check_mode
assert:
that:
- update_description is successful
- update_description is changed

- name: Update Subnet Group Description
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -180,6 +252,24 @@
- subnet_id_d not in update_description.cluster_subnet_group.subnet_ids
- update_description.cluster_subnet_group.vpc_id == vpc_id

- name: Update Subnet Group Description - idempotency - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
group_description: '{{ description_updated }}'
## No longer mandatory
# group_subnets:
# - '{{ subnet_id_a }}'
# - '{{ subnet_id_b }}'
register: update_description
check_mode: True

- name: Check result - Update Subnet Group Description - idempotency - check_mode
assert:
that:
- update_description is successful
- update_description is not changed

- name: Update Subnet Group Description - idempotency
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -215,6 +305,24 @@

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

- name: Update Subnet Group subnets - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
## No longer mandatory
# group_description: '{{ description_updated }}'
group_subnets:
- '{{ subnet_id_c }}'
- '{{ subnet_id_d }}'
register: update_subnets
check_mode: True

- name: Check result - Update Subnet Group subnets - check_mode
assert:
that:
- update_subnets is successful
- update_subnets is changed

- name: Update Subnet Group subnets
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -248,6 +356,24 @@
- subnet_id_d in update_subnets.cluster_subnet_group.subnet_ids
- update_subnets.cluster_subnet_group.vpc_id == vpc_id

- name: Update Subnet Group subnets - idempotency - check_mode
redshift_subnet_group:
state: present
group_name: '{{ group_name }}'
## No longer mandatory
# group_description: '{{ description_updated }}'
group_subnets:
- '{{ subnet_id_c }}'
- '{{ subnet_id_d }}'
register: update_subnets
check_mode: True

- name: Check result - Update Subnet Group subnets - idempotency - check_mode
assert:
that:
- update_subnets is successful
- update_subnets is not changed

- name: Update Subnet Group subnets - idempotency
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -283,6 +409,18 @@

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

- name: Delete Subnet Group - check_mode
redshift_subnet_group:
state: absent
group_name: '{{ group_name }}'
register: delete_group
check_mode: True

- name: Check result - Delete Subnet Group - check_mode
assert:
that:
- delete_group is changed

- name: Delete Subnet Group
redshift_subnet_group:
state: absent
Expand All @@ -294,6 +432,18 @@
that:
- delete_group is changed

- name: Delete Subnet Group - idempotency - check_mode
redshift_subnet_group:
state: absent
group_name: '{{ group_name }}'
register: delete_group
check_mode: True

- name: Check result - Delete Subnet Group - idempotency - check_mode
assert:
that:
- delete_group is not changed

- name: Delete Subnet Group - idempotency
redshift_subnet_group:
state: absent
Expand All @@ -307,6 +457,21 @@

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

- name: Create minimal Subnet Group - check_mode
redshift_subnet_group:
state: present
name: '{{ group_name }}'
subnets:
- '{{ subnet_id_a }}'
register: create_group
check_mode: True

- name: Check result - Create minimal Subnet Group - check_mode
assert:
that:
- create_group is successful
- create_group is changed

- name: Create minimal Subnet Group
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -337,6 +502,21 @@
- subnet_id_d not in create_group.cluster_subnet_group.subnet_ids
- create_group.cluster_subnet_group.vpc_id == vpc_id

- name: Create minimal Subnet Group - idempotency - check_mode
redshift_subnet_group:
state: present
name: '{{ group_name }}'
subnets:
- '{{ subnet_id_a }}'
register: create_group
check_mode: True

- name: Check result - Create minimal Subnet Group - idempotency - check_mode
assert:
that:
- create_group is successful
- create_group is not changed

- name: Create minimal Subnet Group - idempotency
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -369,6 +549,23 @@

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

- name: Full Update Subnet Group - check_mode
redshift_subnet_group:
state: present
name: '{{ group_name }}'
description: '{{ description_updated }}'
subnets:
- '{{ subnet_id_a }}'
- '{{ subnet_id_b }}'
register: update_complex
check_mode: True

- name: Check result - Full Update Subnet Group - check_mode
assert:
that:
- update_complex is successful
- update_complex is changed

- name: Full Update Subnet Group
redshift_subnet_group:
state: present
Expand Down Expand Up @@ -401,6 +598,23 @@
- subnet_id_d not in update_complex.cluster_subnet_group.subnet_ids
- update_complex.cluster_subnet_group.vpc_id == vpc_id

- name: Full Update Subnet Group - idempotency - check_mode
redshift_subnet_group:
state: present
name: '{{ group_name }}'
description: '{{ description_updated }}'
subnets:
- '{{ subnet_id_a }}'
- '{{ subnet_id_b }}'
register: update_complex
check_mode: True

- name: Check result - Full Update Subnet Group - idempotency - check_mode
assert:
that:
- update_complex is successful
- update_complex is not changed

- name: Full Update Subnet Group - idempotency
redshift_subnet_group:
state: present
Expand Down

0 comments on commit 451bef7

Please sign in to comment.