#! /usr/local/bin/python

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

import os
import sys
import getopt
import isi.net.flexnet as flx

def usage(exitCode):
    sys.stderr.write('''
Usage: %s [OPTION]...

Options:
  -s, --subnets		List all subnet names
  -p, --pools		List all pool names
  -r, --rules		List all rule names
  -f, --full		List all names as "full names"
  -F, --short-and-full	List all names as "short names" and "full names"
  -c, --colon		List all names with an appended colon
''' % (os.path.basename(sys.argv[0])))
    sys.exit(exitCode)

def main():
    print_subnets = False
    print_pools = False
    print_rules = False
    full = False
    short = False
    short_and_full = False
    colon = False

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'sprfFc',
                ['subnets,', 'pools', 'rules', 'full', 'short-and-full', 'colon'])
        for o, a in opts:
            print_subnets = print_subnets or o in ['-s', '--subnets']
            print_pools = print_pools or o in ['-p', '--pools']
            print_rules = print_rules or o in ['-r', '--rules']
            full = full or o in ['-f', '--full'] or o in ['-F', '--short-and-full']
            short_and_full = short_and_full or o in ['-F', '--short-and-full']
            colon = colon or o in ['-c', '--colon']
        short = short_and_full or not full
            
    except getopt.GetoptError:
        usage(1)

    try:
        config = flx.config_try_load()
    except Exception, e:
        print >> sys.stderr, (str(e))
        sys.exit(1)

    subnets = config.subnets
    if colon:
        c = ":"
    else:
        c = ""
    if print_subnets:
        print "\n".join(map(lambda s: s.name + c, subnets))
            
    if print_pools:
        pools = []
        for s in subnets:
            for p in s.pools:
                if full:
                    pools.append("%s:%s%s" % (s.name, p.name, c))
                if short and pools.count(p.name) == 0:
                    pools.append(p.name + c)

        print "\n".join(pools)

    if print_rules:
        rules = []
        for s in subnets:
            for p in s.pools:
                for r in p.rules:
                    if full:
                        rules.append("%s:%s:%s%s" % (s.name, p.name, r.name, c))
                    if short and rules.count(r.name) == 0:
                        rules.append(r.name + c)
        print "\n".join(rules)

if __name__ == '__main__':
    main()
