def url_for(request, service_type, admin=False, endpoint_type=None): endpoint_type = endpoint_type or getattr(settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) # jt region_name = request.session.get('region_name', None) current_region = service['endpoints'][0] if region_name: for e in service['endpoints']: if e['region'] == region_name: current_region = e if service: try: if admin: #return service['endpoints'][0]['adminURL'] return current_region['adminURL'] else: #return service['endpoints'][0][endpoint_type] return current_region[endpoint_type] except (IndexError, KeyError): raise exceptions.ServiceCatalogException(service_type) else: raise exceptions.ServiceCatalogException(service_type)
def url_for(request, service_type, admin=False, endpoint_type='internalURL'): catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) if service: try: if admin: return service['endpoints'][0]['adminURL'] else: return service['endpoints'][0][endpoint_type] except (IndexError, KeyError): raise exceptions.ServiceCatalogException(service_type) else: raise exceptions.ServiceCatalogException(service_type)
def url_for(request, service_type, admin=False, endpoint_type=None): endpoint_type = endpoint_type or getattr( settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) if service: try: if admin: return service['endpoints'][0]['adminURL'] else: return service['endpoints'][0][endpoint_type] except (IndexError, KeyError): raise exceptions.ServiceCatalogException(service_type) else: raise exceptions.ServiceCatalogException(service_type)
def test_image_name_no_glance_service(self, mock_image_get): server = self.servers.first() exc_catalog = horizon_exceptions.ServiceCatalogException('image') mock_image_get.side_effect = exc_catalog server = api.nova.Server(server, self.request) self.assertIsNone(server.image_name) mock_image_get.assert_called_once_with(test.IsHttpRequest(), server.image['id'])
def test_image_name_no_glance_service(self): server = self.servers.first() self.mox.StubOutWithMock(api.glance, 'image_get') api.glance.image_get(IsA( http.HttpRequest), server.image['id']).AndRaise( horizon_exceptions.ServiceCatalogException('image')) self.mox.ReplayAll() server = api.nova.Server(server, self.request) self.assertEqual('-', server.image_name)
def test_get_available_networks_except_service_catalog_exception( self, mock_neutron, mock_log): mock_neutron.network_list_for_tenant.side_effect = \ exceptions.ServiceCatalogException('test_exception') result = net.get_available_networks(self.mock_request) self.assertEqual([], result) mock_log.warning.assert_called_once_with( 'Neutron not found. Assuming Nova Network usage') mock_neutron.network_list_for_tenant.assert_called_once_with( self.mock_request, tenant_id='foo_tenant_id')
def url_for(request, service_type, admin=False, endpoint_type=None): endpoint_type = endpoint_type or getattr( settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) if admin: endpoint_type = 'adminURL' if service: try: if IDENTITY_VERSION < 3: return service['endpoints'][0][endpoint_type] else: interface = ENDPOINT_TYPE_TO_INTERFACE.get(endpoint_type, '') for endpoint in service['endpoints']: if endpoint['interface'] == interface: return endpoint['url'] except (IndexError, KeyError): raise exceptions.ServiceCatalogException(service_type) else: raise exceptions.ServiceCatalogException(service_type)
def url_for(request, service_type, endpoint_type=None, region=None): endpoint_type = endpoint_type or settings.OPENSTACK_ENDPOINT_TYPE fallback_endpoint_type = settings.SECONDARY_ENDPOINT_TYPE catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) if service: if not region: region = request.user.services_region url = get_url_for_service(service, region, endpoint_type) if not url and fallback_endpoint_type: url = get_url_for_service(service, region, fallback_endpoint_type) if url: return url raise exceptions.ServiceCatalogException(service_type)
def url_for(request, service_type, endpoint_type=None): endpoint_type = endpoint_type or getattr( settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') fallback_endpoint_type = getattr(settings, 'SECONDARY_ENDPOINT_TYPE', None) catalog = request.user.service_catalog service = get_service_from_catalog(catalog, service_type) if service: url = get_url_for_service(service, request.user.services_region, endpoint_type) if not url and fallback_endpoint_type: url = get_url_for_service(service, request.user.services_region, fallback_endpoint_type) if url: return url raise exceptions.ServiceCatalogException(service_type)
def get_auth_params_from_request(request): auth_url = base.url_for(request, 'identity') cinder_urls = [] for service_name in ('volumev3', 'volumev2', 'volume'): try: cinder_url = base.url_for(request, service_name) cinder_urls.append((service_name, cinder_url)) except exceptions.ServiceCatalogException: pass if not cinder_urls: raise exceptions.ServiceCatalogException( "no volume service configured") cinder_urls = tuple(cinder_urls) # need to make it cacheable return ( request.user.username, request.user.token.id, request.user.tenant_id, cinder_urls, auth_url, )
def get_auth_params_from_request(request): auth_url = base.url_for(request, 'identity') vmexpire_urls = [] for service_name in ('vmexpire', ): try: vmexpire_url = base.url_for(request, service_name) vmexpire_urls.append((service_name, vmexpire_url)) except exceptions.ServiceCatalogException: pass if not vmexpire_urls: raise exceptions.ServiceCatalogException( "no vmexpire service configured") vmexpire_urls = tuple(vmexpire_urls) # need to make it cacheable return ( request.user.username, request.user.token.id, request.user.tenant_id, vmexpire_urls, auth_url, )
def _find_cinder_url(request, version=None): if version is None: api_version = VERSIONS.get_active_version() version = api_version['version'] version = base.Version(version) # We support only cinder v3. # FIXME: 'block-storage' is also a valid service_type for cinder candidates = ['volumev3', 'volume'] for service_name in candidates: try: return version, base.url_for(request, service_name) except exceptions.ServiceCatalogException: pass else: raise exceptions.ServiceCatalogException( ("Cinder %(version)s requested but no '%(service)s' service " "type available in Keystone catalog.") % { 'version': version, 'service': candidates })
def cinderclient(request, version=None): if version is None: api_version = VERSIONS.get_active_version() version = api_version['version'] insecure = settings.OPENSTACK_SSL_NO_VERIFY cacert = settings.OPENSTACK_SSL_CACERT (username, token_id, tenant_id, cinder_urls, auth_url) = get_auth_params_from_request(request) version = base.Version(version) if version == 2: service_names = ('volumev2', 'volume') elif version == 3: service_names = ('volumev3', 'volume') else: service_names = ('volume',) for name, _url in cinder_urls: if name in service_names: cinder_url = _url break else: raise exceptions.ServiceCatalogException( "Cinder {version} requested but no '{service}' service " "type available in Keystone catalog.".format(version=version, service=service_names) ) c = cinder_client.Client( version, username, token_id, project_id=tenant_id, auth_url=auth_url, insecure=insecure, cacert=cacert, http_log_debug=settings.DEBUG, ) c.client.auth_token = token_id c.client.management_url = cinder_url return c
def url_for(request, service_type, endpoint_type=None, region=None): endpoint_type = endpoint_type or getattr( settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') fallback_endpoint_type = getattr(settings, 'SECONDARY_ENDPOINT_TYPE', None) # mj - this should not be required #catalog = request.user.service_catalog catalog = [ service for service in request.user.service_catalog if service['endpoints'][0]['region'] == request.user.services_region ] service = get_service_from_catalog(catalog, service_type) if service: if not region: region = request.user.services_region url = get_url_for_service(service, region, endpoint_type) if not url and fallback_endpoint_type: url = get_url_for_service(service, region, fallback_endpoint_type) if url: return url raise exceptions.ServiceCatalogException(service_type)
def vmexpireclient(request, version=None): if version is None: api_version = VERSIONS.get_active_version() version = api_version['version'] insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) # username, token_id, tenant_id, vmexpire_urls, auth_url = request_auth_params (username, token_id, tenant_id, vmexpire_urls, auth_url) = get_auth_params_from_request(request) try: version = base.Version(version) except Exception: version = str(version) service_names = ('vmexpire', ) for name, vmexpire_url in vmexpire_urls: if name in service_names: break else: raise exceptions.ServiceCatalogException( "VmExpire {version} requested but no '{service}' service " "type available in Keystone catalog.".format( version=version, service=service_names)) c = { 'version': version, 'username': username, 'token_id': token_id, 'project_id': tenant_id, 'auth_url': auth_url, 'insecure': insecure, 'cacert': cacert, 'http_log_debug': settings.DEBUG, 'auth_token': token_id, 'management_url': vmexpire_url } return c