예제 #1
0
    def get(self):
        try:
            selector = self.get_query_argument(REST_QUERY_PARAM_SELECTOR, None)

            app_log.debug("Query arguments %s" % self.request.query_arguments)

            result = yield get.get_resource(self.idl,
                                            self.resource_path,
                                            self.schema,
                                            self.request.path,
                                            selector,
                                            self.request.query_arguments,
                                            fetch_readonly=True,
                                            manager=self.ref_object.manager)

            if result is None:
                self.set_status(httplib.NOT_FOUND)
            elif self.successful_query(result):
                self.set_status(httplib.OK)
                self.set_header(HTTP_HEADER_CONTENT_TYPE,
                                HTTP_CONTENT_TYPE_JSON)
                self.write(json.dumps(result))

        except APIException as e:
            self.on_exception(e)

        except Exception as e:
            self.on_exception(e)

        self.finish()
예제 #2
0
파일: base.py 프로젝트: bluecmd/ops-restd
    def get(self):

        selector = self.get_query_argument(REST_QUERY_PARAM_SELECTOR, None)

        app_log.debug("Query arguments %s" % self.request.query_arguments)

        result = get.get_resource(self.idl, self.resource_path,
                                  self.schema, self.request.path,
                                  selector, self.request.query_arguments)

        if result is None:
            self.set_status(httplib.NOT_FOUND)
        elif self.successful_query(result):
            self.set_status(httplib.OK)
            self.set_header(HTTP_HEADER_CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON)
            self.write(json.dumps(result))

        self.finish()
예제 #3
0
파일: base.py 프로젝트: bluecmd/ops-restd
    def process_if_match(self):
        if HTTP_HEADER_CONDITIONAL_IF_MATCH in self.request.headers:
            selector = self.get_query_argument(REST_QUERY_PARAM_SELECTOR, None)
            result = get.get_resource(self.idl, self.resource_path,
                                      self.schema, self.request.path,
                                      selector, self.request.query_arguments)
            if result is None:
                self.set_status(httplib.PRECONDITION_FAILED)
                return False

            match = False
            etags = self.request.headers.get(HTTP_HEADER_CONDITIONAL_IF_MATCH,
                                             "").split(',')
            current_etag = self.compute_etag(json.dumps(result))
            for e in etags:
                if e == current_etag or e == '"*"':
                    match = True
                    break

            if not match:
                # If is a PUT operation and the change request state
                # is already reflected in the current state of the
                # target resource it must return 2xx(Succesful)
                # https://tools.ietf.org/html/rfc7232#section-3.1
                if self.request.method == REQUEST_TYPE_UPDATE:
                    data = json.loads(self.request.body)
                    if OVSDB_SCHEMA_CONFIG in data and \
                        data[OVSDB_SCHEMA_CONFIG] == \
                        result[OVSDB_SCHEMA_CONFIG]:
                            # Set PUT Successful code and finish
                            self.set_status(httplib.OK)
                            return False
                # For POST, GET, DELETE, PATCH return precondition failed
                self.set_status(httplib.PRECONDITION_FAILED)
                return False
        # Etag matches
        return True
