Skip to content
Snippets Groups Projects
Commit 7bad5cfd authored by Jakob Meng's avatar Jakob Meng
Browse files

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
parent 34b0abb4
No related branches found
No related tags found
No related merge requests found
...@@ -4,103 +4,238 @@ ...@@ -4,103 +4,238 @@
# Copyright (c) 2020, Sagi Shnaidman <sshnaidm@redhat.com> # 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) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: stack_info module: stack_info
short_description: Retrive information about Heat stacks short_description: Retrieve information about Heat stacks
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Get information about Heat stack in openstack - Get information about Heat stack in OpenStack
options: options:
name: name:
description: description:
- Name of the stack as a string. - Name of the stack.
type: str type: str
required: false owner:
status:
description: description:
- Value of the status of the stack so that you can filter on "available" for example - Name or ID of the parent stack.
type: str type: str
required: false aliases: ['owner_id']
project_id: project:
description: description:
- Project ID to be used as filter - Name or ID of the project.
type: str type: str
required: false aliases: ['project_id']
owner_id: status:
description: description:
- Owner (parent) of the stack to be used as a filter - Status of the stack such as C(available)
type: str type: str
required: false
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - 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: stacks:
description: List of dictionaries describing stacks. description: List of dictionaries describing stacks.
type: list type: list
elements: dict elements: dict
returned: always. returned: always.
contains: 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: 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 type: str
sample: "39007a7e-ee4f-4d13-8283-b4da2e037c69"
status: 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 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 from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
class StackInfoModule(OpenStackModule): class StackInfoModule(OpenStackModule):
module_min_sdk_version = '0.53.0'
argument_spec = dict( argument_spec = dict(
name=dict(), name=dict(),
owner=dict(aliases=['owner_id']),
project=dict(aliases=['project_id']),
status=dict(), status=dict(),
project_id=dict(),
owner_id=dict()
) )
module_kwargs = dict( module_kwargs = dict(
supports_check_mode=True supports_check_mode=True
) )
def run(self): def run(self):
data = [] kwargs = {}
attrs = {}
owner_name_or_id = self.params['owner']
for param in ['name', 'status', 'project_id', 'owner_id']: if owner_name_or_id:
if self.params[param]: owner = self.conn.orchestration.find_stack(owner_name_or_id)
attrs[param] = self.params[param] if owner:
kwargs['owner_id'] = owner['id']
for raw in self.conn.orchestration.stacks(**attrs): else:
dt = raw.to_dict() # Owner could not be found so return empty list of stacks
dt.pop('location') # because *_info modules never raise errors on missing
data.append(dt) # resources
self.exit_json(changed=False, stacks=[])
self.exit_json(
changed=False, project_name_or_id = self.params['project']
stacks=data 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=[])
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(): def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment