Add support for neutronclient

The quantum_* modules will now try neutronclient first, and fall back
to quantumclient. If that fails, error out.

The code now references neutron instead of quantum in all internal
cases.
This commit is contained in:
Brad P. Crochet
2013-12-17 13:24:20 -05:00
parent 60e7a72829
commit ab52efc7fb
7 changed files with 249 additions and 227 deletions

View File

@@ -17,10 +17,13 @@
# along with this software. If not, see <http://www.gnu.org/licenses/>.
try:
from quantumclient.quantum import client
try:
from neutronclient.neutron import client
except ImportError:
from quantumclient.quantum import client
from keystoneclient.v2_0 import client as ksclient
except ImportError:
print("failed=True msg='quantum and keystone client are required'")
print("failed=True msg='quantumclient (or neutronclient) and keystoneclient are required'")
DOCUMENTATION = '''
---
@@ -77,7 +80,7 @@ options:
default: None
ip_version:
description:
- The IP version of the subnet 4 or 6
- The IP version of the subnet 4 or 6
required: false
default: 4
enable_dhcp:
@@ -105,7 +108,7 @@ options:
- From the subnet pool the last IP that should be assigned to the virtual machines
required: false
default: None
requirements: ["quantum", "keystoneclient"]
requirements: ["quantumclient", "neutronclient", "keystoneclient"]
'''
EXAMPLES = '''
@@ -125,21 +128,21 @@ def _get_ksclient(module, kwargs):
password=kwargs.get('login_password'),
tenant_name=kwargs.get('login_tenant_name'),
auth_url=kwargs.get('auth_url'))
except Exception as e:
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s" %e.message)
global _os_keystone
global _os_keystone
_os_keystone = kclient
return kclient
return kclient
def _get_endpoint(module, ksclient):
try:
endpoint = ksclient.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
except Exception as e:
module.fail_json(msg = "Error getting endpoint for glance: %s" % e.message)
module.fail_json(msg = "Error getting network endpoint: %s" % e.message)
return endpoint
def _get_quantum_client(module, kwargs):
def _get_neutron_client(module, kwargs):
_ksclient = _get_ksclient(module, kwargs)
token = _ksclient.auth_token
endpoint = _get_endpoint(module, _ksclient)
@@ -148,10 +151,10 @@ def _get_quantum_client(module, kwargs):
'endpoint_url': endpoint
}
try:
quantum = client.Client('2.0', **kwargs)
neutron = client.Client('2.0', **kwargs)
except Exception as e:
module.fail_json(msg = " Error in connecting to quantum: %s" % e.message)
return quantum
module.fail_json(msg = " Error in connecting to neutron: %s" % e.message)
return neutron
def _set_tenant_id(module):
global _os_tenant_id
@@ -167,24 +170,24 @@ def _set_tenant_id(module):
if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
def _get_net_id(quantum, module):
def _get_net_id(neutron, module):
kwargs = {
'tenant_id': _os_tenant_id,
'name': module.params['network_name'],
}
try:
networks = quantum.list_networks(**kwargs)
networks = neutron.list_networks(**kwargs)
except Exception as e:
module.fail_json("Error in listing quantum networks: %s" % e.message)
module.fail_json("Error in listing neutron networks: %s" % e.message)
if not networks['networks']:
return None
return networks['networks'][0]['id']
def _get_subnet_id(module, quantum):
def _get_subnet_id(module, neutron):
global _os_network_id
subnet_id = None
_os_network_id = _get_net_id(quantum, module)
_os_network_id = _get_net_id(neutron, module)
if not _os_network_id:
module.fail_json(msg = "network id of network not found.")
else:
@@ -193,15 +196,15 @@ def _get_subnet_id(module, quantum):
'name': module.params['name'],
}
try:
subnets = quantum.list_subnets(**kwargs)
subnets = neutron.list_subnets(**kwargs)
except Exception as e:
module.fail_json( msg = " Error in getting the subnet list:%s " % e.message)
if not subnets['subnets']:
return None
return subnets['subnets'][0]['id']
def _create_subnet(module, quantum):
quantum.format = 'json'
def _create_subnet(module, neutron):
neutron.format = 'json'
subnet = {
'name': module.params['name'],
'ip_version': module.params['ip_version'],
@@ -214,7 +217,7 @@ def _create_subnet(module, quantum):
}
if module.params['allocation_pool_start'] and module.params['allocation_pool_end']:
allocation_pools = [
{
{
'start' : module.params['allocation_pool_start'],
'end' : module.params['allocation_pool_end']
}
@@ -227,22 +230,22 @@ def _create_subnet(module, quantum):
else:
subnet.pop('dns_nameservers')
try:
new_subnet = quantum.create_subnet(dict(subnet=subnet))
new_subnet = neutron.create_subnet(dict(subnet=subnet))
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']
def _delete_subnet(module, quantum, subnet_id):
def _delete_subnet(module, neutron, subnet_id):
try:
quantum.delete_subnet(subnet_id)
neutron.delete_subnet(subnet_id)
except Exception as e:
module.fail_json( msg = "Error in deleting subnet: %s" % e.message)
return True
def main():
module = AnsibleModule(
argument_spec = dict(
login_username = dict(default='admin'),
@@ -263,23 +266,23 @@ def main():
allocation_pool_end = dict(default=None),
),
)
quantum = _get_quantum_client(module, module.params)
neutron = _get_neutron_client(module, module.params)
_set_tenant_id(module)
if module.params['state'] == 'present':
subnet_id = _get_subnet_id(module, quantum)
subnet_id = _get_subnet_id(module, neutron)
if not subnet_id:
subnet_id = _create_subnet(module, quantum)
subnet_id = _create_subnet(module, neutron)
module.exit_json(changed = True, result = "Created" , id = subnet_id)
else:
module.exit_json(changed = False, result = "success" , id = subnet_id)
else:
subnet_id = _get_subnet_id(module, quantum)
subnet_id = _get_subnet_id(module, neutron)
if not subnet_id:
module.exit_json(changed = False, result = "success")
else:
_delete_subnet(module, quantum, subnet_id)
_delete_subnet(module, neutron, subnet_id)
module.exit_json(changed = True, result = "deleted")
# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
main()