Update os_router to the latest version

This version uses the latest shade for a cleaner interface,
support for check mode, and updating an existing router.
pull/18777/head
David Shrewsbury 10 years ago committed by Matt Clay
parent 5ca11fa255
commit 94ae8bbc9b

@ -30,7 +30,9 @@ short_description: Create or Delete routers from OpenStack
extends_documentation_fragment: openstack extends_documentation_fragment: openstack
version_added: "1.10" version_added: "1.10"
description: description:
- Create or Delete routers from OpenStack - Create or Delete routers from OpenStack. Although Neutron allows
routers to share the same name, this module enforces name uniqueness
to be more user friendly.
options: options:
state: state:
description: description:
@ -43,7 +45,7 @@ options:
required: true required: true
admin_state_up: admin_state_up:
description: description:
- desired admin state of the created router . - Desired admin state of the created router.
required: false required: false
default: true default: true
requirements: ["shade"] requirements: ["shade"]
@ -51,75 +53,82 @@ requirements: ["shade"]
EXAMPLES = ''' EXAMPLES = '''
# Creates a router for tenant admin # Creates a router for tenant admin
- os_router: state=present - os_router:
username=admin state=present
password=admin name=router1
project_name=admin admin_state_up=True
name=router1"
''' '''
def _get_router_id(module, neutron):
kwargs = {
'name': module.params['name'],
}
try:
routers = neutron.list_routers(**kwargs)
except Exception, e:
module.fail_json(msg = "Error in getting the router list: %s " % e.message)
if not routers['routers']:
return None
return routers['routers'][0]['id']
def _create_router(module, neutron):
router = {
'name': module.params['name'],
'admin_state_up': module.params['admin_state_up'],
}
try:
new_router = neutron.create_router(dict(router=router))
except Exception, e:
module.fail_json( msg = "Error in creating router: %s" % e.message)
return new_router['router']['id']
def _delete_router(module, neutron, router_id): def _needs_update(router, admin_state_up):
try: """Decide if the given router needs an update.
neutron.delete_router(router_id)
except: The only attribute of the router that we allow to change is the value
module.fail_json("Error in deleting the router") of admin_state_up. Name changes are not supported here.
return True """
if router['admin_state_up'] != admin_state_up:
return True
return False
def _system_state_change(module, router):
"""Check if the system state would be changed."""
state = module.params['state']
if state == 'absent' and router:
return True
if state == 'present':
if not router:
return True
return _needs_update(router, module.params['admin_state_up'])
return False
def main(): def main():
argument_spec = openstack_full_argument_spec( argument_spec = openstack_full_argument_spec(
name = dict(required=True), name=dict(required=True),
state = dict(default='present', choices=['absent', 'present']), admin_state_up=dict(type='bool', default=True),
admin_state_up = dict(type='bool', default=True), state=dict(default='present', choices=['absent', 'present']),
) )
module_kwargs = openstack_module_kwargs() module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec, **module_kwargs) module = AnsibleModule(argument_spec,
supports_check_mode=True,
**module_kwargs)
if not HAS_SHADE: if not HAS_SHADE:
module.fail_json(msg='shade is required for this module') module.fail_json(msg='shade is required for this module')
name = module.params['name']
admin_state_up = module.params['admin_state_up']
state = module.params['state']
try: try:
cloud = shade.openstack_cloud(**module.params) cloud = shade.openstack_cloud(**module.params)
neutron = cloud.neutron_client router = cloud.get_router(name)
if module.check_mode:
module.exit_json(changed=_system_state_change(module, router))
if module.params['state'] == 'present': if state == 'present':
router_id = _get_router_id(module, neutron) if not router:
if not router_id: router = cloud.create_router(name, admin_state_up)
router_id = _create_router(module, neutron) module.exit_json(changed=True, result="created",
module.exit_json(changed=True, result="Created", id=router_id) id=router['id'])
else: else:
module.exit_json(changed=False, result="success" , id=router_id) if _needs_update(router, admin_state_up):
cloud.update_router(router['id'],
admin_state_up=admin_state_up)
module.exit_json(changed=True, result="updated",
id=router['id'])
else:
module.exit_json(changed=False, result="success",
id=router['id'])
else: elif state == 'absent':
router_id = _get_router_id(module, neutron) if not router:
if not router_id:
module.exit_json(changed=False, result="success") module.exit_json(changed=False, result="success")
else: else:
_delete_router(module, neutron, router_id) cloud.delete_router(name)
module.exit_json(changed=True, result="deleted") module.exit_json(changed=True, result="deleted")
except shade.OpenStackCloudException as e: except shade.OpenStackCloudException as e:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)

Loading…
Cancel
Save