예제 #4
0
파일: base.py 프로젝트: chinhtle/ops-restd
    def process_if_match(self):
        if HTTP_HEADER_CONDITIONAL_IF_MATCH in self.request.headers:

            app_log.debug("Processing If-Match")

            selector = self.get_query_argument(REST_QUERY_PARAM_SELECTOR, None)
            query_arguments = self.request.query_arguments
            result = None

            from opsrest.handlers.ovsdbapi import OVSDBAPIHandler
            if isinstance(self, OVSDBAPIHandler):
                app_log.debug("If-Match is for OVSDBAPIHandler")
                from opsrest import get
                result = get.get_resource(self.idl, self.resource_path,
                                          self.schema, self.request.path,
                                          selector, query_arguments)
            elif self.controller is not None:
                app_log.debug("If-Match is for custom resource")

                if 'resource_id' in self.path_kwargs:
                    item_id = self.path_kwargs['resource_id']
                else:
                    item_id = None

                app_log.debug("Using resource_id=%s" % item_id)
                if item_id:
                    result = self.controller.get(item_id,
                                                 self.get_current_user(),
                                                 selector, query_arguments)
                else:
                    result = self.controller.get_all(self.get_current_user(),
                                                     selector, query_arguments)

            else:
                raise TransactionFailed("Resource cannot handle If-Match")

            if result is None:
                app_log.debug("If-Match's result is empty")
                self.set_status(httplib.PRECONDITION_FAILED)
                return False

            match = False
            etags = self.request.headers.get(HTTP_HEADER_CONDITIONAL_IF_MATCH,
                                             "").split(',')
            current_etag = self.compute_etag(json.dumps(result))
            for e in etags:
                if e == current_etag or e == '"*"':
                    match = True
                    break

            if not match:
                # If is a PUT operation and the change request state
                # is already reflected in the current state of the
                # target resource it must return 2xx(Succesful)
                # https://tools.ietf.org/html/rfc7232#section-3.1
                if self.request.method == REQUEST_TYPE_UPDATE:
                    data = json.loads(self.request.body)
                    if OVSDB_SCHEMA_CONFIG in data and \
                        data[OVSDB_SCHEMA_CONFIG] == \
                            result[OVSDB_SCHEMA_CONFIG]:
                            # Set PUT Successful code and finish
                            self.set_status(httplib.OK)
                            return False
                # For POST, GET, DELETE, PATCH return precondition failed
                self.set_status(httplib.PRECONDITION_FAILED)
                return False
        # Etag matches
        return True
예제 #5
0
    def process_if_match(self):
        if HTTP_HEADER_CONDITIONAL_IF_MATCH in self.request.headers:

            app_log.debug("Processing If-Match")

            selector = self.get_query_argument(REST_QUERY_PARAM_SELECTOR, None)
            query_arguments = self.request.query_arguments
            result = None

            from opsrest.handlers.ovsdbapi import OVSDBAPIHandler
            if isinstance(self, OVSDBAPIHandler):
                app_log.debug("If-Match is for OVSDBAPIHandler")
                from opsrest import get
                result = yield get.get_resource(self.idl,
                                                self.resource_path,
                                                self.schema,
                                                self.request.path,
                                                selector,
                                                query_arguments,
                                                fetch_readonly=True)
            elif self.controller is not None:
                app_log.debug("If-Match is for custom resource")

                if 'resource_id' in self.path_kwargs:
                    item_id = self.path_kwargs['resource_id']
                else:
                    item_id = None

                app_log.debug("Using resource_id=%s" % item_id)
                if item_id:
                    result = yield self.controller.get(item_id,
                                                       self.get_current_user(),
                                                       selector,
                                                       query_arguments)
                else:
                    result = \
                        yield self.controller.get_all(self.get_current_user(),
                                                      selector,
                                                      query_arguments)

            else:
                raise TransactionFailed("Resource cannot handle If-Match")

            if result is None:
                app_log.debug("If-Match's result is empty")
                self.set_status(httplib.PRECONDITION_FAILED)
                raise gen.Return(False)

            match = False
            etags = self.request.headers.get(HTTP_HEADER_CONDITIONAL_IF_MATCH,
                                             "").split(',')
            app_log.debug("Header Etag: %s" % etags)
            current_etag = self.compute_etag(json.dumps(result))
            app_log.debug("Current etag: %s" % current_etag)
            for e in etags:
                if e == current_etag or e == '"*"':
                    match = True
                    break

            if not match:
                # If is a PUT operation and the change request state
                # is already reflected in the current state of the
                # target resource it must return 2xx(Succesful)
                # https://tools.ietf.org/html/rfc7232#section-3.1
                if self.request.method == REQUEST_TYPE_UPDATE:
                    data = json.loads(self.request.body)
                    if OVSDB_SCHEMA_CONFIG in data and \
                        data[OVSDB_SCHEMA_CONFIG] == \
                            result[OVSDB_SCHEMA_CONFIG]:
                        # Set PUT Successful code and finish
                        self.set_status(httplib.OK)
                        raise gen.Return(False)
                # For POST, GET, DELETE, PATCH return precondition failed
                self.set_status(httplib.PRECONDITION_FAILED)
                raise gen.Return(False)
        # Etag matches
        raise gen.Return(True)