mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
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:
committed by
Ricardo Carrillo Cruz
parent
577ff4d949
commit
0f893027c4
@@ -102,16 +102,8 @@ project:
|
||||
sample: True
|
||||
'''
|
||||
|
||||
from distutils.version import StrictVersion
|
||||
|
||||
try:
|
||||
import shade
|
||||
HAS_SHADE = True
|
||||
except ImportError:
|
||||
HAS_SHADE = False
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs
|
||||
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
|
||||
|
||||
|
||||
def _needs_update(module, project):
|
||||
@@ -159,37 +151,35 @@ def main():
|
||||
**module_kwargs
|
||||
)
|
||||
|
||||
if not HAS_SHADE:
|
||||
module.fail_json(msg='shade is required for this module')
|
||||
|
||||
name = module.params['name']
|
||||
description = module.params['description']
|
||||
domain = module.params.pop('domain_id')
|
||||
domain = module.params.get('domain_id')
|
||||
enabled = module.params['enabled']
|
||||
state = module.params['state']
|
||||
|
||||
if domain and StrictVersion(shade.__version__) < StrictVersion('1.8.0'):
|
||||
module.fail_json(msg="The domain argument requires shade >=1.8.0")
|
||||
if domain:
|
||||
min_version = '1.8.0'
|
||||
else:
|
||||
min_version = None
|
||||
|
||||
shade, cloud = openstack_cloud_from_module(
|
||||
module, min_version=min_version)
|
||||
try:
|
||||
if domain:
|
||||
opcloud = shade.operator_cloud(**module.params)
|
||||
try:
|
||||
# We assume admin is passing domain id
|
||||
dom = opcloud.get_domain(domain)['id']
|
||||
dom = cloud.get_domain(domain)['id']
|
||||
domain = dom
|
||||
except:
|
||||
# If we fail, maybe admin is passing a domain name.
|
||||
# Note that domains have unique names, just like id.
|
||||
try:
|
||||
dom = opcloud.search_domains(filters={'name': domain})[0]['id']
|
||||
dom = cloud.search_domains(filters={'name': domain})[0]['id']
|
||||
domain = dom
|
||||
except:
|
||||
# Ok, let's hope the user is non-admin and passing a sane id
|
||||
pass
|
||||
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
|
||||
if domain:
|
||||
project = cloud.get_project(name, domain_id=domain)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user