From 94098269828e37337e72c796c19e038f836222a5 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 24 Jan 2019 16:53:18 +0100 Subject: [PATCH] mso_st_externalepg: Manage external EPGs in templates (#51285) A new module to manage external EPGs in MSO schema templates. --- .../aci/mso_schema_template_externalepg.py | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 lib/ansible/modules/network/aci/mso_schema_template_externalepg.py diff --git a/lib/ansible/modules/network/aci/mso_schema_template_externalepg.py b/lib/ansible/modules/network/aci/mso_schema_template_externalepg.py new file mode 100644 index 0000000000..42537705c3 --- /dev/null +++ b/lib/ansible/modules/network/aci/mso_schema_template_externalepg.py @@ -0,0 +1,218 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2018, Dag Wieers (@dagwieers) +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = r''' +--- +module: mso_schema_template_externalepg +short_description: Manage external EPGs in schema templates +description: +- Manage external EPGs in schema templates on Cisco ACI Multi-Site. +author: +- Dag Wieers (@dagwieers) +version_added: '2.8' +options: + schema: + description: + - The name of the schema. + type: str + required: yes + template: + description: + - The name of the template. + type: list + externalepg: + description: + - The name of the external EPG to manage. + type: str + aliases: [ name ] + display_name: + description: + - The name as displayed on the MSO web interface. + type: str + vrf: + description: + - The VRF associated to this ANP. + type: str + state: + description: + - Use C(present) or C(absent) for adding or removing. + - Use C(query) for listing an object or multiple objects. + type: str + choices: [ absent, present, query ] + default: present +extends_documentation_fragment: mso +''' + +EXAMPLES = r''' +- name: Add a new external EPG + mso_schema_template_externalepg: + host: mso_host + username: admin + password: SomeSecretPassword + schema: Schema 1 + template: Template 1 + externalepg: External EPG 1 + state: present + delegate_to: localhost + +- name: Remove an external EPG + mso_schema_template_externalepg: + host: mso_host + username: admin + password: SomeSecretPassword + schema: Schema 1 + template: Template 1 + externalepg: external EPG1 + state: absent + delegate_to: localhost + +- name: Query a specific external EPGs + mso_schema_template_externalepg: + host: mso_host + username: admin + password: SomeSecretPassword + schema: Schema 1 + template: Template 1 + externalepg: external EPG1 + state: query + delegate_to: localhost + register: query_result + +- name: Query all external EPGs + mso_schema_template_externalepg: + host: mso_host + username: admin + password: SomeSecretPassword + schema: Schema 1 + template: Template 1 + state: query + delegate_to: localhost + register: query_result +''' + +RETURN = r''' +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, mso_reference_spec, issubset + + +def main(): + argument_spec = mso_argument_spec() + argument_spec.update( + schema=dict(type='str', required=True), + template=dict(type='str', required=True), + externalepg=dict(type='str', required=False, aliases=['name']), # This parameter is not required for querying all objects + display_name=dict(type='str'), + vrf=dict(type='dict', options=mso_reference_spec()), + state=dict(type='str', default='present', choices=['absent', 'present', 'query']), + ) + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_if=[ + ['state', 'absent', ['externalepg']], + ['state', 'present', ['externalepg', 'vrf']], + ], + ) + + schema = module.params['schema'] + template = module.params['template'] + externalepg = module.params['externalepg'] + display_name = module.params['display_name'] + vrf = module.params['vrf'] + state = module.params['state'] + + mso = MSOModule(module) + + # Get schema_id + schema_obj = mso.get_obj('schemas', displayName=schema) + if schema_obj: + schema_id = schema_obj['id'] + else: + mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema)) + + path = 'schemas/{id}'.format(id=schema_id) + + # Get template + templates = [t['name'] for t in schema_obj['templates']] + if template not in templates: + mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates))) + template_idx = templates.index(template) + + # Get external EPGs + externalepgs = [e['name'] for e in schema_obj['templates'][template_idx]['externalEpgs']] + + if externalepg is not None and externalepg in externalepgs: + externalepg_idx = externalepgs.index(externalepg) + mso.existing = schema_obj['templates'][template_idx]['externalEpgs'][externalepg_idx] + + if state == 'query': + if externalepg is None: + mso.existing = schema_obj['templates'][template_idx]['externalEpgs'] + elif not mso.existing: + mso.fail_json(msg="External EPG '{externalepg}' not found".format(externalepg=externalepg)) + mso.exit_json() + + mso.previous = mso.existing + if state == 'absent': + if mso.existing: + mso.sent = mso.existing = {} + operation = [dict( + op='remove', + path='/templates/{template}/externalEpgs/{externalepg}'.format(template=template, externalepg=externalepg), + )] + if not module.check_mode: + mso.request(path, method='PATCH', data=operation) + + elif state == 'present': + vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template) + + if display_name is None and not mso.existing: + display_name = externalepg + + payload = dict( + name=externalepg, + displayName=display_name, + vrfRef=vrf_ref, + # FIXME + subnets=[], + contractRelationships=[], + ) + + mso.sanitize(payload, collate=True) + + if mso.existing: + operation = [dict( + op='replace', + path='/templates/{template}/externalEpgs/{externalepg}'.format(template=template, externalepg=externalepg), + value=mso.sent, + )] + + else: + operation = [dict( + op='add', + path='/templates/{template}/externalEpgs/-'.format(template=template), + value=mso.sent, + )] + + mso.existing = mso.proposed + if not module.check_mode: + mso.request(path, method='PATCH', data=operation) + + mso.exit_json() + + +if __name__ == "__main__": + main()