project.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class ProjectConfig(OpenShiftCLIConfig):
  5. ''' project config object '''
  6. def __init__(self, rname, namespace, kubeconfig, project_options):
  7. super(ProjectConfig, self).__init__(rname, None, kubeconfig, project_options)
  8. class Project(Yedit):
  9. ''' Class to wrap the oc command line tools '''
  10. annotations_path = "metadata.annotations"
  11. kind = 'Project'
  12. annotation_prefix = 'openshift.io/'
  13. def __init__(self, content):
  14. '''Project constructor'''
  15. super(Project, self).__init__(content=content)
  16. def get_annotations(self):
  17. ''' return the annotations'''
  18. return self.get(Project.annotations_path) or {}
  19. def add_annotations(self, inc_annos):
  20. ''' add an annotation to the other annotations'''
  21. if not isinstance(inc_annos, list):
  22. inc_annos = [inc_annos]
  23. annos = self.get_annotations()
  24. if not annos:
  25. self.put(Project.annotations_path, inc_annos)
  26. else:
  27. for anno in inc_annos:
  28. for key, value in anno.items():
  29. annos[key] = value
  30. return True
  31. def find_annotation(self, key):
  32. ''' find an annotation'''
  33. annotations = self.get_annotations()
  34. for anno in annotations:
  35. if Project.annotation_prefix + key == anno:
  36. return annotations[anno]
  37. return None
  38. def delete_annotation(self, inc_anno_keys):
  39. ''' remove an annotation from a project'''
  40. if not isinstance(inc_anno_keys, list):
  41. inc_anno_keys = [inc_anno_keys]
  42. annos = self.get(Project.annotations_path) or {}
  43. if not annos:
  44. return True
  45. removed = False
  46. for inc_anno in inc_anno_keys:
  47. anno = self.find_annotation(inc_anno)
  48. if anno:
  49. del annos[Project.annotation_prefix + anno]
  50. removed = True
  51. return removed
  52. def update_annotation(self, key, value):
  53. ''' remove an annotation for a project'''
  54. annos = self.get(Project.annotations_path) or {}
  55. if not annos:
  56. return True
  57. updated = False
  58. anno = self.find_annotation(key)
  59. if anno:
  60. annos[Project.annotation_prefix + key] = value
  61. updated = True
  62. else:
  63. self.add_annotations({Project.annotation_prefix + key: value})
  64. return updated