oc_route.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class OCRoute(OpenShiftCLI):
  5. ''' Class to wrap the oc command line tools '''
  6. kind = 'route'
  7. def __init__(self,
  8. config,
  9. verbose=False):
  10. ''' Constructor for OCVolume '''
  11. super(OCRoute, self).__init__(config.namespace, config.kubeconfig)
  12. self.config = config
  13. self.namespace = config.namespace
  14. self._route = None
  15. @property
  16. def route(self):
  17. ''' property function for route'''
  18. if not self._route:
  19. self.get()
  20. return self._route
  21. @route.setter
  22. def route(self, data):
  23. ''' setter function for route '''
  24. self._route = data
  25. def exists(self):
  26. ''' return whether a route exists '''
  27. if self.route:
  28. return True
  29. return False
  30. def get(self):
  31. '''return route information '''
  32. result = self._get(self.kind, self.config.name)
  33. if result['returncode'] == 0:
  34. self.route = Route(content=result['results'][0])
  35. elif 'routes \"%s\" not found' % self.config.name in result['stderr']:
  36. result['returncode'] = 0
  37. result['results'] = [{}]
  38. return result
  39. def delete(self):
  40. '''delete the object'''
  41. return self._delete(self.kind, self.config.name)
  42. def create(self):
  43. '''create the object'''
  44. return self._create_from_content(self.config.name, self.config.data)
  45. def update(self):
  46. '''update the object'''
  47. # need to update the tls information and the service name
  48. return self._replace_content(self.kind, self.config.name, self.config.data)
  49. def needs_update(self):
  50. ''' verify an update is needed '''
  51. skip = []
  52. return not Utils.check_def_equal(self.config.data, self.route.yaml_dict, skip_keys=skip, debug=True)
  53. # pylint: disable=too-many-return-statements,too-many-branches
  54. @staticmethod
  55. def run_ansible(params, files, check_mode=False):
  56. ''' run the idempotent asnible code
  57. params comes from the ansible portion for this module
  58. files: a dictionary for the certificates
  59. {'cert': {'path': '',
  60. 'content': '',
  61. 'value': ''
  62. }
  63. }
  64. check_mode: does the module support check mode. (module.check_mode)
  65. '''
  66. rconfig = RouteConfig(params['name'],
  67. params['namespace'],
  68. params['kubeconfig'],
  69. files['destcacert']['value'],
  70. files['cacert']['value'],
  71. files['cert']['value'],
  72. files['key']['value'],
  73. params['host'],
  74. params['tls_termination'],
  75. params['service_name'],
  76. params['wildcard_policy'],
  77. params['weight'])
  78. oc_route = OCRoute(rconfig, verbose=params['debug'])
  79. state = params['state']
  80. api_rval = oc_route.get()
  81. #####
  82. # Get
  83. #####
  84. if state == 'list':
  85. return {'changed': False,
  86. 'results': api_rval['results'],
  87. 'state': 'list'}
  88. ########
  89. # Delete
  90. ########
  91. if state == 'absent':
  92. if oc_route.exists():
  93. if check_mode:
  94. return {'changed': False, 'msg': 'CHECK_MODE: Would have performed a delete.'} # noqa: E501
  95. api_rval = oc_route.delete()
  96. return {'changed': True, 'results': api_rval, 'state': "absent"} # noqa: E501
  97. return {'changed': False, 'state': 'absent'}
  98. if state == 'present':
  99. ########
  100. # Create
  101. ########
  102. if not oc_route.exists():
  103. if check_mode:
  104. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'} # noqa: E501
  105. # Create it here
  106. api_rval = oc_route.create()
  107. if api_rval['returncode'] != 0:
  108. return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
  109. # return the created object
  110. api_rval = oc_route.get()
  111. if api_rval['returncode'] != 0:
  112. return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
  113. return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501
  114. ########
  115. # Update
  116. ########
  117. if oc_route.needs_update():
  118. if check_mode:
  119. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'} # noqa: E501
  120. api_rval = oc_route.update()
  121. if api_rval['returncode'] != 0:
  122. return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
  123. # return the created object
  124. api_rval = oc_route.get()
  125. if api_rval['returncode'] != 0:
  126. return {'failed': True, 'msg': api_rval, 'state': "present"} # noqa: E501
  127. return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501
  128. return {'changed': False, 'results': api_rval, 'state': "present"}
  129. # catch all
  130. return {'failed': True, 'msg': "Unknown State passed"}