<commit_before>"""
Ansible action plugin to generate pv and pvc dictionaries lists
"""

from ansible.plugins.action import ActionBase
from ansible import errors


class ActionModule(ActionBase):
    """Action plugin to execute health checks."""

    def get_templated(self, var_to_template):
        """Return a properly templated ansible variable"""
        return self._templar.template(self.task_vars.get(var_to_template))

    def build_common(self, varname=None):
        """Retrieve common variables for each pv and pvc type"""
        volume = self.get_templated(str(varname) + '_volume_name')
        size = self.get_templated(str(varname) + '_volume_size')
        labels = self.task_vars.get(str(varname) + '_labels')
        annotations = self.task_vars.get(str(varname) + '_annotations')
        if labels:
            labels = self._templar.template(labels)
        else:
            labels = dict()
        if annotations:
            annotations = self._templar.template(annotations)
        else:
            annotations = list()
        access_modes = self.get_templated(str(varname) + '_access_modes')
        return (volume, size, labels, annotations, access_modes)

    def build_pv_nfs(self, varname=None):
        """Build pv dictionary for nfs storage type"""
        host = self.task_vars.get(str(varname) + '_host')
        if host:
            self._templar.template(host)
        elif host is None:
            groups = self.task_vars.get('groups')
            default_group_name = self.get_templated('openshift_persistent_volumes_default_nfs_group')
            if groups and default_group_name and default_group_name in groups and len(groups[default_group_name]) > 0:
                host = groups['oo_nfs_to_config'][0]
            else:
                raise errors.AnsibleModuleError("|failed no storage host detected")
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        directory = self.get_templated(str(varname) + '_nfs_directory')
        path = directory + '/' + volume
        result = dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                nfs=dict(
                    server=host,
                    path=path)))
        # Add claimref for NFS as default storageclass can be different
        create_pvc = self.task_vars.get(str(varname) + '_create_pvc')
        if create_pvc and self._templar.template(create_pvc):
            result['storage']['claimName'] = "{0}-claim".format(volume)
        return result

    def build_pv_openstack(self, varname=None):
        """Build pv dictionary for openstack storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        filesystem = self.get_templated(str(varname) + '_openstack_filesystem')
        volume_name = self.get_templated(str(varname) + '_volume_name')
        volume_id = self.get_templated(str(varname) + '_openstack_volumeID')
        if volume_name and not volume_id:
            volume_id = _try_cinder_volume_id_from_name(volume_name)
        return dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                cinder=dict(
                    fsType=filesystem,
                    volumeID=volume_id)))

    def build_pv_glusterfs(self, varname=None):
        """Build pv dictionary for glusterfs storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        endpoints = self.get_templated(str(varname) + '_glusterfs_endpoints')
        path = self.get_templated(str(varname) + '_glusterfs_path')
        read_only = self.get_templated(str(varname) + '_glusterfs_readOnly')
        return dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                glusterfs=dict(
                    endpoints=endpoints,
                    path=path,
                    readOnly=read_only)))

    def build_pv_hostpath(self, varname=None):
        """Build pv dictionary for hostpath storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        # hostpath only supports ReadWriteOnce
        if access_modes[0] != 'ReadWriteOnce':
            msg = "Hostpath storage only supports 'ReadWriteOnce' Was given {}."
            raise errors.AnsibleModuleError(msg.format(access_modes.join(', ')))
        path = self.get_templated(str(varname) + '_hostpath_path')
        return dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                hostPath=dict(
                    path=path
                )
            )
        )

    def build_pv_dict(self, varname=None):
        """Check for the existence of PV variables"""
        kind = self.task_vars.get(str(varname) + '_kind')<commit_msg>glusterfs: bind created volume to the claim<commit_after>"""
