예제 #1
0
파일: api.py 프로젝트: vkmc/manila
    def invoke_elem(self, na_element, enable_tunneling=False):
        """Invoke the API on the server."""
        if na_element and not isinstance(na_element, NaElement):
            ValueError('NaElement must be supplied to invoke API')

        request, request_element = self._create_request(
            na_element, enable_tunneling)

        if self._trace:
            LOG.debug("Request: %s", request_element.to_string(pretty=True))

        if not hasattr(self, '_opener') or not self._opener \
                or self._refresh_conn:
            self._build_opener()
        try:
            if hasattr(self, '_timeout'):
                response = self._opener.open(request, timeout=self._timeout)
            else:
                response = self._opener.open(request)
        except urllib.error.HTTPError as e:
            raise NaApiError(e.code, e.msg)
        except urllib.error.URLError as e:
            raise exception.StorageCommunicationException(six.text_type(e))
        except Exception as e:
            raise NaApiError(message=e)

        response_xml = response.read()
        response_element = self._get_result(response_xml)

        if self._trace:
            LOG.debug("Response: %s", response_element.to_string(pretty=True))

        return response_element
예제 #2
0
    def invoke_elem(self, na_element, api_args=None):
        """Invoke the API on the server."""
        if na_element and not isinstance(na_element, NaElement):
            raise ValueError('NaElement must be supplied to invoke API')

        api_name = na_element.get_name()
        api_name_matches_regex = (re.match(self._api_trace_pattern, api_name)
                                  is not None)
        data = api_args.get("body") if api_args else {}

        if (not hasattr(self, '_session') or not self._session
                or self._refresh_conn):
            self._build_session()
        request_method, action_url = self._get_request_info(
            api_name, self._session)

        url_params = api_args.get("url_params") if api_args else None
        if url_params:
            action_url = action_url % url_params

        query = api_args.get("query") if api_args else None
        if query:
            action_url = self._add_query_params_to_url(action_url,
                                                       api_args['query'])

        url = self._get_base_url() + action_url
        data = jsonutils.dumps(data) if data else data

        if self._trace and api_name_matches_regex:
            message = ("Request: %(method)s %(url)s. Request body "
                       "%(body)s") % {
                           "method": request_method,
                           "url": action_url,
                           "body": api_args.get("body") if api_args else {}
                       }
            LOG.debug(message)

        try:
            if hasattr(self, '_timeout'):
                response = request_method(url,
                                          data=data,
                                          timeout=self._timeout)
            else:
                response = request_method(url, data=data)
        except requests.HTTPError as e:
            raise NaApiError(e.errno, e.strerror)
        except requests.URLRequired as e:
            raise exception.StorageCommunicationException(six.text_type(e))
        except Exception as e:
            raise NaApiError(message=e)

        response = (jsonutils.loads(response.content)
                    if response.content else None)
        if self._trace and api_name_matches_regex:
            LOG.debug("Response: %s", response)

        return response
예제 #3
0
    def invoke_elem(self, na_element, enable_tunneling=False):
        """Invoke the API on the server."""
        if na_element and not isinstance(na_element, NaElement):
            ValueError('NaElement must be supplied to invoke API')

        request_element = self._create_request(na_element, enable_tunneling)
        request_d = request_element.to_string()

        api_name = na_element.get_name()
        api_name_matches_regex = (re.match(self._api_trace_pattern, api_name)
                                  is not None)

        if self._trace and api_name_matches_regex:
            LOG.debug("Request: %s", request_element.to_string(pretty=True))

        if (not hasattr(self, '_session') or not self._session
                or self._refresh_conn):
            self._build_session()
        try:
            if hasattr(self, '_timeout'):
                response = self._session.post(self._get_url(),
                                              data=request_d,
                                              timeout=self._timeout)
            else:
                response = self._session.post(self._get_url(), data=request_d)
        except requests.HTTPError as e:
            raise NaApiError(e.errno, e.strerror)
        except requests.URLRequired as e:
            raise exception.StorageCommunicationException(six.text_type(e))
        except Exception as e:
            raise NaApiError(message=e)

        response_xml = response.text
        response_element = self._get_result(
            bytes(bytearray(response_xml, encoding='utf-8')))

        if self._trace and api_name_matches_regex:
            LOG.debug("Response: %s", response_element.to_string(pretty=True))

        return response_element