+
+
+def create_floating_ip(neutron, keystone, ext_net_name, port_id=None):
+ """
+ Returns the floating IP object that was created with this call
+ :param neutron: the Neutron client
+ :param keystone: the Keystone client
+ :param ext_net_name: the name of the external network on which to apply the
+ floating IP address
+ :param port_id: the ID of the port to which the floating IP will be
+ associated
+ :return: the SNAPS FloatingIp object
+ """
+ logger.info('Creating floating ip to external network - ' + ext_net_name)
+ ext_net = get_network(neutron, keystone, network_name=ext_net_name)
+ if ext_net:
+ body = {'floatingip': {'floating_network_id': ext_net.id}}
+ if port_id:
+ body['floatingip']['port_id'] = port_id
+
+ fip = neutron.create_floatingip(body=body)
+
+ return FloatingIp(id=fip['floatingip']['id'],
+ ip=fip['floatingip']['floating_ip_address'])
+ else:
+ raise NeutronException(
+ 'Cannot create floating IP, external network not found')
+
+
+def get_floating_ip(neutron, floating_ip):
+ """
+ Returns a floating IP object that should be identical to the floating_ip
+ parameter
+ :param neutron: the Neutron client
+ :param floating_ip: the SNAPS FloatingIp object
+ :return: hopefully the same floating IP object input
+ """
+ logger.debug('Attempting to retrieve existing floating ip with IP - %s',
+ floating_ip.ip)
+ os_fip = __get_os_floating_ip(neutron, floating_ip)
+ if os_fip:
+ return FloatingIp(id=os_fip['id'], ip=os_fip['floating_ip_address'])
+
+
+def __get_os_floating_ip(neutron, floating_ip):
+ """
+ Returns an OpenStack floating IP object
+ parameter
+ :param neutron: the Neutron client
+ :param floating_ip: the SNAPS FloatingIp object
+ :return: hopefully the same floating IP object input
+ """
+ logger.debug('Attempting to retrieve existing floating ip with IP - %s',
+ floating_ip.ip)
+ fips = neutron.list_floatingips(floating_ip_address=floating_ip.ip)
+
+ for fip in fips['floatingips']:
+ if fip['id'] == floating_ip.id:
+ return fip
+
+
+def delete_floating_ip(neutron, floating_ip):
+ """
+ Responsible for deleting a floating IP
+ :param neutron: the Neutron client
+ :param floating_ip: the SNAPS FloatingIp object
+ :return:
+ """
+ logger.debug('Attempting to delete existing floating ip with IP - %s',
+ floating_ip.ip)
+ return neutron.delete_floatingip(floating_ip.id)
+
+
+def get_network_quotas(neutron, project_id):
+ """
+ Returns a list of NetworkQuotas objects
+ :param neutron: the neutron client
+ :param project_id: the project's ID of the quotas to lookup
+ :return: an object of type NetworkQuotas or None if not found
+ """
+ quota = neutron.show_quota(project_id)
+ if quota:
+ return NetworkQuotas(**quota['quota'])
+
+
+def update_quotas(neutron, project_id, network_quotas):
+ """
+ Updates the networking quotas for a given project
+ :param neutron: the Neutron client
+ :param project_id: the project's ID that requires quota updates
+ :param network_quotas: an object of type NetworkQuotas containing the
+ values to update
+ :return:
+ """
+ update_body = dict()
+ update_body['security_group'] = network_quotas.security_group
+ update_body['security_group_rule'] = network_quotas.security_group_rule
+ update_body['floatingip'] = network_quotas.floatingip
+ update_body['network'] = network_quotas.network
+ update_body['port'] = network_quotas.port
+ update_body['router'] = network_quotas.router
+ update_body['subnet'] = network_quotas.subnet
+
+ return neutron.update_quota(project_id, {'quota': update_body})
+
+
+class NeutronException(Exception):
+ """
+ Exception when calls to the Keystone client cannot be served properly
+ """