.. versionadded:: 2014.7.0

    Configure the global primary DNS suffix of a DHCP client.

    suffix : None
        The suffix which is advertised for this client when acquiring a DHCP lease
        When none is set, the explicitly configured DNS suffix will be removed.

    updates : False
        Allow syncing the DNS suffix with the AD domain when the client's AD domain membership changes

    .. code-block:: yaml

        primary_dns_suffix:
            win_dns_client.primary_suffix:
                - suffix: sub.domain.tld
                - updates: True

def primary_suffix(name,
        suffix=None,
        updates=False):
    '''
    .. versionadded:: 2014.7.0

    Configure the global primary DNS suffix of a DHCP client.

    suffix : None
        The suffix which is advertised for this client when acquiring a DHCP lease
        When none is set, the explicitly configured DNS suffix will be removed.

    updates : False
        Allow syncing the DNS suffix with the AD domain when the client's AD domain membership changes

    .. code-block:: yaml

        primary_dns_suffix:
            win_dns_client.primary_suffix:
                - suffix: sub.domain.tld
                - updates: True
    '''

    ret = {
            'name': name,
            'changes': {},
            'result': True,
            'comment': 'No changes needed'
    }

    suffix = str(suffix)

    if not isinstance(updates, bool):
        ret['result'] = False
        ret['comment'] = '\'updates\' must be a boolean value'
        return ret

    # TODO: waiting for an implementation of
    # https://github.com/saltstack/salt/issues/6792 to be able to handle the
    # requirement for a reboot to actually apply this state.
    # Until then, this method will only be able to verify that the required
    # value has been written to the registry and rebooting needs to be handled
    # manually

    reg_data = {
            'suffix': {
                'hive': 'HKEY_LOCAL_MACHINE',
                'key': r'SYSTEM\CurrentControlSet\services\Tcpip\Parameters',
                'vname':  'NV Domain',
                'vtype': 'REG_SZ',
                'old':  None,
                'new':  suffix
            },
            'updates': {
                'hive': 'HKEY_LOCAL_MACHINE',
                'key': r'SYSTEM\CurrentControlSet\services\Tcpip\Parameters',
                'vname':  'SyncDomainWithMembership',
                'vtype': 'REG_DWORD',
                'old':  None,
                'new':  updates
            }
    }

    reg_data['suffix']['old'] = __utils__['reg.read_value'](
            reg_data['suffix']['hive'],
            reg_data['suffix']['key'],
            reg_data['suffix']['vname'],)['vdata']

    reg_data['updates']['old'] = bool(__utils__['reg.read_value'](
            reg_data['updates']['hive'],
            reg_data['updates']['key'],
            reg_data['updates']['vname'],)['vdata'])

    updates_operation = 'enabled' if reg_data['updates']['new'] else 'disabled'

    # No changes to suffix needed
    if reg_data['suffix']['new'] == reg_data['suffix']['old']:
        # No changes to updates policy needed
        if reg_data['updates']['new'] == reg_data['updates']['old']:
            return ret
        # Changes to update policy needed
        else:
            ret['comment'] = '{0} suffix updates'.format(updates_operation)
            ret['changes'] = {
                    'old': {
                        'updates': reg_data['updates']['old']},
                    'new': {
                        'updates': reg_data['updates']['new']}}
    # Changes to suffix needed
    else:
        # Changes to updates policy needed
        if reg_data['updates']['new'] != reg_data['updates']['old']:
            ret['comment'] = 'Updated primary DNS suffix ({0}) and {1} suffix updates'.format(suffix, updates_operation)
            ret['changes'] = {
                    'old': {
                        'suffix': reg_data['suffix']['old'],
                        'updates': reg_data['updates']['old']},
                    'new': {
                        'suffix': reg_data['suffix']['new'],
                        'updates': reg_data['updates']['new']}}
        # No changes to updates policy needed
        else:
            ret['comment'] = 'Updated primary DNS suffix ({0})'.format(suffix)
            ret['changes'] = {
                    'old': {
                        'suffix': reg_data['suffix']['old']},
                    'new': {
                        'suffix': reg_data['suffix']['new']}}

    suffix_result = __utils__['reg.set_value'](
            reg_data['suffix']['hive'],
            reg_data['suffix']['key'],
            reg_data['suffix']['vname'],
            reg_data['suffix']['new'],
            reg_data['suffix']['vtype'])

    updates_result = __utils__['reg.set_value'](
            reg_data['updates']['hive'],
            reg_data['updates']['key'],
            reg_data['updates']['vname'],
            reg_data['updates']['new'],
            reg_data['updates']['vtype'])

    ret['result'] = suffix_result & updates_result

    return ret