Add a module_utils OpenStack Cloud constructor (#20974)

Start using this to construct shade OpenStack Cloud objects in a
consistent manner. This will let us centralize things like dealing with
password arguments and whatnot. It also allows us to introduce the
ability to pass a fully formed config dict directly to the module.

Migrate all OpenStack modules to use openstack_cloud_from_module.

Have it return the shade library since it's responsible for
importing shade and shade is needed for the exceptions.

Only pull specific OpenStack arguments for the constructor

Rather than passing **module.params to the shade constructor, pull out
only the values that make sense. This should prevent the issues with
module parameters stepping on shade parameters.

Replace module.params.pop with module.params.get

We don't need to pop these anymore since the shade constructor is now
using opt-in values.

Using real urls is ungood. Use example.com domains. Also, get rid of the
antiquated port numbers.
This commit is contained in:
Monty Taylor 2018-02-15 08:20:49 -06:00 committed by Ricardo Carrillo Cruz
commit 0f893027c4
47 changed files with 270 additions and 673 deletions

View file

@ -76,7 +76,7 @@ def openstack_find_nova_addresses(addresses, ext_tag, key_name=None):
def openstack_full_argument_spec(**kwargs):
spec = dict(
cloud=dict(default=None),
cloud=dict(default=None, type='raw'),
auth_type=dict(default=None),
auth=dict(default=None, type='dict', no_log=True),
region_name=dict(default=None),
@ -88,12 +88,9 @@ def openstack_full_argument_spec(**kwargs):
wait=dict(default=True, type='bool'),
timeout=dict(default=180, type='int'),
api_timeout=dict(default=None, type='int'),
endpoint_type=dict(
default='public', choices=['public', 'internal', 'admin']
),
identity_api_version=dict(
default=None, choices=['2.0', '3']
)
interface=dict(
default='public', choices=['public', 'internal', 'admin'],
aliases=['endpoint_type']),
)
spec.update(kwargs)
return spec
@ -109,3 +106,46 @@ def openstack_module_kwargs(**kwargs):
ret[key] = kwargs[key]
return ret
def openstack_cloud_from_module(module, min_version=None):
from distutils.version import StrictVersion
try:
import shade
except ImportError:
module.fail_json(msg='shade is required for this module')
if min_version:
if StrictVersion(shade.__version__) < StrictVersion(min_version):
module.fail_json(
msg="To utilize this module, the installed version of"
"the shade library MUST be >={min_version}".format(
min_version=min_version))
cloud_config = module.params.pop('cloud', None)
if isinstance(cloud_config, dict):
fail_message = (
"A cloud config dict was provided to the cloud parameter"
" but also a value was provided for {param}. If a cloud"
" config dict is provided, {param} should be"
" excluded.")
for param in (
'auth', 'region_name', 'verify',
'cacert', 'key', 'api_timeout', 'interface'):
if module.params[param] is not None:
module.fail_json(fail_message.format(param=param))
if module.params['auth_type'] != 'password':
module.fail_json(fail_message.format(param='auth_type'))
return shade, shade.operator_cloud(**cloud_config)
else:
return shade, shade.operator_cloud(
cloud=cloud_config,
auth_type=module.params['auth_type'],
auth=module.params['auth'],
region_name=module.params['region_name'],
verify=module.params['verify'],
cacert=module.params['cacert'],
key=module.params['key'],
api_timeout=module.params['api_timeout'],
interface=module.params['interface'],
)