<s> import netaddr <EOL> import six <EOL> import webob . exc <EOL> from nova . api . openstack . compute . schemas import floating_ips_bulk <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> import nova . conf <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import objects <EOL> CONF = nova . conf . CONF <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ALIAS = '<STR_LIT>' <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class FloatingIPBulkController ( wsgi . Controller ) : <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> return self . _get_floating_ip_info ( context ) <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> return self . _get_floating_ip_info ( context , id ) <EOL> def _get_floating_ip_info ( self , context , host = None ) : <EOL> floating_ip_info = { "<STR_LIT>" : [ ] } <EOL> if host is None : <EOL> try : <EOL> floating_ips = objects . FloatingIPList . get_all ( context ) <EOL> except exception . NoFloatingIpsDefined : <EOL> return floating_ip_info <EOL> else : <EOL> try : <EOL> floating_ips = objects . FloatingIPList . get_by_host ( context , <EOL> host ) <EOL> except exception . FloatingIpNotFoundForHost as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> for floating_ip in floating_ips : <EOL> instance_uuid = None <EOL> fixed_ip = None <EOL> if floating_ip . fixed_ip : <EOL> instance_uuid = floating_ip . fixed_ip . instance_uuid <EOL> fixed_ip = str ( floating_ip . fixed_ip . address ) <EOL> result = { '<STR_LIT:address>' : str ( floating_ip . address ) , <EOL> '<STR_LIT>' : floating_ip . pool , <EOL> '<STR_LIT>' : floating_ip . interface , <EOL> '<STR_LIT>' : floating_ip . project_id , <EOL> '<STR_LIT>' : instance_uuid , <EOL> '<STR_LIT>' : fixed_ip } <EOL> floating_ip_info [ '<STR_LIT>' ] . append ( result ) <EOL> return floating_ip_info <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( floating_ips_bulk . create ) <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> params = body [ '<STR_LIT>' ] <EOL> ip_range = params [ '<STR_LIT>' ] <EOL> pool = params . get ( '<STR_LIT>' , CONF . default_floating_pool ) <EOL> interface = params . get ( '<STR_LIT>' , CONF . public_interface ) <EOL> try : <EOL> ips = [ objects . FloatingIPList . make_ip_info ( addr , pool , interface ) <EOL> for addr in self . _address_to_hosts ( ip_range ) ] <EOL> except exception . InvalidInput as exc : <EOL> raise webob . exc . HTTPBadRequest ( explanation = exc . format_message ( ) ) <EOL> try : <EOL> objects . FloatingIPList . create ( context , ips ) <EOL> except exception . FloatingIpExists as exc : <EOL> raise webob . exc . HTTPConflict ( explanation = exc . format_message ( ) ) <EOL> return { "<STR_LIT>" : { "<STR_LIT>" : ip_range , <EOL> "<STR_LIT>" : pool , <EOL> "<STR_LIT>" : interface } } <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( floating_ips_bulk . delete ) <EOL> def update ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> if id != "<STR_LIT>" : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPNotFound ( explanation = msg ) <EOL> ip_range = body [ '<STR_LIT>' ] <EOL> try : <EOL> ips = ( objects . FloatingIPList . make_ip_info ( address , None , None ) <EOL> for address in self . _address_to_hosts ( ip_range ) ) <EOL> except exception . InvalidInput as exc : <EOL> raise webob . exc . HTTPBadRequest ( explanation = exc . format_message ( ) ) <EOL> objects . FloatingIPList . destroy ( context , ips ) <EOL> return { "<STR_LIT>" : ip_range } <EOL> def _address_to_hosts ( self , addresses ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return [ netaddr . IPAddress ( addresses ) ] <EOL> except ValueError : <EOL> net = netaddr . IPNetwork ( addresses ) <EOL> if net . size < <NUM_LIT:4> : <EOL> reason = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % net . prefixlen <EOL> raise exception . InvalidInput ( reason = reason ) <EOL> else : <EOL> return net . iter_hosts ( ) <EOL> except netaddr . AddrFormatError as exc : <EOL> raise exception . InvalidInput ( reason = six . text_type ( exc ) ) <EOL> class FloatingIpsBulk ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> resource = [ extensions . ResourceExtension ( ALIAS , <EOL> FloatingIPBulkController ( ) ) ] <EOL> return resource <EOL> def get_controller_extensions ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] </s>
