Debian Codegraph @

lib/ansible/module_utils/facts/network/base.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import typing as t
19
20 from ansible.module_utils.facts.collector import BaseFactCollector
21
22
23 class Network:
24 """
25 This is a generic Network subclass of Facts. This should be further
26 subclassed to implement per platform. If you subclass this,
27 you must define:
28 - interfaces (a list of interface names)
29 - interface_<name> dictionary of ipv4, ipv6, and mac address information.
30
31 All subclasses MUST define platform.
32 """
33 platform = 'Generic'
34
35 # FIXME: remove load_on_init when we can
36 def __init__(self, module, load_on_init=False):
37 self.module = module
38
39 # TODO: more or less abstract/NotImplemented
40 def populate(self, collected_facts=None):
41 return {}
42
43
44 class NetworkCollector(BaseFactCollector):
45 # MAYBE: we could try to build this based on the arch specific implementation of Network() or its kin
46 name = 'network'
47 _fact_class = Network
48 _fact_ids = set(['interfaces',
49 'default_ipv4',
50 'default_ipv6',
51 'all_ipv4_addresses',
52 'all_ipv6_addresses']) # type: t.Set[str]
53
54 IPV6_SCOPE = {'0': 'global',
55 '10': 'host',
56 '20': 'link',
57 '40': 'admin',
58 '50': 'site',
59 '80': 'organization'}
60
61 def collect(self, module=None, collected_facts=None):
62 collected_facts = collected_facts or {}
63 if not module:
64 return {}
65
66 # Network munges cached_facts by side effect, so give it a copy
67 facts_obj = self._fact_class(module)
68
69 facts_dict = facts_obj.populate(collected_facts=collected_facts)
70
71 return facts_dict