Update aci_tenant to support check_mode (#28090)

This commit is contained in:
Jacob McGill 2017-08-11 18:51:49 -04:00 committed by ansibot
parent c04349088e
commit 8b92369678

View file

@ -3,7 +3,7 @@
# 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)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.0',
@ -15,26 +15,30 @@ DOCUMENTATION = r'''
module: aci_tenant
short_description: Manage tenants on Cisco ACI fabrics
description:
- Manage tenants on a Cisco ACI fabric.
- Manage tenants on Cisco ACI fabrics.
author:
- Swetha Chunduri (@schunduri)
- Dag Wieers (@dagwieers)
- Jacob McGill (@jmcgill298)
version_added: '2.4'
requirements:
- ACI Fabric 1.0(3f)+
options:
tenant_name:
tenant:
description:
- The name of the tenant.
required: yes
aliases: [ name, tenant_name ]
description:
description:
- Description for the Tenant.
- Description for the tenant.
aliases: [ descr ]
state:
description:
- present, absent, query
default: present
- Use C(present) or C(absent) for adding or removing.
- Use C(query) for listing an object or multiple objects.
choices: [ absent, present, query ]
default: present
extends_documentation_fragment: aci
'''
@ -44,8 +48,8 @@ EXAMPLES = r'''
hostname: apic
username: admin
password: SomeSecretPassword
tenant_name: Name of the tenant
description: Description for the tenant
tenant: production
description: Production tenant
state: present
- name: Remove a tenant
@ -53,7 +57,7 @@ EXAMPLES = r'''
hostname: apic
username: admin
password: SomeSecretPassword
tenant_name: Name of the tenant
tenant: production
state: absent
- name: Query a tenant
@ -61,7 +65,7 @@ EXAMPLES = r'''
hostname: apic
username: admin
password: SomeSecretPassword
tenant_name: Name of the tenant
tenant: production
state: query
- name: Query all tenants
@ -73,20 +77,9 @@ EXAMPLES = r'''
'''
RETURN = r'''
status:
description: The status code of the http request
returned: upon making a successful GET, POST or DELETE request to the APIC
type: int
sample: 200
response:
description: Response text returned by APIC
returned: when a HTTP request has been made to APIC
type: string
sample: '{"totalCount":"0","imdata":[]}'
#
'''
import json
from ansible.module_utils.aci import ACIModule, aci_argument_spec
from ansible.module_utils.basic import AnsibleModule
@ -94,7 +87,7 @@ from ansible.module_utils.basic import AnsibleModule
def main():
argument_spec = aci_argument_spec
argument_spec.update(
tenant_name=dict(type='str', aliases=['name']),
tenant=dict(type='str', required=False, aliases=['name', 'tenant_name']), # Not required for querying all objects
description=dict(type='str', aliases=['descr']),
state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
method=dict(type='str', choices=['delete', 'get', 'post'], aliases=['action'], removed_in_version='2.6'), # Deprecated starting from v2.6
@ -105,31 +98,40 @@ def main():
supports_check_mode=True,
)
tenant_name = module.params['tenant_name']
description = str(module.params['description'])
tenant = module.params['tenant']
description = module.params['description']
state = module.params['state']
aci = ACIModule(module)
if tenant_name is not None:
# Work with a specific tenant
path = 'api/mo/uni/tn-%(tenant_name)s.json' % module.params
if tenant is not None:
# Work with a specific object
path = 'api/mo/uni/tn-%(tenant)s.json' % module.params
elif state == 'query':
# Query all tenants
path = 'api/node/class/fvTenant.json'
# Query all objects
path = 'api/class/fvTenant.json'
else:
module.fail_json("Parameter 'tenant_name' is required for state 'absent' or 'present'")
module.fail_json(msg="Parameter 'tenant' is required for state 'absent' or 'present'")
if state == 'query':
aci.request(path)
elif module.check_mode:
# TODO: Implement proper check-mode (presence check)
aci.result(changed=True, response='OK (Check mode)', status=200)
else:
payload = {'fvTenant': {'attributes': {'name': tenant_name, 'descr': description}}}
aci.request_diff(path, payload=json.dumps(payload))
aci.result['url'] = '%(protocol)s://%(hostname)s/' % aci.params + path
aci.get_existing()
if state == 'present':
# Filter out module parameters with null values
aci.payload(aci_class='fvTenant', class_config=dict(name=tenant, descr=description))
# Generate config diff which will be used as POST request body
aci.get_diff(aci_class='fvTenant')
# Submit changes if module not in check_mode and the proposed is different than existing
aci.post_config()
elif state == 'absent':
aci.delete_config()
module.exit_json(**aci.result)
if __name__ == "__main__":
main()