Lots of formatting fixes

This commit is contained in:
Michael DeHaan 2013-05-24 23:46:23 -04:00
parent 333577c944
commit 4105813ebe
10 changed files with 86 additions and 87 deletions

View file

@ -144,7 +144,7 @@ def _get_glance_client(module, kwargs):
'token': token, 'token': token,
} }
try: try:
client = glanceclient.Client('1', endpoint, **kwargs) client = glanceclient.Client('1', endpoint, **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to glance: %s" %e.message) module.fail_json(msg = "Error in connecting to glance: %s" %e.message)
return client return client
@ -179,7 +179,7 @@ def _glance_image_create(module, params, client):
break break
time.sleep(5) time.sleep(5)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in creating image: %s" %e.message ) module.fail_json(msg = "Error in creating image: %s" %e.message)
if image.status == 'active': if image.status == 'active':
module.exit_json(changed = True, result = image.status, id=image.id) module.exit_json(changed = True, result = image.status, id=image.id)
else: else:
@ -197,38 +197,38 @@ def _glance_delete_image(module, params, client):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
login_username = dict(default='admin'), login_username = dict(default='admin'),
login_password = dict(required=True), login_password = dict(required=True),
login_tenant_name = dict(required=True), login_tenant_name = dict(required=True),
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'), auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
region_name = dict(default=None), region_name = dict(default=None),
name = dict(required=True), name = dict(required=True),
disk_format = dict(default='qcow2', choices=['aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso']), disk_format = dict(default='qcow2', choices=['aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso']),
container_format = dict(default='bare', choices=['aki', 'ari', 'bare', 'ovf']), container_format = dict(default='bare', choices=['aki', 'ari', 'bare', 'ovf']),
owner = dict(default=None), owner = dict(default=None),
min_disk = dict(default=None), min_disk = dict(default=None),
min_ram = dict(default=None), min_ram = dict(default=None),
is_public = dict(default=True), is_public = dict(default=True),
copy_from = dict(default= None), copy_from = dict(default= None),
timeout = dict(default=180), timeout = dict(default=180),
file = dict(default=None), file = dict(default=None),
state = dict(default='present', choices=['absent', 'present']) state = dict(default='present', choices=['absent', 'present'])
), ),
mutually_exclusive = [['file','copy_from']], mutually_exclusive = [['file','copy_from']],
) )
if module.params['state'] == 'present': if module.params['state'] == 'present':
if not module.params['file'] and not module.params['copy_from']: if not module.params['file'] and not module.params['copy_from']:
module.fail_json(msg = "Either file or copy_from variable should be set to create the image") module.fail_json(msg = "Either file or copy_from variable should be set to create the image")
client = _get_glance_client(module, module.params) client = _get_glance_client(module, module.params)
id = _glance_image_present(module, module.params, client) id = _glance_image_present(module, module.params, client)
if not id: if not id:
_glance_image_create(module, module.params, client) _glance_image_create(module, module.params, client)
module.exit_json(changed = False, id = id, result = "success") module.exit_json(changed = False, id = id, result = "success")
if module.params['state'] == 'absent': if module.params['state'] == 'absent':
client = _get_glance_client(module, module.params) client = _get_glance_client(module, module.params)
id = _glance_image_present(module, module.params, client) id = _glance_image_present(module, module.params, client)
if not id: if not id:
module.exit_json(changed = False, result = "Success") module.exit_json(changed = False, result = "Success")
else: else:

View file

@ -152,7 +152,7 @@ def _create_server(module, nova):
if not module.params['key_name']: if not module.params['key_name']:
del bootkwargs['key_name'] del bootkwargs['key_name']
try: try:
server = nova.servers.create(*bootargs, **bootkwargs ) server = nova.servers.create(*bootargs, **bootkwargs)
server = nova.servers.get(server.id) server = nova.servers.get(server.id)
except Exception as e: except Exception as e:
module.fail_json( msg = "Error in creating instance: %s " % e.message) module.fail_json( msg = "Error in creating instance: %s " % e.message)

View file

@ -107,7 +107,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message) module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
return quantum return quantum
@ -214,7 +214,7 @@ def main():
fixed_ip, port_id = _get_port_info(quantum, module, server_info['id']) fixed_ip, port_id = _get_port_info(quantum, module, server_info['id'])
if not port_id: if not port_id:
module.fail_json(msg = "Cannot find a port for this instance, maybe fixed ip is not assigned") module.fail_json(msg = "Cannot find a port for this instance, maybe fixed ip is not assigned")
floating_id, floating_ip = _get_floating_ip(module, quantum, fixed_ip) floating_id, floating_ip = _get_floating_ip(module, quantum, fixed_ip)
if module.params['state'] == 'present': if module.params['state'] == 'present':
if floating_ip: if floating_ip:
module.exit_json(changed = False, public_ip=floating_ip) module.exit_json(changed = False, public_ip=floating_ip)

View file

