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