コード例 #1
0
    def get_object(cls, context, fields=None, **kwargs):
        """Fetch a single object

        Return the first result of given context or None if the result doesn't
        contain any row. Next, convert it to a versioned object.

        :param context:
        :param fields: indicate which fields the caller is interested in
                       using. Note that currently this is limited to
                       avoid loading synthetic fields when possible, and
                       does not affect db queries. Default is None, which
                       is the same as []. Example: ['id', 'name']
        :param kwargs: multiple keys defined by key=value pairs
        :return: single object of NeutronDbObject class or None
        """
        lookup_keys = set(kwargs.keys())
        all_keys = itertools.chain([cls.primary_keys], cls.unique_keys)
        if not any(lookup_keys.issuperset(keys) for keys in all_keys):
            missing_keys = set(cls.primary_keys).difference(lookup_keys)
            raise o_exc.NeutronPrimaryKeyMissing(object_class=cls,
                                                 missing_keys=missing_keys)

        with cls.db_context_reader(context):
            db_obj = obj_db_api.get_object(cls, context,
                                           **cls.modify_fields_to_db(kwargs))
            if db_obj:
                return cls._load_object(context, db_obj, fields=fields)
コード例 #2
0
ファイル: base.py プロジェクト: weizai118/neutron
    def get_object(cls, context, **kwargs):
        """
        Return the first result of given context or None if the result doesn't
        contain any row. Next, convert it to a versioned object.

        :param context:
        :param kwargs: multiple keys defined by key=value pairs
        :return: single object of NeutronDbObject class or None
        """
        lookup_keys = set(kwargs.keys())
        all_keys = itertools.chain([cls.primary_keys], cls.unique_keys)
        if not any(lookup_keys.issuperset(keys) for keys in all_keys):
            missing_keys = set(cls.primary_keys).difference(lookup_keys)
            raise o_exc.NeutronPrimaryKeyMissing(object_class=cls,
                                                 missing_keys=missing_keys)

        with cls.db_context_reader(context):
            db_obj = obj_db_api.get_object(
                cls, context, **cls.modify_fields_to_db(kwargs))
            if db_obj:
                return cls._load_object(context, db_obj)