def call(self, method, *args, **kwargs): """Call a glance client method. If we get a connection error, retry the request according to CONF.num_retries. :param context: The request context, for access checks. :param method: The method requested to be called. :param args: A list of positional arguments for the method called :param kwargs: A dict of keyword arguments for the method called :raises: GlanceConnectionFailed """ retry_excs = (glance_exc.ServiceUnavailable, glance_exc.InvalidEndpoint, glance_exc.CommunicationError) image_excs = (glance_exc.Forbidden, glance_exc.Unauthorized, glance_exc.NotFound, glance_exc.BadRequest) try: return getattr(self.client.images, method)(*args, **kwargs) except retry_excs as e: error_msg = ("Error contacting glance endpoint " "%(endpoint)s for '%(method)s'") LOG.exception(error_msg, { 'endpoint': self.endpoint, 'method': method }) raise exception.GlanceConnectionFailed(endpoint=self.endpoint, reason=e) except image_excs: exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_image_exception(args[0], exc_value) raise type(new_exc)(new_exc).with_traceback(exc_trace)
def call(self, method, *args, **kwargs): """Call a glance client method. If we get a connection error, retry the request according to CONF.glance_num_retries. :param context: The request context, for access checks. :param version: The requested API version.v :param method: The method requested to be called. :param args: A list of positional arguments for the method called :param kwargs: A dict of keyword arguments for the method called :raises: GlanceConnectionFailed """ retry_excs = (exception.ServiceUnavailable, exception.InvalidEndpoint, exception.CommunicationError) image_excs = (exception.Forbidden, exception.Unauthorized, exception.NotFound, exception.BadRequest) num_attempts = 1 + CONF.glance.glance_num_retries for attempt in range(1, num_attempts + 1): try: return getattr(self.client.images, method)(*args, **kwargs) except retry_excs as e: host = self.glance_host port = self.glance_port extra = "retrying" error_msg = _LE("Error contacting glance server " "'%(host)s:%(port)s' for '%(method)s', " "%(extra)s.") if attempt == num_attempts: extra = 'done trying' LOG.exception( error_msg, { 'host': host, 'port': port, 'num_attempts': num_attempts, 'method': method, 'extra': extra }) raise exception.GlanceConnectionFailed(host=host, port=port, reason=str(e)) LOG.exception( error_msg, { 'host': host, 'port': port, 'num_attempts': num_attempts, 'attempt': attempt, 'method': method, 'extra': extra }) time.sleep(1) except image_excs as e: exc_type, exc_value, exc_trace = sys.exc_info() if method == 'list': new_exc = _translate_plain_exception(exc_value) else: new_exc = _translate_image_exception(args[0], exc_value) raise new_exc, None, exc_trace
def test_validate_fail_glance_conn_problem(self, mock_glance): exceptions = (exception.GlanceConnectionFailed('connection fail'), exception.ImageNotAuthorized('not authorized'), exception.Invalid('invalid')) mock_glance.side_effect = exceptions for exc in exceptions: with task_manager.acquire(self.context, self.node.uuid, shared=True) as task: self.assertRaises(exception.InvalidParameterValue, task.driver.deploy.validate, task)
def call(self, method, *args, **kwargs): """Call a glance client method. If we get a connection error, retry the request according to CONF.glance_num_retries. :param context: The request context, for access checks. :param version: The requested API version.v :param method: The method requested to be called. :param args: A list of positional arguments for the method called :param kwargs: A dict of keyword arguments for the method called :raises: GlanceConnectionFailed """ retry_excs = (glance_exc.ServiceUnavailable, glance_exc.InvalidEndpoint, glance_exc.CommunicationError) image_excs = (glance_exc.Forbidden, glance_exc.Unauthorized, glance_exc.NotFound, glance_exc.BadRequest) num_attempts = 1 + CONF.glance.glance_num_retries # TODO(pas-ha) use retrying lib here for attempt in range(1, num_attempts + 1): try: return getattr(self.client.images, method)(*args, **kwargs) except retry_excs as e: error_msg = ("Error contacting glance server " "'%(endpoint)s' for '%(method)s', attempt" " %(attempt)s of %(num_attempts)s failed.") LOG.exception(error_msg, {'endpoint': self.endpoint, 'num_attempts': num_attempts, 'attempt': attempt, 'method': method}) if attempt == num_attempts: raise exception.GlanceConnectionFailed( endpoint=self.endpoint, reason=str(e)) time.sleep(1) except image_excs as e: exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_image_exception( args[0], exc_value) six.reraise(type(new_exc), new_exc, exc_trace)