From ddf2a736400a5ff766b786273e8460d4c267a12e Mon Sep 17 00:00:00 2001 From: "Ryan S. Brown" Date: Wed, 6 Jul 2016 10:28:54 -0400 Subject: [PATCH] Make it possible to use boto3_conn outside modules The `boto3_conn` function requires a module argument, and calls `module.fail_json` if the connection doesn't receive enough arguments. In non-module settings like inventory scripts, there is no module to be passed. The `boto3_inventory_conn` function takes the same arguments except for `module`, and both call _boto3_conn which doesn't require a module be passed. --- lib/ansible/module_utils/ec2.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index 494d118f64c..aabf78414fd 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -55,10 +55,19 @@ class AnsibleAWSError(Exception): def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None, **params): + try: + return _boto3_conn(conn_type=None, resource=None, region=None, endpoint=None, **params) + except ValueError: + module.fail_json(msg='There is an issue in the code of the module. You must specify either both, resource or client to the conn_type parameter in the boto3_conn function call') + +def _boto3_conn(conn_type=None, resource=None, region=None, endpoint=None, **params): profile = params.pop('profile_name', None) if conn_type not in ['both', 'resource', 'client']: - module.fail_json(msg='There is an issue in the code of the module. You must specify either both, resource or client to the conn_type parameter in the boto3_conn function call') + raise ValueError('There is an issue in the calling code. You ' + 'must specify either both, resource, or client to ' + '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) @@ -71,6 +80,7 @@ def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None client = boto3.session.Session(profile_name=profile).client(resource, region_name=region, endpoint_url=endpoint, **params) return client, resource +boto3_inventory_conn = _boto3_conn def aws_common_argument_spec(): return dict(