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

@@ -0,0 +1,11 @@
expected_fields:
- availability_zone
- binary
- disabled_reason
- host
- id
- is_forced_down
- name
- state
- status
- updated_at

View File

@@ -0,0 +1,23 @@
---
- name: Fetch compute services
openstack.cloud.compute_service_info:
cloud: "{{ cloud }}"
register: compute_services
- name: Assert return values of compute_service_info module
assert:
that:
- compute_services.compute_services | length > 0
# allow new fields to be introduced but prevent fields from being removed
- expected_fields|difference(compute_services.compute_services[0].keys())|length == 0
- name: Fetch compute services with filters
openstack.cloud.compute_service_info:
cloud: "{{ cloud }}"
binary: "nova-compute"
register: compute_services
- name: Assert return values of compute_service_info module
assert:
that:
- compute_services.compute_services | length > 0

View File

@@ -1,33 +0,0 @@
---
- name: Get nova compute services info
openstack.cloud.compute_service_info:
cloud: "{{ cloud }}"
register: result
failed_when: "result.openstack_compute_services | length <= 0"
- name: Assert fields on OpenStack SDK before 0.53
when: sdk_version is version(0.53, '<')
assert:
that:
- '["availability_zone", "binary", "disables_reason",
"host", "name", "state", "status", "id"] |
difference(result.openstack_compute_services.0.keys()) | length == 0'
- name: Assert fields on OpenStack SDK 0.53 and later
when: sdk_version is version(0.53, '>=')
assert:
that:
- '["availability_zone", "binary", "disabled_reason", "is_forced_down",
"host", "name", "state", "status", "updated_at", "id"] |
difference(result.openstack_compute_services.0.keys()) | length == 0'
- name: Filter compute services. Supported since OpenStack SDK 0.53.0 (Wallaby).
when: sdk_version is version(0.53, '>=')
block:
- name: Get nova compute services info
openstack.cloud.compute_service_info:
cloud: "{{ cloud }}"
binary: "nova-compute"
register: result
failed_when: "result.openstack_compute_services | length <= 0"

View File

@@ -11,6 +11,7 @@
- { role: coe_cluster_template, tags: coe_cluster_template }
- { role: compute_flavor, tags: compute_flavor }
- { role: compute_flavor_access, tags: compute_flavor_access }
- { role: compute_service, tags: compute_service }
- { role: config, tags: config }
- { role: dns_zone, tags: dns_zone }
- { role: endpoint, tags: endpoint }
@@ -37,9 +38,6 @@
- { role: logging, tags: logging }
- { role: network, tags: network }
- { role: neutron_rbac_policy, tags: neutron_rbac_policy }
- role: nova_services
tags: nova_services
when: sdk_version is version(0.44, '>=')
- { role: object, tags: object }
- { role: object_container, tags: object_container }
- { role: port, tags: port }

View File

@@ -4,94 +4,77 @@
# 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.
- Filter the service list result by binary name of the service.
type: str
host:
description:
- Filter by service host. Requires openstacksdk>=0.53.
- Filter the service list result by the host name.
type: str
requirements:
- "python >= 3.6"
- "openstacksdk"
extends_documentation_fragment:
- 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
RETURN = r'''
compute_services:
description: List of dictionaries describing Compute (Nova) services.
returned: always
type: list
elements: dict
contains:
id:
description: Unique UUID.
returned: success
availability_zone:
description: The availability zone name.
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
id:
description: Unique UUID.
type: str
is_forced_down:
description: If the service has been forced down or nova-compute
returned: success
type: bool
host:
description: The name of the host.
type: str
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
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
returned: success
type: str
'''
@@ -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():