4 from sfa.generic import Generic
5 from optparse import OptionParser
7 from sfa.util.xrn import Xrn
9 def args(*args, **kwargs):
11 func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
15 class Commands(object):
17 def _get_commands(self):
18 available_methods = []
19 for attrib in dir(self):
20 if callable(getattr(self, attrib)) and not attrib.startswith('_'):
21 available_methods.append(attrib)
22 return available_methods
24 class RegistryCommands(Commands):
26 def __init__(self, *args, **kwds):
27 self.api= Generic.the_flavour().make_api(interface='registry')
32 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
33 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
34 def list(self, xrn, type=None):
36 records = self.api.manager.List(self.api, xrn.get_hrn())
37 for record in records:
38 if not type or record['type'] == type:
39 print "%s (%s)" % (record['hrn'], record['type'])
41 def show(self, xrn, type=None, full=True):
42 records = self.api.manager.Resolve(self.api, xrn, type, full)
44 def register(self, record):
47 def update(self, record):
50 def remove(self, xrn):
53 def credential(self, xrn):
57 class CerficiateCommands(Commands):
59 def import_records(self, xrn):
62 def export(self, xrn):
66 def display(self, xrn):
71 class AggregateCommands(Commands):
73 def __init__(self, *args, **kwds):
74 self.api= Generic.the_flavour().make_api(interface='aggregate')
82 def status(self, xrn):
85 def resources(self, xrn):
88 def create(self, xrn, rspec):
91 def delete(self, xrn):
100 def reset(self, xrn):
107 class SliceManagerCommands(AggregateCommands):
109 def __init__(self, *args, **kwds):
110 self.api= Generic().make_api(interface='slicemgr')
113 CATEGORIES = {'registry': RegistryCommands,
114 'aggregate': AggregateCommands,
115 'slicemgr': SliceManagerCommands}
118 argv = copy.deepcopy(sys.argv)
119 script_name = argv.pop(0)
121 print script_name + " category action [<args>]"
122 print "Available categories:"
127 category = argv.pop(0)
128 usage = "%%prog %s action <args> [options]" % (category)
129 parser = OptionParser(usage=usage)
130 command_class = CATEGORIES[category]
131 command_instance = command_class()
132 actions = command_instance._get_commands()
134 if hasattr(command_instance, '__call__'):
136 command = command_instance.__call__
138 print script_name + " category action [<args>]"
139 print "Available actions for %s category:" % category
145 command = getattr(command_instance, action)
147 options = getattr(command, 'options', [])
148 usage = "%%prog %s %s <args> [options]" % (category, action)
149 parser = OptionParser(usage=usage)
150 for arg, kwd in options:
151 parser.add_option(*arg, **kwd)
152 (opts, cmd_args) = parser.parse_args(argv)
153 cmd_kwds = vars(opts)
155 # dont overrride meth
156 for k, v in cmd_kwds.items():
161 command(*cmd_args, **cmd_kwds)
164 print "Possible wrong number of arguments supplied"
165 print command.__doc__
169 print "Command failed, please check log for more info"
173 if __name__ == '__main__':