From dda7d9e7042f02504f32bc7a3cf22b0d876202c8 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Thu, 17 May 2018 11:41:14 -0400 Subject: [PATCH] [AWS] Add Ansible Version to botocore user agent string (#39993) Pull `config` out if it was provided to boto3_conn and add the useragent string --- lib/ansible/module_utils/ansible_release.py | 1 + lib/ansible/module_utils/ec2.py | 26 ++++++++++++++------- test/runner/lib/target.py | 4 +++- 3 files changed, 22 insertions(+), 9 deletions(-) create mode 120000 lib/ansible/module_utils/ansible_release.py diff --git a/lib/ansible/module_utils/ansible_release.py b/lib/ansible/module_utils/ansible_release.py new file mode 120000 index 0000000000..fee410852e --- /dev/null +++ b/lib/ansible/module_utils/ansible_release.py @@ -0,0 +1 @@ +../release.py \ No newline at end of file diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index 7d7f7c2d3a..71f64447e5 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -29,6 +29,7 @@ import os import re +from ansible.module_utils.ansible_release import __version__ from ansible.module_utils._text import to_native, to_text from ansible.module_utils.cloud import CloudRetry from ansible.module_utils.six import string_types, binary_type, text_type @@ -126,15 +127,24 @@ def _boto3_conn(conn_type=None, resource=None, region=None, endpoint=None, **par 'the conn_type parameter in the boto3_conn function ' 'call') - if conn_type == 'resource': - resource = boto3.session.Session(profile_name=profile).resource(resource, region_name=region, endpoint_url=endpoint, **params) - return resource - elif conn_type == 'client': - client = boto3.session.Session(profile_name=profile).client(resource, region_name=region, endpoint_url=endpoint, **params) - return client + if params.get('config'): + config = params.pop('config') + config.user_agent_extra = 'Ansible/{0}'.format(__version__) else: - client = boto3.session.Session(profile_name=profile).client(resource, region_name=region, endpoint_url=endpoint, **params) - resource = boto3.session.Session(profile_name=profile).resource(resource, region_name=region, endpoint_url=endpoint, **params) + config = botocore.config.Config( + user_agent_extra='Ansible/{0}'.format(__version__), + ) + session = boto3.session.Session( + profile_name=profile, + ) + + if conn_type == 'resource': + return session.resource(resource, config=config, region_name=region, endpoint_url=endpoint, **params) + elif conn_type == 'client': + return session.client(resource, config=config, region_name=region, endpoint_url=endpoint, **params) + else: + client = session.client(resource, region_name=region, endpoint_url=endpoint, **params) + resource = session.resource(resource, region_name=region, endpoint_url=endpoint, **params) return client, resource diff --git a/test/runner/lib/target.py b/test/runner/lib/target.py index 3038edab44..260189f206 100644 --- a/test/runner/lib/target.py +++ b/test/runner/lib/target.py @@ -319,7 +319,9 @@ def walk_test_targets(path=None, module_path=None, extensions=None, prefix=None, file_path = os.path.join(root, file_name) if os.path.islink(file_path): - continue + # special case to allow a symlink of ansible_release.py -> ../release.py + if file_path != 'lib/ansible/module_utils/ansible_release.py': + continue yield TestTarget(file_path, module_path, prefix, path)