Ignore:
Timestamp:
Dec 8, 2009 4:51:06 PM (14 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
Children:
7d2814a
Parents:
7183b48
Message:

Cleanup and split creation into two operations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd_client.py

    r7183b48 r7b26c39  
    265265    print>>out, "Description: %s" % dict['desc']
    266266    sys.exit(dict.get('code', 20))
    267 # Base class that will do a the SOAP/XMLRPC exchange for a request.
     267# Base class for the various client operations.  It includes some commonly used
     268# functions and classes the most important of which are the exception classes
     269# and the service calling classes.
    268270class fedd_rpc:
     271
    269272    class RPCException:
     273        """
     274        An error during the RPC exchange.  It unifies errors from both SOAP and
     275        XMLPRC calls.
     276        """
    270277        def __init__(self, fb):
    271278            self.desc = fb.get('desc', None)
     
    273280            self.errstr = fb.get('errstr', None)
    274281
    275     def __init__(self, pre):
     282    class caller(service_caller):
     283        """
     284        The caller is used by fedd_rpc.do_rpc to make the rpc call.  The extra
     285        stashed information is used to parse responses a little.
     286        """
     287        def __init__(self, pre):
     288            self.ResponseBody="%sResponseBody" % pre
     289            self.method = pre
     290            service_caller.__init__(self, self.method)
     291
     292    def __init__(self):
    276293        """
    277294        Specialize the class for the pre method
    278295        """
    279         self.RequestBody="%sRequestBody" % pre
    280         self.ResponseBody="%sResponseBody" % pre
    281         self.method = pre
    282 
    283         self.caller = service_caller(self.method)
     296        self.caller = fedd_rpc.caller
    284297        self.RPCException = fedd_rpc.RPCException
    285298
     
    333346
    334347    def do_rpc(self, req_dict, url, transport, cert, trusted, tracefile=None,
    335             serialize_only=False):
     348            serialize_only=False, caller=None):
    336349        """
    337350        The work of sending and parsing the RPC as either XMLRPC or SOAP
    338351        """
     352
     353        if caller is None:
     354            raise RuntimeError("Must provide caller to do_rpc")
    339355
    340356        context = None
     
    355371            else:
    356372                try:
    357                     resp = self.caller.call_soap_service(url, req_dict,
     373                    resp = caller.call_soap_service(url, req_dict,
    358374                            context=context, tracefile=tracefile)
    359375                except service_error, e:
     
    370386            else:
    371387                try:
    372                     resp = self.caller.call_xmlrpc_service(url, req_dict,
     388                    resp = caller.call_xmlrpc_service(url, req_dict,
    373389                            context=context, tracefile=tracefile)
    374390                except service_error, e:
     
    382398            raise RuntimeError("Unknown RPC transport: %s" % transport)
    383399
    384         if resp.has_key(self.ResponseBody):
    385             return resp[self.ResponseBody]
     400        if resp.has_key(caller.ResponseBody):
     401            return resp[caller.ResponseBody]
    386402        else:
    387403            raise RuntimeError("No body in response??")
    388404
    389405class exp_data_base(fedd_rpc):
    390     def __init__(self, op='Info'):
     406    def __init__(self):
    391407        """
    392408        Init the various conversions
    393409        """
    394410
    395         fedd_rpc.__init__(self, op)
     411        fedd_rpc.__init__(self)
    396412        # List of things one could ask for and what formatting routine is
    397413        # called.
     
    441457class exp_data(exp_data_base):
    442458    def __init__(self):
    443         exp_data_base.__init__(self, 'Info')
     459        exp_data_base.__init__(self)
    444460
    445461    def __call__(self):
     
    491507                    opts.url, opts.transport, cert, opts.trusted,
    492508                    serialize_only=opts.serialize_only,
    493                     tracefile=opts.tracefile)
     509                    tracefile=opts.tracefile,
     510                    caller=self.caller('Info'))
    494511        except self.RPCException, e:
    495512            exit_with_fault(\
     
    542559class multi_exp_data(exp_data_base):
    543560    def __init__(self):
    544         exp_data_base.__init__(self, 'MultiInfo')
     561        exp_data_base.__init__(self)
    545562
    546563
     
    578595                    opts.url, opts.transport, cert, opts.trusted,
    579596                    serialize_only=opts.serialize_only,
    580                     tracefile=opts.tracefile)
     597                    tracefile=opts.tracefile,
     598                    caller=self.caller('MultiInfo'))
    581599        except self.RPCException, e:
    582600            exit_with_fault(\
     
    601619class multi_status(exp_data_base):
    602620    def __init__(self):
    603         exp_data_base.__init__(self, 'MultiInfo')
     621        exp_data_base.__init__(self)
    604622
    605623
     
    637655                    opts.url, opts.transport, cert, opts.trusted,
    638656                    serialize_only=opts.serialize_only,
    639                     tracefile=opts.tracefile)
     657                    tracefile=opts.tracefile,
     658                    caller=self.caller('MultiInfo'))
    640659        except self.RPCException, e:
    641660            exit_with_fault(\
     
    688707
    689708class image(fedd_rpc):
    690     def __init__(self, op='Vtopo'):
     709    def __init__(self):
    691710        """
    692711        Null constructor
    693712        """
    694713
    695         fedd_rpc.__init__(self, op)
     714        fedd_rpc.__init__(self)
    696715
    697716    @staticmethod
     
    869888                    opts.url, opts.transport, cert, opts.trusted,
    870889                    serialize_only=opts.serialize_only,
    871                     tracefile=opts.tracefile)
     890                    tracefile=opts.tracefile,
     891                    caller=self.caller('Vtopo'))
    872892        except self.RPCException, e:
    873893            exit_with_fault(\
     
    885905
    886906class ns_image(image):
    887     def __init__(self, op='Ns2Split'):
     907    def __init__(self):
    888908        """
    889909        Null constructor
    890910        """
    891911
    892         image.__init__(self, 'Ns2Split')
     912        image.__init__(self)
    893913
    894914    def generate_topo_dict(self, splitout):
     
    10241044                    opts.url, opts.transport, cert, opts.trusted,
    10251045                    serialize_only=opts.serialize_only,
    1026                     tracefile=opts.tracefile)
     1046                    tracefile=opts.tracefile,
     1047                    caller=self.caller('Ns2Split'))
    10271048        except self.RPCException, e:
    10281049            exit_with_fault(\
     
    10421063
    10431064class topdl_image(image):
    1044     def __init__(self, op='Ns2Split'):
     1065    def __init__(self):
    10451066        """
    10461067        Null constructor
    10471068        """
    10481069
    1049         image.__init__(self, 'Ns2Split')
     1070        image.__init__(self)
    10501071
    10511072    @staticmethod
     
    11571178        """
    11581179
    1159         fedd_rpc.__init__(self, "Terminate")
     1180        fedd_rpc.__init__(self)
    11601181
    11611182    def __call__(self):
     
    12191240                    opts.url, opts.transport, cert, opts.trusted,
    12201241                    serialize_only=opts.serialize_only,
    1221                     tracefile=opts.tracefile)
     1242                    tracefile=opts.tracefile,
     1243                    caller=self.caller('Terminate'))
    12221244        except self.RPCException, e:
    12231245            exit_with_fault(\
     
    12421264        """
    12431265
    1244         fedd_rpc.__init__(self, "TerminateSegment")
     1266        fedd_rpc.__init__(self)
    12451267
    12461268    def __call__(self):
     
    13041326                    opts.url, opts.transport, cert, opts.trusted,
    13051327                    serialize_only=opts.serialize_only,
    1306                     tracefile=opts.tracefile)
     1328                    tracefile=opts.tracefile,
     1329                    caller=self.caller('TerminateSegment'))
    13071330        except self.RPCException, e:
    13081331            exit_with_fault(\
     
    13231346class new(fedd_rpc):
    13241347    def __init__(self):
    1325         fedd_rpc.__init__(self, "New")
     1348        fedd_rpc.__init__(self)
    13261349    def __call__(self):
    13271350        access_keys = []
     
    13621385                    opts.url, opts.transport, cert, opts.trusted,
    13631386                    serialize_only=opts.serialize_only,
    1364                     tracefile=opts.tracefile)
     1387                    tracefile=opts.tracefile,
     1388                    caller=self.caller("New"))
    13651389        except self.RPCException, e:
    13661390            exit_with_fault(\
     
    13911415class create(fedd_rpc):
    13921416    def __init__(self):
    1393         fedd_rpc.__init__(self, "Create")
     1417        fedd_rpc.__init__(self)
    13941418    def __call__(self):
    13951419        access_keys = []
    1396         # Process the options using the customized option parser defined above
    1397         parser = fedd_create_opts(access_keys, self.add_ssh_key,
    1398                 self.add_x509_cert)
     1420        parser = fedd_create_opts(access_keys, self.add_ssh_key,
     1421                self.add_x509_cert)
    13991422
    14001423        (opts, args) = parser.parse_args()
     
    14421465        out_certfile = opts.out_certfile
    14431466
     1467        msg = { }
     1468
     1469        if opts.exp_name:
     1470            msg['experimentID'] = { 'localname': opts.exp_name }
     1471
     1472        if opts.debug > 1: print >>sys.stderr, msg
     1473
     1474        try:
     1475            resp_dict = self.do_rpc(msg,
     1476                    opts.url, opts.transport, cert, opts.trusted,
     1477                    serialize_only=opts.serialize_only,
     1478                    tracefile=opts.tracefile,
     1479                    caller=self.caller('New'))
     1480        except self.RPCException, e:
     1481            exit_with_fault(\
     1482                    {'desc': e.desc, 'errstr': e.errstr, 'code': e.code})
     1483        except RuntimeError, e:
     1484            sys.exit("Error processing RPC: %s" % e)
     1485
     1486        if opts.debug > 1: print >>sys.stderr, resp_dict
     1487
     1488        ea = resp_dict.get('experimentAccess', None)
     1489        if out_certfile and ea and ea.has_key('X509'):
     1490            try:
     1491                f = open(out_certfile, "w")
     1492                print >>f, ea['X509']
     1493                f.close()
     1494            except IOError:
     1495                sys.exit('Could not write to %s' %  out_certfile)
     1496        eid = resp_dict.get('experimentID', None)
     1497        e_fedid = None
     1498        e_local = None
     1499        if eid:
     1500            for id in eid:
     1501                for k in id.keys():
     1502                    print "%s: %s" % (k, id[k])
     1503                    if k =='fedid':
     1504                        e_fedid = id[k]
     1505                    elif k =='localname':
     1506                        e_local = id[k]
     1507
     1508        st = resp_dict.get('experimentStatus', None)
     1509        if st:
     1510            print "status: %s" % st
     1511
     1512
    14441513        msg = {
    14451514                'experimentdescription': { 'ns2description': exp_desc },
     
    14521521                }
    14531522
    1454         if opts.exp_name:
    1455             msg['experimentID'] = { 'localname': opts.exp_name }
     1523        if e_fedid:
     1524            msg['experimentID'] = { 'fedid': e_fedid }
     1525        elif e_local:
     1526            msg['experimentID'] = { 'localname': e_local }
     1527        else:
     1528            sys.exit("New did not return an experiment ID??")
    14561529
    14571530        if opts.debug > 1: print >>sys.stderr, msg
     
    14611534                    opts.url, opts.transport, cert, opts.trusted,
    14621535                    serialize_only=opts.serialize_only,
    1463                     tracefile=opts.tracefile)
     1536                    tracefile=opts.tracefile,
     1537                    caller=self.caller('Create'))
    14641538        except self.RPCException, e:
    14651539            exit_with_fault(\
     
    14891563class split(fedd_rpc):
    14901564    def __init__(self):
    1491         fedd_rpc.__init__(self, "Ns2Split")
     1565        fedd_rpc.__init__(self)
    14921566    def __call__(self):
    14931567        access_keys = []
     
    15481622                    opts.url, opts.transport, cert, opts.trusted,
    15491623                    serialize_only=opts.serialize_only,
    1550                     tracefile=opts.tracefile)
     1624                    tracefile=opts.tracefile,
     1625                    caller=self.caller('Ns2Split'))
    15511626        except self.RPCException, e:
    15521627            exit_with_fault(\
     
    15641639class access(fedd_rpc):
    15651640    def __init__(self):
    1566         fedd_rpc.__init__(self, "RequestAccess")
     1641        fedd_rpc.__init__(self)
    15671642
    15681643    def print_response_as_testbed(self, resp, label, out=sys.stdout):
     
    16641739                    opts.url, opts.transport, cert, opts.trusted,
    16651740                    serialize_only=opts.serialize_only,
    1666                     tracefile=opts.tracefile)
     1741                    tracefile=opts.tracefile,
     1742                    caller=self.caller('RequestAccess'))
    16671743        except self.RPCException, e:
    16681744            exit_with_fault(\
     
    16821758        """
    16831759
    1684         fedd_rpc.__init__(self, 'Info')
     1760        fedd_rpc.__init__(self)
    16851761
    16861762    def __call__(self):
     
    17401816                        opts.url, opts.transport, cert, opts.trusted,
    17411817                        serialize_only=opts.serialize_only,
    1742                         tracefile=opts.tracefile)
     1818                        tracefile=opts.tracefile,
     1819                        caller=self.caller('Info'))
    17431820            except self.RPCException, e:
    17441821                exit_with_fault(\
     
    17651842class start_segment(fedd_rpc):
    17661843    def __init__(self):
    1767         fedd_rpc.__init__(self, "StartSegment")
     1844        fedd_rpc.__init__(self)
    17681845    def __call__(self):
    17691846        # Process the options using the customized option parser defined above
     
    18111888                    opts.url, opts.transport, cert, opts.trusted,
    18121889                    serialize_only=opts.serialize_only,
    1813                     tracefile=opts.tracefile)
     1890                    tracefile=opts.tracefile,
     1891                    caller=self.caller('StartSegment'))
    18141892        except self.RPCException, e:
    18151893            exit_with_fault(\
Note: See TracChangeset for help on using the changeset viewer.