| 1 |
# base classes for virtualization facts |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import typing as t |
| 22 |
|
| 23 |
from ansible.module_utils.facts.collector import BaseFactCollector |
| 24 |
|
| 25 |
|
| 26 |
class Virtual: |
| 27 |
""" |
| 28 |
This is a generic Virtual subclass of Facts. This should be further |
| 29 |
subclassed to implement per platform. If you subclass this, |
| 30 |
you should define: |
| 31 |
- virtualization_type |
| 32 |
- virtualization_role |
| 33 |
- container (e.g. solaris zones, freebsd jails, linux containers) |
| 34 |
|
| 35 |
All subclasses MUST define platform. |
| 36 |
""" |
| 37 |
platform = 'Generic' |
| 38 |
|
| 39 |
# FIXME: remove load_on_init if we can |
| 40 |
def __init__(self, module, load_on_init=False): |
| 41 |
self.module = module |
| 42 |
|
| 43 |
# FIXME: just here for existing tests cases till they are updated |
| 44 |
def populate(self, collected_facts=None): |
| 45 |
virtual_facts = self.get_virtual_facts() |
| 46 |
|
| 47 |
return virtual_facts |
| 48 |
|
| 49 |
def get_virtual_facts(self): |
| 50 |
virtual_facts = { |
| 51 |
'virtualization_type': '', |
| 52 |
'virtualization_role': '', |
| 53 |
'virtualization_tech_guest': set(), |
| 54 |
'virtualization_tech_host': set(), |
| 55 |
} |
| 56 |
return virtual_facts |
| 57 |
|
| 58 |
|
| 59 |
class VirtualCollector(BaseFactCollector): |
| 60 |
name = 'virtual' |
| 61 |
_fact_class = Virtual |
| 62 |
_fact_ids = set([ |
| 63 |
'virtualization_type', |
| 64 |
'virtualization_role', |
| 65 |
'virtualization_tech_guest', |
| 66 |
'virtualization_tech_host', |
| 67 |
]) # type: t.Set[str] |
| 68 |
|
| 69 |
def collect(self, module=None, collected_facts=None): |
| 70 |
collected_facts = collected_facts or {} |
| 71 |
if not module: |
| 72 |
return {} |
| 73 |
|
| 74 |
# Network munges cached_facts by side effect, so give it a copy |
| 75 |
facts_obj = self._fact_class(module) |
| 76 |
|
| 77 |
facts_dict = facts_obj.populate(collected_facts=collected_facts) |
| 78 |
|
| 79 |
return facts_dict |