def main():
    """
    Main function

    :returns: Server Information
    """
    module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'),
                                              region=dict(default='na',
                                                          type='str'),
                                              datacenter=dict(required=True,
                                                              type='str'),
                                              network_domain=dict(
                                                  required=False, type='str'),
                                              vlan=dict(default=None,
                                                        required=False,
                                                        type='str'),
                                              name=dict(required=False,
                                                        type='str'),
                                              id=dict(required=False,
                                                      type='str')),
                           supports_check_mode=True)

    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))
    return_data = return_object('server')
    name = module.params.get('name')
    server_id = module.params.get('id')
    datacenter = module.params.get('datacenter')
    network_domain_name = module.params.get('network_domain')
    vlan_name = module.params.get('vlan')
    network_domain_id = vlan_id = None

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Get the CND object based on the supplied name
    try:
        if network_domain_name:
            network_domain = client.get_network_domain_by_name(
                name=network_domain_name, datacenter=datacenter)
            network_domain_id = network_domain.get('id')
        else:
            network_domain_id = None
        if network_domain_name and not network_domain:
            module.fail_json(
                msg='Failed to locate the Cloud Network Domain - {0}'.format(
                    network_domain_name))
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException):
        module.fail_json(msg='Failed to locate the Cloud Network Domain - {0}'.
                         format(network_domain_name))

    # Get the VLAN object based on the supplied name
    try:
        if vlan_name:
            vlan = client.get_vlan_by_name(name=vlan_name,
                                           datacenter=datacenter,
                                           network_domain_id=network_domain_id)
            vlan_id = vlan.get('id')
        else:
            vlan_id = None
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException):
        module.fail_json(
            msg='Failed to locate the VLAN - {0}'.format(vlan_name))

    try:
        if server_id:
            server = client.get_server_by_id(server_id=server_id)
            if server:
                return_data['server'].append(server)
        elif name:
            server = client.get_server_by_name(
                datacenter=datacenter,
                network_domain_id=network_domain_id,
                name=name)
            if server:
                return_data['server'].append(server)
        else:
            servers = client.list_servers(datacenter, network_domain_id,
                                          vlan_id, name)
            return_data['server'] = servers
    except (KeyError, IndexError, AttributeError):
        module.fail_json(msg='Could not find the server - {0} in {1}'.format(
            name, datacenter))

    return_data['count'] = len(return_data.get('server'))

    module.exit_json(data=return_data)
def main():
    """
    Main function
    :returns: Snapshot Information
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        datacenter=dict(required=True, type='str'),
        network_domain=dict(required=False, type='str'),
        network_domain_id=dict(required=False, type='str'),
        server=dict(required=False, type='str'),
        server_id=dict(required=False, type='str'),
        plan=dict(required=False, default=None, type='str'),
        window=dict(required=False, default=None, type='int'),
        replication=dict(required=False, default=None, type='str'),
        take_snapshot=dict(required=False, default=False, type='bool'),
        state=dict(default='present', choices=['present', 'absent'])),
                           supports_check_mode=True)
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    state = module.params.get('state')
    datacenter = module.params.get('datacenter')
    network_domain_name = module.params.get('network_domain')
    server_name = module.params.get('server')
    server_id = module.params.get('server_id')
    plan = module.params.get('plan')
    window_id = None
    network_domain_id = module.params.get('network_domain_id')
    take_snapshot = module.params.get('take_snapshot')

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Get the CND
    if server_id is None and network_domain_id is None:
        try:
            network = client.get_network_domain_by_name(
                name=network_domain_name, datacenter=datacenter)
            network_domain_id = network.get('id')
        except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
            module.fail_json(
                msg='Could not find the Cloud Network Domain: {0}'.format(e))

    # Check if the Server exists based on the supplied name
    try:
        if server_name is None and server_id is None:
            module.fail_json(
                msg='A valid value for server or server_id is required')
        if server_id:
            server = client.get_server_by_id(server_id=server_id)
        else:
            server = client.get_server_by_name(
                datacenter=datacenter,
                network_domain_id=network_domain_id,
                name=server_name)
        if not server.get('id'):
            raise NTTMCPAPIException('No server object found for {0}'.format(
                server_name or server_id))
        server_id = server.get('id')
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(
            msg='Could not locate any existing server - {0}'.format(e))

    if state == 'present':
        # Check for required arguments
        if (module.params.get('plan') is None or module.params.get('window') is
                None) and module.params.get('replication') is None:
            module.fail_json(msg='plan and window are required arguments')
        # Attempt to find the Window for the specified Service Plan
        if not module.check_mode and (module.params.get('window')
                                      or module.params.get('plan')):
            window_id = get_window_id(module, client)
        if not server.get('snapshotService'):
            if module.check_mode:
                module.exit_json(
                    msg=
                    'Input verified, Snapshots can be enabled for the server')
            enable_snapshot(module, client, server_id, plan, window_id,
                            module.params.get('replication'), take_snapshot)
        else:
            result = compare_snapshot(module, server.get('snapshotService'))
            if True in result:
                if result[0]:
                    update_snapshot(module, client, server_id, plan, window_id)
                if result[1]:
                    enable_replication(module, client, server_id,
                                       module.params.get('replication'))
                module.exit_json(
                    changed=True,
                    msg=
                    'Snapshot Service configuration has been successfully updated'
                )
            else:
                module.exit_json(msg='No update required.')
    elif state == 'absent':
        if not server.get('snapshotService'):
            module.exit_json(
                msg='Snapshots are not currently configured for this server')
        if module.check_mode:
            module.exit_json(
                msg=
                'The Snapshot service and all associated snapshots will be removed from this server'
            )
        if module.params.get('replication'):
            disable_replication(module, client, server_id)
        else:
            disable_snapshot(module, client, server_id)
        module.exit_json(msg='Snapshot replication successfully disabled')