1# NVMe initiator related facts collection for Ansible.
2#
3# This file is part of Ansible
4#
5# Ansible is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Ansible is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
18from __future__ import (absolute_import, division, print_function)
19__metaclass__ = type
20
21import sys
22import subprocess
23
24from ansible.module_utils.facts.utils import get_file_content
25from ansible.module_utils.facts.network.base import NetworkCollector
26
27
28class NvmeInitiatorNetworkCollector(NetworkCollector):
29    name = 'nvme'
30    _fact_ids = set()
31
32    def collect(self, module=None, collected_facts=None):
33        """
34        Currently NVMe is only supported in some Linux distributions.
35        If NVMe is configured on the host then a file will have been created
36        during the NVMe driver installation. This file holds the unique NQN
37        of the host.
38
39        Example of contents of /etc/nvme/hostnqn:
40
41        # cat /etc/nvme/hostnqn
42        nqn.2014-08.org.nvmexpress:fc_lif:uuid:2cd61a74-17f9-4c22-b350-3020020c458d
43
44        """
45
46        nvme_facts = {}
47        nvme_facts['hostnqn'] = ""
48        if sys.platform.startswith('linux'):
49            for line in get_file_content('/etc/nvme/hostnqn', '').splitlines():
50                if line.startswith('#') or line.startswith(';') or line.strip() == '':
51                    continue
52                if line.startswith('nqn.'):
53                    nvme_facts['hostnqn'] = line
54                    break
55        return nvme_facts
56