mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-22 22:11:44 -07:00
Fix tabs and spaces in OpenStack modules.
This commit is contained in:
parent
e5c2d0b247
commit
9c5d6f11f0
11 changed files with 472 additions and 471 deletions
98
library/cloud/quantum_floating_ip_associate
Executable file → Normal file
98
library/cloud/quantum_floating_ip_associate
Executable file → Normal file
|
@ -110,24 +110,24 @@ def _get_quantum_client(module, kwargs):
|
|||
quantum = client.Client('2.0', **kwargs)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
|
||||
return quantum
|
||||
return quantum
|
||||
|
||||
def _get_server_state(module, nova):
|
||||
server_info = None
|
||||
server = None
|
||||
try:
|
||||
for server in nova.servers.list():
|
||||
if server:
|
||||
info = server._info
|
||||
if info['name'] == module.params['instance_name']:
|
||||
if info['status'] != 'ACTIVE' and module.params['state'] == 'present':
|
||||
module.fail_json( msg="The VM is available but not Active. state:" + info['status'])
|
||||
server_info = info
|
||||
break
|
||||
for server in nova.servers.list():
|
||||
if server:
|
||||
info = server._info
|
||||
if info['name'] == module.params['instance_name']:
|
||||
if info['status'] != 'ACTIVE' and module.params['state'] == 'present':
|
||||
module.fail_json( msg="The VM is available but not Active. state:" + info['status'])
|
||||
server_info = info
|
||||
break
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "Error in getting the server list: %s" % e.message)
|
||||
return server_info, server
|
||||
|
||||
module.fail_json(msg = "Error in getting the server list: %s" % e.message)
|
||||
return server_info, server
|
||||
|
||||
def _get_port_id(quantum, module, instance_id):
|
||||
kwargs = {
|
||||
device_id': instance_id,
|
||||
|
@ -139,72 +139,72 @@ def _get_port_id(quantum, module, instance_id):
|
|||
if not ports['ports']:
|
||||
return None
|
||||
return ports['ports'][0]['id']
|
||||
|
||||
|
||||
def _get_floating_ip_id(module, quantum):
|
||||
kwargs = {
|
||||
'floating_ip_address': module.params['ip_address']
|
||||
'floating_ip_address': module.params['ip_address']
|
||||
}
|
||||
try:
|
||||
ips = quantum.list_floatingips(**kwargs)
|
||||
ips = quantum.list_floatingips(**kwargs)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "error in fetching the floatingips's %s" % e.message)
|
||||
module.fail_json(msg = "error in fetching the floatingips's %s" % e.message)
|
||||
if not ips['floatingips']:
|
||||
module.fail_json(msg = "Could find the ip specified in parameter, Please check")
|
||||
module.fail_json(msg = "Could find the ip specified in parameter, Please check")
|
||||
ip = ips['floatingips'][0]['id']
|
||||
if not ips['floatingips'][0]['port_id']:
|
||||
state = "detached"
|
||||
state = "detached"
|
||||
else:
|
||||
state = "attached"
|
||||
state = "attached"
|
||||
return state, ip
|
||||
|
||||
def _update_floating_ip(quantum, module, port_id, floating_ip_id):
|
||||
kwargs = {
|
||||
'port_id': port_id
|
||||
'port_id': port_id
|
||||
}
|
||||
try:
|
||||
result = quantum.update_floatingip(floating_ip_id, {'floatingip': kwargs})
|
||||
result = quantum.update_floatingip(floating_ip_id, {'floatingip': kwargs})
|
||||
except Exception as e:
|
||||
module.fail_json( msg = "There was an error in updating the floating ip address: %s" % e.message)
|
||||
module.fail_json( msg = "There was an error in updating the floating ip address: %s" % e.message)
|
||||
module.exit_json( changed = True, result = result, public_ip=module.params['ip_address'])
|
||||
|
||||
def main():
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
login_username = dict(default='admin'),
|
||||
login_password = dict(required=True),
|
||||
login_tenant_name = dict(required='True'),
|
||||
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
|
||||
region_name = dict(default=None),
|
||||
ip_address = dict(required=True),
|
||||
instance_name = dict(required=True),
|
||||
state = dict(default='present', choices=['absent', 'present'])
|
||||
argument_spec = dict(
|
||||
login_username = dict(default='admin'),
|
||||
login_password = dict(required=True),
|
||||
login_tenant_name = dict(required='True'),
|
||||
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
|
||||
region_name = dict(default=None),
|
||||
ip_address = dict(required=True),
|
||||
instance_name = dict(required=True),
|
||||
state = dict(default='present', choices=['absent', 'present'])
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
try:
|
||||
nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], module.params['auth_url'], service_type='compute')
|
||||
nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], module.params['auth_url'], service_type='compute')
|
||||
except Exception as e:
|
||||
module.fail_json( msg = " Error in authenticating to nova: %s" % e.message)
|
||||
module.fail_json( msg = " Error in authenticating to nova: %s" % e.message)
|
||||
quantum = _get_quantum_client(module, module.params)
|
||||
state, floating_ip_id = _get_floating_ip_id(module, quantum)
|
||||
state, floating_ip_id = _get_floating_ip_id(module, quantum)
|
||||
if module.params['state'] == 'present':
|
||||
if state == 'attached':
|
||||
module.exit_json(changed = False, result = 'attached', public_ip=module.params['ip_address'])
|
||||
server_info, server_obj = _get_server_state(module, nova)
|
||||
if not server_info:
|
||||
module.fail_json( msg = " The instance name provided cannot be found")
|
||||
port_id = _get_port_id(quantum, module, server_info['id'])
|
||||
if not port_id:
|
||||
module.fail_json(msg = "Cannot find a port for this instance, maybe fixed ip is not assigned")
|
||||
_update_floating_ip(quantum, module, port_id, floating_ip_id)
|
||||
if state == 'attached':
|
||||
module.exit_json(changed = False, result = 'attached', public_ip=module.params['ip_address'])
|
||||
server_info, server_obj = _get_server_state(module, nova)
|
||||
if not server_info:
|
||||
module.fail_json( msg = " The instance name provided cannot be found")
|
||||
port_id = _get_port_id(quantum, module, server_info['id'])
|
||||
if not port_id:
|
||||
module.fail_json(msg = "Cannot find a port for this instance, maybe fixed ip is not assigned")
|
||||
_update_floating_ip(quantum, module, port_id, floating_ip_id)
|
||||
|
||||
if module.params['state'] == 'absent':
|
||||
if state == 'detached':
|
||||
module.exit_json(changed = False, result = 'detached')
|
||||
if state == 'attached':
|
||||
_update_floating_ip(quantum, module, None, floating_ip_id)
|
||||
module.exit_json( changed = True, result = "detached")
|
||||
if state == 'detached':
|
||||
module.exit_json(changed = False, result = 'detached')
|
||||
if state == 'attached':
|
||||
_update_floating_ip(quantum, module, None, floating_ip_id)
|
||||
module.exit_json( changed = True, result = "detached")
|
||||
# this is magic, see lib/ansible/module.params['common.py
|
||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue