From 70c773fe6d10dbe7f0eb627bc14993a70c7966c1 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Wed, 6 Jan 2021 12:00:12 +0100 Subject: [PATCH] Add stack_info module We lost stack_info module during transition from github. Implement it using newer interface and add tests. Change depends on SDK change adding missing query filters. Depends-On: https://review.opendev.org/c/openstack/openstacksdk/+/769484 Change-Id: Ie7e6d04ea298ba068f547a53643806b6bc84f873 --- ci/roles/orchestration/defaults/main.yaml | 2 + ci/roles/orchestration/files/hello-world.yaml | 11 ++ ci/roles/orchestration/tasks/main.yaml | 44 +++++++ ci/roles/volume/tasks/main.yml | 1 - ci/run-collection.yml | 3 + plugins/modules/stack_info.py | 109 ++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 ci/roles/orchestration/defaults/main.yaml create mode 100644 ci/roles/orchestration/files/hello-world.yaml create mode 100644 ci/roles/orchestration/tasks/main.yaml create mode 100644 plugins/modules/stack_info.py diff --git a/ci/roles/orchestration/defaults/main.yaml b/ci/roles/orchestration/defaults/main.yaml new file mode 100644 index 00000000..798cdf86 --- /dev/null +++ b/ci/roles/orchestration/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +stack_name: "test-stack" diff --git a/ci/roles/orchestration/files/hello-world.yaml b/ci/roles/orchestration/files/hello-world.yaml new file mode 100644 index 00000000..9ce1d15c --- /dev/null +++ b/ci/roles/orchestration/files/hello-world.yaml @@ -0,0 +1,11 @@ +# +# Minimal HOT template defining a single compute server. +# +heat_template_version: 2013-05-23 + +description: > + Minimal HOT template for stack + +parameters: +resources: +outputs: diff --git a/ci/roles/orchestration/tasks/main.yaml b/ci/roles/orchestration/tasks/main.yaml new file mode 100644 index 00000000..0818cac1 --- /dev/null +++ b/ci/roles/orchestration/tasks/main.yaml @@ -0,0 +1,44 @@ +--- +- name: Create minimal stack + openstack.cloud.stack: + cloud: "{{ cloud }}" + # template is searched related to playbook location or as absolute path + template: "roles/orchestration/files/hello-world.yaml" + name: "{{ stack_name }}" + +- name: List stacks + openstack.cloud.stack_info: + cloud: "{{ cloud }}" + register: stacks + +- assert: + that: + - stacks['stacks']|length > 0 + +- name: Get Single stack + openstack.cloud.stack_info: + cloud: "{{ cloud }}" + name: "{{ stack_name }}" + register: test_stack + +- assert: + that: + - test_stack is defined + - test_stack['stacks'][0]['name'] == stack_name + +- name: Delete stack + openstack.cloud.stack: + cloud: "{{ cloud }}" + name: "{{ stack_name }}" + state: absent + +- name: Get Single stack + openstack.cloud.stack_info: + cloud: "{{ cloud }}" + name: "{{ stack_name }}" + register: stacks + +- assert: + that: + - stacks is defined + - stacks['stacks']|length == 0 diff --git a/ci/roles/volume/tasks/main.yml b/ci/roles/volume/tasks/main.yml index 1fe425ea..db36c32a 100644 --- a/ci/roles/volume/tasks/main.yml +++ b/ci/roles/volume/tasks/main.yml @@ -68,4 +68,3 @@ display_name: ansible_volume - include_tasks: volume_info.yml - diff --git a/ci/run-collection.yml b/ci/run-collection.yml index 249cb06d..33865d3a 100644 --- a/ci/run-collection.yml +++ b/ci/run-collection.yml @@ -39,3 +39,6 @@ - { role: user_group, tags: user_group } - { role: user_role, tags: user_role } - { role: volume, tags: volume } + - role: orchestration + tags: orchestrate + when: sdk_version is version("0.53.0", '>=') diff --git a/plugins/modules/stack_info.py b/plugins/modules/stack_info.py new file mode 100644 index 00000000..289c7670 --- /dev/null +++ b/plugins/modules/stack_info.py @@ -0,0 +1,109 @@ +#!/usr/bin/python +# coding: utf-8 -*- + +# Copyright (c) 2020, Sagi Shnaidman +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +DOCUMENTATION = ''' +--- +module: stack_info +short_description: Retrive information about Heat stacks +author: OpenStack Ansible SIG +description: + - Get information about Heat stack in openstack +options: + name: + description: + - Name of the stack as a string. + type: str + required: false + status: + description: + - Value of the status of the stack so that you can filter on "available" for example + 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 = ''' +stacks: + description: List of dictionaries describing stacks. + type: list + elements: dict + returned: always. + contains: + id: + description: Unique UUID. + type: str + sample: "39007a7e-ee4f-4d13-8283-b4da2e037c69" + status: + description: Stack status. + 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(required=False, type='str'), + status=dict(required=False, type='str'), + project_id=dict(required=False, type='str'), + owner_id=dict(required=False, type='str') + ) + + def run(self): + data = [] + attrs = {} + + for param in ['name', 'status', 'project_id', 'owner_id']: + if self.params[param]: + attrs[param] = self.params[param] + + for raw in self.conn.orchestration.stacks(**attrs): + dt = raw.to_dict() + dt.pop('location') + data.append(dt) + + self.exit_json( + changed=False, + stacks=data + ) + + +def main(): + module = StackInfoModule() + module() + + +if __name__ == '__main__': + main()