From 5c40fc4698ac0d31e421f61412cf5782206d3af5 Mon Sep 17 00:00:00 2001 From: Dirk Goetz Date: Thu, 14 Dec 2023 16:57:43 +0100 Subject: [PATCH] virt_who_config - add a new module to support virt_who_config Fixes: https://github.com/theforeman/foreman-ansible-modules/issues/1483 --- changelogs/fragments/1483-virt_who_config.yml | 2 + galaxy.yml | 1 + meta/runtime.yml | 1 + plugins/modules/virt_who_config.py | 221 ++++++++++++++++++ .../test_playbooks/tasks/virt_who_config.yml | 27 +++ tests/test_playbooks/virt_who_config.yml | 9 + 6 files changed, 261 insertions(+) create mode 100644 changelogs/fragments/1483-virt_who_config.yml create mode 100644 plugins/modules/virt_who_config.py create mode 100644 tests/test_playbooks/tasks/virt_who_config.yml create mode 100644 tests/test_playbooks/virt_who_config.yml diff --git a/changelogs/fragments/1483-virt_who_config.yml b/changelogs/fragments/1483-virt_who_config.yml new file mode 100644 index 000000000..27aca761e --- /dev/null +++ b/changelogs/fragments/1483-virt_who_config.yml @@ -0,0 +1,2 @@ +minor_changes: + - virt_who_config - add a new module to support virt_who_config (https://github.com/theforeman/foreman-ansible-modules/issues/1483) diff --git a/galaxy.yml b/galaxy.yml index 3e39af559..a79689d6b 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -20,6 +20,7 @@ authors: - "Christoffer Reijer " - "Dave Thomas <11580510+dthomastx@users.noreply.github.com>" - "Deric Crago " + - "Dirk Goetz " - "Eric D. Helms " - "Eric L " - "Ethan " diff --git a/meta/runtime.yml b/meta/runtime.yml index 9e34e240e..c32cf73d3 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -81,6 +81,7 @@ action_groups: - user - usergroup - wait_for_task + - virt_who_configure plugin_routing: modules: foreman_architecture: diff --git a/plugins/modules/virt_who_config.py b/plugins/modules/virt_who_config.py new file mode 100644 index 000000000..621e6de86 --- /dev/null +++ b/plugins/modules/virt_who_config.py @@ -0,0 +1,221 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: virt_who_config +version_added: 3.15.0 +short_description: Manage Virt Who Configs +description: + - Manage Virt Who Configs +author: + - "Dirk Goetz (@dgoetz)" +options: + name: + description: + - The name of the Virt Who Configuration + type: str + required: true + interval: + description: + - The interval to run Virt Who + choices: + - 60 + - 120 + - 240 + - 480 + - 720 + - 1440 + - 2880 + - 4320 + type: int + required: true + filtering_mode: + description: + - The filtering mode for Hypervisors + - 0 means no filtering, 1 means whitelist, 2 means blacklist + choices: + - 0 + - 1 + - 2 + type: int + required: true + whitelist: + description: + - Hypervisor whitelist, applicable only when filtering mode is set to 1 + - Wildcards and regular expressions are supported, multiple records must be separated by comma + type: str + blacklist: + description: + - Hypervisor blacklist, applicable only when filtering mode is set to 2 + - Wildcards and regular expressions are supported, multiple records must be separated by comma + type: str + filter_host_parents: + description: + - Applicable only for esx provider type + - Only hosts which parent (usually ComputeResource) name is specified in comma-separated list in this option will be reported + - Wildcards and regular expressions are supported, multiple records must be separated by comma + - Put the value into the double-quotes if it contains special characters like comma + - All new line characters will be removed in resulting configuration file, white spaces are removed from beginning and end + type: str + exclude_host_parents: + description: + - Applicable only for esx provider type + - Hosts which parent (usually ComputeResource) name is specified in comma-separated list in this option will NOT be reported + - Wildcards and regular expressions are supported, multiple records must be separated by comma + - Put the value into the double-quotes if it contains special characters like comma + - All new line characters will be removed in resulting configuration file, white spaces are removed from beginning and end + type: str + hypervisor_id: + description: + - Specifies how the hypervisor will be identified + choices: + - hostname + - uuid + - hwuuid + type: str + required: true + hypervisor_type: + description: + - Hypervisor type + choices: + - esx + - hyperv + - libvirt + - kubevirt + - ahv + type: str + required: true + hypervisor_server: + description: + - Fully qualified host name or IP address of the hypervisor + type: str + hypervisor_username: + description: + - Account name by which virt-who is to connect to the hypervisor + type: str + hypervisor_password: + description: + - Hypervisor password, required for all hypervisor types except for libvirt/kubevirt + type: str + satellite_url: + description: + - Foreman server FQDN + type: str + required: true + debug: + description: + - Enable debugging output + type: bool + kubeconfig_path: + description: + - Configuration file containing details about how to connect to the cluster and authentication details + type: str + http_proxy_id: + description: + - HTTP proxy that should be used for communication between the server on which virt-who is running and the hypervisors and virtualization managers + type: str + no_proxy: + description: + - A comma-separated list of hostnames or domains or ip addresses to ignore proxy settings for + - Optionally this may be set to * to bypass proxy settings for all hostnames domains or ip addresses + type: str + prism_flavor: + description: + - Select the Prism flavor you are connecting to + choices: + - central + - element + type: str + ahv_internal_debug: + description: + - Enable debugging output is required to enable AHV internal debug + - It provides extra AHV debug information when both options are enabled + type: bool +extends_documentation_fragment: + - theforeman.foreman.foreman + - theforeman.foreman.foreman.entity_state +''' + +EXAMPLES = ''' +- name: "Create a Virt Who Configuration" + theforeman.foreman.virt_who_config: + username: "admin" + password: "changeme" + server_url: "https://foreman.example.com" + organization: "Default Organization" + name: "A Virt Who Configuration" + interval: 120 + filtering_mode: 0 + hypervisor_id: "hostname" + hypervisor_type: "libvirt" + satellite_url: "foreman.example.com" + state: "present" +''' + +RETURN = ''' +entity: + description: Final state of the affected entities grouped by their type. + returned: success + type: dict + contains: + activation_keys: + description: List of Virt Who Configs + type: list + elements: dict +''' + + +from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloEntityAnsibleModule + + +class KatelloConfigModule(KatelloEntityAnsibleModule): + pass + + +def main(): + module = KatelloConfigModule( + foreman_spec=dict( + name=dict(required=True), + interval=dict(required=True, type='int', choices=[60, 120, 240, 480, 720, 1440, 2880, 4320]), + filtering_mode=dict(required=True, type='int', choices=[0, 1, 2]), + whitelist=dict(), + blacklist=dict(), + filter_host_parents=dict(), + exclude_host_parents=dict(), + hypervisor_id=dict(required=True, choices=['hostname', 'uuid', 'hwuuid']), + hypervisor_type=dict(required=True, choices=['esx', 'hyperv', 'libvirt', 'kubevirt', 'ahv']), + hypervisor_server=dict(), + hypervisor_username=dict(), + hypervisor_password=dict(no_log=True), + satellite_url=dict(required=True), + debug=dict(type='bool'), + kubeconfig_path=dict(), + http_proxy_id=dict(type='entity', resource_type='http_proxies', scope=['organization']), + no_proxy=dict(), + prism_flavor=dict(choices=['central', 'element']), + ahv_internal_debug=dict(type='bool'), + ), + ) + with module.api_connection(): + module.run() + + +if __name__ == '__main__': + main() diff --git a/tests/test_playbooks/tasks/virt_who_config.yml b/tests/test_playbooks/tasks/virt_who_config.yml new file mode 100644 index 000000000..c386f825b --- /dev/null +++ b/tests/test_playbooks/tasks/virt_who_config.yml @@ -0,0 +1,27 @@ +--- +- name: "Create/update Virt Who config" + vars: + virt_who_config_name: "Test Virt Who config" + virt_who_config_state: "present" + virt_who_config_organization: "Test Organization" + virt_who_config: + username: "{{ foreman_username }}" + password: "{{ foreman_password }}" + server_url: "{{ foreman_server_url }}" + organization: "{{ virt_who_config_organization }}" + name: "{{ virt_who_config_name }}" + interval: "{{ virt_who_config_interval | default(120) }}" + filtering_mode: "{{ virt_who_config_filtering_mode | default(0) }}" + hypervisor_id: "{{ virt_who_config_hypervisor_id | default('hostname') }}" + hypervisor_type: "{{ virt_who_config_hypervisor_type | default('libvirt') }}" + satellite_url: "{{ virt_who_config_satellite_url | default('foreman.example.com') }}" + state: "{{ virt_who_config_organization }}" +- assert: + fail_msg: "Creating/updating Virt Who config failed! (expected_change: {{ expected_change | default('unknown') }})" + that: + - result.changed == expected_change + when: expected_change is defined +- include_tasks: _assert_diff.yml + vars: + module: virt_who_config +... diff --git a/tests/test_playbooks/virt_who_config.yml b/tests/test_playbooks/virt_who_config.yml new file mode 100644 index 000000000..1d76a11f3 --- /dev/null +++ b/tests/test_playbooks/virt_who_config.yml @@ -0,0 +1,9 @@ +--- +- hosts: localhost + collections: + - theforeman.foreman + gather_facts: false + vars_files: + - vars/server.yml + tasks: + - include_tasks: tasks/virt_who_config.yml