示例#1
0
 def _test_delete(self):
     self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True)
     self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True)
     
     guid = 'blah'
     ArtifactInfo.get_by_guid(guid).AndReturn(MockEntity(MockKey(name=guid)))
     ArtifactContent.get_by_guid(guid).AndReturn(MockEntity(MockKey(name=guid)))
     
     self.moxer.ReplayAll()
     ArtifactAccessor.delete(guid)
     self.moxer.VerifyAll()
示例#2
0
 def test_delete_nonexistent(self):
     self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True)
     self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True)
     
     guid = 'blah'
     ArtifactInfo.get_by_guid(guid)
     ArtifactContent.get_by_guid(guid)
     
     self.moxer.ReplayAll()
     try:
         ArtifactAccessor.delete(guid)
         self.fail("exception expected")
     except NotFoundException, ex:
         pass
示例#3
0
    def find_or_create(cls, **kw):
        """
        returns:
            tuple: (ArtifactInfo key, ArtifactContent key, ArtifactSource key, created)
        """
        if not kw:
            raise IllegalArgumentException("keywords must be provided")

        source_name = kw.pop("source", None)
        content_type = kw.get("content_type")

        if not source_name:
            raise IllegalArgumentException("source keyword must be provided.")
        elif not content_type:
            raise IllegalArgumentException("content_type keyword must be provided.")

        # I pop "body" since I can't include it as a keyword for ArtifactInfo.create()
        body = kw.pop("body", None)

        # hashes content to avoid saving a duplicate
        content_md5 = cls._content_md5(source_name, content_type, body)

        found_artifact = ArtifactInfo.find_by_content_md5(content_md5).get()
        if found_artifact:
            info_key = found_artifact.key()
            content_key = ArtifactContent.get_by_guid(found_artifact.guid).key()
            source_key = found_artifact.source.key()
            created = False
        else:
            info_key, content_key, source_key = cls._create(source_name, body, content_md5, **kw)
            created = True
        return (info_key, content_key, source_key, created)
示例#4
0
    def find_or_create(cls, **kw):
        """
        returns:
            tuple: (ArtifactInfo key, ArtifactContent key, ArtifactSource key, created)
        """
        if not kw:
            raise IllegalArgumentException("keywords must be provided")

        source_name = kw.pop("source", None)
        content_type = kw.get("content_type")

        if not source_name:
            raise IllegalArgumentException("source keyword must be provided.")
        elif not content_type:
            raise IllegalArgumentException(
                "content_type keyword must be provided.")

        # I pop "body" since I can't include it as a keyword for ArtifactInfo.create()
        body = kw.pop("body", None)

        # hashes content to avoid saving a duplicate
        content_md5 = cls._content_md5(source_name, content_type, body)

        found_artifact = ArtifactInfo.find_by_content_md5(content_md5).get()
        if found_artifact:
            info_key = found_artifact.key()
            content_key = ArtifactContent.get_by_guid(
                found_artifact.guid).key()
            source_key = found_artifact.source.key()
            created = False
        else:
            info_key, content_key, source_key = cls._create(
                source_name, body, content_md5, **kw)
            created = True
        return (info_key, content_key, source_key, created)
示例#5
0
    def _test_delete(self):
        self.moxer.StubOutWithMock(ArtifactInfo,
                                   "get_by_guid",
                                   use_mock_anything=True)
        self.moxer.StubOutWithMock(ArtifactContent,
                                   "get_by_guid",
                                   use_mock_anything=True)

        guid = 'blah'
        ArtifactInfo.get_by_guid(guid).AndReturn(MockEntity(
            MockKey(name=guid)))
        ArtifactContent.get_by_guid(guid).AndReturn(
            MockEntity(MockKey(name=guid)))

        self.moxer.ReplayAll()
        ArtifactAccessor.delete(guid)
        self.moxer.VerifyAll()
示例#6
0
    def test_delete_nonexistent(self):
        self.moxer.StubOutWithMock(ArtifactInfo,
                                   "get_by_guid",
                                   use_mock_anything=True)
        self.moxer.StubOutWithMock(ArtifactContent,
                                   "get_by_guid",
                                   use_mock_anything=True)

        guid = 'blah'
        ArtifactInfo.get_by_guid(guid)
        ArtifactContent.get_by_guid(guid)

        self.moxer.ReplayAll()
        try:
            ArtifactAccessor.delete(guid)
            self.fail("exception expected")
        except NotFoundException, ex:
            pass
示例#7
0
 def get(cls, rhandler, guid, **kw):
     helper = RequestHelper(rhandler)
     
     artifact_info = ArtifactInfo.get_by_guid(guid)
     artifact_content = ArtifactContent.get_by_guid(guid)
     if artifact_info and artifact_content:
         artifact_hash = ArtifactsHelper.artifact_to_hash(artifact_info, artifact_content)
         helper.write_json(artifact_hash)
     else:
         helper.error(404)
示例#8
0
    def delete(cls, guid):
        logger = LoggerFactory.logger(cls.__name__)

        a_info = ArtifactInfo.get_by_guid(guid)
        a_content_key = ArtifactContent.get_by_guid(guid)
        if not (a_info or a_content_key):
            # neither record found
            raise NotFoundException("artifact %s" % guid)
        elif not (a_info and a_content_key):
            # one record found; one missing
            logger.warn("artifact %s; missing data; info=%s; content=%s" %
                        (guid, a_info.key().name(), a_content_key))

        # I delete what I can
        keys = []
        if a_info: keys.append(a_info)
        if a_content_key: keys.append(a_content_key)

        db.delete(keys)

        # decrease source counter
        Counters.source_counter(a_info.source.name).decrement()
示例#9
0
    def delete(cls, guid):
        logger = LoggerFactory.logger(cls.__name__)

        a_info = ArtifactInfo.get_by_guid(guid)
        a_content_key = ArtifactContent.get_by_guid(guid)
        if not (a_info or a_content_key):
            # neither record found
            raise NotFoundException("artifact %s" % guid)
        elif not (a_info and a_content_key):
            # one record found; one missing
            logger.warn("artifact %s; missing data; info=%s; content=%s" % (guid, a_info.key().name(), a_content_key))

        # I delete what I can
        keys = []
        if a_info:
            keys.append(a_info)
        if a_content_key:
            keys.append(a_content_key)

        db.delete(keys)

        # decrease source counter
        Counters.source_counter(a_info.source.name).decrement()
示例#10
0
 def get_content_by_guid(cls, guid):
     return ArtifactContent.get_by_guid(guid)
示例#11
0
 def get_content_by_guid(cls, guid):
     return ArtifactContent.get_by_guid(guid)