Skip to content

Commit 16fa5d4

Browse files
authored
Add Datasource bootsource property (RedHatQE#1601)
* Add snapshot property to Datasource class Signed-off-by: Harel Meir <[email protected]> * Add deprecation warning for DataSource pvc property Signed-off-by: Harel Meir <[email protected]> --------- Signed-off-by: Harel Meir <[email protected]>
1 parent 912b862 commit 16fa5d4

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fcn_exclude_functions =
4545
yaml,
4646
benedict,
4747
logger,
48+
warn,
4849
pytest,
4950
json,
5051

ocp_resources/data_source.py

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
1+
from warnings import warn
12
from kubernetes.dynamic.exceptions import ResourceNotFoundError
2-
3-
from ocp_resources.constants import TIMEOUT_4MINUTES
43
from ocp_resources.persistent_volume_claim import PersistentVolumeClaim
54
from ocp_resources.resource import NamespacedResource
5+
from ocp_resources.volume_snapshot import VolumeSnapshot
66

77

88
class DataSource(NamespacedResource):
9+
"""
10+
DataSource object.
11+
12+
https://kubevirt.io/cdi-api-reference/main/definitions.html#_v1beta1_datasource
13+
"""
14+
915
api_group = NamespacedResource.ApiGroup.CDI_KUBEVIRT_IO
1016

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
3224

3325
def to_dict(self):
3426
super().to_dict()
3527
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
4132

4233
@property
4334
def pvc(self):
35+
warn("pvc will be deprecated in v4.16, Use source instead", DeprecationWarning, stacklevel=2)
4436
data_source_pvc = self.instance.spec.source.pvc
4537
pvc_name = data_source_pvc.name
4638
pvc_namespace = data_source_pvc.namespace
@@ -55,3 +47,15 @@ def pvc(self):
5547
f"dataSource {self.name} is pointing to a non-existing PVC, name:"
5648
f" {pvc_name}, namespace: {pvc_namespace}"
5749
)
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

Comments
 (0)