| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Shreyas Srish (@shrsr) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: mso_schema_template_deploy_status |
| 16 |
short_description: Check query of objects before deployment to site |
| 17 |
description: |
| 18 |
- Check query of objects in a template of a schema |
| 19 |
author: |
| 20 |
- Shreyas Srish (@shrsr) |
| 21 |
options: |
| 22 |
schema: |
| 23 |
description: |
| 24 |
- The name of the schema. |
| 25 |
type: str |
| 26 |
aliases: [ name ] |
| 27 |
template: |
| 28 |
description: |
| 29 |
- The name of the template. |
| 30 |
type: str |
| 31 |
site: |
| 32 |
description: |
| 33 |
- The name of the site. |
| 34 |
type: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Use C(query) for listing query of objects. |
| 38 |
type: str |
| 39 |
choices: [ query ] |
| 40 |
default: query |
| 41 |
extends_documentation_fragment: cisco.mso.modules |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = r""" |
| 45 |
|
| 46 |
- name: Query status of objects in a template |
| 47 |
cisco.mso.mso_schema_template_deploy_status: |
| 48 |
host: mso_host |
| 49 |
username: admin |
| 50 |
password: SomeSecretPassword |
| 51 |
schema: Schema 1 |
| 52 |
template: Template 1 |
| 53 |
state: query |
| 54 |
register: query_result |
| 55 |
|
| 56 |
- name: Query status of objects using site |
| 57 |
cisco.mso.mso_schema_template_deploy_status: |
| 58 |
host: mso_host |
| 59 |
username: admin |
| 60 |
password: SomeSecretPassword |
| 61 |
schema: Schema 1 |
| 62 |
site: ansible_test |
| 63 |
state: query |
| 64 |
register: query_result |
| 65 |
|
| 66 |
- name: Query status of objects in a template associated with a site |
| 67 |
cisco.mso.mso_schema_template_deploy_status: |
| 68 |
host: mso_host |
| 69 |
username: admin |
| 70 |
password: SomeSecretPassword |
| 71 |
schema: Schema 1 |
| 72 |
template: Template 1 |
| 73 |
site: ansible_test |
| 74 |
state: query |
| 75 |
register: query_result |
| 76 |
|
| 77 |
- name: Query status of objects in all templates |
| 78 |
cisco.mso.mso_schema_template_deploy_status: |
| 79 |
host: mso_host |
| 80 |
username: admin |
| 81 |
password: SomeSecretPassword |
| 82 |
schema: Schema 1 |
| 83 |
state: query |
| 84 |
register: query_result |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = r""" |
| 88 |
""" |
| 89 |
|
| 90 |
from ansible.module_utils.basic import AnsibleModule |
| 91 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 92 |
|
| 93 |
|
| 94 |
def main(): |
| 95 |
argument_spec = mso_argument_spec() |
| 96 |
argument_spec.update( |
| 97 |
schema=dict(type="str", aliases=["name"]), |
| 98 |
template=dict(type="str"), |
| 99 |
site=dict(type="str"), |
| 100 |
state=dict(type="str", default="query", choices=["query"]), |
| 101 |
) |
| 102 |
|
| 103 |
module = AnsibleModule( |
| 104 |
argument_spec=argument_spec, |
| 105 |
supports_check_mode=True, |
| 106 |
required_if=[ |
| 107 |
["state", "query", ["schema"]], |
| 108 |
], |
| 109 |
) |
| 110 |
|
| 111 |
schema = module.params.get("schema") |
| 112 |
template = module.params.get("template") |
| 113 |
if template is not None: |
| 114 |
template = template.replace(" ", "") |
| 115 |
site = module.params.get("site") |
| 116 |
state = module.params.get("state") |
| 117 |
|
| 118 |
mso = MSOModule(module) |
| 119 |
|
| 120 |
schema_id = None |
| 121 |
path = "schemas" |
| 122 |
|
| 123 |
get_schema = mso.get_obj(path, displayName=schema) |
| 124 |
if get_schema: |
| 125 |
schema_id = get_schema.get("id") |
| 126 |
path = "schemas/{id}/policy-states".format(id=schema_id) |
| 127 |
else: |
| 128 |
mso.fail_json(msg="Schema '{0}' not found.".format(schema)) |
| 129 |
|
| 130 |
if state == "query": |
| 131 |
get_data = mso.request(path, method="GET") |
| 132 |
mso.existing = [] |
| 133 |
if template: |
| 134 |
for configuration_objects in get_data.get("policyStates"): |
| 135 |
if configuration_objects.get("templateName") == template: |
| 136 |
mso.existing.append(configuration_objects) |
| 137 |
if not mso.existing: |
| 138 |
mso.fail_json(msg="Template '{0}' not found.".format(template)) |
| 139 |
|
| 140 |
if site: |
| 141 |
mso.existing.clear() |
| 142 |
for configuration_objects in get_data.get("policyStates"): |
| 143 |
if configuration_objects.get("siteId") == mso.lookup_site(site): |
| 144 |
if template: |
| 145 |
if configuration_objects.get("templateName") == template: |
| 146 |
mso.existing = configuration_objects |
| 147 |
else: |
| 148 |
mso.existing.append(configuration_objects) |
| 149 |
if template is not None and not mso.existing: |
| 150 |
mso.fail_json(msg="Provided Template '{0}' not associated with Site '{1}'.".format(template, site)) |
| 151 |
|
| 152 |
if template is None and site is None: |
| 153 |
mso.existing = get_data |
| 154 |
|
| 155 |
mso.exit_json() |
| 156 |
|
| 157 |
|
| 158 |
if __name__ == "__main__": |
| 159 |
main() |