예제 #1
0
    def post(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        tag_name = data.get('tag_name')
        users = data.get('users')
        dbs = data.get('dbs')  ### ID列表
        servers = data.get('servers')  ### ID列表
        proxy_host = data.get('proxy_host', None)

        if not tag_name or not users:
            return self.write(dict(code=-1, msg='标签名称不能为空'))

        with DBContext('r') as session:
            exist_id = session.query(Tag.id).filter(Tag.tag_name == tag_name).first()

        if exist_id:
            return self.write(dict(code=-2, msg='标签名称重复'))

        ### 判断是否存在
        with DBContext('w', None, True) as session:
            if users:
                users = ','.join(users)
            new_tag = Tag(tag_name=tag_name, users=users, proxy_host=proxy_host)
            session.add(new_tag)
            session.commit()
            if dbs:
                for db in dbs:
                    session.add(DBTag(db_id=int(db), tag_id=new_tag.id))
            if servers:
                for server in servers:
                    session.add(ServerTag(server_id=int(server), tag_id=new_tag.id))

        self.write(dict(code=0, msg='添加成功'))
예제 #2
0
    def put(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        tag_id = data.get('id')
        users = data.get('users')
        db_id_list = data.get('dbs')  ### ID列表
        server_id_list = data.get('servers')  ### ID列表
        proxy_host = data.get('proxy_host', None)

        if not users:
            return self.write(dict(code=-1, msg='授权用户不能为空'))

        with DBContext('w', None, True) as session:
            # if db_id_list:
            # db_list, new_db_list, del_db_list = [], [], []
            # in_db_tags = session.query(DB.id).outerjoin(DBTag, DBTag.db_id == DB.id).filter(
            #     DBTag.tag_id == tag_id).all()
            # for i in in_db_tags:
            #     i = i[0]
            #     db_list.append(i)
            #     if i not in db_id_list:
            #         del_db_list.append(i)
            #         session.query(DBTag).filter(DBTag.db_id == i).delete(synchronize_session=False)
            #
            # for i in db_id_list:
            #     if i not in db_list:
            #         session.add(DBTag(db_id=int(i), tag_id=tag_id))
            #         new_db_list.append(i)

            # if server_id_list:
            # server_list, new_server_list, del_server_list = [], [], []
            # in_server_tags = session.query(Server.id).outerjoin(ServerTag, ServerTag.server_id == Server.id).filter(
            #     ServerTag.tag_id == tag_id).all()
            #
            # for i in in_server_tags:
            #     i = i[0]
            #     server_list.append(i)
            #     if i not in server_id_list:
            #         del_server_list.append(i)
            #
            #         session.query(ServerTag).filter(ServerTag.server_id == i).delete(synchronize_session=False)
            #
            # for i in server_id_list:
            #     if i not in server_list:
            #         session.add(ServerTag(server_id=int(i), tag_id=tag_id))
            #         new_server_list.append(i)
            session.query(DBTag).filter(DBTag.tag_id == int(tag_id)).delete(synchronize_session=False)
            session.add_all([
                DBTag(db_id=i, tag_id=tag_id) for i in db_id_list
            ])

            session.query(ServerTag).filter(ServerTag.tag_id == int(tag_id)).delete(synchronize_session=False)
            session.add_all([
                ServerTag(server_id=i, tag_id=tag_id) for i in server_id_list
            ])
            if users:
                users = ','.join(users)
            session.query(Tag).filter(Tag.id == int(tag_id)).update({Tag.users: users, Tag.proxy_host: proxy_host})
            session.commit()

        self.write(dict(code=0, msg='修改成功'))
예제 #3
0
    def put(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        nickname = self.get_current_nickname()
        db_id = data.get('id', None)
        db_code = data.get('db_code', None)
        db_host = data.get('db_host', None)
        db_port = data.get('db_port', 3306)
        db_user = data.get('db_user', None)
        db_pwd = data.get('db_pwd', None)
        db_env = data.get('db_env', None)
        proxy_host = data.get('proxy_host', None)
        db_type = data.get('db_type', 'mysql')
        db_version = data.get('db_version', None)
        db_mark = data.get('db_mark', '写')
        idc = data.get('idc', None)
        tag_list = data.get('tag_list', [])
        db_detail = data.get('db_detail', None)
        db_instance = data.get('db_instance', '')
        if not db_id or not db_code or not db_host or not db_port or not db_user:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        with DBContext('r') as session:
            old_password = session.query(DB.db_pwd).filter(DB.id == int(db_id)).first()[0]

        if old_password != db_pwd:
            # 加密密码
            db_pwd = encrypt(db_pwd)

        with DBContext('w', None, True) as session:
            all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all()
            session.query(DBTag).filter(DBTag.db_id == db_id).delete(synchronize_session=False)
            if all_tags:
                for tag_id in all_tags:
                    session.add(DBTag(db_id=int(db_id), tag_id=tag_id[0]))

            session.query(DB).filter(DB.id == int(db_id)).update({DB.db_code: db_code, DB.db_host: db_host,
                                                                  DB.db_port: db_port, DB.db_user: db_user,
                                                                  DB.db_pwd: db_pwd, DB.db_env: db_env,
                                                                  DB.proxy_host: proxy_host, DB.db_type: db_type,
                                                                  DB.db_version: db_version, DB.idc: idc,
                                                                  DB.db_mark: db_mark, DB.db_detail: db_detail,
                                                                  DB.db_instance: db_instance
                                                                  })
        # 记录操作,不成功直接Pass
        try:
            modify_data = data
            with DBContext('w', None, True) as session:
                data_info = session.query(DB).filter(DB.id == int(db_id)).all()
                for data in data_info:
                    origion_data = model_to_dict(data)
                    origion_data['create_time'] = str(origion_data['create_time'])
                    origion_data['update_time'] = str(origion_data['update_time'])
                    new_record = AssetOperationalAudit(username=nickname, request_object='数据库', request_host=db_host,
                                                       request_method='更新', original_data=origion_data,
                                                       modify_data=modify_data)
                    session.add(new_record)
        except Exception as err:
            ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err))

        return self.write(dict(code=0, msg='编辑成功'))
예제 #4
0
    def put(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        db_id = data.get('id', None)
        db_code = data.get('db_code', None)
        db_host = data.get('db_host', None)
        db_port = data.get('db_port', 3306)
        db_user = data.get('db_user', None)
        db_pwd = data.get('db_pwd', None)
        db_env = data.get('db_env', None)
        proxy_host = data.get('proxy_host', None)
        db_type = data.get('db_type', 'mysql')
        db_version = data.get('db_version', None)
        db_mark = data.get('db_mark', '写')
        tag_list = data.get('tag_list', [])
        db_detail = data.get('db_detail', None)
        if not db_id or not db_code or not db_host or not db_port or not db_user or not db_pwd or not db_env or not db_detail:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        with DBContext('w', None, True) as session:
            all_tags = session.query(Tag.id).filter(
                Tag.tag_name.in_(tag_list)).order_by(Tag.id).all()
            session.query(DBTag).filter(DBTag.db_id == db_id).delete(
                synchronize_session=False)
            if all_tags:
                for tag_id in all_tags:
                    session.add(DBTag(db_id=int(db_id), tag_id=tag_id[0]))

            session.query(DB).filter(DB.id == int(db_id)).update({
                DB.db_code:
                db_code,
                DB.db_host:
                db_host,
                DB.db_port:
                db_port,
                DB.db_user:
                db_user,
                DB.db_pwd:
                db_pwd,
                DB.db_env:
                db_env,
                DB.proxy_host:
                proxy_host,
                DB.db_type:
                db_type,
                DB.db_version:
                db_version,
                DB.db_mark:
                db_mark,
                DB.db_detail:
                db_detail
            })

        return self.write(dict(code=0, msg='编辑成功'))
예제 #5
0
    def post(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        nickname = self.get_current_nickname()
        db_code = data.get('db_code', None)
        db_host = data.get('db_host', None)
        db_port = data.get('db_port', 3306)
        db_user = data.get('db_user', None)
        db_pwd = data.get('db_pwd', None)
        db_env = data.get('db_env', None)
        proxy_host = data.get('proxy_host', None)
        db_type = data.get('db_type', 'mysql')
        db_version = data.get('db_version', None)
        db_mark = data.get('db_mark', '写')
        tag_list = data.get('tag_list', [])
        db_detail = data.get('db_detail', None)
        db_instance = data.get('db_instance', '')
        if not db_code or not db_host or not db_port or not db_user:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        with DBContext('r') as session:
            exist_id = session.query(DB.id).filter(DB.db_code == db_code, DB.db_host == db_host, DB.db_port == db_port,
                                                   DB.db_user == db_user, DB.db_env == db_env,
                                                   DB.proxy_host == proxy_host, DB.db_type == db_type,
                                                   db_version == db_version,
                                                   DB.db_mark == db_mark).first()
        if exist_id:
            return self.write(dict(code=-2, msg='不要重复记录'))

        # 加密密码
        db_pwd = encrypt(db_pwd)

        with DBContext('w', None, True) as session:
            new_db = DB(db_code=db_code, db_host=db_host, db_port=db_port, db_user=db_user, db_pwd=db_pwd,
                        db_env=db_env, proxy_host=proxy_host, db_type=db_type, db_version=db_version, db_mark=db_mark,
                        db_detail=db_detail, db_instance=db_instance)
            session.add(new_db)

            all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all()
            if all_tags:
                for tag_id in all_tags:
                    session.add(DBTag(db_id=new_db.id, tag_id=tag_id[0]))
        # 记录,记录错误也不要影响用户正常添加
        try:
            with DBContext('w', None, True) as session:

                new_record = AssetOperationalAudit(username=nickname, request_object='数据库', request_host=db_host,
                                                   request_method='新增', modify_data=data)
                session.add(new_record)
        except Exception as err:
            ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err))

        return self.write(dict(code=0, msg='添加成功'))
예제 #6
0
    def post(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        db_code = data.get('db_code', None)
        db_host = data.get('db_host', None)
        db_port = data.get('db_port', 3306)
        db_user = data.get('db_user', None)
        db_pwd = data.get('db_pwd', None)
        db_env = data.get('db_env', None)
        proxy_host = data.get('proxy_host', None)
        db_type = data.get('db_type', 'mysql')
        db_version = data.get('db_version', None)
        db_mark = data.get('db_mark', '写')
        tag_list = data.get('tag_list', [])
        db_detail = data.get('db_detail', None)
        if not db_code or not db_host or not db_port or not db_user or not db_pwd or not db_env or not db_detail:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        with DBContext('r') as session:
            exist_id = session.query(
                DB.id).filter(DB.db_code == db_code, DB.db_host == db_host,
                              DB.db_port == db_port, DB.db_user == db_user,
                              DB.db_env == db_env, DB.proxy_host == proxy_host,
                              DB.db_type == db_type, db_version == db_version,
                              DB.db_mark == db_mark).first()
        if exist_id:
            return self.write(dict(code=-2, msg='不要重复记录'))

        with DBContext('w', None, True) as session:
            new_db = DB(db_code=db_code,
                        db_host=db_host,
                        db_port=db_port,
                        db_user=db_user,
                        db_pwd=db_pwd,
                        db_env=db_env,
                        proxy_host=proxy_host,
                        db_type=db_type,
                        db_version=db_version,
                        db_mark=db_mark,
                        db_detail=db_detail)
            session.add(new_db)

            all_tags = session.query(Tag.id).filter(
                Tag.tag_name.in_(tag_list)).order_by(Tag.id).all()
            if all_tags:
                for tag_id in all_tags:
                    session.add(DBTag(db_id=new_db.id, tag_id=tag_id[0]))

        return self.write(dict(code=0, msg='添加成功'))