| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <[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_label |
| 16 |
short_description: Manage labels |
| 17 |
description: |
| 18 |
- Manage labels on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Dag Wieers (@dagwieers) |
| 21 |
options: |
| 22 |
label: |
| 23 |
description: |
| 24 |
- The name of the label. |
| 25 |
type: str |
| 26 |
aliases: [ name ] |
| 27 |
type: |
| 28 |
description: |
| 29 |
- The type of the label. |
| 30 |
type: str |
| 31 |
choices: [ site ] |
| 32 |
default: site |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Use C(present) or C(absent) for adding or removing. |
| 36 |
- Use C(query) for listing an object or multiple objects. |
| 37 |
type: str |
| 38 |
choices: [ absent, present, query ] |
| 39 |
default: present |
| 40 |
extends_documentation_fragment: cisco.mso.modules |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: Add a new label |
| 45 |
cisco.mso.mso_label: |
| 46 |
host: mso_host |
| 47 |
username: admin |
| 48 |
password: SomeSecretPassword |
| 49 |
label: Belgium |
| 50 |
type: site |
| 51 |
state: present |
| 52 |
|
| 53 |
- name: Remove a label |
| 54 |
cisco.mso.mso_label: |
| 55 |
host: mso_host |
| 56 |
username: admin |
| 57 |
password: SomeSecretPassword |
| 58 |
label: Belgium |
| 59 |
state: absent |
| 60 |
|
| 61 |
- name: Query a label |
| 62 |
cisco.mso.mso_label: |
| 63 |
host: mso_host |
| 64 |
username: admin |
| 65 |
password: SomeSecretPassword |
| 66 |
label: Belgium |
| 67 |
state: query |
| 68 |
register: query_result |
| 69 |
|
| 70 |
- name: Query all labels |
| 71 |
cisco.mso.mso_label: |
| 72 |
host: mso_host |
| 73 |
username: admin |
| 74 |
password: SomeSecretPassword |
| 75 |
state: query |
| 76 |
register: query_result |
| 77 |
""" |
| 78 |
|
| 79 |
RETURN = r""" |
| 80 |
""" |
| 81 |
|
| 82 |
from ansible.module_utils.basic import AnsibleModule |
| 83 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 84 |
|
| 85 |
|
| 86 |
def main(): |
| 87 |
argument_spec = mso_argument_spec() |
| 88 |
argument_spec.update( |
| 89 |
label=dict(type="str", aliases=["name"]), |
| 90 |
type=dict(type="str", default="site", choices=["site"]), |
| 91 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 92 |
) |
| 93 |
|
| 94 |
module = AnsibleModule( |
| 95 |
argument_spec=argument_spec, |
| 96 |
supports_check_mode=True, |
| 97 |
required_if=[ |
| 98 |
["state", "absent", ["label"]], |
| 99 |
["state", "present", ["label"]], |
| 100 |
], |
| 101 |
) |
| 102 |
|
| 103 |
label = module.params.get("label") |
| 104 |
label_type = module.params.get("type") |
| 105 |
state = module.params.get("state") |
| 106 |
|
| 107 |
mso = MSOModule(module) |
| 108 |
|
| 109 |
label_id = None |
| 110 |
path = "labels" |
| 111 |
|
| 112 |
# Query for existing object(s) |
| 113 |
if label: |
| 114 |
mso.existing = mso.get_obj(path, displayName=label) |
| 115 |
if mso.existing: |
| 116 |
label_id = mso.existing.get("id") |
| 117 |
# If we found an existing object, continue with it |
| 118 |
path = "labels/{id}".format(id=label_id) |
| 119 |
else: |
| 120 |
mso.existing = mso.query_objs(path) |
| 121 |
|
| 122 |
if state == "query": |
| 123 |
pass |
| 124 |
|
| 125 |
elif state == "absent": |
| 126 |
mso.previous = mso.existing |
| 127 |
if mso.existing: |
| 128 |
if module.check_mode: |
| 129 |
mso.existing = {} |
| 130 |
else: |
| 131 |
mso.existing = mso.request(path, method="DELETE") |
| 132 |
|
| 133 |
elif state == "present": |
| 134 |
mso.previous = mso.existing |
| 135 |
|
| 136 |
payload = dict( |
| 137 |
id=label_id, |
| 138 |
displayName=label, |
| 139 |
type=label_type, |
| 140 |
) |
| 141 |
|
| 142 |
mso.sanitize(payload, collate=True) |
| 143 |
|
| 144 |
if mso.existing: |
| 145 |
if mso.check_changed(): |
| 146 |
if module.check_mode: |
| 147 |
mso.existing = mso.proposed |
| 148 |
else: |
| 149 |
mso.existing = mso.request(path, method="PUT", data=mso.sent) |
| 150 |
else: |
| 151 |
if module.check_mode: |
| 152 |
mso.existing = mso.proposed |
| 153 |
else: |
| 154 |
mso.existing = mso.request(path, method="POST", data=mso.sent) |
| 155 |
|
| 156 |
mso.exit_json() |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__": |
| 160 |
main() |