#!/usr/bin/env python

# Copyright (c) 2009, Purdue University
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 
# Neither the name of the Purdue University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""make views and dns server set view assignments for roster"""


__copyright__ = 'Copyright (C) 2009, Purdue University'
__license__ = 'BSD'
__version__ = '0.17'


import sys

from roster_user_tools import cli_common_lib
from roster_user_tools import roster_client_lib
from roster_user_tools.action_flags import Make
from roster_user_tools.data_flags import View


class Args(Make, View):
  pass


def main(args):
  """Collects command line arguments.

  Inputs:
    args: list of arguments from the command line
  """
  command = None
  if( args and not args[0].startswith('-') ):
    command = args.pop(0)
  usage = ('\n'
           '\n'
           'To make views:\n'
           '\t%s view -v <view-name> -a <acl> \n'
           '\t[--allow/--deny] [-o <options>]\n'
           '\n'
           'To make dns server set view assignments:\n'
           '\t%s dns_server_set -v <view-name> \n'
           '\t-e <dns-server-set>\n'
           '\n'
           'To make acl view assignments:\n'
           '\t%s acl -v <view-name> -a <acl-name> '
           '[--allow/--deny]\n'
           '\n'
           'To make view assignments:\n'
           '\t%s view_subset -v <view-superset-name>\n'
           '\t-V <view-subset-name>\n' % tuple(
               [sys.argv[0] for _ in range(4)]))

  args_instance = Args(command,
      ['view', 'dns_server_set', 'acl', 'view_subset'], args, usage)
  options = args_instance.options

  try:
    cli_common_lib_instance = cli_common_lib.CliCommonLib(options)
  except cli_common_lib.ArgumentError, error:
    cli_common_lib.DnsError(str(error), 1)

  views = roster_client_lib.RunFunction(
      u'ListViews', options.username, credfile=options.credfile,
      credstring=options.credstring, server_name=options.server,
      kwargs={u'view_name': options.view_name})['core_return']

  if( command == 'view' ):
    if( options.acl and options.allow is None ):
      cli_common_lib.ArgumentError('Either --allow or --deny must be used.')
    acls = roster_client_lib.RunFunction(
        u'ListACLs', options.username, credfile=options.credfile,
        credstring=options.credstring, server_name=options.server,
        kwargs={u'acl_name': options.acl})['core_return']
    if( acls == {} ):
      cli_common_lib.DnsError(
          'ACL "%s" does not exist.' % options.acl, 1)
    else:
      allowed = 0
      if( options.allow ):
        allowed = 1

    if( options.view_name in views ):
      cli_common_lib.DnsError(
          'View "%s" already exists.' % options.view_name, 1)

    roster_client_lib.RunFunction(
        u'MakeView', options.username, credfile=options.credfile,
        credstring=options.credstring, server_name=options.server,
        args=[options.view_name, options.options])

    if( not options.quiet ):
      print 'ADDED VIEW: view_name: %s options %s' % (options.view_name,
                                                      options.options)
    roster_client_lib.RunFunction(
        u'MakeViewToACLAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server, args=[options.view_name, options.acl,
                                          allowed])
    if( not options.quiet ):
      print 'ADDED VIEW ACL ASSIGNMENT: view: %s acl: %s allowed: %s' % (
          options.view_name, options.acl, allowed)

  elif( command == 'dns_server_set' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    dns_server_sets = roster_client_lib.RunFunction(
        u'ListDnsServerSets', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server)['core_return']
    if( not options.dns_server_set in dns_server_sets ):
      cli_common_lib.DnsError(
          'Dns Server Set "%s" does not exist.' % options.dns_server_set, 1)
    roster_client_lib.RunFunction(
        u'MakeDnsServerSetViewAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        args=[options.view_name, options.dns_server_set])
    if( not options.quiet ):
      print ('ADDED DNS SERVER SET VIEW ASSIGNMENT: view_name: %s '
             'dns_server_set: %s' % (options.view_name, options.dns_server_set))

  elif( command == 'acl' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    view_acl_assignments = roster_client_lib.RunFunction(
        u'ListViewToACLAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        kwargs={'view_name': options.view_name, u'acl_name': options.acl})[
            'core_return']
    if( view_acl_assignments ):
      cli_common_lib.DnsError('View ACL Assignment already exists.', 1)

    allowed = 0
    if( options.allow ):
      allowed = 1
    roster_client_lib.RunFunction(
        u'MakeViewToACLAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server, args=[options.view_name, options.acl,
                                          allowed])
    if( not options.quiet ):
      print 'ADDED VIEW ACL ASSIGNMENT: view: %s acl: %s' % (
          options.view_name, options.acl)

  elif( command == 'view_subset' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    view_assignments = roster_client_lib.RunFunction(
        u'ListViewAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        kwargs={'view_superset': options.view_name,
                'view_subset': options.view_subset})['core_return']
    view_assignment_exists = True
    if( options.view_name not in view_assignments ):
      view_assignment_exists = False
    elif( options.view_subset not in view_assignments[options.view_name] ):
      view_assignment_exists = False

    if( view_assignment_exists ):
      cli_common_lib.DnsError('View assignment already exists.', 1)
    else:
      roster_client_lib.RunFunction(
          u'MakeViewAssignment', options.username,
          credfile=options.credfile, credstring=options.credstring,
          server_name=options.server,
          args=[options.view_name, options.view_subset])
      if( not options.quiet ):
        print 'ADDED VIEW ASSIGNMENT: view_name: %s view_subset: %s' % (
            options.view_name, options.view_subset)
  else:
    cli_common_lib.DnsError(
        'Command %s exists, but codepath doesn\'t.' % command, 1)

if __name__ == "__main__":
  main(sys.argv[1:])
