Skip to content

Commit

Permalink
Updating some functions with new names based on Ken's recommendation …
Browse files Browse the repository at this point in the history
…of using metadata.
  • Loading branch information
Brandon Minnix authored and Brandon Minnix committed Apr 26, 2024
1 parent 02692a9 commit 8d9b534
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
| compare_version_loose | netutils.os_version.compare_version_loose |
| compare_version_strict | netutils.os_version.compare_version_strict |
| get_upgrade_path | netutils.os_version.get_upgrade_path |
| juniper_junos_version_parser | netutils.os_version.juniper_junos_version_parser |
| vendorize_version | netutils.os_version.vendorize_version |
| juniper_junos_metadata | netutils.os_version.juniper_junos_metadata |
| version_metadata | netutils.os_version.version_metadata |
| compare_cisco_type5 | netutils.password.compare_cisco_type5 |
| compare_cisco_type7 | netutils.password.compare_cisco_type7 |
| compare_cisco_type9 | netutils.password.compare_cisco_type9 |
Expand Down
28 changes: 14 additions & 14 deletions netutils/os_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def compare_version_strict(current_version: str, comparison: str, target_version
return _compare_version(current_version, comparison, target_version, "strict")


def juniper_junos_version_parser(version: str) -> t.Dict[str, t.Any]:
def juniper_junos_metadata(version: str) -> t.Dict[str, t.Any]:
"""Parses JunOS Version into usable bits matching JunOS Standards.
Args:
Expand All @@ -141,7 +141,7 @@ def juniper_junos_version_parser(version: str) -> t.Dict[str, t.Any]:
A dictionary containing parsed version information
Examples:
>>> juniper_junos_version_parser("12.3R4")
>>> juniper_junos_metadata("12.3R4")
{'isservice': False, 'ismaintenance': True, 'isfrs': False, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '3', 'type': 'R', 'build': '4'}
"""
# Use regex to group the main, minor, type and build into useable pieces
Expand Down Expand Up @@ -214,14 +214,14 @@ def juniper_junos_version_parser(version: str) -> t.Dict[str, t.Any]:
return parsed_version


os_version_parsers = {
version_metadata_parsers = {
"juniper": {
"junos": juniper_junos_version_parser,
"junos": juniper_junos_metadata,
}
}


def vendorize_version(vendor: str, os_type: str, version: str) -> t.Dict[str, t.Any]:
def version_metadata(vendor: str, os_type: str, version: str) -> t.Dict[str, t.Any]:
"""If a custom version parser is avaialable, use it.
Args:
Expand All @@ -232,17 +232,17 @@ def vendorize_version(vendor: str, os_type: str, version: str) -> t.Dict[str, t.
Returns:
dict: Dict of broken down version into vendor standards.
Example:
>>> from netutils.os_version import vendorize_version
>>> vendorize_version("Cisco", "IOS", "15.5")
{'vendor': 'Cisco', 'os_type': 'IOS', 'version': '15.5', 'vendorized': False}
>>> vendorize_version("juniper", "junos", "12.4R")
{'isservice': False, 'ismaintenance': False, 'isfrs': True, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '4', 'type': 'R', 'build': None, 'vendorized': True}
Examples:
>>> from netutils.os_version import version_metadata
>>> version_metadata("Cisco", "IOS", "15.5")
{'vendor': 'Cisco', 'os_type': 'IOS', 'version': '15.5', 'metadata': False}
>>> version_metadata("juniper", "junos", "12.4R")
{'isservice': False, 'ismaintenance': False, 'isfrs': True, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '4', 'type': 'R', 'build': None, 'metadata': True}
"""
try:
parsed_version = os_version_parsers[vendor][os_type](version)
parsed_version.update({"vendorized": True})
parsed_version = version_metadata_parsers[vendor][os_type](version)
parsed_version.update({"metadata": True})
except KeyError:
parsed_version = {"vendor": vendor, "os_type": os_type, "version": version, "vendorized": False}
parsed_version = {"vendor": vendor, "os_type": os_type, "version": version, "metadata": False}

return parsed_version
18 changes: 11 additions & 7 deletions netutils/platform_mapper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Platform Mappers."""
# The intent of this script is to take a given platform, determine the format, and reformat it for another purpose
# An example of this is a platform being formatted for NIST Database Query
"""Unified Services."""
# The intent of this module is to offer common services for consumption by multiple platforms.
# An example of this is an os platform/version being set up for NIST NVD Query.

import abc
import dataclasses
import typing as t

from netutils.nist import get_nist_url_funcs
from netutils.os_version import os_version_parsers
from netutils.os_version import version_metadata_parsers

PLATFORM_FIELDS: t.Dict[str, t.Any] = {
"default": [
Expand Down Expand Up @@ -61,24 +62,27 @@ def os_platform_object_builder(vendor: str, platform: str, version: str) -> obje
"""Creates a platform object relative to its need and definition.
Args:
vendor
vendor (str): Name of vendor
platform (str): Name of os/other platform
version (str): Version value
Returns:
A platform object
object: Platform object
Examples:
>>> jp = os_platform_object_builder("juniper", "junos", "12.1R3-S4.1")
>>> jp.get_nist_urls()
['https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:12.1r3:s4.1:*:*:*:*:*:*', 'https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:12.1r3-s4.1:*:*:*:*:*:*:*']
"""

platform = platform.lower()
vendor = vendor.lower()

class_fields = [*PLATFORM_FIELDS["default"]]
vendor_platform_fields = PLATFORM_FIELDS.get(vendor, {}).get(platform, [])
class_fields.extend(vendor_platform_fields)

version_parser = os_version_parsers.get(vendor, {}).get(platform, None)
version_parser = version_metadata_parsers.get(vendor, {}).get(platform, None)
field_values = {
"vendor": vendor,
"os_type": platform,
Expand Down
4 changes: 2 additions & 2 deletions netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
"hash_data": "hash.hash_data",
"get_ips_sorted": "ip.get_ips_sorted",
"os_platform_object_builder": "platform_mapper.os_platform_object_builder",
"juniper_junos_version_parser": "os_version.juniper_junos_version_parser",
"vendorize_version": "os_version.vendorize_version",
"juniper_junos_metadata": "os_version.juniper_junos_metadata",
"version_metadata": "os_version.version_metadata",
}


Expand Down

0 comments on commit 8d9b534

Please sign in to comment.