Refactored stack_info module and allow to filter by owner and project names

Allow to filter stacks by owner names and project names instead of
ids only, in order to be consistent with other *_info modules.

Renamed stack_info's module attributes 'owner_id' to 'owner' and
'project_id' to 'project' to account for the new filter by name
functionality. Added the *_id attributes as aliases to keep
backward compatibility.

Updated and extended DOCUMENTATION, EXAMPLES and RETURN docstrings.

The stack_info module will convert its return values into dictionaries
without computed (redundant) values. Thus dropping values such as
location is not required anymore.

Change-Id: I9cdfb44dd424f63c05943616cf5918ceb3a57b1f
This commit is contained in:
Jakob Meng 2022-09-28 12:21:32 +02:00
parent 34b0abb4ca
commit 7bad5cfd42
1 changed files with 186 additions and 51 deletions

View File

@ -4,103 +4,238 @@
# Copyright (c) 2020, Sagi Shnaidman <sshnaidm@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = '''
DOCUMENTATION = r'''
---
module: stack_info
short_description: Retrive information about Heat stacks
short_description: Retrieve information about Heat stacks
author: OpenStack Ansible SIG
description:
- Get information about Heat stack in openstack
- Get information about Heat stack in OpenStack
options:
name:
description:
- Name of the stack as a string.
- Name of the stack.
type: str
required: false
owner:
description:
- Name or ID of the parent stack.
type: str
aliases: ['owner_id']
project:
description:
- Name or ID of the project.
type: str
aliases: ['project_id']
status:
description:
- Value of the status of the stack so that you can filter on "available" for example
- Status of the stack such as C(available)
type: str
required: false
project_id:
description:
- Project ID to be used as filter
type: str
required: false
owner_id:
description:
- Owner (parent) of the stack to be used as a filter
type: str
required: false
requirements:
- "python >= 3.6"
- "openstacksdk"
extends_documentation_fragment:
- openstack.cloud.openstack
'''
RETURN = '''
EXAMPLES = r'''
- name: Fetch all Heat stacks
openstack.cloud.stack_info:
cloud: devstack
- name: Fetch a single Heat stack
openstack.cloud.stack_info:
cloud: devstack
name: my_stack
'''
RETURN = r'''
stacks:
description: List of dictionaries describing stacks.
type: list
elements: dict
returned: always.
contains:
added:
description: List of resource objects that will be added.
type: list
capabilities:
description: AWS compatible template listing capabilities.
type: list
created_at:
description: Time when created.
type: str
sample: "2016-07-05T17:38:12Z"
deleted:
description: A list of resource objects that will be deleted.
type: list
deleted_at:
description: Time when the deleted.
type: str
sample: "2016-07-05T17:38:12Z"
description:
description: >
Description of the Stack provided in the heat
template.
type: str
sample: "HOT template to create a new instance and networks"
environment:
description: A JSON environment for the stack.
type: dict
environment_files:
description: >
An ordered list of names for environment files found
in the files dict.
type: list
files:
description: >
Additional files referenced in the template or
the environment
type: dict
files_container:
description: >
Name of swift container with child templates and
files.
type: str
id:
description: Unique UUID.
description: Stack ID.
type: str
sample: "97a3f543-8136-4570-920e-fd7605c989d6"
is_rollback_disabled:
description: Whether the stack will support a rollback.
type: bool
links:
description: Links to the current Stack.
type: list
elements: dict
sample: "[{'href': 'http://foo:8004/v1/7f6a/stacks/test-stack/
97a3f543-8136-4570-920e-fd7605c989d6']"
name:
description: Name of the Stack
type: str
sample: "test-stack"
notification_topics:
description: Stack related events.
type: str
sample: "HOT template to create a new instance and networks"
outputs:
description: Output returned by the Stack.
type: list
elements: dict
sample: "[{'description': 'IP of server1 in private network',
'output_key': 'server1_private_ip',
'output_value': '10.1.10.103'}]"
owner_id:
description: The ID of the owner stack if any.
type: str
parameters:
description: Parameters of the current Stack
type: dict
sample: "{'OS::project_id': '7f6a3a3e01164a4eb4eecb2ab7742101',
'OS::stack_id': '97a3f543-8136-4570-920e-fd7605c989d6',
'OS::stack_name': 'test-stack',
'stack_status': 'CREATE_COMPLETE',
'stack_status_reason':
'Stack CREATE completed successfully',
'status': 'COMPLETE',
'template_description':
'HOT template to create a new instance and nets',
'timeout_mins': 60,
'updated_time': null}"
parent_id:
description: The ID of the parent stack if any.
type: str
replaced:
description: A list of resource objects that will be replaced.
type: str
sample: "39007a7e-ee4f-4d13-8283-b4da2e037c69"
status:
description: Stack status.
description: stack status.
type: str
status_reason:
description: >
Explaining how the stack transits to its current
status.
type: str
tags:
description: A list of strings used as tags on the stack
type: list
template:
description: A dict containing the template use for stack creation.
type: dict
template_description:
description: Stack template description text.
type: str
template_url:
description: The URL where a stack template can be found.
type: str
timeout_mins:
description: Stack operation timeout in minutes.
type: str
unchanged:
description: >
A list of resource objects that will remain unchanged
if a stack.
type: list
updated:
description: >
A list of resource objects that will have their
properties updated.
type: list
updated_at:
description: Timestamp of last update on the stack.
type: str
user_project_id:
description: The ID of the user project created for this stack.
type: str
'''
EXAMPLES = '''
# Get backups.
- openstack.cloud.stack_info:
register: stack
- openstack.cloud.stack_info:
name: my_stack
register: stack
'''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
class StackInfoModule(OpenStackModule):
module_min_sdk_version = '0.53.0'
argument_spec = dict(
name=dict(),
owner=dict(aliases=['owner_id']),
project=dict(aliases=['project_id']),
status=dict(),
project_id=dict(),
owner_id=dict()
)
module_kwargs = dict(
supports_check_mode=True
)
def run(self):
data = []
attrs = {}
kwargs = {}
for param in ['name', 'status', 'project_id', 'owner_id']:
if self.params[param]:
attrs[param] = self.params[param]
owner_name_or_id = self.params['owner']
if owner_name_or_id:
owner = self.conn.orchestration.find_stack(owner_name_or_id)
if owner:
kwargs['owner_id'] = owner['id']
else:
# Owner could not be found so return empty list of stacks
# because *_info modules never raise errors on missing
# resources
self.exit_json(changed=False, stacks=[])
for raw in self.conn.orchestration.stacks(**attrs):
dt = raw.to_dict()
dt.pop('location')
data.append(dt)
project_name_or_id = self.params['project']
if project_name_or_id:
project = self.conn.identity.find_project(project_name_or_id)
if project:
kwargs['project_id'] = project['id']
else:
# Project could not be found so return empty list of stacks
# because *_info modules never raise errors on missing
# resources
self.exit_json(changed=False, stacks=[])
self.exit_json(
changed=False,
stacks=data
)
for k in ['name', 'status']:
if self.params[k] is not None:
kwargs[k] = self.params[k]
stacks = [stack.to_dict(computed=False)
for stack in self.conn.orchestration.stacks(**kwargs)]
self.exit_json(changed=False, stacks=stacks)
def main():