Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add datasource and parser for nmcli_conn_show_uuid #4273

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions insights/parsers/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from insights.core import CommandParser
from insights.core.exceptions import SkipComponent
from insights.core.filters import add_filter
from insights.core.plugins import parser
from insights.parsers import get_active_lines, parse_fixed_table
from insights.specs import Specs
Expand Down Expand Up @@ -191,3 +192,39 @@ def parse_content(self, content):
def disconnected_connection(self):
"""(list): It will return all the disconnected static route connections."""
return self._disconnected_connection


add_filter(Specs.nmcli_conn_show_uuids, ["connection.uuid", "connection.id"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required to add in the parser? If so, the spec will be 100% collected. It would be good to add them in the rules, so, when such rules were retire, we do not need to collect this spec in that case.

Copy link
Contributor Author

@wushiqinlou wushiqinlou Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiangce - removed filter



@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid any miss-understanding, please explicitly obfuscate this uuid, e.g. 5fb06bd0-0bb0-7ffb-45f1-ffffffffffff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiangce - updated

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()
JoySnow marked this conversation as resolved.
Show resolved Hide resolved

for line in get_active_lines(content):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec is based on a command, which won't output any "# xxx" comment lines, it's not recommended to use the get_active_lines which will iterate the lines before parsing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiangce - removed get_active_lines

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, no_obfuscate=['hostname', 'ip'], filterable=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]
JoySnow marked this conversation as resolved.
Show resolved Hide resolved
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,
JoySnow marked this conversation as resolved.
Show resolved Hide resolved
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 --
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above, please obfuscate these uuids by hand explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiangce - updated

'''.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
Loading