Exemple #1
0
    def get_cr_scene_instance_data(self, obj):
        request = self.context.get('request')
        user = request.user if request else self.context.get('user')
        if obj.cr_scene_instance and user:
            handler = SceneHandler(user, scene=obj.cr_scene_instance)

            main = request.query_params.get('main') if request else None
            if main and main.isdigit():
                main = int(main)

            if (not main or main == obj.pk) and common.can_role_get_scene(
                    user.id, obj):
                fields_config = common.get_role_scene_fields_config(
                    user.id, obj)
                data = handler.get(fields=fields_config)
            else:
                data = handler.get()
                common.filter_public_nodes(data)
                fields_config = common.get_role_scene_fields_config(
                    user.id, obj, public=True)
                common.filter_scene_data_fields(data, fields=fields_config)

            return data
        else:
            return None
Exemple #2
0
    def prepare_scene(self, request, pk=None):
        cr_scene = self.get_object()
        if cr_scene.scene:
            delete_cr_scene_instance(request.user, cr_scene)

        handler = SceneHandler(request.user)
        scene = handler.create(cr_scene.scene_config,
                               status_updated={
                                   'func': _cr_scene_instance_status_updated,
                                   'params': {
                                       'user_id': request.user.pk,
                                       'cr_scene_id': cr_scene.pk,
                                   }
                               },
                               prepare=True,
                               super_viewer=True)
        cr_scene.scene = scene
        try:
            cr_scene.save()
        except Exception:
            delete_cr_scene_instance(request.user, cr_scene)
            raise exceptions.APIException(error.SAVE_FAILED)

        self.clear_cache()

        data = mserializers.CrSceneSerializer(cr_scene,
                                              fields=('scene_data', ),
                                              context={
                                                  'user': request.user
                                              }).data

        return Response(data['scene_data'], status=status.HTTP_201_CREATED)
Exemple #3
0
 def _get_scene_terminal_data(cls, user, scene_terminal, fields=None):
     user = get_user(user)
     scene_terminal = get_obj(scene_terminal, SceneTerminal)
     handler = SceneHandler(user)
     terminal_util = handler.get_terminal_util(scene_terminal)
     data = terminal_util.get_data(fields=fields)
     return data
Exemple #4
0
 def _get_scene_net_data(cls, user, scene_net, fields=None):
     user = get_user(user)
     scene_net = get_obj(scene_net, SceneNet)
     handler = SceneHandler(user)
     net_util = handler.get_net_util(scene_net)
     data = net_util.get_data(fields=fields)
     return data
Exemple #5
0
 def get_cr_scene_instance_data(self, obj):
     request = self.context.get('request')
     user = request.user if request else self.context.get('user')
     if obj.cr_scene_instance and user:
         handler = SceneHandler(user, scene=obj.cr_scene_instance)
         return handler.get()
     else:
         return None
Exemple #6
0
def delete_cr_scene_instance(user, cr_scene):
    handler = SceneHandler(user, scene=cr_scene.scene)
    handler.delete()

    cr_scene.scene = None
    try:
        cr_scene.save()
    except Exception as e:
        logger.error('cr scene save error: %s', e)
        raise exceptions.APIException(error.SAVE_FAILED)
Exemple #7
0
    def scene_terminal_create_prepared(self, request, pk=None):
        cr_scene = self.get_object()
        terminal_id = self.shift_data.get('id', int)
        if not terminal_id or not cr_scene.scene or not cr_scene.scene.sceneterminal_set.filter(
                pk=terminal_id).exists():
            raise exceptions.PermissionDenied(error.NO_PERMISSION)

        handler = SceneHandler(request.user, scene=cr_scene.scene)
        handler.create_prepared_terminal(scene_terminal=terminal_id)
        return Response(status=status.HTTP_200_OK)
