project.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, rname, kubeconfig, project_options)
  8. class Project(Yedit):
  9. ''' Class to wrap the oc command line tools '''
  10. annotations_path = "metadata.annotations"
  11. kind = 'Service'
  12. annotation_prefix = 'openshift.io/'
  13. def __init__(self, content):
  14. '''Service constructor'''
  15. super(Project, self).__init__(content=content)
  16. def get_annotations(self):
  17. ''' get a list of ports '''
  18. return self.get(Project.annotations_path) or {}
  19. def add_annotations(self, inc_annos):
  20. ''' add a port object to the ports list '''
  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 a specific port '''
  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 from 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