netdevops / hier_config

Hierarchical Configuration
MIT License
126 stars 24 forks source link

Extend host obj #33

Closed jtdub closed 6 years ago

jtdub commented 6 years ago

Ref: #28

In [1]: from hier_config.host import Host
In [1]: from hier_config.host import Host

In [2]: import yaml

In [3]: options = yaml.load(open('./tests/files/test_options_ios.yml'))

In [4]: host = Host('example.rtr', 'ios', options)

In [5]: host.load_config_from(config_type="running", name="./tests/files/running_config.conf")
Out[5]: HConfig(host=Host(hostname=example.rtr))

In [6]: host.load_config_from(config_type="compiled", name="./tests/files/compiled_config.conf")
Out[6]: HConfig(host=Host(hostname=example.rtr))

In [7]: host.load_remediation()
Out[7]: HConfig(host=Host(hostname=example.rtr))

In [8]: rem1 = host.facts['remediation_config_raw']

In [9]: host.load_tags('./tests/files/test_tags_ios.yml')
Out[9]:
[{'add_tags': 'safe',
  'lineage': [{'equals': ['no ip http secure-server',
     'no ip http server',
     'vlan',
     'no vlan']}]},
 {'add_tags': 'safe',
  'lineage': [{'startswith': 'interface Vlan'},
   {'startswith': ['description']}]},
 {'add_tags': 'manual',
  'lineage': [{'startswith': ['ip access-list',
     'no ip access-list',
     'access-list',
     'no access-list']}]},
 {'add_tags': 'manaual',
  'lineage': [{'startswith': 'interface Vlan'},
   {'startswith': ['ip address',
     'no ip address',
     'mtu',
     'no mtu',
     'ip access-group',
     'no ip access-group',
     'shutdown',
     'no shutdown']}]}]

In [10]: host.load_remediation()
Out[10]: HConfig(host=Host(hostname=example.rtr))

In [11]: rem1 = host.facts['remediation_config_raw']

In [12]: rem1
Out[12]: 'vlan 3\n  name switch_mgmt_10.0.3.0/24\nvlan 4\n  name switch_mgmt_10.0.4.0/24\ninterface Vlan2\n  no shutdown\n  mtu 9000\n  ip access-group TEST in\ninterface Vlan3\n  description switch_mgmt_10.0.3.0/24\n  ip address 10.0.3.1 255.255.0.0\ninterface Vlan4\n  mtu 9000\n  description switch_mgmt_10.0.4.0/24\n  ip address 10.0.4.1 255.255.0.0\n  ip access-group TEST in\n  no shutdown\n'

In [13]: host.filter_remediation(include_tags='safe')
Out[13]: 'interface Vlan3\n  description switch_mgmt_10.0.3.0/24\ninterface Vlan4\n  description switch_mgmt_10.0.4.0/24\n'

In [14]: rem2 = host.facts['remediation_config_raw']

In [15]: rem1
Out[15]: 'vlan 3\n  name switch_mgmt_10.0.3.0/24\nvlan 4\n  name switch_mgmt_10.0.4.0/24\ninterface Vlan2\n  no shutdown\n  mtu 9000\n  ip access-group TEST in\ninterface Vlan3\n  description switch_mgmt_10.0.3.0/24\n  ip address 10.0.3.1 255.255.0.0\ninterface Vlan4\n  mtu 9000\n  description switch_mgmt_10.0.4.0/24\n  ip address 10.0.4.1 255.255.0.0\n  ip access-group TEST in\n  no shutdown\n'

In [16]: rem2
Out[16]: 'interface Vlan3\n  description switch_mgmt_10.0.3.0/24\ninterface Vlan4\n  description switch_mgmt_10.0.4.0/24\n'

In [17]:
aedwardstx commented 6 years ago

@jtdub What are your thoughts on having the following methods v.s. a single load method that takes a dict: Host.load_running_config(str), Host.load_running_config_from_file(str), Host.load_compiled_config(str), and Host.load_compiled_config_from_file(str)?

Further, what are your thoughts on doing something like:

class Host:
    def __init__(self):
        self._running_config = None
        self._compiled_config = None

    @property
    def running_config(self):
        if self._running_config is None:
            self._running_config = self._get_running_config()
        return self._running_config

    @property
    def compiled_config(self):
        if self._compiled_config is None:
            self._compiled_config = self._get_compiled_config()
        return self._compiled_config

    def _get_running_config()
        return NotImplemented

    def _get_compiled_config()
        return NotImplemented

This leaves the user free to inherit Host and implement their own logic to retrieve running and compiled configs.

aedwardstx commented 6 years ago

Consider ways to separate the concepts of generating the remediation and querying(e.g. get remediation by tag+lineage_rules) the remediation.