implement show
[sfa.git] / sfa / client / sfaadmin.py
1 #!/usr/bin/python
2 import sys
3 import copy
4 from sfa.generic import Generic
5 from optparse import OptionParser
6
7 from sfa.util.xrn import Xrn
8 from sfa.storage.record import SfaRecord 
9
10 def args(*args, **kwargs):
11     def _decorator(func):
12         func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
13         return func
14     return _decorator
15
16 class Commands(object):
17
18     def _get_commands(self):
19         available_methods = []
20         for attrib in dir(self):
21             if callable(getattr(self, attrib)) and not attrib.startswith('_'):
22                 available_methods.append(attrib)
23         return available_methods         
24
25 class RegistryCommands(Commands):
26
27     def __init__(self, *args, **kwds):
28         self.api= Generic.the_flavour().make_api(interface='registry')
29  
30     def version(self): 
31         pass           
32
33     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn') 
34     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
35     def list(self, xrn, type=None):
36         xrn = Xrn(xrn, type) 
37         records = self.api.manager.List(self.api, xrn.get_hrn())
38         for record in records:
39             if not type or record['type'] == type:
40                 print "%s (%s)" % (record['hrn'], record['type'])
41
42
43     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn') 
44     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
45     @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file') 
46     def show(self, xrn, type=None, full=True):
47         records = self.api.manager.Resolve(self.api, xrn, type, full)
48         for record in records:
49             sfa_record = SfaRecord(record)
50             print sfa_record.dump() 
51
52     def register(self, record):
53         pass
54
55     def update(self, record):
56         pass
57         
58     def remove(self, xrn):            
59         pass
60
61     def credential(self, xrn):
62         pass
63
64
65 class CerficiateCommands(Commands):
66     
67     def import_records(self, xrn):
68         pass
69
70     def export(self, xrn):
71         pass
72
73
74     def display(self, xrn):
75         pass
76     def nuke(self):
77         pass  
78
79 class AggregateCommands(Commands):
80
81     def __init__(self, *args, **kwds):
82         self.api= Generic.the_flavour().make_api(interface='aggregate')
83    
84     def version(self):
85         pass
86
87     def slices(self):
88         pass        
89
90     def status(self, xrn):
91         pass
92  
93     def resources(self, xrn):
94         pass
95
96     def create(self, xrn, rspec):
97         pass
98
99     def delete(self, xrn):
100         pass 
101     
102     def start(self, xrn):
103         pass
104
105     def stop(self, xrn):
106         pass      
107
108     def reset(self, xrn):
109         pass
110
111     def ticket(self):
112         pass
113
114
115 class SliceManagerCommands(AggregateCommands):
116     
117     def __init__(self, *args, **kwds):
118         self.api= Generic().make_api(interface='slicemgr')
119
120
121 CATEGORIES = {'registry': RegistryCommands,
122               'aggregate': AggregateCommands,
123               'slicemgr': SliceManagerCommands}
124
125 def main():
126     argv = copy.deepcopy(sys.argv)
127     script_name = argv.pop(0)
128     if len(argv) < 1:
129         print script_name + " category action [<args>]"
130         print "Available categories:"
131         for k in CATEGORIES:
132             print "\t%s" % k
133         sys.exit(2)
134
135     category = argv.pop(0)
136     usage = "%%prog %s action <args> [options]" % (category)
137     parser = OptionParser(usage=usage)
138     command_class =  CATEGORIES[category]
139     command_instance = command_class()
140     actions = command_instance._get_commands()
141     if len(argv) < 1:
142         if hasattr(command_instance, '__call__'):
143             action = ''
144             command = command_instance.__call__
145         else:
146             print script_name + " category action [<args>]"
147             print "Available actions for %s category:" % category
148             for k in actions:
149                 print "\t%s" % k 
150             sys.exit(2)
151     else:
152         action = argv.pop(0)
153         command = getattr(command_instance, action)
154
155     options = getattr(command, 'options', [])
156     usage = "%%prog %s %s <args> [options]" % (category, action)
157     parser = OptionParser(usage=usage)
158     for arg, kwd in options:
159         parser.add_option(*arg, **kwd)
160     (opts, cmd_args) = parser.parse_args(argv)
161     cmd_kwds = vars(opts)
162
163     # dont overrride meth
164     for k, v in cmd_kwds.items():
165         if v is None:
166             del cmd_kwds[k]
167
168     try:
169         command(*cmd_args, **cmd_kwds)
170         sys.exit(0)
171     except TypeError:
172         print "Possible wrong number of arguments supplied"
173         print command.__doc__
174         parser.print_help()
175         #raise
176     except Exception:
177         print "Command failed, please check log for more info"
178         raise
179
180
181 if __name__ == '__main__':
182     main()
183     
184      
185         
186