Esempio n. 1
0
    def do_single_request(self, method, action, body=None):
        action = config.OFC.path_prefix + action
        LOG.debug("Client request: %(host)s:%(port)s "
                  "%(method)s %(action)s [%(body)s]",
                  {'host': self.host, 'port': self.port,
                   'method': method, 'action': action, 'body': body})
        if type(body) is dict:
            body = jsonutils.dumps(body)
        try:
            res = self._get_response(method, action, body)
            data = res.text
            LOG.debug("OFC returns [%(status)s:%(data)s]",
                      {'status': res.status_code,
                       'data': data})

            # Try to decode JSON data if possible.
            try:
                data = jsonutils.loads(data)
            except (ValueError, TypeError):
                pass

            if res.status_code in (requests.codes.OK,
                                   requests.codes.CREATED,
                                   requests.codes.ACCEPTED,
                                   requests.codes.NO_CONTENT):
                return data
            elif res.status_code == requests.codes.SERVICE_UNAVAILABLE:
                retry_after = res.headers.get('retry-after')
                LOG.warning(_LW("OFC returns ServiceUnavailable "
                                "(retry-after=%s)"), retry_after)
                raise nexc.OFCServiceUnavailable(retry_after=retry_after)
            elif res.status_code == requests.codes.NOT_FOUND:
                LOG.info(_LI("Specified resource %s does not exist on OFC "),
                         action)
                raise nexc.OFCResourceNotFound(resource=action)
            else:
                LOG.warning(_LW("Operation on OFC failed: "
                                "status=%(status)s, detail=%(detail)s"),
                            {'status': res.status_code, 'detail': data})
                params = {'reason': _("Operation on OFC failed"),
                          'status': res.status_code}
                if isinstance(data, dict):
                    params['err_code'] = data.get('err_code')
                    params['err_msg'] = data.get('err_msg')
                else:
                    params['err_msg'] = data
                raise nexc.OFCException(**params)
        except requests.exceptions.RequestException as e:
            reason = _("Failed to connect OFC : %s") % e
            LOG.error(reason)
            raise nexc.OFCException(reason=reason)
Esempio n. 2
0
    def do_request(self, method, action, body=None):
        LOG.debug(_("Client request: %(host)s:%(port)s "
                    "%(method)s %(action)s [%(body)s]"),
                  {'host': self.host, 'port': self.port,
                   'method': method, 'action': action, 'body': body})
        if type(body) is dict:
            body = json.dumps(body)
        try:
            conn = self.get_connection()
            headers = {"Content-Type": "application/json"}
            conn.request(method, action, body, headers)
            res = conn.getresponse()
            data = res.read()
            LOG.debug(_("OFC returns [%(status)s:%(data)s]"),
                      {'status': res.status,
                       'data': data})

            # Try to decode JSON data if possible.
            try:
                data = json.loads(data)
            except (ValueError, TypeError):
                pass

            if res.status in (httplib.OK,
                              httplib.CREATED,
                              httplib.ACCEPTED,
                              httplib.NO_CONTENT):
                return data
            elif res.status == httplib.NOT_FOUND:
                LOG.info(_("Specified resource %s does not exist on OFC "),
                         action)
                raise nexc.OFCResourceNotFound(resource=action)
            else:
                LOG.warning(_("Operation on OFC failed: "
                              "status=%(status)s, detail=%(detail)s"),
                            {'status': res.status, 'detail': data})
                params = {'reason': _("Operation on OFC failed"),
                          'status': res.status}
                if isinstance(data, dict):
                    params['err_code'] = data.get('err_code')
                    params['err_msg'] = data.get('err_msg')
                else:
                    params['err_msg'] = data
                raise nexc.OFCException(**params)
        except (socket.error, IOError) as e:
            reason = _("Failed to connect OFC : %s") % e
            LOG.error(reason)
            raise nexc.OFCException(reason=reason)
Esempio n. 3
0
 def test_delete_port_for_nonexist_ofc_port(self):
     self._test_delete_port_for_disappeared_ofc_port(
         nexc.OFCResourceNotFound(resource='ofc_port'))