From f4d896c7164ab6aaea53d1204a521e31c810e6e4 Mon Sep 17 00:00:00 2001
From: Ryan Brown <sb@ryansb.com>
Date: Fri, 9 Feb 2018 14:28:52 -0500
Subject: [PATCH] Tower callback fixes (#35927)

* Fix tower_callback script return code

* Add ec2_key parameter to integration tests
---
 lib/ansible/modules/cloud/amazon/ec2_instance.py   | 10 ++++------
 .../tasks/external_resource_attach.yml             | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/lib/ansible/modules/cloud/amazon/ec2_instance.py b/lib/ansible/modules/cloud/amazon/ec2_instance.py
index d9cbdcfdd3e..ad946c6ec47 100644
--- a/lib/ansible/modules/cloud/amazon/ec2_instance.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_instance.py
@@ -639,21 +639,19 @@ def tower_callback_script(tower_conf, windows=False, passwd=None):
             if p not in tower_conf:
                 module.fail_json(msg="Incomplete tower_callback configuration. tower_callback.{0} not set.".format(p))
 
-        tpl = string.Template(textwrap.dedent("""
-        #!/bin/bash
-        exec > /tmp/tower_callback.log 2>&1
+        tpl = string.Template(textwrap.dedent("""#!/bin/bash
         set -x
 
         retry_attempts=10
         attempt=0
         while [[ $attempt -lt $retry_attempts ]]
         do
-          status_code=`curl -k -s -i \
+          status_code=`curl --max-time 10 -v -k -s -i \
                   --data "host_config_key=${host_config_key}" \
                   https://${tower_address}/api/v1/job_templates/${template_id}/callback/ \
                   | head -n 1 \
                   | awk '{print $2}'`
-          if [[ $status_code == 202 ]]
+          if [[ $status_code == 201 ]]
             then
             exit 0
           fi
@@ -928,7 +926,7 @@ def build_top_level_options(params):
     if params.get('detailed_monitoring', False):
         spec['Monitoring'] = {'Enabled': True}
     if params.get('cpu_credit_specification') is not None:
-        spec['CreditSpecification'] = params.get('cpu_credit_specification')
+        spec['CreditSpecification'] = {'CpuCredits': params.get('cpu_credit_specification')}
     if params.get('tenancy') is not None:
         spec['Placement'] = {'Tenancy': params.get('tenancy')}
     if (params.get('network') or {}).get('ebs_optimized') is not None:
diff --git a/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml b/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml
index a3bae904925..5931ee67bd8 100644
--- a/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml
+++ b/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml
@@ -22,9 +22,14 @@
     <<: *aws_connection_info
   register: eni_b
 
+- ec2_key:
+    name: "{{ resource_prefix }}_test_key"
+    <<: *aws_connection_info
+
 - name: Make instance in the testing subnet created in the test VPC
   ec2_instance:
     name: "{{ resource_prefix }}-test-eni-vpc"
+    key_name: "{{ resource_prefix }}_test_key"
     network:
       interfaces:
         - id: "{{ eni_a.interface.id }}"
@@ -36,6 +41,11 @@
     <<: *aws_connection_info
   register: in_test_vpc
 
+- assert:
+    that:
+      - 'in_test_vpc.instances[0].key_name == "{{ resource_prefix }}_test_key"'
+      - '(in_test_vpc.instances[0].network_interfaces | length) == 1'
+
 - name: Add a second interface
   ec2_instance:
     name: "{{ resource_prefix }}-test-eni-vpc"
@@ -68,6 +78,10 @@
 - assert:
     that: not result.changed
 
+- ec2_key:
+    name: "{{ resource_prefix }}_test_key"
+    state: absent
+    <<: *aws_connection_info
 
 - ec2_eni:
     eni_id: "{{ item }}"