Exemple #8
0
def report_server_status(request):
    try:
        scene_id = request.shift_data.get('scene', int)
        server_id = request.shift_data.get('server')
        server_status = request.shift_data.get('status', int)
    except Exception as e:
        logger.error('invalid params[%s]: %s' % (request.data, e))
        raise exceptions.ParseError(error.INVALID_PARAMS)

    if server_status not in SceneTerminal.Status.values():
        raise exceptions.ValidationError(error.INVALID_PARAMS)
    else:
        logger.info('report server status [scene=%s, server=%s, status=%s]' %
                    (scene_id, server_id, server_status))

    try:
        scene_terminal = SceneTerminal.objects.get(scene=scene_id,
                                                   sub_id=server_id)
    except SceneTerminal.DoesNotExist as e:
        raise exceptions.NotFound(error.TERMINAL_NOT_FOUND)

    handler = SceneHandler(scene_terminal.scene.user,
                           scene=scene_terminal.scene)
    try:
        handler.scene_util.report_terminal_status(scene_terminal,
                                                  server_status)
    except Exception as e:
        logger.error('terminal[server=%s] save error: %s', server_id, e)
        raise exceptions.APIException(error.ERROR)

    return Response()
Exemple #9
0
    def console_url(self, request, pk=None):
        cr_scene = self.get_object()

        if not cr_scene.scene:
            raise exceptions.PermissionDenied()

        try:
            scene_terminal_id = self.query_data.get('id', int)
            scene_terminal = cr_scene.scene.sceneterminal_set.filter(
                pk=scene_terminal_id).first()
            if not scene_terminal:
                raise Exception()
        except Exception:
            raise exceptions.ParseError(error.INVALID_PARAMS)

        handler = SceneHandler(request.user)
        url = handler.get_console_url(scene_terminal)
        return Response({'url': url})
Exemple #10
0
def gateway_firewall_rule(request):
    if request.method == 'POST':
        try:
            scene_id = request.shift_data.get('scene', int)
            gateway_id = request.shift_data.get('gateway')
            firewall_rule = json.loads(request.shift_data.get('firewall_rule'))
        except Exception as e:
            raise exceptions.ParseError(error.INVALID_PARAMS)

        try:
            scene_gateway = SceneGateway.objects.get(scene=scene_id, sub_id=gateway_id)
        except SceneGateway.DoesNotExist as e:
            raise exceptions.NotFound(error.GATEWAY_NOT_FOUND)

        gateway_util = SceneHandler(request.user).get_gateway_util(scene_gateway)
        try:
            gateway_util.add_firewall_rule(firewall_rule)
        except SceneException as e:
            raise exceptions.PermissionDenied(e.message)

        return Response(status=status.HTTP_201_CREATED)
    elif request.method == 'DELETE':
        try:
            scene_id = request.shift_data.get('scene', int)
            gateway_id = request.shift_data.get('gateway')
            firewall_rule = json.loads(request.shift_data.get('firewall_rule'))
        except Exception as e:
            raise exceptions.ParseError(error.INVALID_PARAMS)

        try:
            scene_gateway = SceneGateway.objects.get(scene=scene_id, sub_id=gateway_id)
        except SceneGateway.DoesNotExist as e:
            raise exceptions.NotFound(error.GATEWAY_NOT_FOUND)

        gateway_util = SceneHandler(request.user).get_gateway_util(scene_gateway)
        try:
            gateway_util.remove_firewall_rule(firewall_rule)
        except SceneException as e:
            raise exceptions.PermissionDenied(e.message)

        return Response(status=status.HTTP_204_NO_CONTENT)
Exemple #11
0
 def scene_handler(self):
     return SceneHandler(self.user,
                         proxy=self.proxy,
                         remote=self.remote,
                         scene=self.scene)
Exemple #12
0
 def _get_scene_data(cls, user, scene, fields=None):
     user = get_user(user)
     scene = get_obj(scene, Scene)
     handler = SceneHandler(user, scene=scene)
     data = handler.scene_util.get_data(fields=fields)
     return data