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
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) # 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 __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.0', ANSIBLE_METADATA = {'metadata_version': '1.0',
@ -15,26 +15,30 @@ DOCUMENTATION = r'''
module: aci_tenant module: aci_tenant
short_description: Manage tenants on Cisco ACI fabrics short_description: Manage tenants on Cisco ACI fabrics
description: description:
- Manage tenants on a Cisco ACI fabric. - Manage tenants on Cisco ACI fabrics.
author: author:
- Swetha Chunduri (@schunduri) - Swetha Chunduri (@schunduri)
- Dag Wieers (@dagwieers) - Dag Wieers (@dagwieers)
- Jacob McGill (@jmcgill298)
version_added: '2.4' version_added: '2.4'
requirements: requirements:
- ACI Fabric 1.0(3f)+ - ACI Fabric 1.0(3f)+
options: options:
tenant_name: tenant:
description: description:
- The name of the tenant. - The name of the tenant.
required: yes required: yes
aliases: [ name, tenant_name ]
description: description:
description: description:
- Description for the Tenant. - Description for the tenant.
aliases: [ descr ]
state: state:
description: description:
- present, absent, query - Use C(present) or C(absent) for adding or removing.
default: present - Use C(query) for listing an object or multiple objects.
choices: [ absent, present, query ] choices: [ absent, present, query ]
default: present
extends_documentation_fragment: aci extends_documentation_fragment: aci
''' '''
@ -44,8 +48,8 @@ EXAMPLES = r'''
hostname: apic hostname: apic
username: admin username: admin
password: SomeSecretPassword password: SomeSecretPassword
tenant_name: Name of the tenant tenant: production
description: Description for the tenant description: Production tenant
state: present state: present
- name: Remove a tenant - name: Remove a tenant
@ -53,7 +57,7 @@ EXAMPLES = r'''
hostname: apic hostname: apic
username: admin username: admin
password: SomeSecretPassword password: SomeSecretPassword
tenant_name: Name of the tenant tenant: production
state: absent state: absent
- name: Query a tenant - name: Query a tenant
@ -61,7 +65,7 @@ EXAMPLES = r'''
hostname: apic hostname: apic
username: admin username: admin
password: SomeSecretPassword password: SomeSecretPassword
tenant_name: Name of the tenant tenant: production
state: query state: query
- name: Query all tenants - name: Query all tenants
@ -73,20 +77,9 @@ EXAMPLES = r'''
''' '''
RETURN = 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.aci import ACIModule, aci_argument_spec
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -94,7 +87,7 @@ from ansible.module_utils.basic import AnsibleModule
def main(): def main():
argument_spec = aci_argument_spec argument_spec = aci_argument_spec
argument_spec.update( 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']), description=dict(type='str', aliases=['descr']),
state=dict(type='str', default='present', choices=['absent', 'present', 'query']), 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 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, supports_check_mode=True,
) )
tenant_name = module.params['tenant_name'] tenant = module.params['tenant']
description = str(module.params['description']) description = module.params['description']
state = module.params['state'] state = module.params['state']
aci = ACIModule(module) aci = ACIModule(module)
if tenant_name is not None: if tenant is not None:
# Work with a specific tenant # Work with a specific object
path = 'api/mo/uni/tn-%(tenant_name)s.json' % module.params path = 'api/mo/uni/tn-%(tenant)s.json' % module.params
elif state == 'query': elif state == 'query':
# Query all tenants # Query all objects
path = 'api/node/class/fvTenant.json' path = 'api/class/fvTenant.json'
else: 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.result['url'] = '%(protocol)s://%(hostname)s/' % aci.params + path
aci.request(path)
elif module.check_mode: aci.get_existing()
# TODO: Implement proper check-mode (presence check)
aci.result(changed=True, response='OK (Check mode)', status=200) if state == 'present':
else: # Filter out module parameters with null values
payload = {'fvTenant': {'attributes': {'name': tenant_name, 'descr': description}}} aci.payload(aci_class='fvTenant', class_config=dict(name=tenant, descr=description))
aci.request_diff(path, payload=json.dumps(payload))
# 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) module.exit_json(**aci.result)
if __name__ == "__main__": if __name__ == "__main__":
main() main()