From 6ffd408c4b6bac81b6fa04a1b02e4533e5ffab56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Fri, 17 Jul 2015 23:25:33 +0000 Subject: [PATCH] Keystone fixes use newer ansible_module fix admin_port connection --- example-puppet.py | 10 +-- library/keystone_service.py | 168 ++++++++++++++++++------------------ 2 files changed, 88 insertions(+), 90 deletions(-) diff --git a/example-puppet.py b/example-puppet.py index fee1dcee..72d4f431 100644 --- a/example-puppet.py +++ b/example-puppet.py @@ -118,9 +118,9 @@ def deploy(): signals.connect(node1, keystone_service_endpoint) signals.connect(keystone_puppet, keystone_service_endpoint, { 'admin_token': 'admin_token', - 'admin_port': 'keystone_admin_port', + 'admin_port': ['admin_port', 'keystone_admin_port'], 'ip': ['keystone_host', 'admin_ip', 'internal_ip', 'public_ip'], - 'port': ['admin_port', 'internal_port', 'public_port'], + 'port': ['internal_port', 'public_port'], }) signals.connect(keystone_puppet, admin_tenant) @@ -327,7 +327,7 @@ def undeploy(): 'neutron_keystone_role', 'neutron_keystone_user', 'services_tenant', - #'keystone_service_endpoint', + 'keystone_service_endpoint', 'admin_role', 'admin_user', 'admin_tenant', @@ -338,7 +338,7 @@ def undeploy(): 'mariadb_service1', 'openstack_rabbitmq_user', 'openstack_vhost', - 'rabbitmq1', + 'rabbitmq_service1', ] resources = map(resource.wrap_resource, db.get_list(collection=db.COLLECTIONS.resource)) @@ -376,7 +376,7 @@ def undeploy(): # actions.resource_action(resources['openstack_rabbitmq_user'], 'remove') # actions.resource_action(resources['openstack_vhost'], 'remove') - # actions.resource_action(resources['rabbitmq1'], 'remove') + # actions.resource_action(resources['rabbitmq_service1'], 'remove') db.clear() diff --git a/library/keystone_service.py b/library/keystone_service.py index ab0425c8..4298445a 100644 --- a/library/keystone_service.py +++ b/library/keystone_service.py @@ -1,8 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copied from: https://github.com/openstack-ansible/openstack-ansible-modules/blob/master/keystone_service - DOCUMENTATION = ''' --- module: keystone_service @@ -124,116 +122,116 @@ def get_endpoint(keystone, name): return endpoints[0] -def ensure_service_present(keystone, name, service_type, description, - check_mode): - """ Ensure the service is present and has the right values +def ensure_present(keystone, name, service_type, description, public_url, + internal_url, admin_url, region, check_mode): + """ Ensure the service and its endpoint are present and have the right values. - Returns a pair, where the first element is a boolean that indicates - a state change, and the second element is the service uuid, or None - if running in check mode""" + Returns a tuple, where the first element is a boolean that indicates + a state change, the second element is the service uuid (or None in + check mode), and the third element is the endpoint uuid (or None in + check mode).""" + # Fetch service and endpoint, if they exist. service = None - try: - service = get_service(keystone, name) - except: - # Service doesn't exist yet, we'll need to create one - pass - else: - # See if it matches exactly - if service.name == name and \ - service.type == service_type and \ - service.description == description: - - # Same, no changes needed - return (False, service.id) - - # At this point, we know we will need to make a change - if check_mode: - return (True, None) - - if service is None: - service = keystone.services.create(name=name, - service_type=service_type, - description=description) - return (True, service.id) - else: - msg = "keystone v2 API doesn't support updating services" - raise ValueError(msg) - - -def ensure_endpoint_present(keystone, name, public_url, internal_url, - admin_url, region, check_mode): - """ Ensure the service endpoint is present and have the right values - - Assumes the service object has already been created at this point""" - - service = get_service(keystone, name) endpoint = None - try: - endpoint = get_endpoint(keystone, name) - except: - # Endpoint doesn't exist yet, we'll need to create one - pass - else: - # See if it matches - if endpoint.publicurl == public_url and \ - endpoint.adminurl == admin_url and \ - endpoint.internalurl == internal_url and \ - endpoint.region == region: + try: service = get_service(keystone, name) + except: pass + try: endpoint = get_endpoint(keystone, name) + except: pass - # Same, no changes needed - return (False, endpoint.id) + changed = False - # At this point, we know we will need to make a change - if check_mode: - return (True, None) + # Delete endpoint if it exists and doesn't match. + if endpoint is not None: + identical = endpoint.publicurl == public_url and \ + endpoint.adminurl == admin_url and \ + endpoint.internalurl == internal_url and \ + endpoint.region == region + if not identical: + changed = True + ensure_endpoint_absent(keystone, name, check_mode) + endpoint = None + # Delete service and its endpoint if the service exists and doesn't match. + if service is not None: + identical = service.name == name and \ + service.type == service_type and \ + service.description == description + if not identical: + changed = True + ensure_endpoint_absent(keystone, name, check_mode) + endpoint = None + ensure_service_absent(keystone, name, check_mode) + service = None + + # Recreate service, if necessary. + if service is None: + if not check_mode: + service = keystone.services.create( + name=name, + service_type=service_type, + description=description, + ) + changed = True + + # Recreate endpoint, if necessary. if endpoint is None: - endpoint = keystone.endpoints.create(region=region, - service_id=service.id, - publicurl=public_url, - adminurl=admin_url, - internalurl=internal_url) - return (True, endpoint.id) - else: - msg = "keystone v2 API doesn't support updating endpoints" - raise ValueError(msg) + if not check_mode: + endpoint = keystone.endpoints.create( + region=region, + service_id=service.id, + publicurl=public_url, + adminurl=admin_url, + internalurl=internal_url, + ) + changed = True + + if check_mode: + # In check mode, the service/endpoint uuids will be the old uuids, + # so omit them. + return changed, None, None + return changed, service.id, endpoint.id def ensure_service_absent(keystone, name, check_mode): """ Ensure the service is absent""" + try: + service = get_service(keystone, name) + if not check_mode: + keystone.services.delete(service.id) + return True + except KeyError: + # Service doesn't exist, so we're done. + return False - service = get_service(keystone, name) - keystone.services.delete(service.id) - return True def ensure_endpoint_absent(keystone, name, check_mode): """ Ensure the service endpoint """ - endpoint = get_endpoint(keystone, name) - keystone.endpoints.delete(endpoint.id) - return True + try: + endpoint = get_endpoint(keystone, name) + if not check_mode: + keystone.endpoints.delete(endpoint.id) + return True + except KeyError: + # Endpoint doesn't exist, so we're done. + return False def dispatch(keystone, name, service_type, description, public_url, internal_url, admin_url, region, state, check_mode): if state == 'present': - (service_changed, service_id) = ensure_service_present(keystone, - name, - service_type, - description, - check_mode) - - (endpoint_changed, endpoint_id) = ensure_endpoint_present( + (changed, service_id, endpoint_id) = ensure_present( keystone, name, + service_type, + description, public_url, internal_url, admin_url, region, - check_mode) - return dict(changed=service_changed or endpoint_changed, - service_id=service_id, - endpoint_id=endpoint_id) + check_mode, + ) + return dict(changed=changed, service_id=service_id, endpoint_id=endpoint_id) elif state == 'absent': endpoint_changed = ensure_endpoint_absent(keystone, name, check_mode) service_changed = ensure_service_absent(keystone, name, check_mode)