oc_storageclass.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class OCStorageClass(OpenShiftCLI):
  5. ''' Class to wrap the oc command line tools '''
  6. kind = 'storageclass'
  7. # pylint allows 5
  8. # pylint: disable=too-many-arguments
  9. def __init__(self,
  10. config,
  11. verbose=False):
  12. ''' Constructor for OCStorageClass '''
  13. super(OCStorageClass, self).__init__(None, kubeconfig=config.kubeconfig, verbose=verbose)
  14. self.config = config
  15. self.storage_class = None
  16. def exists(self):
  17. ''' return whether a storageclass exists'''
  18. if self.storage_class:
  19. return True
  20. return False
  21. def get(self):
  22. '''return storageclass '''
  23. result = self._get(self.kind, self.config.name)
  24. if result['returncode'] == 0:
  25. self.storage_class = StorageClass(content=result['results'][0])
  26. elif '\"%s\" not found' % self.config.name in result['stderr']:
  27. result['returncode'] = 0
  28. result['results'] = [{}]
  29. return result
  30. def delete(self):
  31. '''delete the object'''
  32. return self._delete(self.kind, self.config.name)
  33. def create(self):
  34. '''create the object'''
  35. return self._create_from_content(self.config.name, self.config.data)
  36. def update(self):
  37. '''update the object'''
  38. # parameters are currently unable to be updated. need to delete and recreate
  39. self.delete()
  40. # pause here and attempt to wait for delete.
  41. # Better option would be to poll
  42. time.sleep(5)
  43. return self.create()
  44. def needs_update(self):
  45. ''' verify an update is needed '''
  46. # check if params have updated
  47. if self.storage_class.get_parameters() != self.config.parameters:
  48. return True
  49. for anno_key, anno_value in self.storage_class.get_annotations().items():
  50. if 'is-default-class' in anno_key and anno_value != self.config.default_storage_class:
  51. return True
  52. return False
  53. @staticmethod
  54. # pylint: disable=too-many-return-statements,too-many-branches
  55. # TODO: This function should be refactored into its individual parts.
  56. def run_ansible(params, check_mode):
  57. '''run the ansible idempotent code'''
  58. rconfig = StorageClassConfig(params['name'],
  59. provisioner="kubernetes.io/{}".format(params['provisioner']),
  60. parameters=params['parameters'],
  61. annotations=params['annotations'],
  62. api_version="storage.k8s.io/{}".format(params['api_version']),
  63. default_storage_class=params.get('default_storage_class', 'false'),
  64. kubeconfig=params['kubeconfig'],
  65. )
  66. oc_sc = OCStorageClass(rconfig, verbose=params['debug'])
  67. state = params['state']
  68. api_rval = oc_sc.get()
  69. #####
  70. # Get
  71. #####
  72. if state == 'list':
  73. return {'changed': False, 'results': api_rval['results'], 'state': 'list'}
  74. ########
  75. # Delete
  76. ########
  77. if state == 'absent':
  78. if oc_sc.exists():
  79. if check_mode:
  80. return {'changed': True, 'msg': 'Would have performed a delete.'}
  81. api_rval = oc_sc.delete()
  82. return {'changed': True, 'results': api_rval, 'state': 'absent'}
  83. return {'changed': False, 'state': 'absent'}
  84. if state == 'present':
  85. ########
  86. # Create
  87. ########
  88. if not oc_sc.exists():
  89. if check_mode:
  90. return {'changed': True, 'msg': 'Would have performed a create.'}
  91. # Create it here
  92. api_rval = oc_sc.create()
  93. if api_rval['returncode'] != 0:
  94. return {'failed': True, 'msg': api_rval}
  95. # return the created object
  96. api_rval = oc_sc.get()
  97. if api_rval['returncode'] != 0:
  98. return {'failed': True, 'msg': api_rval}
  99. return {'changed': True, 'results': api_rval, 'state': 'present'}
  100. ########
  101. # Update
  102. ########
  103. if oc_sc.needs_update():
  104. api_rval = oc_sc.update()
  105. if api_rval['returncode'] != 0:
  106. return {'failed': True, 'msg': api_rval}
  107. # return the created object
  108. api_rval = oc_sc.get()
  109. if api_rval['returncode'] != 0:
  110. return {'failed': True, 'msg': api_rval}
  111. return {'changed': True, 'results': api_rval, 'state': 'present'}
  112. return {'changed': False, 'results': api_rval, 'state': 'present'}
  113. return {'failed': True,
  114. 'changed': False,
  115. 'msg': 'Unknown state passed. %s' % state,
  116. 'state': 'unknown'}