Example #1
0
    def test_endpoint_not_found(self, mock_v3):
        class FakeKeystone(fake_ks.FakeKeystoneClient):
            def __init__(self):
                super(FakeKeystone, self).__init__()
                self.client = self
                self.version = 'v3'

        self.stub_keystoneclient(fake_client=FakeKeystone())
        con = mock.MagicMock(auth_token="1234", trust_id=None)
        c = clients.Clients(con)
        con.clients = c

        con.keystone_session = mock.Mock(name="keystone_session")
        get_endpoint_side_effects = [keystone_exc.EmptyCatalog(), None]
        con.keystone_session.get_endpoint = mock.Mock(
            name="get_endpoint", side_effect=get_endpoint_side_effects)

        mock_token_obj = mock.Mock()
        mock_v3.return_value = mock_token_obj
        mock_access = mock.Mock()
        self.patchobject(mock_token_obj,
                         'get_access',
                         return_value=mock_access)
        self.patchobject(mock_access,
                         'has_service_catalog',
                         return_value=False)
        plugin = FooClientsPlugin(con)

        self.assertRaises(keystone_exc.EndpointNotFound,
                          plugin.url_for,
                          service_type='nonexistent')
Example #2
0
    def test_get_missing_service_catalog(self, mock_v3):
        class FakeKeystone(fake_ks.FakeKeystoneClient):
            def __init__(self):
                super(FakeKeystone, self).__init__()
                self.client = self
                self.version = 'v3'

        self.stub_keystoneclient(fake_client=FakeKeystone())
        con = mock.MagicMock(auth_token="1234", trust_id=None)
        c = clients.Clients(con)
        con.clients = c

        con.keystone_session = mock.Mock(name="keystone_session")
        get_endpoint_side_effects = [
            keystone_exc.EmptyCatalog(), None, 'http://192.0.2.1/bar'
        ]
        con.keystone_session.get_endpoint = mock.Mock(
            name="get_endpoint", side_effect=get_endpoint_side_effects)

        mock_token_obj = mock.Mock()
        mock_token_obj.get_auth_ref.return_value = {'catalog': 'foo'}
        mock_v3.return_value = mock_token_obj

        plugin = FooClientsPlugin(con)

        self.assertEqual('http://192.0.2.1/bar',
                         plugin.url_for(service_type='bar'))
    def url_for(self, service_type=None, interface='public',
                region_name=None, service_name=None,
                service_id=None, endpoint_id=None):
        """Fetch an endpoint from the service catalog.

        Fetch the specified endpoint from the service catalog for
        a particular endpoint attribute. If no attribute is given, return
        the first endpoint of the specified type.

        Valid endpoint types: `public` or `publicURL`,
                              `internal` or `internalURL`,
                              `admin` or 'adminURL`

        :param string service_type: Service type of the endpoint.
        :param string interface: Type of endpoint.
        :param string region_name: Region of the endpoint.
        :param string service_name: The assigned name of the service.
        :param string service_id: The identifier of a service.
        :param string endpoint_id: The identifier of an endpoint.
        """
        if not self._catalog:
            raise exceptions.EmptyCatalog('The service catalog is empty.')

        urls = self.get_urls(service_type=service_type,
                             interface=interface,
                             region_name=region_name,
                             service_name=service_name,
                             service_id=service_id,
                             endpoint_id=endpoint_id)

        try:
            return urls[0]
        except Exception:
            pass

        if service_name and region_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'named %(service_name)s in %(region_name)s region not '
                   'found' %
                   {'interface': interface,
                    'service_type': service_type, 'service_name': service_name,
                    'region_name': region_name})
        elif service_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'named %(service_name)s not found' %
                   {'interface': interface,
                    'service_type': service_type,
                    'service_name': service_name})
        elif region_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'in %(region_name)s region not found' %
                   {'interface': interface,
                    'service_type': service_type, 'region_name': region_name})
        else:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'not found' %
                   {'interface': interface,
                    'service_type': service_type})

        raise exceptions.EndpointNotFound(msg)
Example #4
0
    def endpoint_data_for(self,
                          service_type=None,
                          interface='public',
                          region_name=None,
                          service_name=None,
                          service_id=None,
                          endpoint_id=None):
        """Fetch endpoint data from the service catalog.

        Fetch the specified endpoint data from the service catalog for
        a particular endpoint attribute. If no attribute is given, return
        the first endpoint of the specified type.

        Valid interface types: `public` or `publicURL`,
                               `internal` or `internalURL`,
                               `admin` or 'adminURL`

        :param string service_type: Service type of the endpoint.
        :param interface: Type of endpoint. Can be a single value or a list
                          of values. If it's a list of values, they will be
                          looked for in order of preference.
        :param string region_name: Region of the endpoint.
        :param string service_name: The assigned name of the service.
        :param string service_id: The identifier of a service.
        :param string endpoint_id: The identifier of an endpoint.
        """
        if not self._catalog:
            raise exceptions.EmptyCatalog('The service catalog is empty.')

        endpoint_data_list = self.get_endpoint_data_list(
            service_type=service_type,
            interface=interface,
            region_name=region_name,
            service_name=service_name,
            service_id=service_id,
            endpoint_id=endpoint_id)

        if endpoint_data_list:
            return endpoint_data_list[0]

        if service_name and region_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'named %(service_name)s in %(region_name)s region not '
                   'found' % {
                       'interface': interface,
                       'service_type': service_type,
                       'service_name': service_name,
                       'region_name': region_name
                   })
        elif service_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'named %(service_name)s not found' % {
                       'interface': interface,
                       'service_type': service_type,
                       'service_name': service_name
                   })
        elif region_name:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'in %(region_name)s region not found' % {
                       'interface': interface,
                       'service_type': service_type,
                       'region_name': region_name
                   })
        else:
            msg = ('%(interface)s endpoint for %(service_type)s service '
                   'not found' % {
                       'interface': interface,
                       'service_type': service_type
                   })

        raise exceptions.EndpointNotFound(msg)