def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        device=dict(type='str', aliases=['device', 'host-name']),
        gather_subset=dict(type='list', options=subset_options),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, original_message='', message='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_device = Device(vmanage.auth, vmanage.host)
    vmanage_monitor = MonitorNetwork(vmanage.auth, vmanage.host)

    if vmanage.params['device']:
        # If we are passed in a device, we return specific facts about that device

        device_facts = {}
        # Check to see if we were passed in a device IP address or a device name
        try:
            system_ip = ipaddress.ip_address(vmanage.params['device'])
            device_status = vmanage_device.get_device_status(system_ip)
        except ValueError:
            device_status = vmanage_device.get_device_status(
                vmanage.params['device'], key='host-name')
            system_ip = device_status['system-ip']

        if device_status:
            if vmanage.params['gather_subset']:
                requested_subsets = vmanage.params['gather_subset']
            else:
                requested_subsets = subset_options
            device_facts['status'] = device_status
            if 'config' in requested_subsets:
                device_facts['config'] = vmanage_device.get_device_config(
                    device_status['device-type'], system_ip)
            if 'omp' in requested_subsets:
                omp_routes_received = vmanage_monitor.get_omp_routes_received(
                    system_ip)
                omp_routes_advertised = vmanage_monitor.get_omp_routes_advertised(
                    system_ip)
                omp_routes = {
                    'received': omp_routes_received,
                    'advertised': omp_routes_advertised,
                }
                omp = {'routes': omp_routes}
                device_facts['omp'] = omp
            if 'control' in requested_subsets:
                control_connections = vmanage_monitor.get_control_connections(
                    system_ip)
                control_connections_history = vmanage_monitor.get_control_connections_history(
                    system_ip)
                control = {
                    'connections': control_connections,
                    'connections_history': control_connections_history
                }
                device_facts['control'] = control
        vmanage.result['device_facts'] = device_facts
    else:
        if vmanage.params['gather_subset']:
            vmanage.fail_json(
                msg=
                "gather_subset argument can only be secified with device argument",
                **vmanage.result)
        # Otherwise, we return facts for all devices sorted by device type
        vmanage.result['vedges'] = vmanage_device.get_device_config_list(
            'vedges')
        vmanage.result['controllers'] = vmanage_device.get_device_config_list(
            'controllers')

    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'activated', 'deactivated'], default='present'),
                         name=dict(type='str', alias='policyName'),
                         description=dict(type='str', alias='policyDescription'),
                         definition=dict(type='str', alias='policyDefinition'),
                         type=dict(type='list', alias='policyType'),
                         wait=dict(type='bool', default=False),
                         update=dict(type='bool', default=False),
                         push=dict(type='bool', default=False),
                         aggregate=dict(type='list'),
                         )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    vmanage = Vmanage(module)
    vmanage_central_policy = CentralPolicy(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        policy_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'policyDescription': vmanage.params['description'],
                    'policyType': vmanage.params['type'],
                    'policyDefinition': vmanage.params['definition'],
                }
            ]
        else:
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'state': 'absent'
                }
            ]

    central_policy_updates = []
    if vmanage.params['state'] == 'present':
        central_policy_updates = vmanage_central_policy.import_central_policy_list(
            policy_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push']
        )
        if central_policy_updates:
            vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'absent':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        for policy in policy_list:
            if policy in central_policy_dict:
                if not module.check_mode:
                    vmanage_central_policy.delete_central_policy(central_policy_dict[policy['policyName']]['policyId'])
                vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'activated':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if vmanage.params['name'] in central_policy_dict:
            if not central_policy_dict[vmanage.params['name']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.activate_central_policy(vmanage.params['name'],
                                                                               central_policy_dict[vmanage.params['name']]['policyId'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')

        else:
            # TODO: The reference to 'policy' in the following line is wrong. What should it be?
            vmanage.fail_json(msg="Cannot find central policy {0}".format(vmanage.params['name']))
    if vmanage.params['state'] in ['absent', 'deactivated']:
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if policy['policyName'] in central_policy_dict:
            if central_policy_dict[policy['policyName']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.deactivate_central_policy(vmanage.params['name'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')
        else:
            vmanage.fail_json(msg="Cannot find central policy {0}".format(policy['policyName']))

    vmanage.params['updates'] = central_policy_updates
    vmanage.exit_json(**vmanage.result)