Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Ansible module for appliance info #847

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions common/src/stack/ansible/plugins/modules/stacki_appliance_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# @copyright@
# Copyright (c) 2006 - 2020 Teradata
# All rights reserved. Stacki(r) v5.x stacki.com
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt
# @copyright@

DOCUMENTATION = """
module: stacki_appliance_info
short_description: Return data about Stacki appliances
description:
- If name is supplied, returns data about a single appliance
- If name is not supplied, returns data about all appliances in the system

options:
name:
description:
- The name of the appliance to return data about
required: false
"""

EXAMPLES = """
- name: Get appliance backend
stacki_appliance_info:
name: backend
register: result

- name: Get all appliances
stacki_appliance_info:
register: result
"""

RETURN = """
appliances:
description:
- List of appliances
returned: on success
type: complex
contains:
name:
description:
- Name of the appliance
type: str

public:
description:
- True if the appliance is considered public
type: bool
"""

from stack.bool import str2bool

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.stacki import run_stack_command, StackCommandError


def main():
# Define the arguments for this module
argument_spec = dict(
name=dict(type="str", required=False, default=None)
)

# Create our module object
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True
)

# Initialize a blank result
result = {
"changed": False,
"appliances": []
}

# Bail if the user is just checking syntax of their playbook
if module.check_mode:
module.exit_json(**result)

# Fetch our appliance info from Stacki
args = []
if module.params["name"]:
args.append(module.params["name"])

try:
for appliance in run_stack_command("list.appliance", args):
# Public needs to be a bool
appliance["public"] = str2bool(appliance["public"])

result["appliances"].append(appliance)

except StackCommandError as e:
# Fetching the data failed
module.fail_json(msg=e.message, **result)

# Return our data
module.exit_json(**result)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class TestStackiApplianceInfo:
def test_no_name(self, run_ansible_module):
result = run_ansible_module("stacki_appliance_info")

assert result.status == "SUCCESS"
assert result.data["changed"] == False

assert len(result.data["appliances"]) > 1

def test_with_name(self, run_ansible_module):
result = run_ansible_module("stacki_appliance_info", name="backend")

assert result.status == "SUCCESS"
assert result.data["changed"] == False
assert result.data["appliances"] == [{
"appliance": "backend",
"public": True
}]

def test_bad_name(self, run_ansible_module):
result = run_ansible_module("stacki_appliance_info", name="foo")

assert result.status == "FAILED!"
assert result.data["changed"] == False

assert "error" in result.data["msg"]
assert "not a valid appliance" in result.data["msg"]