Refactored compute_service_info module

Change-Id: I1773b72f8c7eaec77f0480045c45073dc522c5cf
This commit is contained in:
Jakob Meng
2023-01-04 11:27:28 +01:00
parent 124e174d27
commit 0071fdcd97
5 changed files with 104 additions and 118 deletions

View File

@@ -4,95 +4,78 @@
# Copyright (c) 2016 Hewlett-Packard Enterprise Corporation
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = '''
DOCUMENTATION = r'''
---
module: compute_service_info
short_description: Retrieve information about one or more OpenStack compute services
short_description: Fetch OpenStack Compute (Nova) services
author: OpenStack Ansible SIG
description:
- Retrieve information about nova compute services
- Fetch OpenStack Compute (Nova) services.
options:
binary:
description:
- Filter by service binary type. Requires openstacksdk>=0.53.
type: str
host:
description:
- Filter by service host. Requires openstacksdk>=0.53.
type: str
binary:
description:
- Filter the service list result by binary name of the service.
type: str
host:
description:
- Filter the service list result by the host name.
type: str
requirements:
- "python >= 3.6"
- "openstacksdk"
- "python >= 3.6"
- "openstacksdk"
extends_documentation_fragment:
- openstack.cloud.openstack
- openstack.cloud.openstack
'''
EXAMPLES = '''
# Gather information about compute services
- openstack.cloud.compute_service_info:
EXAMPLES = r'''
- name: Fetch all OpenStack Compute (Nova) services
openstack.cloud.compute_service_info:
cloud: awesomecloud
- name: Fetch a subset of OpenStack Compute (Nova) services
openstack.cloud.compute_service_info:
cloud: awesomecloud
binary: "nova-compute"
host: "localhost"
register: result
- openstack.cloud.compute_service_info:
cloud: awesomecloud
register: result
- debug:
msg: "{{ result.openstack_compute_services }}"
'''
RETURN = '''
openstack_compute_services:
description: has all the OpenStack information about compute services
returned: always, but can be null
type: complex
contains:
id:
description: Unique UUID.
returned: success
type: str
binary:
description: The binary name of the service.
returned: success
type: str
host:
description: The name of the host.
returned: success
type: str
disabled_reason:
description: The reason why the service is disabled
returned: success and OpenStack SDK >= 0.53
type: str
disables_reason:
description: The reason why the service is disabled
returned: success and OpenStack SDK < 0.53
type: str
availability_zone:
description: The availability zone name.
returned: success
type: str
is_forced_down:
description: If the service has been forced down or nova-compute
returned: success
type: bool
name:
description: Service name
returned: success
type: str
status:
description: The status of the service. One of enabled or disabled.
returned: success
type: str
state:
description: The state of the service. One of up or down.
returned: success
type: str
update_at:
description: The date and time when the resource was updated
returned: success
type: str
RETURN = r'''
compute_services:
description: List of dictionaries describing Compute (Nova) services.
returned: always
type: list
elements: dict
contains:
availability_zone:
description: The availability zone name.
type: str
binary:
description: The binary name of the service.
type: str
disabled_reason:
description: The reason why the service is disabled
type: str
id:
description: Unique UUID.
type: str
is_forced_down:
description: If the service has been forced down or nova-compute
type: bool
host:
description: The name of the host.
type: str
name:
description: Service name
type: str
state:
description: The state of the service. One of up or down.
type: str
status:
description: The status of the service. One of enabled or disabled.
type: str
update_at:
description: The date and time when the resource was updated
type: str
'''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@@ -100,19 +83,23 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class ComputeServiceInfoModule(OpenStackModule):
argument_spec = dict(
binary=dict(min_ver='0.53.0'),
host=dict(min_ver='0.53.0'),
binary=dict(),
host=dict(),
)
module_kwargs = dict(
supports_check_mode=True
)
def run(self):
filters = self.check_versioned(binary=self.params['binary'], host=self.params['host'])
filters = {k: v for k, v in filters.items() if v is not None}
services = self.conn.compute.services(**filters)
services = [service.to_dict(computed=False) for service in services]
self.exit_json(changed=False, openstack_compute_services=services)
kwargs = {k: self.params[k]
for k in ['binary', 'host']
if self.params[k] is not None}
compute_services = self.conn.compute.services(**kwargs)
self.exit_json(changed=False,
compute_services=[s.to_dict(computed=False)
for s in compute_services])
def main():