Ansible action plugin to generate pv and pvc dictionaries lists
"""

from ansible.plugins.action import ActionBase
from ansible import errors


class ActionModule(ActionBase):
    """Action plugin to execute health checks."""

    def get_templated(self, var_to_template):
        """Return a properly templated ansible variable"""
        return self._templar.template(self.task_vars.get(var_to_template))

    def build_common(self, varname=None):
        """Retrieve common variables for each pv and pvc type"""
        volume = self.get_templated(str(varname) + '_volume_name')
        size = self.get_templated(str(varname) + '_volume_size')
        labels = self.task_vars.get(str(varname) + '_labels')
        annotations = self.task_vars.get(str(varname) + '_annotations')
        if labels:
            labels = self._templar.template(labels)
        else:
            labels = dict()
        if annotations:
            annotations = self._templar.template(annotations)
        else:
            annotations = list()
        access_modes = self.get_templated(str(varname) + '_access_modes')
        return (volume, size, labels, annotations, access_modes)

    def build_pv_nfs(self, varname=None):
        """Build pv dictionary for nfs storage type"""
        host = self.task_vars.get(str(varname) + '_host')
        if host:
            self._templar.template(host)
        elif host is None:
            groups = self.task_vars.get('groups')
            default_group_name = self.get_templated('openshift_persistent_volumes_default_nfs_group')
            if groups and default_group_name and default_group_name in groups and len(groups[default_group_name]) > 0:
                host = groups['oo_nfs_to_config'][0]
            else:
                raise errors.AnsibleModuleError("|failed no storage host detected")
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        directory = self.get_templated(str(varname) + '_nfs_directory')
        path = directory + '/' + volume
        result = dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                nfs=dict(
                    server=host,
                    path=path)))
        # Add claimref for NFS as default storageclass can be different
        create_pvc = self.task_vars.get(str(varname) + '_create_pvc')
        if create_pvc and self._templar.template(create_pvc):
            result['storage']['claimName'] = "{0}-claim".format(volume)
        return result

    def build_pv_openstack(self, varname=None):
        """Build pv dictionary for openstack storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        filesystem = self.get_templated(str(varname) + '_openstack_filesystem')
        volume_name = self.get_templated(str(varname) + '_volume_name')
        volume_id = self.get_templated(str(varname) + '_openstack_volumeID')
        if volume_name and not volume_id:
            volume_id = _try_cinder_volume_id_from_name(volume_name)
        return dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                cinder=dict(
                    fsType=filesystem,
                    volumeID=volume_id)))

    def build_pv_glusterfs(self, varname=None):
        """Build pv dictionary for glusterfs storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        endpoints = self.get_templated(str(varname) + '_glusterfs_endpoints')
        path = self.get_templated(str(varname) + '_glusterfs_path')
        read_only = self.get_templated(str(varname) + '_glusterfs_readOnly')
        result = dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                glusterfs=dict(
                    endpoints=endpoints,
                    path=path,
                    readOnly=read_only)))
        # Add claimref for glusterfs as default storageclass can be different
        create_pvc = self.task_vars.get(str(varname) + '_create_pvc')
        if create_pvc and self._templar.template(create_pvc):
            result['storage']['claimName'] = "{0}-claim".format(volume)
        return result

    def build_pv_hostpath(self, varname=None):
        """Build pv dictionary for hostpath storage type"""
        volume, size, labels, _, access_modes = self.build_common(varname=varname)
        # hostpath only supports ReadWriteOnce
        if access_modes[0] != 'ReadWriteOnce':
            msg = "Hostpath storage only supports 'ReadWriteOnce' Was given {}."
            raise errors.AnsibleModuleError(msg.format(access_modes.join(', ')))
        path = self.get_templated(str(varname) + '_hostpath_path')
        return dict(
            name="{0}-volume".format(volume),
            capacity=size,
            labels=labels,
            access_modes=access_modes,
            storage=dict(
                hostPath=dict(
                    path=path
                )
            )
        )

    def build_pv_dict(self, varname=None):
        """Check for the existence of PV variables"""
        kind = self.task_vars.get(str(varname) + '_kind')
{'fraction_non_alphanumeric': 0.10159176029962547, 'fraction_numerical': 0.001591760299625468, 'mean_word_length': 2.4091924672837535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '673623', 'n_tokens_mistral': 3105, 'n_tokens_neox': 2996, 'n_words': 634}