예제 #1
0
 def unmake_link(self, parameters):
     social_network_name = parameters.get("social_network")
     if social_network_name not in SOCIAL_NETWORKS:
         raise errors.InvalidParameter("the social network {} is not implemented".format(social_network_name))
     if not SOCIAL_NETWORKS[social_network_name].is_Connected():
         raise errors.InvalidParameter("cannot unlink from social network {} as it is in state {}".format(
             social_network_name, SOCIAL_NETWORKS[social_network_name].state))
     SOCIAL_NETWORKS[social_network_name].t_remove_connection()
예제 #2
0
 def make_link(self, parameters):
     social_network_name = parameters.get("social_network")
     token = parameters.get("token")
     if social_network_name not in SOCIAL_NETWORKS:
         raise errors.InvalidParameter("the social network {} is not implemented".format(social_network_name))
     if not SOCIAL_NETWORKS[social_network_name].is_Disconnected():
         raise errors.InvalidParameter("cannot link to social network {} as it is in state {}".format(
             social_network_name, SOCIAL_NETWORKS[social_network_name].state))
     SOCIAL_NETWORKS[social_network_name].t_make_connection(token)
예제 #3
0
 def call(self, parameters):
     social_network_name = parameters.get("social_network")
     endpoint = parameters.get("endpoint")
     call_parameters = parameters.get("parameters")
     if social_network_name not in SOCIAL_NETWORKS:
         raise errors.InvalidParameter("the social network {} is not implemented".format(social_network_name))
     if not SOCIAL_NETWORKS[social_network_name].is_Connected():
         raise errors.InvalidParameter("cannot make API call to social network {} as it is in state {}".format(
             social_network_name, SOCIAL_NETWORKS[social_network_name].state))
     result = SOCIAL_NETWORKS[social_network_name].call_api(endpoint, call_parameters)
     return result if result is not None else {}
예제 #4
0
    def get_drive_info(self, parameters):
        """Returns the storage drive information

        Returns:
            - ``InternalError``
            - Structure::

                {
                    'results': {
                        'used': string,
                        'percent': float,
                        'free': string,
                        'label': string,
                        'fs': string,
                        'device': string,
                        'mountpoint': string,
                        'total': string
                    }
                }

        Note:
            Calling this function can create some overhead
        """
        try:
            drive = parameters['drive']
        except:
            raise errors.InvalidParameter('No specified drive')
        mp_stats = self.monitor.drive_info(drive)
        if mp_stats is None:
            raise errors.InternalError(
                'No information found for drive {}'.format(parameters))
예제 #5
0
    def post(self):
        try:
            data = json.loads(self.request.body)
        except ValueError:
            raise tornado.web.HTTPError(400, "Ill-formed message command")

        # Split command name
        command_name = data.get("name")
        if command_name is None:
            raise tornado.web.HTTPError(400, "No command name")
        if command_name.count(".") != 1:
            raise tornado.web.HTTPError(400, "Invalid command name")
        (class_name, function_name) = command_name.split(".")
        # Create a handler instance
        class_instance = self.apiHandlers.get(class_name)
        if class_instance is None:
            raise tornado.web.HTTPError(400, "Unknown handler " + class_name)
        instance = class_instance(self.extra)

        # Validate parameters
        validate = data.get("validate", True)
        log = data.get("log", True)
        try:
            parameters = data.get("parameters")
            if self.verbose and log:
                logger.info(
                    "> " + class_name + "." + function_name + "(" + \
                    json.dumps(parameters) + ")")
            if validate:
                schema.api.validate_parameters(command_name, parameters)
        except jsonschema.ValidationError as ve:
            e = errors.InvalidParameter(str(ve)).payload
            m = {'error': e}
            logger.error(command_name + ", invalid parameter:\n" + str(ve))
        else:
            # Call instance method
            if not hasattr(instance, function_name):
                raise tornado.web.HTTPError(400,
                                            "Unknown function " + command_name)
            # call method
            function = getattr(instance, function_name)
            future = function(parameters)
            try:
                if isinstance(future, Future) or isinstance(
                        future, tornado.concurrent.Future):
                    result = yield future
                else:
                    f = Future()
                    f.set_result(future)
                    result = yield f
            except errors.VSError as e:
                m = {'error': e.payload}
            else:
                # Validate result
                try:
                    if validate:
                        schema.api.validate_result(command_name, result)
                except jsonschema.ValidationError as ve:
                    e = errors.InvalidReturnValue(str(ve)).payload
                    m = {'error': e}
                    logger.error(command_name + ", invalid return value:\n" +
                                 str(ve))
                else:
                    m = {'results': result}

        if self.verbose and log:
            logger.info("< " + json.dumps(m))
        self.write(m)
        self.finish()