Skip to content

Commit

Permalink
Add datasource nmcli_conn_show_uuid
Browse files Browse the repository at this point in the history
Signed-off-by: jiazhang <[email protected]>

rh-pre-commit.version: 2.3.1
rh-pre-commit.check-secrets: ENABLED
  • Loading branch information
wushiqinlou committed Nov 20, 2024
1 parent 1d8b213 commit d43de4d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
33 changes: 33 additions & 0 deletions insights/parsers/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,36 @@ def parse_content(self, content):
def disconnected_connection(self):
"""(list): It will return all the disconnected static route connections."""
return self._disconnected_connection


@parser(Specs.nmcli_conn_show_uuids)
class NmcliConnShowUuids(CommandParser, dict):
"""
This file will parse the output of nmcli connection detail information.
Sample configuration from an interface in file ``/usr/bin/nmcli conn show XXX``::
connection.id: System eth0
connection.uuid: 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: eth0
connection.autoconnect: yes
Examples:
>>> type(conn_show_uuids)
<class 'insights.parsers.nmcli.NmcliConnShowUuids'>
>>> conn_show_uuids["connection.uuid"]
'5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03'
Attributes:
data (dict): parameter-value format
"""
def parse_content(self, content):
if not content:
raise SkipComponent()

for line in get_active_lines(content):
key, val = line.split(": ")
self[key.strip()] = val.strip()
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ class Specs(SpecSet):
nginx_ssl_cert_enddate = RegistryPoint(multi_output=True)
nmap_ssh = RegistryPoint()
nmcli_conn_show = RegistryPoint(no_obfuscate=['hostname', 'ip'])
nmcli_conn_show_uuids = RegistryPoint(multi_output=True)
nmcli_dev_show = RegistryPoint()
nmcli_dev_show_sos = RegistryPoint(multi_output=True)
nova_api_log = RegistryPoint(filterable=True)
Expand Down
12 changes: 12 additions & 0 deletions insights/specs/datasources/nmcli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Custom datasources for ``nmcli`` command
"""
from insights.core.plugins import datasource
from insights.parsers.nmcli import NmcliConnShow


@datasource(NmcliConnShow)
def nmcli_conn_show_uuids(broker):
""" Return a list of connection uuids """
nmcli_conn_show = broker[NmcliConnShow]
return [item["UUID"] for item in nmcli_conn_show.data]
2 changes: 2 additions & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
ls,
lsattr,
luks_devices,
nmcli,
machine_ids,
md5chk,
mdadm,
Expand Down Expand Up @@ -650,6 +651,7 @@ class DefaultSpecs(Specs):
)
nmap_ssh = simple_command("/usr/bin/nmap --script ssh2-enum-algos -sV -p 22 127.0.0.1")
nmcli_conn_show = simple_command("/usr/bin/nmcli conn show")
nmcli_conn_show_uuids = foreach_execute(nmcli.nmcli_conn_show_uuids, "/usr/bin/nmcli conn show %s")
nmcli_dev_show = simple_command("/usr/bin/nmcli dev show")
nova_compute_log = first_file(
["/var/log/containers/nova/nova-compute.log", "/var/log/nova/nova-compute.log"]
Expand Down
19 changes: 19 additions & 0 deletions insights/tests/datasources/test_nmcli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from insights.parsers.nmcli import NmcliConnShow
from insights.specs.datasources.nmcli import nmcli_conn_show_uuids
from insights.tests import context_wrap


NMCLI_CONN_SHOW = '''
NAME UUID TYPE DEVICE
Wired connection 2 5fb06bd0-b09a-4573-b393-b54e832ddce9 ethernet enp0s20f0u5c2
lo 10f69a0f-0bb0-409f-831f-b5b729ba81af loopback lo
Wired connection 1 9dc5d4c5-71ae-44fb-804a-d6edd65f3e03 ethernet --
enp0s29f2 bb30e099-8220-3eba-45f1-47fb09a4ec80 ethernet --
'''.strip()


def test_nmcli_conn_show_uuids():
nmcli_conn_show = NmcliConnShow(context_wrap(NMCLI_CONN_SHOW))
broker = {NmcliConnShow: nmcli_conn_show}
result = nmcli_conn_show_uuids(broker)
assert result == ['5fb06bd0-b09a-4573-b393-b54e832ddce9', '10f69a0f-0bb0-409f-831f-b5b729ba81af', '9dc5d4c5-71ae-44fb-804a-d6edd65f3e03', 'bb30e099-8220-3eba-45f1-47fb09a4ec80']
32 changes: 31 additions & 1 deletion insights/tests/parsers/test_nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from insights.core.exceptions import SkipComponent
from insights.parsers import nmcli
from insights.parsers.nmcli import NmcliConnShow, NmcliDevShow, NmcliDevShowSos
from insights.parsers.nmcli import NmcliConnShow, NmcliDevShow, NmcliDevShowSos, NmcliConnShowUuids
from insights.tests import context_wrap

NMCLI_SHOW = """
Expand Down Expand Up @@ -175,6 +175,28 @@
IP6.GATEWAY:
""".strip()

NMCLI_CONN_SHOW_UUID = """
connection.id: System eth0
connection.uuid: 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: eth0
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0 (default)
connection.auth-retries: -1
connection.timestamp: 1724660474
connection.read-only: no
connection.permissions: --
ipv4.dad-timeout: -1 (default)
ipv4.dhcp-vendor-class-identifier: --
ipv4.link-local: 0 (default)
IP4.DOMAIN[1]: gsslab.pek2.redhat.com
DHCP4.OPTION[1]: dhcp_client_identifier = 01:52:54:00:fc:b1:cb
DHCP4.OPTION[2]: dhcp_lease_time = 86400
""".strip()


def test_nmcli():
nmcli_obj = NmcliDevShow(context_wrap(NMCLI_SHOW))
Expand All @@ -197,6 +219,13 @@ def test_nmcli():
assert nmcli_obj.data is not None


def test_nmcli_conn_show_uuids():
nmcli_conn_show_uuid_obj = NmcliConnShowUuids(context_wrap(NMCLI_CONN_SHOW_UUID))
assert nmcli_conn_show_uuid_obj['connection.uuid'] == "5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03"
assert nmcli_conn_show_uuid_obj['ipv4.dhcp-vendor-class-identifier'] == "--"
assert nmcli_conn_show_uuid_obj['DHCP4.OPTION[2]'] == "dhcp_lease_time = 86400"


def test_nmcli_sos():
nmcli_obj = NmcliDevShowSos(context_wrap(NMCLI_SHOW_SOS))
con_dev = nmcli_obj.connected_devices
Expand Down Expand Up @@ -248,6 +277,7 @@ def test_nmcli_doc_examples():
'nmcli_obj': NmcliDevShow(context_wrap(NMCLI_SHOW)),
'nmcli_obj_sos': NmcliDevShowSos(context_wrap(NMCLI_SHOW_SOS)),
'static_conn': NmcliConnShow(context_wrap(STATIC_CONNECTION_SHOW_1)),
'conn_show_uuids': NmcliConnShowUuids(context_wrap(NMCLI_CONN_SHOW_UUID))
}
failed, total = doctest.testmod(nmcli, globs=env)
assert failed == 0
Expand Down

0 comments on commit d43de4d

Please sign in to comment.