Return resource objects from OpenStack modules
It's not uncommon for people to want to do additional things after creating a module. Also, add a note about it to the dev notes.
This commit is contained in:
parent
89f95471f8
commit
b955b2f5c8
7 changed files with 30 additions and 22 deletions
|
@ -21,6 +21,13 @@ Naming
|
|||
* If the module is one that a cloud admin and a cloud consumer could both use,
|
||||
the cloud consumer rules apply.
|
||||
|
||||
Interface
|
||||
---------
|
||||
|
||||
* If the resource being managed has an id, it should be returned.
|
||||
* If the resource being managed has an associated object more complex than
|
||||
an id, it should also be returned.
|
||||
|
||||
Interoperability
|
||||
----------------
|
||||
|
||||
|
|
|
@ -148,7 +148,7 @@ def main():
|
|||
|
||||
if module.params['state'] == 'present':
|
||||
if not image:
|
||||
result = cloud.create_image(
|
||||
image = cloud.create_image(
|
||||
name=module.params['name'],
|
||||
filename=module.params['filename'],
|
||||
disk_format=module.params['disk_format'],
|
||||
|
@ -158,26 +158,26 @@ def main():
|
|||
)
|
||||
changed = True
|
||||
if not module.params['wait']:
|
||||
module.exit_json(changed=changed, result=result)
|
||||
image = cloud.get_image(name_or_id=result['id'])
|
||||
module.exit_json(changed=changed, image=image, id=image.id)
|
||||
|
||||
cloud.update_image_properties(
|
||||
image=image,
|
||||
kernel=module.params['kernel'],
|
||||
ramdisk=module.params['ramdisk'],
|
||||
**module.params['properties'])
|
||||
image = cloud.get_image(name_or_id=image.id)
|
||||
module.exit_json(changed=changed, image=image, id=image.id)
|
||||
|
||||
elif module.params['state'] == 'absent':
|
||||
if not image:
|
||||
module.exit_json(changed=False, result="success")
|
||||
changed = False
|
||||
else:
|
||||
cloud.delete_image(
|
||||
name_or_id=module.params['name'],
|
||||
wait=module.params['wait'],
|
||||
timeout=module.params['timeout'])
|
||||
changed = True
|
||||
|
||||
module.exit_json(changed=changed, id=image.id, result="success")
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
except shade.OpenStackCloudException as e:
|
||||
module.fail_json(msg=e.message, extra_data=e.extra_data)
|
||||
|
|
|
@ -88,16 +88,14 @@ def main():
|
|||
if state == 'present':
|
||||
if not net:
|
||||
net = cloud.create_network(name, shared, admin_state_up)
|
||||
module.exit_json(changed=True, result="Created", id=net['id'])
|
||||
else:
|
||||
module.exit_json(changed=False, result="Success", id=net['id'])
|
||||
module.exit_json(changed=False, network=net, id=net['id'])
|
||||
|
||||
elif state == 'absent':
|
||||
if not net:
|
||||
module.exit_json(changed=False, result="Success")
|
||||
module.exit_json(changed=False)
|
||||
else:
|
||||
cloud.delete_network(name)
|
||||
module.exit_json(changed=True, result="Deleted")
|
||||
module.exit_json(changed=True)
|
||||
|
||||
except shade.OpenStackCloudException as e:
|
||||
module.fail_json(msg=e.message)
|
||||
|
|
|
@ -115,7 +115,7 @@ def main():
|
|||
|
||||
changed = process_object(cloud, **module.params)
|
||||
|
||||
module.exit_json(changed=changed, result="success")
|
||||
module.exit_json(changed=changed)
|
||||
except shade.OpenStackCloudException as e:
|
||||
module.fail_json(msg=e.message)
|
||||
|
||||
|
|
|
@ -241,7 +241,8 @@ EXAMPLES = '''
|
|||
|
||||
def _exit_hostvars(module, cloud, server, changed=True):
|
||||
hostvars = meta.get_hostvars_from_server(cloud, server)
|
||||
module.exit_json(changed=changed, id=server.id, openstack=hostvars)
|
||||
module.exit_json(
|
||||
changed=changed, server=server, id=server.id, openstack=hostvars)
|
||||
|
||||
|
||||
def _network_args(module, cloud):
|
||||
|
|
|
@ -227,7 +227,7 @@ def main():
|
|||
dns_nameservers=dns,
|
||||
allocation_pools=pool,
|
||||
host_routes=host_routes)
|
||||
module.exit_json(changed=True, result="created")
|
||||
changed = True
|
||||
else:
|
||||
if _needs_update(subnet, module):
|
||||
cloud.update_subnet(subnet['id'],
|
||||
|
@ -237,16 +237,18 @@ def main():
|
|||
dns_nameservers=dns,
|
||||
allocation_pools=pool,
|
||||
host_routes=host_routes)
|
||||
module.exit_json(changed=True, result="updated")
|
||||
changed = True
|
||||
else:
|
||||
module.exit_json(changed=False, result="success")
|
||||
changed = False
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
elif state == 'absent':
|
||||
if not subnet:
|
||||
module.exit_json(changed=False, result="success")
|
||||
changed = False
|
||||
else:
|
||||
changed = True
|
||||
cloud.delete_subnet(subnet_name)
|
||||
module.exit_json(changed=True, result="deleted")
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
except shade.OpenStackCloudException as e:
|
||||
module.fail_json(msg=e.message)
|
||||
|
|
|
@ -89,7 +89,7 @@ EXAMPLES = '''
|
|||
def _present_volume(module, cloud):
|
||||
if cloud.volume_exists(module.params['display_name']):
|
||||
v = cloud.get_volume(module.params['display_name'])
|
||||
module.exit_json(changed=False, id=v['id'])
|
||||
module.exit_json(changed=False, id=v['id'], volume=v)
|
||||
|
||||
volume_args = dict(
|
||||
size=module.params['size'],
|
||||
|
@ -106,7 +106,7 @@ def _present_volume(module, cloud):
|
|||
volume = cloud.create_volume(
|
||||
wait=module.params['wait'], timeout=module.params['timeout'],
|
||||
**volume_args)
|
||||
module.exit_json(changed=True, id=volume['id'])
|
||||
module.exit_json(changed=True, id=volume['id'], volume=volume)
|
||||
|
||||
|
||||
def _absent_volume(module, cloud):
|
||||
|
@ -116,8 +116,8 @@ def _absent_volume(module, cloud):
|
|||
wait=module.params['wait'],
|
||||
timeout=module.params['timeout'])
|
||||
except shade.OpenStackCloudTimeout:
|
||||
module.exit_json(changed=False, result="Volume deletion timed-out")
|
||||
module.exit_json(changed=True, result='Volume Deleted')
|
||||
module.exit_json(changed=False)
|
||||
module.exit_json(changed=True)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
Loading…
Reference in a new issue