From d52af75c68b3f31c994d8b234b9c1e2387f9a4dd Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 21 Nov 2019 08:42:37 -0800 Subject: [PATCH] Add aws elb multiple host header support (#65021) * Add support for aws elbv2 multiple host header actions * Add example of multiple host header * Add test for multiple host headers * Add missing quote * Update test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml Co-Authored-By: Jill R <4121322+jillr@users.noreply.github.com> * Update test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml Co-Authored-By: Jill R <4121322+jillr@users.noreply.github.com> * Remove debug statements --- lib/ansible/module_utils/aws/elbv2.py | 6 +- .../cloud/amazon/elb_application_lb.py | 9 +++ .../tasks/test_modifying_alb_listeners.yml | 63 +++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/aws/elbv2.py b/lib/ansible/module_utils/aws/elbv2.py index 1a197c8e0b3..0e4a5cd7ec7 100644 --- a/lib/ansible/module_utils/aws/elbv2.py +++ b/lib/ansible/module_utils/aws/elbv2.py @@ -720,7 +720,7 @@ class ELBListenerRules(object): current_condition['SourceIpConfig']['Values'][0] == condition['SourceIpConfig']['Values'][0]): condition_found = True break - elif current_condition['Field'] == condition['Field'] and current_condition['Values'][0] == condition['Values'][0]: + elif current_condition['Field'] == condition['Field'] and current_condition['Values'] == condition['Values']: condition_found = True break @@ -735,7 +735,7 @@ class ELBListenerRules(object): modified_rule = {} # Priority - if int(current_rule['Priority']) != new_rule['Priority']: + if int(current_rule['Priority']) != int(new_rule['Priority']): modified_rule['Priority'] = new_rule['Priority'] # Actions @@ -757,8 +757,6 @@ class ELBListenerRules(object): if len(current_rule['Actions']) == 1 and len(new_rule['Actions']) == 1: if current_rule['Actions'] != new_rule['Actions']: modified_rule['Actions'] = new_rule['Actions'] - print("modified_rule:") - print(new_rule['Actions']) # if actions have multiple elements, we'll have to order them first before comparing. # multiple actions will have an 'Order' key for this purpose else: diff --git a/lib/ansible/modules/cloud/amazon/elb_application_lb.py b/lib/ansible/modules/cloud/amazon/elb_application_lb.py index 4ee26446a5c..d406ab3538a 100644 --- a/lib/ansible/modules/cloud/amazon/elb_application_lb.py +++ b/lib/ansible/modules/cloud/amazon/elb_application_lb.py @@ -293,6 +293,15 @@ EXAMPLES = ''' ContentType: "text/plain" MessageBody: "This is the page you're looking for" StatusCode: "200" + - Conditions: + - Field: host-header + Values: + - "hostname.domain.com" + - "alternate.domain.com" + Priority: '4' + Actions: + - TargetGroupName: test-target-group + Type: forward state: present # Remove an ELB diff --git a/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml b/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml index 31c1569168b..943d766340d 100644 --- a/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml +++ b/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml @@ -118,6 +118,69 @@ - alb.changed - alb.listeners[0].rules|length == 3 + - name: add a rule that uses the host header condition to the listener + elb_application_lb: + name: "{{ alb_name }}" + subnets: "{{ alb_subnets }}" + security_groups: "{{ sec_group.group_id }}" + state: present + purge_rules: no + listeners: + - Protocol: HTTP + Port: 80 + DefaultActions: + - Type: forward + TargetGroupName: "{{ tg_name }}" + Rules: + - Conditions: + - Field: host-header + Values: + - 'local.mydomain.com' + Priority: '3' + Actions: + - TargetGroupName: "{{ tg_name }}" + Type: forward + <<: *aws_connection_info + register: alb + + - assert: + that: + - alb.changed + - alb.listeners[0].rules|length == 4 + - '{{ alb|json_query("listeners[].rules[].conditions[].host_header_config.values[]")|length == 1 }}' + + - name: test replacing the rule that uses the host header condition with multiple host header conditions + elb_application_lb: + name: "{{ alb_name }}" + subnets: "{{ alb_subnets }}" + security_groups: "{{ sec_group.group_id }}" + purge_rules: no + state: present + listeners: + - Protocol: HTTP + Port: 80 + DefaultActions: + - Type: forward + TargetGroupName: "{{ tg_name }}" + Rules: + - Conditions: + - Field: host-header + Values: + - 'local.mydomain.com' + - 'alternate.mydomain.com' + Priority: '3' + Actions: + - TargetGroupName: "{{ tg_name }}" + Type: forward + <<: *aws_connection_info + register: alb + + - assert: + that: + - alb.changed + - alb.listeners[0].rules|length == 4 + - '{{ alb|json_query("listeners[].rules[].conditions[].host_header_config.values[]")|length == 2 }}' + - name: remove the rule elb_application_lb: name: "{{ alb_name }}"