grp_tpl.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 .bean import Bean
  17. from .template import Template
  18. from .host_group import HostGroup
  19. class GrpTpl(Bean):
  20. _tbl = 'grp_tpl'
  21. _cols = 'grp_id, tpl_id, bind_user'
  22. _id = ''
  23. def __init__(self, grp_id, tpl_id, bind_user):
  24. self.grp_id = grp_id
  25. self.tpl_id = tpl_id
  26. self.bind_user = bind_user
  27. @classmethod
  28. def tpl_list(cls, grp_id=None):
  29. if not grp_id:
  30. return []
  31. tpl_ids = cls.column('tpl_id', where='grp_id=%s', params=[grp_id])
  32. if not tpl_ids:
  33. return []
  34. tpl_ids = ['%s' % i for i in tpl_ids]
  35. ids = ','.join(tpl_ids)
  36. return Template.select_vs(where='id in (%s)' % ids)
  37. @classmethod
  38. def tpl_set(cls, group_ids=None):
  39. if group_ids is None:
  40. group_ids = []
  41. if not group_ids:
  42. return []
  43. grp_ids = ['%s' % i for i in group_ids]
  44. tpl_ids = cls.column('tpl_id', where='grp_id in (%s)' % ', '.join(grp_ids))
  45. if not tpl_ids:
  46. return []
  47. tpl_ids = ['%s' % i for i in tpl_ids]
  48. ids = ','.join(tpl_ids)
  49. return Template.select_vs(where='id in (%s)' % ids)
  50. @classmethod
  51. def grp_list(cls, tpl_id=None):
  52. if not tpl_id:
  53. return []
  54. grp_ids = cls.column('grp_id', where='tpl_id=%s', params=[tpl_id])
  55. if not grp_ids:
  56. return []
  57. grp_ids = ['%s' % i for i in grp_ids]
  58. ids = ','.join(grp_ids)
  59. return HostGroup.select_vs(where='id in (%s)' % ids)
  60. @classmethod
  61. def unbind(cls, grp_id, tpl_id):
  62. return cls.delete('grp_id = %s and tpl_id = %s', [grp_id, tpl_id])
  63. @classmethod
  64. def bind(cls, grp_id, tpl_id, login_user):
  65. if cls.exists('grp_id=%s and tpl_id=%s', [grp_id, tpl_id]):
  66. return
  67. cls.insert({
  68. 'grp_id': grp_id,
  69. 'tpl_id': tpl_id,
  70. 'bind_user': login_user,
  71. })
  72. @classmethod
  73. def unbind_tpl(cls, tpl_id):
  74. return cls.delete('tpl_id=%s', [tpl_id])
  75. @classmethod
  76. def unbind_group(cls, grp_id):
  77. return cls.delete('grp_id=%s', [grp_id])