@ -107,7 +107,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message) module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
return quantum return quantum
@ -119,30 +119,30 @@ def _get_server_state(module, nova):
for server in nova.servers.list(): for server in nova.servers.list():
if server: if server:
info = server._info info = server._info
if info['name'] == module.params['instance_name']: if info['name'] == module.params['instance_name']:
if info['status'] != 'ACTIVE' and module.params['state'] == 'present': if info['status'] != 'ACTIVE' and module.params['state'] == 'present':
module.fail_json( msg="The VM is available but not Active. state:" + info['status']) module.fail_json(msg="The VM is available but not Active. state:" + info['status'])
server_info = info server_info = info
break break
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in getting the server list: %s" % e.message) module.fail_json(msg = "Error in getting the server list: %s" % e.message)
return server_info, server return server_info, server
def _get_port_id(quantum, module, instance_id): def _get_port_id(quantum, module, instance_id):
kwargs = { kwargs = {
device_id': instance_id, device_id': instance_id,
} }
try: try:
ports = quantum.list_ports(**kwargs) ports = quantum.list_ports(**kwargs)
except Exception as e: except Exception as e:
module.fail_json( msg = "Error in listing ports: %s" % e.message) module.fail_json( msg = "Error in listing ports: %s" % e.message)
if not ports['ports']: if not ports['ports']:
return None return None
return ports['ports'][0]['id'] return ports['ports'][0]['id']
def _get_floating_ip_id(module, quantum): def _get_floating_ip_id(module, quantum):
kwargs = { kwargs = {
'floating_ip_address': module.params['ip_address'] 'floating_ip_address': module.params['ip_address']
} }
try: try:
ips = quantum.list_floatingips(**kwargs) ips = quantum.list_floatingips(**kwargs)
@ -152,14 +152,14 @@ def _get_floating_ip_id(module, quantum):
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'] ip = ips['floatingips'][0]['id']
if not ips['floatingips'][0]['port_id']: if not ips['floatingips'][0]['port_id']:
state = "detached" state = "detached"
else: else:
state = "attached" state = "attached"
return state, ip return state, ip
def _update_floating_ip(quantum, module, port_id, floating_ip_id): def _update_floating_ip(quantum, module, port_id, floating_ip_id):
kwargs = { kwargs = {
'port_id': port_id 'port_id': port_id
} }
try: try:
result = quantum.update_floatingip(floating_ip_id, {'floatingip': kwargs}) result = quantum.update_floatingip(floating_ip_id, {'floatingip': kwargs})

View file

@ -139,7 +139,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = " Error in connecting to quantum: %s " %e.message) module.fail_json(msg = " Error in connecting to quantum: %s " %e.message)
return quantum return quantum
@ -154,7 +154,7 @@ def _set_tenant_id(module):
for tenant in _os_keystone.tenants.list(): for tenant in _os_keystone.tenants.list():
if tenant.name == tenant_name: if tenant.name == tenant_name:
_os_tenant_id = tenant.id _os_tenant_id = tenant.id
break; break
if not _os_tenant_id: if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters") module.fail_json(msg = "The tenant id cannot be found, please check the paramters")

View file

@ -112,7 +112,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message) module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
return quantum return quantum
@ -127,7 +127,7 @@ def _set_tenant_id(module):
for tenant in _os_keystone.tenants.list(): for tenant in _os_keystone.tenants.list():
if tenant.name == login_tenant_name: if tenant.name == login_tenant_name:
_os_tenant_id = tenant.id _os_tenant_id = tenant.id
break; break
if not _os_tenant_id: if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters") module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
@ -152,9 +152,9 @@ def _create_router(module, quantum):
'admin_state_up': module.params['admin_state_up'], 'admin_state_up': module.params['admin_state_up'],
} }
try: try:
new_router = quantum.create_router({'router': router }) new_router = quantum.create_router(dict(router=router))
except Exception as e: except Exception as e:
module.fail_json( msg = "Error in creating router: %s" % e.message) module.fail_json( msg = "Error in creating router: %s" % e.message)
return new_router['router']['id'] return new_router['router']['id']
def _delete_router(module, quantum, router_id): def _delete_router(module, quantum, router_id):
@ -191,14 +191,11 @@ def main():
else: else:
router_id = _get_router_id(module, quantum) router_id = _get_router_id(module, quantum)
if not router_id: if not router_id:
module.exit_json(changed = False, result = "success" ) module.exit_json(changed = False, result = "success")
else: else:
_delete_router(module, quantum, router_id) _delete_router(module, quantum, router_id)
module.exit_json(changed = True, result = "deleted" ) module.exit_json(changed = True, result = "deleted")
# this is magic, see lib/ansible/module.params['common.py # this is magic, see lib/ansible/module.params['common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()

View file

@ -105,7 +105,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message) module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
return quantum return quantum

View file

