<s> import time <EOL> from xml . dom import minidom <EOL> from webob import exc <EOL> import nova <EOL> import nova . api . openstack . views . addresses <EOL> from nova . api . openstack import wsgi <EOL> from nova import db <EOL> class Controller ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . compute_api = nova . compute . API ( ) <EOL> def _get_instance ( self , req , server_id ) : <EOL> try : <EOL> instance = self . compute_api . get ( <EOL> req . environ [ '<STR_LIT>' ] , server_id ) <EOL> except nova . exception . NotFound : <EOL> raise exc . HTTPNotFound ( ) <EOL> return instance <EOL> def create ( self , req , server_id , body ) : <EOL> raise exc . HTTPNotImplemented ( ) <EOL> def delete ( self , req , server_id , id ) : <EOL> raise exc . HTTPNotImplemented ( ) <EOL> class ControllerV10 ( Controller ) : <EOL> def index ( self , req , server_id ) : <EOL> instance = self . _get_instance ( req , server_id ) <EOL> builder = nova . api . openstack . views . addresses . ViewBuilderV10 ( ) <EOL> return { '<STR_LIT>' : builder . build ( instance ) } <EOL> def show ( self , req , server_id , id ) : <EOL> instance = self . _get_instance ( req , server_id ) <EOL> builder = self . _get_view_builder ( req ) <EOL> if id == '<STR_LIT>' : <EOL> view = builder . build_private_parts ( instance ) <EOL> elif id == '<STR_LIT>' : <EOL> view = builder . build_public_parts ( instance ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> return { id : view } <EOL> def _get_view_builder ( self , req ) : <EOL> return nova . api . openstack . views . addresses . ViewBuilderV10 ( ) <EOL> class ControllerV11 ( Controller ) : <EOL> def index ( self , req , server_id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> interfaces = self . _get_virtual_interfaces ( context , server_id ) <EOL> networks = self . _get_view_builder ( req ) . build ( interfaces ) <EOL> return { '<STR_LIT>' : networks } <EOL> def show ( self , req , server_id , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> interfaces = self . _get_virtual_interfaces ( context , server_id ) <EOL> network = self . _get_view_builder ( req ) . build_network ( interfaces , id ) <EOL> if network is None : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> return network <EOL> def _get_virtual_interfaces ( self , context , server_id ) : <EOL> try : <EOL> return db . api . virtual_interface_get_by_instance ( context , server_id ) <EOL> except nova . exception . InstanceNotFound : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> def _get_view_builder ( self , req ) : <EOL> return nova . api . openstack . views . addresses . ViewBuilderV11 ( ) <EOL> class IPXMLSerializer ( wsgi . XMLDictSerializer ) : <EOL> def __init__ ( self , xmlns = wsgi . XMLNS_V11 ) : <EOL> super ( IPXMLSerializer , self ) . __init__ ( xmlns = xmlns ) <EOL> def _ip_to_xml ( self , xml_doc , ip_dict ) : <EOL> ip_node = xml_doc . createElement ( '<STR_LIT>' ) <EOL> ip_node . setAttribute ( '<STR_LIT>' , ip_dict [ '<STR_LIT>' ] ) <EOL> ip_node . setAttribute ( '<STR_LIT:version>' , str ( ip_dict [ '<STR_LIT:version>' ] ) ) <EOL> return ip_node <EOL> def _network_to_xml ( self , xml_doc , network_id , ip_dicts ) : <EOL> network_node = xml_doc . createElement ( '<STR_LIT>' ) <EOL> network_node . setAttribute ( '<STR_LIT:id>' , network_id ) <EOL> for ip_dict in ip_dicts : <EOL> ip_node = self . _ip_to_xml ( xml_doc , ip_dict ) <EOL> network_node . appendChild ( ip_node ) <EOL> return network_node <EOL> def networks_to_xml ( self , xml_doc , networks_container ) : <EOL> addresses_node = xml_doc . createElement ( '<STR_LIT>' ) <EOL> for ( network_id , ip_dicts ) in networks_container . items ( ) : <EOL> network_node = self . _network_to_xml ( xml_doc , network_id , ip_dicts ) <EOL> addresses_node . appendChild ( network_node ) <EOL> return addresses_node <EOL> def show ( self , network_container ) : <EOL> ( network_id , ip_dicts ) = network_container . items ( ) [ <NUM_LIT:0> ] <EOL> xml_doc = minidom . Document ( ) <EOL> node = self . _network_to_xml ( xml_doc , network_id , ip_dicts ) <EOL> return self . to_xml_string ( node , False ) <EOL> def index ( self , addresses_container ) : <EOL> xml_doc = minidom . Document ( ) <EOL> node = self . networks_to_xml ( xml_doc , addresses_container [ '<STR_LIT>' ] ) <EOL> return self . to_xml_string ( node , False ) <EOL> def create_resource ( version ) : <EOL> controller = { <EOL> '<STR_LIT:1.0>' : ControllerV10 , <EOL> '<STR_LIT>' : ControllerV11 , <EOL> } [ version ] ( ) <EOL> metadata = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> } , <EOL> } <EOL> xml_serializer = { <EOL> '<STR_LIT:1.0>' : wsgi . XMLDictSerializer ( metadata = metadata , xmlns = wsgi . XMLNS_V11 ) , <EOL> '<STR_LIT>' : IPXMLSerializer ( ) , <EOL> } [ version ] <EOL> serializer = wsgi . ResponseSerializer ( { '<STR_LIT>' : xml_serializer } ) <EOL> return wsgi . Resource ( controller , serializer = serializer ) </s>
