def disable(self, task_id):
        id_list = []
        for each_id in task_id:
            conn = pecan.request.storage_conn
            tasks = conn.get_rpttasks(task_id=each_id)
            if not tasks:
                LOG.error("Fail to get task %s." % each_id)
                raise wsme.exc.ClientSideError(
                    _("Reporttask with task_id=%s not exsit") %
                    task_id,
                    404)

            if tasks[0]['task_status'] == "Inactive":
                LOG.error("Task with task_id=%s already disabled", each_id)
                raise wsme.exc.ClientSideError(
                    _("Reporttask with task_id=%s already disabled!") %
                    each_id,
                    404)

            ctx = context.RequestContext()
            self.rpcclient.disable_task(ctx, task=tasks[0])

            # sleep 1s and wait for agent update db
            time.sleep(1)

            task_now = conn.get_rpttasks(task_id=each_id)
            id_list.append(each_id)
        # return ReportTask.from_dict(task_now[0])
        return id_list
    def post(self, data):
        ctx = context.RequestContext()
        conn = pecan.request.storage_conn

        tasks = list(conn.get_rpttasks(task_name=data.task_name,
                                       ))

        if tasks:
            raise wsme.exc.ClientSideError(
                _("Task with task_name='%s' exists") %
                (data.task_name),
                409)

        data = data.to_dict()

        now = datetime.now()
        data['task_createtime'] = datetime.now()
        data['task_updatetime'] = datetime.now()
        data['task_id'] = str(uuid.uuid4())

        try:
            conn.add_rpttask(data)
        except Exception as e:
            raise wsme.exc.ClientSideError(
                _("Add the task failed, catch exception: %s") %
                data.task_name,
                500)

        self.rpcclient.add_task(ctx, task=data)

        # sleep 1s and wait for agent update db
        time.sleep(1)

        task_now = conn.get_rpttasks(task_id=data['task_id'])
        return ReportTask.from_dict(task_now[0])
Esempio n. 3
0
    def __call__(self, req):
        user_id = req.headers.get('X_USER')
        user_id = req.headers.get('X_USER_ID', user_id)
        if user_id is None:
            LOG.debug("Neither X_USER_ID nor X_USER found in request")
            # return webob.exc.HTTPUnauthorized()

        roles = self._get_roles(req)
        """
        if 'X_TENANT_ID' in req.headers:
            # This is the new header since Keystone went to ID/Name
            project_id = req.headers['X_TENANT_ID']
        else:
            # This is for legacy compatibility
            project_id = req.headers['X_TENANT']
        """
        project_id = None

        project_name = req.headers.get('X_TENANT_NAME')
        user_name = req.headers.get('X_USER_NAME')

        req_id = req.environ.get(request_id.ENV_REQUEST_ID)

        # Get the auth token
        auth_token = req.headers.get('X_AUTH_TOKEN',
                                     req.headers.get('X_STORAGE_TOKEN'))

        # Build a context, including the auth_token...
        remote_address = req.remote_addr
        if CONF.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)

        service_catalog = None
        if req.headers.get('X_SERVICE_CATALOG') is not None:
            try:
                catalog_header = req.headers.get('X_SERVICE_CATALOG')
                service_catalog = jsonutils.loads(catalog_header)
            except ValueError:
                raise webob.exc.HTTPInternalServerError(
                    _('Invalid service catalog json.'))

        # NOTE(jamielennox): This is a full auth plugin set by auth_token
        # middleware in newer versions.
        user_auth_plugin = req.environ.get('keystone.token_auth')

        ctx = context.RequestContext(user_id,
                                     project_id,
                                     user_name=user_name,
                                     project_name=project_name,
                                     roles=roles,
                                     auth_token=auth_token,
                                     remote_address=remote_address,
                                     service_catalog=service_catalog,
                                     request_id=req_id,
                                     user_auth_plugin=user_auth_plugin)

        req.environ['report.context'] = ctx
        return self.application
 def get_all(self, time_s=None, time_e=None):
     result = []
     ctx = context.RequestContext()
     conn = pecan.request.storage_conn
     rpttasks = conn.get_rpttasks(time_s, time_e)
     rpttasks.sort(key=operator.itemgetter('task_createtime'),reverse=True)
     for task in rpttasks:
         result.append(ReportTask.from_dict(task))
     return result
 def dels(self, task_id):
     id_list = []
     for each_id in task_id:
         conn = pecan.request.storage_conn
         id = conn.get_rpttasks(task_id = each_id)
         if not id:
             raise wsme.exc.ClientSideError(
                     _("Task_id %s doesn\'t exist!") %
                     each_id,
                     404)
         ctx = context.RequestContext()
         self.rpcclient.del_task(ctx, task_id=each_id)
         id_list.append(each_id)
     return id_list
    def get(self, task_id):
        conn = pecan.request.storage_conn
        rpttasks = conn.get_rpttasks(task_id=task_id)
        ctx = context.RequestContext()
        task_next_time = self.rpcclient.get_next_run_time(ctx,task_id=task_id)
        if task_next_time:
            rpttasks[0]['task_next_rtime'] = datetime.strptime(str(task_next_time), "%Y-%m-%d %H:%M:%S")
        else:
            rpttasks[0]['task_next_rtime'] = None

        if not rpttasks:
            LOG.error("Fail to get task %s." % task_id)
            raise wsme.exc.ClientSideError("Reporttask with id=%s not exsit"
                                           % task_id, 404)

        return ReportTask.from_dict(rpttasks[0])
    def put(self, task_id, data):
        """Update a task"""
        conn = pecan.request.storage_conn
        tasks = conn.get_rpttasks(task_id=task_id)
        if not tasks:
            LOG.error("Fail to get task %s." % task_id)
            raise wsme.exc.ClientSideError(
                _("Reporttask with task_id=%s not exsit") %
                task_id,
                404)
        data['task_updatetime'] = datetime.now()

        def _update(task):
            task_content = json.loads(task.get('task_content'))
            task_metadata = json.loads(task.get('task_metadata'))
            for key in data.keys():
                if task.get(key):
                    task[key] = data.pop(key)
                # elif task_content.get(key):
                #   task_content[key] = data.pop(key)
                # elif task_metadata.get(key):
                #    task_metadata[key] = data.pop(key)

            # task["task_content"] = json.dumps(task_content)
            # task["task_metadata"] = json.dumps(task_metadata)

            return task

        ctx = context.RequestContext()

        task_dict = _update(tasks[0])
        self.rpcclient.update_task(ctx, task_dict)

        # sleep 1s and wait for agent update db
        time.sleep(1)

        task_now = conn.get_rpttasks(task_id=task_id)
        # result = result.append(ReportTask.from_dict(task_now[0]))
        return ReportTask.from_dict(task_now[0])