group_service.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from rrd.store import db
  17. from rrd.model.portal.host_group import HostGroup
  18. from rrd.utils.logger import logging
  19. log = logging.getLogger(__file__)
  20. def delete_group(group_id=None):
  21. try:
  22. cursor = db.execute('delete from grp where id = %s', [group_id])
  23. db.execute('delete from grp_host where grp_id = %s', [group_id], cursor=cursor)
  24. db.execute('delete from grp_tpl where grp_id = %s', [group_id], cursor=cursor)
  25. db.execute('delete from plugin_dir where grp_id = %s', [group_id], cursor=cursor)
  26. db.commit()
  27. return ''
  28. except Exception, e:
  29. log.error(e)
  30. db.rollback()
  31. return 'delete group %s fail' % group_id
  32. finally:
  33. cursor and cursor.close()
  34. def rename(old_str=None, new_str=None, login_user=None):
  35. print(old_str, new_str, login_user)
  36. gs = HostGroup.select_vs(where='create_user = %s and come_from = %s', params=[login_user, 1])
  37. for g in gs:
  38. HostGroup.update_dict({'grp_name': g.grp_name.replace(old_str, new_str)}, 'id=%s', [g.id])
  39. return ''