예제 #1
0
    def get_next_event(self, session, timeout=None):
        """
        """
        event_queue_key = 'eventqueue:%s' % session.redis_key

        logger.debug('Waiting for event to be popped out of event queue: %s' %
                     event_queue_key)

        blob = self.redis.brpop([event_queue_key], timeout=timeout)

        logger.debug('Popping one event from the session event queue %s' %
                     event_queue_key)

        if not blob:
            return

        key, pack = blob
        event = GAPushEvent.from_dict(data=msgpack.unpackb(pack))
        resources = [
            GAResource(name=event.entity.rest_name, value=event.entity.id)
        ]
        request = GARequest(action=GARequest.ACTION_READ, resources=resources)
        context = GAContext(request=request, session=session)
        context.object = event.entity

        # operation_manager = GAOperationsController(context=context,
        #                                            logic_controller=self.core_controller.logic_controller,
        #                                            storage_controller=self.core_controller.storage_controller)
        # operation_manager.run()

        # if not context.has_errors:
        #     logger.debug('Returning one event to session %s' % session.redis_key)
        return event
예제 #2
0
    def test_perform_delegate(self):
        """
        """
        plugin1 = LogicPlugin1()
        plugin2 = LogicPlugin2()

        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[plugin1, plugin2],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)

        self.assertEquals(context.object, 'modified by LogicPlugin1')

        logic_controller.perform_delegate(delegate='nope',
                                          context=context)  # should not crash
예제 #3
0
    def test_clear_errors(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        error1 = GAError(type=GAError.TYPE_INVALID,
                         title='title1',
                         description='description2',
                         suggestion='nope',
                         property_name='prop1')
        error2 = GAError(type=GAError.TYPE_CONFLICT,
                         title='title2',
                         description='description3',
                         suggestion='nope',
                         property_name='prop2')
        error3 = GAError(type=GAError.TYPE_NOTFOUND,
                         title='title3',
                         description='description4',
                         suggestion='nope',
                         property_name='prop3')

        context.add_errors([error1, error2, error3])
        self.assertEquals(len(context.errors), 3)
        self.assertEquals(context.has_errors, True)

        context.clear_errors()
        self.assertEquals(len(context.errors), 0)
        self.assertEquals(context.has_errors, False)
예제 #4
0
    def test_make_response_with_errors(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        error1 = GAError(type=GAError.TYPE_INVALID,
                         title='title1',
                         description='description2',
                         suggestion='nope',
                         property_name='prop1')
        error2 = GAError(type=GAError.TYPE_CONFLICT,
                         title='title2',
                         description='description3',
                         suggestion='nope',
                         property_name='prop2')
        error3 = GAError(type=GAError.TYPE_NOTFOUND,
                         title='title3',
                         description='description4',
                         suggestion='nope',
                         property_name='prop3')
        context.add_errors([error1, error2, error3])

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseFailure)
        self.assertEquals(response.content, [error1, error2, error3])
예제 #5
0
    def test_copy(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        context.add_error(
            GAError(type=GAError.TYPE_INVALID,
                    title='title1',
                    description='description2',
                    suggestion='nope',
                    property_name='prop1'))
        context.add_error(
            GAError(type=GAError.TYPE_CONFLICT,
                    title='title2',
                    description='description3',
                    suggestion='nope',
                    property_name='prop2'))
        context.add_error(
            GAError(type=GAError.TYPE_NOTFOUND,
                    title='title3',
                    description='description4',
                    suggestion='nope',
                    property_name='prop3'))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_UPDATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_CREATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_DELETE,
                        entity=tstdk.GAEnterprise()))

        context.object = tstdk.GAEnterprise(name='enterprise1')
        context.objects = [
            tstdk.GAEnterprise(name='enterprise2'),
            tstdk.GAEnterprise(name='enterprise3')
        ]

        context_copy = context.copy()

        self.assertEquals(context_copy.session.uuid, session.uuid)
        self.assertEquals(context_copy.request.action, GARequest.ACTION_READ)
        self.assertEquals([obj.name for obj in context_copy.objects],
                          [obj.name for obj in context.objects])
        self.assertEquals(context_copy.object.name, context.object.name)

        self.assertEquals(context_copy.has_errors, True)
        self.assertEquals(len(context_copy.errors), 3)

        self.assertEquals(context_copy.has_events, True)
        self.assertEquals(len(context_copy.events), 3)
예제 #6
0
    def test_make_response_for_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        context.object = enterprise1

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, enterprise1)
예제 #7
0
    def test_make_response_for_read_all(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        enterprise2 = tstdk.GAEnterprise(name='enterprise2')
        context.objects = [enterprise1, enterprise2]

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, [enterprise1, enterprise2])
예제 #8
0
    def execute_model_request(self, request):
        """
        """
        session_uuid = self.sessions_controller.extract_session_identifier(
            request=request)
        session = None

        logger.debug("finding session: %s" % session_uuid)
        if session_uuid:
            session = self.sessions_controller.get_session(
                session_uuid=session_uuid)

        if not session:
            session = self.sessions_controller.create_session(request=request)

            if session:
                return GAResponseSuccess(content=[session.root_object])

        context = GAContext(session=session, request=request)

        if not session:
            error = GAError(
                type=GAError.TYPE_UNAUTHORIZED,
                title='Unauthorized access',
                description='Could not grant access. Please log in.')

            context.add_error(error)

            return GAResponseFailure(content=context.errors)

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        logger.debug('Execute action %s on session UUID=%s' %
                     (request.action, session_uuid))

        operations_controller = GAOperationsController(
            context=context,
            logic_controller=self.logic_controller,
            storage_controller=self.storage_controller)
        operations_controller.run()

        response = context.make_response()

        if len(context.events) > 0:  # pragma: no cover
            self.push_controller.push_events(events=context.events)

        return response
예제 #9
0
    def test_initialization(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        self.assertEquals(len(context.objects), 0)
        self.assertEquals(context.object, None)
        self.assertEquals(context.parent_object, None)
        self.assertEquals(context.request, request)
        self.assertEquals(context.session, session)
        self.assertEquals(len(context.errors), 0)
        self.assertEquals(context.has_errors, False)
        self.assertEquals(len(context.events), 0)
        self.assertEquals(context.has_events, False)
예제 #10
0
    def test_add_events(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        event1 = GAPushEvent(action=GARequest.ACTION_UPDATE,
                             entity=tstdk.GAEnterprise())
        event2 = GAPushEvent(action=GARequest.ACTION_CREATE,
                             entity=tstdk.GAEnterprise())
        event3 = GAPushEvent(action=GARequest.ACTION_DELETE,
                             entity=tstdk.GAEnterprise())

        context.add_event(event1)
        self.assertEquals(len(context.events), 1)
        self.assertEquals(context.has_events, True)

        context.add_events([event2, event3])
        self.assertEquals(len(context.events), 3)
        self.assertEquals(context.has_events, True)
예제 #11
0
    def execute_events_request(self, request):
        """
        """
        session_uuid = request.token
        session = self.sessions_controller.get_session(
            session_uuid=session_uuid)
        context = GAContext(session=session, request=request)

        if session is None:
            error = GAError(
                type=GAError.TYPE_UNAUTHORIZED,
                title='Unauthorized access',
                description='Could not grant access. Please login.')

            context.add_error(error)
            return (None, GAResponseFailure(content=context.errors))

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        return (session, None)
예제 #12
0
    def test_perform_delegate_with_no_plugin(self):
        """
        """
        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)