@ -112,7 +112,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = "Error in connecting to quantum: %s " % e.message) module.fail_json(msg = "Error in connecting to quantum: %s " % e.message)
return quantum return quantum
@ -126,15 +126,15 @@ def _set_tenant_id(module):
for tenant in _os_keystone.tenants.list(): for tenant in _os_keystone.tenants.list():
if tenant.name == login_tenant_name: if tenant.name == login_tenant_name:
_os_tenant_id = tenant.id _os_tenant_id = tenant.id
break; break
if not _os_tenant_id: if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters") module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
def _get_router_id(module, quantum): def _get_router_id(module, quantum):
kwargs = { kwargs = {
'name': module.params['router_name'], 'name': module.params['router_name'],
} }
try: try:
routers = quantum.list_routers(**kwargs) routers = quantum.list_routers(**kwargs)

View file

@ -140,7 +140,7 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint 'endpoint_url': endpoint
} }
try: try:
quantum = client.Client('2.0', **kwargs) quantum = client.Client('2.0', **kwargs)
except Exception as e: except Exception as e:
module.fail_json(msg = " Error in connecting to quantum: %s" % e.message) module.fail_json(msg = " Error in connecting to quantum: %s" % e.message)
return quantum return quantum
@ -155,7 +155,7 @@ def _set_tenant_id(module):
for tenant in _os_keystone.tenants.list(): for tenant in _os_keystone.tenants.list():
if tenant.name == tenant_name: if tenant.name == tenant_name:
_os_tenant_id = tenant.id _os_tenant_id = tenant.id
break; break
if not _os_tenant_id: if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters") module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
@ -204,17 +204,19 @@ def _create_subnet(module, quantum):
'cidr': module.params['cidr'], 'cidr': module.params['cidr'],
} }
if module.params['allocation_pool_start'] and module.params['allocation_pool_end']: if module.params['allocation_pool_start'] and module.params['allocation_pool_end']:
allocation_pools = [ allocation_pools = [
{'start': module.params['allocation_pool_start'], {
'end': module.params['allocation_pool_end']} 'start' : module.params['allocation_pool_start'],
] 'end' : module.params['allocation_pool_end']
subnet.update({'allocation_pools': allocation_pools}) }
]
subnet.update({'allocation_pools': allocation_pools})
if not module.params['gateway_ip']: if not module.params['gateway_ip']:
subnet.pop('gateway_ip') subnet.pop('gateway_ip')
try: try:
new_subnet = quantum.create_subnet({'subnet': subnet }) new_subnet = quantum.create_subnet(dict(subnet=subnet))
except Exception as e: except Exception, e:
module.fail_json( msg = "Failure in creating subnet: %s" %e.message) module.fail_json(msg = "Failure in creating subnet: %s" % e.message)
return new_subnet['subnet']['id'] return new_subnet['subnet']['id']
@ -229,22 +231,22 @@ def _delete_subnet(module, quantum, subnet_id):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
login_username = dict(default='admin'), login_username = dict(default='admin'),
login_password = dict(required=True), login_password = dict(required=True),
login_tenant_name = dict(required='True'), login_tenant_name = dict(required='True'),
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'), auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
region_name = dict(default=None), region_name = dict(default=None),
name = dict(required=True), name = dict(required=True),
network_name = dict(required=True), network_name = dict(required=True),
cidr = dict(required=True), cidr = dict(required=True),
tenant_name = dict(default=None), tenant_name = dict(default=None),
state = dict(default='present', choices=['absent', 'present']), state = dict(default='present', choices=['absent', 'present']),
ip_version = dict(default='4', choices=['4', '6']), ip_version = dict(default='4', choices=['4', '6']),
enable_dhcp = dict(default='true', choices=BOOLEANS), enable_dhcp = dict(default='true', choices=BOOLEANS),
gateway_ip = dict(default=None), gateway_ip = dict(default=None),
allocation_pool_start = dict(default=None), allocation_pool_start = dict(default=None),
allocation_pool_end = dict(default=None), allocation_pool_end = dict(default=None),
), ),
) )
quantum = _get_quantum_client(module, module.params) quantum = _get_quantum_client(module, module.params)
@ -259,10 +261,10 @@ def main():
else: else:
subnet_id = _get_subnet_id(module, quantum) subnet_id = _get_subnet_id(module, quantum)
if not subnet_id: if not subnet_id:
module.exit_json(changed = False, result = "success" ) module.exit_json(changed = False, result = "success")
else: else:
_delete_subnet(module, quantum, subnet_id) _delete_subnet(module, quantum, subnet_id)
module.exit_json(changed = True, result = "deleted" ) module.exit_json(changed = True, result = "deleted")
# this is magic, see lib/ansible/module.params['common.py # this is magic, see lib/ansible/module.params['common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>

View file

@ -112,9 +112,9 @@ def main():
# deb http://myserver/path/to/repo free non-free # deb http://myserver/path/to/repo free non-free
for i in repo_url.split(): for i in repo_url.split():
for prot in ['http', 'file', 'ftp']: for prot in ['http', 'file', 'ftp']:
if prot in i: if prot in i:
repo_url = i repo_url = i
break break
exists = repo_exists(module, repo_url) exists = repo_exists(module, repo_url)
rc = 0 rc = 0