Coverage for ocp_resources/data_source.py: 0%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1from warnings import warn 

2from kubernetes.dynamic.exceptions import ResourceNotFoundError 

3from ocp_resources.persistent_volume_claim import PersistentVolumeClaim 

4from ocp_resources.resource import MissingRequiredArgumentError, NamespacedResource 

5from ocp_resources.volume_snapshot import VolumeSnapshot 

6 

7 

8class DataSource(NamespacedResource): 

9 """ 

10 DataSource object. 

11 

12 https://kubevirt.io/cdi-api-reference/main/definitions.html#_v1beta1_datasource 

13 """ 

14 

15 api_group = NamespacedResource.ApiGroup.CDI_KUBEVIRT_IO 

16 

17 def __init__(self, source=None, **kwargs): 

18 """ 

19 Args: 

20 source (dict): The source of the data. 

21 """ 

22 super().__init__(**kwargs) 

23 self._source = source 

24 

25 def to_dict(self) -> None: 

26 super().to_dict() 

27 if not self.kind_dict and not self.yaml_file: 

28 if not self._source: 

29 raise MissingRequiredArgumentError(argument="source") 

30 

31 self.res.update({"spec": {"source": self._source}}) 

32 

33 @property 

34 def pvc(self): 

35 warn("pvc will be deprecated in v4.16, Use source instead", DeprecationWarning, stacklevel=2) 

36 data_source_pvc = self.instance.spec.source.pvc 

37 pvc_name = data_source_pvc.name 

38 pvc_namespace = data_source_pvc.namespace 

39 try: 

40 return PersistentVolumeClaim( 

41 client=self.client, 

42 name=pvc_name, 

43 namespace=pvc_namespace, 

44 ) 

45 except ResourceNotFoundError: 

46 self.logger.warning( 

47 f"dataSource {self.name} is pointing to a non-existing PVC, name:" 

48 f" {pvc_name}, namespace: {pvc_namespace}" 

49 ) 

50 

51 @property 

52 def source(self): 

53 _instance_source = self.instance.spec.source 

54 _source = [*_instance_source][0][0] 

55 _source_mapping = {"pvc": PersistentVolumeClaim, "snapshot": VolumeSnapshot} 

56 

57 return _source_mapping[_source]( 

58 client=self.client, 

59 name=_instance_source[_source].name, 

60 namespace=_instance_source[_source].namespace, 

61 )