|
| 1 | +import logging |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from ocs_ci.ocs import constants, ocp |
| 5 | +from ocs_ci.framework import config |
| 6 | +from ocs_ci.utility import templating |
| 7 | +from ocs_ci.utility.templating import dump_data_to_temp_yaml |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class ClientProfile: |
| 13 | + """ |
| 14 | + Base ClientProfile class |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, client_profile_name, consumer_context=None): |
| 18 | + |
| 19 | + self.consumer_context = consumer_context |
| 20 | + self.name = client_profile_name |
| 21 | + self.ocp = ocp.OCP( |
| 22 | + resource_name=self.name, |
| 23 | + kind=constants.CLIENT_PROFILE, |
| 24 | + namespace=config.cluster_ctx.ENV_DATA["cluster_namespace"], |
| 25 | + ) |
| 26 | + if self.consumer_context: |
| 27 | + self.provider_context = config.cluster_ctx.MULTICLUSTER[ |
| 28 | + "multicluster_index" |
| 29 | + ] |
| 30 | + else: |
| 31 | + self.provider_context = None |
| 32 | + |
| 33 | + def get_ceph_connection_reference(self): |
| 34 | + """ |
| 35 | + Get the CephConnectionReference name |
| 36 | + """ |
| 37 | + with config.RunWithConfigContext(self.consumer_context): |
| 38 | + return ( |
| 39 | + self.ocp.get(resource_name=self.name) |
| 40 | + .get("spec") |
| 41 | + .get("cephConnectionRef") |
| 42 | + .get("name") |
| 43 | + ) |
| 44 | + |
| 45 | + def get_ceph_fs_map(self): |
| 46 | + """ |
| 47 | + Get the CephFSMap from the client profile |
| 48 | +
|
| 49 | + SubVolumeGroup string `json:"subVolumeGroup,omitempty"` |
| 50 | + KernelMountOptions map[string] string `json:"kernelMountOptions,omitempty"` |
| 51 | + FuseMountOptions map[string] string `json:"fuseMountOptions,omitempty"` |
| 52 | +
|
| 53 | + Starting from ODF 4.19 (Converged) this CR has optional Spec field RadosNamespace. |
| 54 | + It is to ensure ceph fs has namespace for storing metadata (OMAP data) |
| 55 | + RadosNamespace *string `json:"radosNamespace,omitempty"` |
| 56 | + """ |
| 57 | + with config.RunWithConfigContext(self.consumer_context): |
| 58 | + return self.ocp.get(resource_name=self.name).get("spec").get("cephFs") |
| 59 | + |
| 60 | + def get_rbd_map(self): |
| 61 | + """ |
| 62 | + Get the RBDMap from the client profile |
| 63 | +
|
| 64 | + radosNamespace string `json:"radosNamespace,omitempty" |
| 65 | + """ |
| 66 | + with config.RunWithConfigContext(self.consumer_context): |
| 67 | + return self.ocp.get(resource_name=self.name).get("spec").get("rbd") |
| 68 | + |
| 69 | + def get_nfs_map(self): |
| 70 | + """ |
| 71 | + Get the NFSMap from the client profile |
| 72 | +
|
| 73 | + """ |
| 74 | + with config.RunWithConfigContext(self.consumer_context): |
| 75 | + return self.ocp.get(resource_name=self.name).get("spec").get("nfs") |
| 76 | + |
| 77 | + def create_client_profile( |
| 78 | + self, |
| 79 | + name, |
| 80 | + ceph_connection_reference, |
| 81 | + ceph_fs_map: dict, |
| 82 | + rbd_map: dict, |
| 83 | + nfs_map: dict, |
| 84 | + ): |
| 85 | + """ |
| 86 | + Create a client profile |
| 87 | + """ |
| 88 | + with config.RunWithConfigContext(self.consumer_context): |
| 89 | + client_profile_data = templating.load_yaml(constants.CLIENT_PROFILE_PATH) |
| 90 | + client_profile_data["metadata"]["name"] = name |
| 91 | + client_profile_data["spec"]["cephConnectionRef"][ |
| 92 | + "name" |
| 93 | + ] = ceph_connection_reference |
| 94 | + if ceph_fs_map: |
| 95 | + client_profile_data["spec"]["cephFs"] = ceph_fs_map |
| 96 | + if rbd_map: |
| 97 | + client_profile_data["spec"]["rbd"] = rbd_map |
| 98 | + if nfs_map: |
| 99 | + client_profile_data["spec"]["nfs"] = nfs_map |
| 100 | + |
| 101 | + client_profile_file = tempfile.NamedTemporaryFile( |
| 102 | + mode="w+", prefix="client_profile", delete=False |
| 103 | + ) |
| 104 | + dump_data_to_temp_yaml(client_profile_data, client_profile_file.name) |
| 105 | + |
| 106 | + return self.ocp.create(yaml_file=client_profile_file.name) |
0 commit comments