oc_clusterrole.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class OCClusterRole(OpenShiftCLI):
  5. ''' Class to manage clusterrole objects'''
  6. kind = 'clusterrole'
  7. def __init__(self,
  8. name,
  9. rules=None,
  10. kubeconfig=None,
  11. verbose=False):
  12. ''' Constructor for OCClusterRole '''
  13. super(OCClusterRole, self).__init__(None, kubeconfig=kubeconfig, verbose=verbose)
  14. self.verbose = verbose
  15. self.name = name
  16. self._clusterrole = None
  17. self._inc_clusterrole = ClusterRole.builder(name, rules)
  18. @property
  19. def clusterrole(self):
  20. ''' property for clusterrole'''
  21. if self._clusterrole is None:
  22. self.get()
  23. return self._clusterrole
  24. @clusterrole.setter
  25. def clusterrole(self, data):
  26. ''' setter function for clusterrole property'''
  27. self._clusterrole = data
  28. @property
  29. def inc_clusterrole(self):
  30. ''' property for inc_clusterrole'''
  31. return self._inc_clusterrole
  32. @inc_clusterrole.setter
  33. def inc_clusterrole(self, data):
  34. ''' setter function for inc_clusterrole property'''
  35. self._inc_clusterrole = data
  36. def exists(self):
  37. ''' return whether a clusterrole exists '''
  38. if self.clusterrole:
  39. return True
  40. return False
  41. def get(self):
  42. '''return a clusterrole '''
  43. result = self._get(self.kind, self.name)
  44. if result['returncode'] == 0:
  45. self.clusterrole = ClusterRole(content=result['results'][0])
  46. result['results'] = self.clusterrole.yaml_dict
  47. elif 'clusterrole "{}" not found'.format(self.name) in result['stderr']:
  48. result['returncode'] = 0
  49. self.clusterrole = None
  50. return result
  51. def delete(self):
  52. '''delete the object'''
  53. return self._delete(self.kind, self.name)
  54. def create(self):
  55. '''create a clusterrole from the proposed incoming clusterrole'''
  56. return self._create_from_content(self.name, self.inc_clusterrole.yaml_dict)
  57. def update(self):
  58. '''update a project'''
  59. return self._replace_content(self.kind, self.name, self.inc_clusterrole.yaml_dict)
  60. def needs_update(self):
  61. ''' verify an update is needed'''
  62. return not self.clusterrole.compare(self.inc_clusterrole, self.verbose)
  63. # pylint: disable=too-many-return-statements,too-many-branches
  64. @staticmethod
  65. def run_ansible(params, check_mode):
  66. '''run the idempotent ansible code'''
  67. oc_clusterrole = OCClusterRole(params['name'],
  68. params['rules'],
  69. params['kubeconfig'],
  70. params['debug'])
  71. state = params['state']
  72. api_rval = oc_clusterrole.get()
  73. #####
  74. # Get
  75. #####
  76. if state == 'list':
  77. return {'changed': False, 'results': api_rval, 'state': state}
  78. ########
  79. # Delete
  80. ########
  81. if state == 'absent':
  82. if oc_clusterrole.exists():
  83. if check_mode:
  84. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete.'}
  85. api_rval = oc_clusterrole.delete()
  86. if api_rval['returncode'] != 0:
  87. return {'failed': True, 'msg': api_rval}
  88. return {'changed': True, 'results': api_rval, 'state': state}
  89. return {'changed': False, 'state': state}
  90. if state == 'present':
  91. ########
  92. # Create
  93. ########
  94. if not oc_clusterrole.exists():
  95. if check_mode:
  96. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'}
  97. # Create it here
  98. api_rval = oc_clusterrole.create()
  99. if api_rval['returncode'] != 0:
  100. return {'failed': True, 'msg': api_rval}
  101. # return the created object
  102. api_rval = oc_clusterrole.get()
  103. if api_rval['returncode'] != 0:
  104. return {'failed': True, 'msg': api_rval}
  105. return {'changed': True, 'results': api_rval, 'state': state}
  106. ########
  107. # Update
  108. ########
  109. if oc_clusterrole.needs_update():
  110. if check_mode:
  111. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'}
  112. api_rval = oc_clusterrole.update()
  113. if api_rval['returncode'] != 0:
  114. return {'failed': True, 'msg': api_rval}
  115. # return the created object
  116. api_rval = oc_clusterrole.get()
  117. if api_rval['returncode'] != 0:
  118. return {'failed': True, 'msg': api_rval}
  119. return {'changed': True, 'results': api_rval, 'state': state}
  120. return {'changed': False, 'results': api_rval, 'state': state}
  121. return {'failed': True,
  122. 'changed': False,
  123. 'msg': 'Unknown state passed. [%s]' % state}