2019-01-15 12:14:34 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-25 14:27:55 +00:00
|
|
|
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <dag@wieers.com>
|
2019-01-15 12:14:34 +00:00
|
|
|
# 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'''
|
|
|
|
---
|
2019-01-16 13:53:38 +00:00
|
|
|
module: mso_schema
|
2019-01-15 12:14:34 +00:00
|
|
|
short_description: Manage schemas
|
|
|
|
description:
|
|
|
|
- Manage schemas on Cisco ACI Multi-Site.
|
|
|
|
author:
|
|
|
|
- Dag Wieers (@dagwieers)
|
|
|
|
version_added: '2.8'
|
|
|
|
options:
|
|
|
|
schema_id:
|
|
|
|
description:
|
|
|
|
- The ID of the schema.
|
|
|
|
type: str
|
|
|
|
required: yes
|
|
|
|
schema:
|
|
|
|
description:
|
|
|
|
- The name of the schema.
|
|
|
|
type: str
|
|
|
|
required: yes
|
|
|
|
aliases: [ name, schema_name ]
|
|
|
|
templates:
|
|
|
|
description:
|
|
|
|
- A list of templates for this schema.
|
|
|
|
type: list
|
|
|
|
sites:
|
|
|
|
description:
|
|
|
|
- A list of sites mapped to templates in this schema.
|
|
|
|
type: list
|
|
|
|
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
|
2019-01-24 13:39:06 +00:00
|
|
|
notes:
|
|
|
|
- This module cannot create empty schemas (i.e. schemas without templates).
|
|
|
|
Use the M(mso_schema_template) to automatically create schemas with templates.
|
|
|
|
seealso:
|
|
|
|
- module: mso_schema_site
|
|
|
|
- module: mso_schema_template
|
2019-01-16 13:53:38 +00:00
|
|
|
extends_documentation_fragment: mso
|
2019-01-15 12:14:34 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Add a new schema
|
2019-01-16 13:53:38 +00:00
|
|
|
mso_schema:
|
|
|
|
host: mso_host
|
2019-01-15 12:14:34 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
schema: Schema 1
|
|
|
|
state: present
|
|
|
|
templates:
|
|
|
|
- name: Template1
|
|
|
|
displayName: Template 1
|
|
|
|
tenantId: north_europe
|
|
|
|
anps:
|
|
|
|
<...>
|
|
|
|
- name: Template2
|
|
|
|
displayName: Template 2
|
|
|
|
tenantId: nort_europe
|
|
|
|
anps:
|
|
|
|
<...>
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Remove schemas
|
2019-01-16 13:53:38 +00:00
|
|
|
mso_schema:
|
|
|
|
host: mso_host
|
2019-01-15 12:14:34 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
schema: Schema 1
|
|
|
|
state: absent
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Query a schema
|
2019-01-16 13:53:38 +00:00
|
|
|
mso_schema:
|
|
|
|
host: mso_host
|
2019-01-15 12:14:34 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
schema: Schema 1
|
|
|
|
state: query
|
|
|
|
delegate_to: localhost
|
|
|
|
register: query_result
|
|
|
|
|
|
|
|
- name: Query all schemas
|
2019-01-16 13:53:38 +00:00
|
|
|
mso_schema:
|
|
|
|
host: mso_host
|
2019-01-15 12:14:34 +00:00
|
|
|
username: admin
|
|
|
|
password: SomeSecretPassword
|
|
|
|
state: query
|
|
|
|
delegate_to: localhost
|
|
|
|
register: query_result
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2019-01-16 13:53:38 +00:00
|
|
|
from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-01-16 13:53:38 +00:00
|
|
|
argument_spec = mso_argument_spec()
|
2019-01-15 12:14:34 +00:00
|
|
|
argument_spec.update(
|
|
|
|
schema=dict(type='str', required=False, aliases=['name', 'schema_name']),
|
|
|
|
schema_id=dict(type='str', required=False),
|
|
|
|
templates=dict(type='list'),
|
|
|
|
sites=dict(type='list'),
|
|
|
|
# messages=dict(type='dict'),
|
|
|
|
# associations=dict(type='list'),
|
|
|
|
# health_faults=dict(type='list'),
|
|
|
|
# references=dict(type='dict'),
|
|
|
|
# policy_states=dict(type='list'),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
required_if=[
|
|
|
|
['state', 'absent', ['schema']],
|
2019-01-24 13:39:06 +00:00
|
|
|
['state', 'present', ['schema', 'templates']],
|
2019-01-15 12:14:34 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
schema = module.params['schema']
|
|
|
|
schema_id = module.params['schema_id']
|
|
|
|
templates = module.params['templates']
|
|
|
|
sites = module.params['sites']
|
|
|
|
state = module.params['state']
|
|
|
|
|
2019-01-16 13:53:38 +00:00
|
|
|
mso = MSOModule(module)
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
path = 'schemas'
|
|
|
|
|
|
|
|
# Query for existing object(s)
|
|
|
|
if schema_id is None and schema is None:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.query_objs(path)
|
2019-01-15 12:14:34 +00:00
|
|
|
elif schema_id is None:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.get_obj(path, displayName=schema)
|
|
|
|
if mso.existing:
|
|
|
|
schema_id = mso.existing['id']
|
2019-01-15 12:14:34 +00:00
|
|
|
elif schema is None:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.get_obj(path, id=schema_id)
|
2019-01-15 12:14:34 +00:00
|
|
|
else:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.get_obj(path, id=schema_id)
|
|
|
|
existing_by_name = mso.get_obj(path, displayName=schema)
|
2019-01-15 12:14:34 +00:00
|
|
|
if existing_by_name and schema_id != existing_by_name['id']:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.fail_json(msg="Provided schema '{1}' with id '{2}' does not match existing id '{3}'.".format(schema, schema_id, existing_by_name['id']))
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
if schema_id:
|
|
|
|
path = 'schemas/{id}'.format(id=schema_id)
|
|
|
|
|
|
|
|
if state == 'query':
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif state == 'absent':
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.previous = mso.existing
|
|
|
|
if mso.existing:
|
2019-01-15 12:14:34 +00:00
|
|
|
if module.check_mode:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = {}
|
2019-01-15 12:14:34 +00:00
|
|
|
else:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.request(path, method='DELETE')
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
elif state == 'present':
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.previous = mso.existing
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
id=schema_id,
|
|
|
|
displayName=schema,
|
|
|
|
templates=templates,
|
|
|
|
sites=sites,
|
|
|
|
)
|
|
|
|
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.sanitize(payload, collate=True)
|
2019-01-15 12:14:34 +00:00
|
|
|
|
2019-01-16 13:53:38 +00:00
|
|
|
if mso.existing:
|
|
|
|
if not issubset(mso.sent, mso.existing):
|
2019-01-15 12:14:34 +00:00
|
|
|
if module.check_mode:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.proposed
|
2019-01-15 12:14:34 +00:00
|
|
|
else:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.request(path, method='PUT', data=mso.sent)
|
2019-01-15 12:14:34 +00:00
|
|
|
else:
|
|
|
|
if module.check_mode:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.proposed
|
2019-01-15 12:14:34 +00:00
|
|
|
else:
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.existing = mso.request(path, method='POST', data=mso.sent)
|
2019-01-15 12:14:34 +00:00
|
|
|
|
2019-01-16 13:53:38 +00:00
|
|
|
mso.exit_json()
|
2019-01-15 12:14:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|