1
+ from warnings import warn
1
2
from kubernetes .dynamic .exceptions import ResourceNotFoundError
2
-
3
- from ocp_resources .constants import TIMEOUT_4MINUTES
4
3
from ocp_resources .persistent_volume_claim import PersistentVolumeClaim
5
4
from ocp_resources .resource import NamespacedResource
5
+ from ocp_resources .volume_snapshot import VolumeSnapshot
6
6
7
7
8
8
class DataSource (NamespacedResource ):
9
+ """
10
+ DataSource object.
11
+
12
+ https://kubevirt.io/cdi-api-reference/main/definitions.html#_v1beta1_datasource
13
+ """
14
+
9
15
api_group = NamespacedResource .ApiGroup .CDI_KUBEVIRT_IO
10
16
11
- def __init__ (
12
- self ,
13
- name = None ,
14
- namespace = None ,
15
- client = None ,
16
- source = None ,
17
- teardown = True ,
18
- yaml_file = None ,
19
- delete_timeout = TIMEOUT_4MINUTES ,
20
- ** kwargs ,
21
- ):
22
- super ().__init__ (
23
- name = name ,
24
- namespace = namespace ,
25
- client = client ,
26
- teardown = teardown ,
27
- yaml_file = yaml_file ,
28
- delete_timeout = delete_timeout ,
29
- ** kwargs ,
30
- )
31
- self .source = source
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
32
24
33
25
def to_dict (self ):
34
26
super ().to_dict ()
35
27
if not self .yaml_file :
36
- self .res .update ({
37
- "spec" : {
38
- "source" : self .source ,
39
- },
40
- })
28
+ if not self ._source :
29
+ raise ValueError ("Passing yaml_file or parameter 'source' is required" )
30
+
31
+ self .res ["spec" ]["source" ] = self ._source
41
32
42
33
@property
43
34
def pvc (self ):
35
+ warn ("pvc will be deprecated in v4.16, Use source instead" , DeprecationWarning , stacklevel = 2 )
44
36
data_source_pvc = self .instance .spec .source .pvc
45
37
pvc_name = data_source_pvc .name
46
38
pvc_namespace = data_source_pvc .namespace
@@ -55,3 +47,15 @@ def pvc(self):
55
47
f"dataSource { self .name } is pointing to a non-existing PVC, name:"
56
48
f" { pvc_name } , namespace: { pvc_namespace } "
57
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
+ )
0 commit comments