From dc26f89a04dc3240cde48e3611c7e522180852a4 Mon Sep 17 00:00:00 2001
From: Mandar Kulkarni <mandar242@gmail.com>
Date: Tue, 25 Jan 2022 13:36:58 -0800
Subject: [PATCH] ec2_lc: add volume throughput parameter support (#790)

ec2_lc: add volume throughput parameter support

SUMMARY

Adding throughput parameter support to ec2_lc.
Fixes #784.

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

community.aws.ec2_lc
UPDATE:
Integration tests being added in a separate PR: #824

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Jill R <None>
---
 ...90-ec2_lc-add-throughput-param-support.yml |  2 +
 plugins/modules/ec2_lc.py                     | 13 ++-
 .../integration/targets/ec2_lc/tasks/main.yml | 86 ++++++++++++++++++-
 3 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/fragments/790-ec2_lc-add-throughput-param-support.yml

diff --git a/changelogs/fragments/790-ec2_lc-add-throughput-param-support.yml b/changelogs/fragments/790-ec2_lc-add-throughput-param-support.yml
new file mode 100644
index 00000000000..aa7e3e09803
--- /dev/null
+++ b/changelogs/fragments/790-ec2_lc-add-throughput-param-support.yml
@@ -0,0 +1,2 @@
+minor_changes:
+- ec2_lc - add support for throughput parameter (https://github.com/ansible-collections/community.aws/pull/790).
diff --git a/plugins/modules/ec2_lc.py b/plugins/modules/ec2_lc.py
index 19f8dfe2972..de3a7a5443f 100644
--- a/plugins/modules/ec2_lc.py
+++ b/plugins/modules/ec2_lc.py
@@ -107,6 +107,12 @@
         description:
           - The number of IOPS per second to provision for the volume.
           - Required when I(volume_type=io1).
+      throughput:
+        type: int
+        description:
+          - The throughput to provision for a gp3 volume.
+          - Valid Range is a minimum value of 125 and a maximum value of 1000.
+        version_added: 3.1.0
       encrypted:
         type: bool
         default: false
@@ -478,7 +484,7 @@ def create_block_device_meta(module, volume):
     if 'no_device' in volume:
         return_object['NoDevice'] = volume.get('no_device')
 
-    if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'iops', 'encrypted']):
+    if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'iops', 'throughput', 'encrypted']):
         return_object['Ebs'] = {}
 
     if 'snapshot' in volume:
@@ -496,6 +502,11 @@ def create_block_device_meta(module, volume):
     if 'iops' in volume:
         return_object['Ebs']['Iops'] = volume.get('iops')
 
+    if 'throughput' in volume:
+        if volume.get('volume_type') != 'gp3':
+            module.fail_json(msg='The throughput parameter is supported only for GP3 volumes.')
+        return_object['Ebs']['Throughput'] = volume.get('throughput')
+
     if 'encrypted' in volume:
         return_object['Ebs']['Encrypted'] = volume.get('encrypted')
 
diff --git a/tests/integration/targets/ec2_lc/tasks/main.yml b/tests/integration/targets/ec2_lc/tasks/main.yml
index b8c255f4481..e61fc6feb5b 100644
--- a/tests/integration/targets/ec2_lc/tasks/main.yml
+++ b/tests/integration/targets/ec2_lc/tasks/main.yml
@@ -112,6 +112,58 @@
           - lc_2_create_idem is not changed
           - '"autoscaling:CreateLaunchConfiguration" not in lc_2_create_idem.resource_actions'
 
+    - name: Create launch configuration 3 - test throughput parameter
+      vars:
+        ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
+      community.aws.ec2_lc:
+        name: '{{ resource_prefix }}-lc3'
+        image_id: '{{ ec2_ami_id }}'
+        instance_type: '{{ ec2_instance_type }}'
+        volumes:
+          - device_name: /dev/sda1
+            volume_size: 10
+            volume_type: gp3
+            throughput: 250
+            delete_on_termination: true
+      register: lc_3_create
+
+    - name: Gather information about launch configuration 3
+      vars:
+        ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
+      community.aws.ec2_lc_info:
+        name: '{{ resource_prefix }}-lc3'
+      register: lc_3_info_result
+
+    - assert:
+        that:
+          - lc_3_create is changed
+          - '"throughput" in lc_3_info_result.launch_configurations[0].block_device_mappings[0].ebs'
+          - lc_3_info_result.launch_configurations[0].block_device_mappings[0].ebs.throughput == 250
+          - lc_3_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_size == 10
+          - lc_3_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_type == 'gp3'
+          - lc_3_info_result.launch_configurations[0].instance_type == 't2.micro'
+          - '"autoscaling:CreateLaunchConfiguration" in lc_3_create.resource_actions'
+
+    - name: Create launch configuration 3 - Idempotency
+      vars:
+          ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
+      community.aws.ec2_lc:
+        name: '{{ resource_prefix }}-lc3'
+        image_id: '{{ ec2_ami_id }}'
+        instance_type: '{{ ec2_instance_type }}'
+        volumes:
+          - device_name: /dev/sda1
+            volume_size: 10
+            volume_type: gp3
+            throughput: 250
+            delete_on_termination: true
+      register: lc_3_create_idem
+
+    - assert:
+        that:
+          - lc_3_create_idem is not changed
+          - '"autoscaling:CreateLaunchConfiguration" not in lc_3_create_idem.resource_actions'
+
     - name: Search for the Launch Configurations that start with test resource_prefix
       community.aws.ec2_lc_find:
         name_regex: '{{ resource_prefix }}*'
@@ -120,7 +172,7 @@
 
     - assert:
         that:
-          - lc_find_result.results | length == 2
+          - lc_find_result.results | length == 3
           - '"autoscaling:DescribeLaunchConfigurations" in lc_find_result.resource_actions'
 
     - name: Delete launch configuration 1
@@ -187,6 +239,38 @@
           - lc_2_info_result is not changed
           - lc_2_info_result.launch_configurations | length == 0
 
+    - name: Delete launch configuration 3
+      community.aws.ec2_lc:
+        name: '{{ resource_prefix }}-lc3'
+        state: absent
+      register: lc_3_delete
+
+    - assert:
+        that:
+          - lc_3_delete is changed
+          - '"autoscaling:DeleteLaunchConfiguration" in lc_3_delete.resource_actions'
+
+    - name: Delete launch configuration 3 - Idempotency
+      community.aws.ec2_lc:
+        name: '{{ resource_prefix }}-lc3'
+        state: absent
+      register: lc_3_delete_idem
+
+    - assert:
+        that:
+          - lc_3_delete_idem is not changed
+          - '"autoscaling:DeleteLaunchConfiguration" not in lc_3_delete_idem.resource_actions'
+
+    - name: Gather information about launch configuration 3
+      community.aws.ec2_lc_info:
+        name: '{{ resource_prefix }}-lc2'
+      register: lc_3_info_result
+
+    - assert:
+        that:
+          - lc_3_info_result is not changed
+          - lc_3_info_result.launch_configurations | length == 0
+
   always:
 
     - include_tasks: env_cleanup.yml