secret.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class SecretConfig(object):
  5. ''' Handle secret options '''
  6. # pylint: disable=too-many-arguments
  7. def __init__(self,
  8. sname,
  9. namespace,
  10. kubeconfig,
  11. secrets=None,
  12. stype=None,
  13. annotations=None):
  14. ''' constructor for handling secret options '''
  15. self.kubeconfig = kubeconfig
  16. self.name = sname
  17. self.type = stype
  18. self.namespace = namespace
  19. self.secrets = secrets
  20. self.annotations = annotations
  21. self.data = {}
  22. self.create_dict()
  23. def create_dict(self):
  24. ''' assign the correct properties for a secret dict '''
  25. self.data['apiVersion'] = 'v1'
  26. self.data['kind'] = 'Secret'
  27. self.data['type'] = self.type
  28. self.data['metadata'] = {}
  29. self.data['metadata']['name'] = self.name
  30. self.data['metadata']['namespace'] = self.namespace
  31. self.data['data'] = {}
  32. if self.secrets:
  33. for key, value in self.secrets.items():
  34. self.data['data'][key] = value
  35. if self.annotations:
  36. self.data['metadata']['annotations'] = self.annotations
  37. # pylint: disable=too-many-instance-attributes
  38. class Secret(Yedit):
  39. ''' Class to wrap the oc command line tools '''
  40. secret_path = "data"
  41. kind = 'secret'
  42. def __init__(self, content):
  43. '''secret constructor'''
  44. super(Secret, self).__init__(content=content)
  45. self._secrets = None
  46. @property
  47. def secrets(self):
  48. '''secret property getter'''
  49. if self._secrets is None:
  50. self._secrets = self.get_secrets()
  51. return self._secrets
  52. @secrets.setter
  53. def secrets(self):
  54. '''secret property setter'''
  55. if self._secrets is None:
  56. self._secrets = self.get_secrets()
  57. return self._secrets
  58. def get_secrets(self):
  59. ''' returns all of the defined secrets '''
  60. return self.get(Secret.secret_path) or {}
  61. def add_secret(self, key, value):
  62. ''' add a secret '''
  63. if self.secrets:
  64. self.secrets[key] = value
  65. else:
  66. self.put(Secret.secret_path, {key: value})
  67. return True
  68. def delete_secret(self, key):
  69. ''' delete secret'''
  70. try:
  71. del self.secrets[key]
  72. except KeyError as _:
  73. return False
  74. return True
  75. def find_secret(self, key):
  76. ''' find secret'''
  77. rval = None
  78. try:
  79. rval = self.secrets[key]
  80. except KeyError as _:
  81. return None
  82. return {'key': key, 'value': rval}
  83. def update_secret(self, key, value):
  84. ''' update a secret'''
  85. if key in self.secrets:
  86. self.secrets[key] = value
  87. else:
  88. self.add_secret(key, value)
  89. return True