Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a generalized OpenStack cloud constructor #20974

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 6 additions & 14 deletions contrib/inventory/openstack.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
clouds:
mordred:
cloud: hp
vexxhost:
profile: vexxhost
auth:
username: mordred@example.com
password: my-wonderful-password
project_name: mordred-tenant
region_name: region-b.geo-1
monty:
cloud: hp
auth:
username: monty.taylor@example.com
password: another-wonderful-password
project_name: monty.taylor@example.com-default-tenant
region_name: region-b.geo-1
project_name: 39e296b2-fc96-42bf-8091-cb742fa13da9
username: fb886a9b-c37b-442a-9be3-964bed961e04
password: fantastic-password1
rax:
cloud: rackspace
auth:
Expand All @@ -22,7 +14,7 @@ clouds:
region_name: DFW,ORD,IAD
devstack:
auth:
auth_url: http://127.0.0.1:35357/v2.0/
auth_url: https://devstack.example.com
username: stack
password: stack
project_name: stack
Expand Down
54 changes: 47 additions & 7 deletions lib/ansible/module_utils/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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'],
)
14 changes: 2 additions & 12 deletions lib/ansible/modules/cloud/openstack/os_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@

import traceback

try:
import shade
HAS_SHADE = True
except ImportError:
HAS_SHADE = False

# this is magic, see lib/ansible/module_common.py
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 main():
Expand All @@ -60,11 +53,8 @@ def main():
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')

shade, cloud = openstack_cloud_from_module(module)
try:
cloud = shade.openstack_cloud(**module.params)
module.exit_json(
changed=False,
ansible_facts=dict(
Expand Down
39 changes: 16 additions & 23 deletions lib/ansible/modules/cloud/openstack/os_flavor_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
---
module: os_flavor_facts
Expand Down Expand Up @@ -171,16 +170,9 @@
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 main():
Expand All @@ -200,33 +192,34 @@ def main():
)
module = AnsibleModule(argument_spec, **module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')

name = module.params['name']
vcpus = module.params['vcpus']
ram = module.params['ram']
ephemeral = module.params['ephemeral']
limit = module.params['limit']

filters = {}
if vcpus:
filters['vcpus'] = vcpus
if ram:
filters['ram'] = ram
if ephemeral:
filters['ephemeral'] = ephemeral

if filters:
# Range search added in 1.5.0
min_version = '1.5.0'
else:
min_version = None

shade, cloud = openstack_cloud_from_module(module, min_version=min_version)
try:
cloud = shade.openstack_cloud(**module.params)
if name:
flavors = cloud.search_flavors(filters={'name': name})

else:
flavors = cloud.list_flavors()
filters = {}
if vcpus:
filters['vcpus'] = vcpus
if ram:
filters['ram'] = ram
if ephemeral:
filters['ephemeral'] = ephemeral
if filters:
# Range search added in 1.5.0
if StrictVersion(shade.__version__) < StrictVersion('1.5.0'):
module.fail_json(msg="Shade >= 1.5.0 needed for this functionality")
flavors = cloud.range_search(flavors, filters)

if limit is not None:
Expand Down
23 changes: 6 additions & 17 deletions lib/ansible/modules/cloud/openstack/os_floating_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,8 @@
server: cattle001
'''

from distutils.version import StrictVersion

try:
import shade
HAS_SHADE = True
except ImportError:
HAS_SHADE = False

from ansible.module_utils.basic import AnsibleModule, remove_values
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 _get_floating_ip(cloud, floating_ip_address):
Expand Down Expand Up @@ -167,13 +159,10 @@ def main():
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')

if (module.params['nat_destination'] and
StrictVersion(shade.__version__) < StrictVersion('1.8.0')):
module.fail_json(msg="To utilize nat_destination, the installed version of"
"the shade library MUST be >= 1.8.0")
if module.params['nat_destination']:
min_version = '1.8.0'
else:
min_version = None

server_name_or_id = module.params['server']
state = module.params['state']
Expand All @@ -186,7 +175,7 @@ def main():
timeout = module.params['timeout']
purge = module.params['purge']

cloud = shade.openstack_cloud(**module.params)
shade, cloud = openstack_cloud_from_module(module, min_version=min_version)

try:
server = cloud.get_server(server_name_or_id)
Expand Down
18 changes: 5 additions & 13 deletions lib/ansible/modules/cloud/openstack/os_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,8 @@
sample: "default"
'''

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 _system_state_change(state, description, group):
Expand All @@ -132,16 +126,14 @@ def main():
supports_check_mode=True,
**module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')
name = module.params.get('name')
description = module.params.get('description')
state = module.params.get('state')

name = module.params.pop('name')
description = module.params.pop('description')
domain_id = module.params.pop('domain_id')
state = module.params.pop('state')

shade, cloud = openstack_cloud_from_module(module)
try:
cloud = shade.operator_cloud(**module.params)
if domain_id:
group = cloud.get_group(name, filters={'domain_id': domain_id})
else:
Expand Down
15 changes: 3 additions & 12 deletions lib/ansible/modules/cloud/openstack/os_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
# Upload an image from a local file named cirros-0.3.0-x86_64-disk.img
- os_image:
auth:
auth_url: http://localhost/auth/v2.0
auth_url: https://identity.example.com
username: admin
password: passme
project_name: admin
Expand All @@ -124,14 +124,8 @@
distro: ubuntu
'''

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 main():
Expand All @@ -155,11 +149,8 @@ def main():
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')

shade, cloud = openstack_cloud_from_module(module)
try:
cloud = shade.openstack_cloud(**module.params)

changed = False
if module.params['checksum']:
Expand Down
15 changes: 3 additions & 12 deletions lib/ansible/modules/cloud/openstack/os_image_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
- name: Gather facts about a previously created image named image1
os_image_facts:
auth:
auth_url: https://your_api_url.com:9000/v2.0
auth_url: https://identity.example.com
username: user
password: password
project_name: someproject
Expand Down Expand Up @@ -127,14 +127,8 @@
type: int
'''

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 main():
Expand All @@ -145,11 +139,8 @@ def main():
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs)

if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')

shade, cloud = openstack_cloud_from_module(module)
try:
cloud = shade.openstack_cloud(**module.params)
image = cloud.get_image(module.params['image'])
module.exit_json(changed=False, ansible_facts=dict(
openstack_image=image))
Expand Down