mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
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:
@@ -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='quantumclient and keystone client are required'")
|
||||
print("failed=True msg='quantumclient (or neutronclient) and keystone client are required'")
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
@@ -67,7 +70,7 @@ options:
|
||||
default: present
|
||||
name:
|
||||
description:
|
||||
- Name to be assigned to the nework
|
||||
- Name to be assigned to the nework
|
||||
required: true
|
||||
default: None
|
||||
provider_network_type:
|
||||
@@ -100,7 +103,7 @@ options:
|
||||
- Whether the state should be marked as up or down
|
||||
required: false
|
||||
default: true
|
||||
requirements: ["quantumclient", "keystoneclient"]
|
||||
requirements: ["quantumclient", "neutronclient", "keystoneclient"]
|
||||
|
||||
'''
|
||||
|
||||
@@ -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 Quantum: %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
|
||||
@@ -159,7 +162,7 @@ def _set_tenant_id(module):
|
||||
tenant_name = module.params['login_tenant_name']
|
||||
else:
|
||||
tenant_name = module.params['tenant_name']
|
||||
|
||||
|
||||
for tenant in _os_keystone.tenants.list():
|
||||
if tenant.name == tenant_name:
|
||||
_os_tenant_id = tenant.id
|
||||
@@ -168,22 +171,22 @@ def _set_tenant_id(module):
|
||||
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['name'],
|
||||
}
|
||||
try:
|
||||
networks = quantum.list_networks(**kwargs)
|
||||
networks = neutron.list_networks(**kwargs)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "Error in listing quantum networks: %s" % e.message)
|
||||
if not networks['networks']:
|
||||
module.fail_json(msg = "Error in listing neutron networks: %s" % e.message)
|
||||
if not networks['networks']:
|
||||
return None
|
||||
return networks['networks'][0]['id']
|
||||
|
||||
def _create_network(module, quantum):
|
||||
def _create_network(module, neutron):
|
||||
|
||||
quantum.format = 'json'
|
||||
neutron.format = 'json'
|
||||
|
||||
network = {
|
||||
'name': module.params.get('name'),
|
||||
@@ -212,21 +215,21 @@ def _create_network(module, quantum):
|
||||
network.pop('provider:segmentation_id', None)
|
||||
|
||||
try:
|
||||
net = quantum.create_network({'network':network})
|
||||
net = neutron.create_network({'network':network})
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "Error in creating network: %s" % e.message)
|
||||
return net['network']['id']
|
||||
|
||||
def _delete_network(module, net_id, quantum):
|
||||
|
||||
def _delete_network(module, net_id, neutron):
|
||||
|
||||
try:
|
||||
id = quantum.delete_network(net_id)
|
||||
except Exception as e:
|
||||
id = neutron.delete_network(net_id)
|
||||
except Exception as e:
|
||||
module.fail_json(msg = "Error in deleting the network: %s" % e.message)
|
||||
return True
|
||||
|
||||
def main():
|
||||
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
login_username = dict(default='admin'),
|
||||
@@ -237,8 +240,8 @@ def main():
|
||||
name = dict(required=True),
|
||||
tenant_name = dict(default=None),
|
||||
provider_network_type = dict(default=None, choices=['local', 'vlan', 'flat', 'gre']),
|
||||
provider_physical_network = dict(default=None),
|
||||
provider_segmentation_id = dict(default=None),
|
||||
provider_physical_network = dict(default=None),
|
||||
provider_segmentation_id = dict(default=None),
|
||||
router_external = dict(default=False, type='bool'),
|
||||
shared = dict(default=False, type='bool'),
|
||||
admin_state_up = dict(default=True, type='bool'),
|
||||
@@ -254,24 +257,24 @@ def main():
|
||||
if not module.params['provider_segmentation_id']:
|
||||
module.fail_json(msg = " for vlan & gre networks, variable provider_segmentation_id should be set.")
|
||||
|
||||
quantum = _get_quantum_client(module, module.params)
|
||||
neutron = _get_neutron_client(module, module.params)
|
||||
|
||||
_set_tenant_id(module)
|
||||
_set_tenant_id(module)
|
||||
|
||||
if module.params['state'] == 'present':
|
||||
network_id = _get_net_id(quantum, module)
|
||||
if module.params['state'] == 'present':
|
||||
network_id = _get_net_id(neutron, module)
|
||||
if not network_id:
|
||||
network_id = _create_network(module, quantum)
|
||||
network_id = _create_network(module, neutron)
|
||||
module.exit_json(changed = True, result = "Created", id = network_id)
|
||||
else:
|
||||
module.exit_json(changed = False, result = "Success", id = network_id)
|
||||
|
||||
if module.params['state'] == 'absent':
|
||||
network_id = _get_net_id(quantum, module)
|
||||
network_id = _get_net_id(neutron, module)
|
||||
if not network_id:
|
||||
module.exit_json(changed = False, result = "Success")
|
||||
else:
|
||||
_delete_network(module, network_id, quantum)
|
||||
_delete_network(module, network_id, neutron)
|
||||
module.exit_json(changed = True, result = "Deleted")
|
||||
|
||||
# this is magic, see lib/ansible/module.params['common.py
|
||||
|
||||
Reference in New Issue
Block a user