# -*- coding: utf-8 -*-
'''
Management of NFS exports
===============================================

To ensure an NFS export exists:

.. code-block:: yaml

    add_simple_export:
      nfs_export.present:
        - name:     '/srv/nfs'
        - hosts:    '10.0.2.0/24'
        - options:
          - 'rw'

This creates the following in /etc/exports:

.. code-block:: bash

    /srv/nfs 10.0.2.0/24(rw)

For more complex exports with multiple groups of hosts, use 'clients':

.. code-block:: yaml

    add_complex_export:
      nfs_export.present:
        - name: '/srv/nfs'
        - clients:
          # First export, same as simple one above
          - hosts: '10.0.2.0/24'
            options:
              - 'rw'
          # Second export
          - hosts: '*.example.com'
            options:
              - 'ro'
              - 'subtree_check'

This creates the following in /etc/exports:

.. code-block:: bash

    /srv/nfs 10.0.2.0/24(rw) 192.168.0.0/24,172.19.0.0/16(ro,subtree_check)

Any export of the given path will be modified to match the one specified.

To ensure an NFS export is absent:

.. code-block:: yaml

    delete_export:
      nfs_export.absent:
        - name: '/srv/nfs'

'''

def present(name, clients=None, hosts=None, options=None, exports='/etc/exports'):
    '''
    Ensure that the named export is present with the given options

    name
        The export path to configure

    clients
        A list of hosts and the options applied to them.
        This option may not be used in combination with
        the 'hosts' or 'options' shortcuts.

    .. code-block:: yaml

        - clients:
          # First export
          - hosts: '10.0.2.0/24'
            options:
              - 'rw'
          # Second export
          - hosts: '*.example.com'
            options:
              - 'ro'
              - 'subtree_check'

    hosts
        A string matching a number of hosts, for example:

    .. code-block:: yaml

        hosts: '10.0.2.123'

        hosts: '10.0.2.0/24'

        hosts: 'minion1.example.com'

        hosts: '*.example.com'

        hosts: '*'

    options
        A list of NFS options, for example:

    .. code-block:: yaml

        options:
          - 'rw'
          - 'subtree_check'

    '''


def absent(name, exports='/etc/exports'):
    '''
    Ensure that the named path is not exported

    name
        The export path to remove
    '''

    path = name
    ret = {'name': name,
           'changes': {},
           'result': None,
           'comment': ''}

    old = __salt__['nfs3.list_exports'](exports)
    if path in old:
        if __opts__['test']:
            ret['comment']  = 'Export {0} would be removed'.format(path)
            ret['result']   = None
            return ret

        __salt__['nfs3.del_export'](exports, path)
        ret['comment']  = 'Export {0} removed'.format(path)
        ret['changes'][path] = old[path]
        ret['result']   = True
    else:
        ret['comment'] = 'Export {0} already absent'.format(path)
        ret['result']   = True

    return ret

@@ -2260,16 +2260,1386 @@
     '''%0A
+    path = name%0A    ret = %7B'name': name,%0A           'changes': %7B%7D,%0A           'result': None,%0A           'comment': ''%7D%0A%0A    if not clients:%0A        if not hosts:%0A            ret%5B'result'%5D   = False%0A            ret%5B'comment'%5D  = 'Either %5C'clients%5C' or %5C'hosts%5C' must be defined'%0A            return ret%0A        # options being None is handled by add_export()%0A        clients = %5B%7B'hosts': hosts, 'options': options%7D%5D%0A%0A    old = __salt__%5B'nfs3.list_exports'%5D(exports)%0A    if path in old:%0A        if old%5Bpath%5D == clients:%0A            ret%5B'result'%5D   = True%0A            ret%5B'comment'%5D  = 'Export %7B0%7D already configured'.format(path)%0A            return ret%0A%0A        ret%5B'changes'%5D%5B'new'%5D = clients%0A        ret%5B'changes'%5D%5B'old'%5D = old%5Bpath%5D%0A        if __opts__%5B'test'%5D:%0A            ret%5B'result'%5D = None%0A            ret%5B'comment'%5D  = 'Export %7B0%7D would be changed'.format(path)%0A            return ret%0A%0A        __salt__%5B'nfs3.del_export'%5D(exports, path)%0A%0A    else:%0A        ret%5B'changes'%5D%5B'old'%5D = None%0A        ret%5B'changes'%5D%5B'new'%5D = clients%0A        if __opts__%5B'test'%5D:%0A            ret%5B'result'%5D = None%0A            ret%5B'comment'%5D  = 'Export %7B0%7D would be added'.format(path)%0A            return ret%0A%0A    for exp in clients:%0A        __salt__%5B'nfs3.add_export'%5D(exports, path, exp%5B'hosts'%5D, exp%5B'options'%5D)%0A%0A    ret%5B'result'%5D = True%0A    ret%5B'changes'%5D%5B'new'%5D = clients%0A    return ret
 %0A%0Adef ab

add present()