"""
-def neutron_client(os_creds):
+def neutron_client(os_creds, session=None):
"""
Instantiates and returns a client for communications with OpenStack's
Neutron server
:param os_creds: the credentials for connecting to the OpenStack remote API
+ :param session: the keystone session object (optional)
:return: the client object
"""
+ if not session:
+ session = keystone_utils.keystone_session(os_creds)
return Client(api_version=os_creds.network_api_version,
- session=keystone_utils.keystone_session(os_creds),
+ session=session,
region_name=os_creds.region_name)
for network, netInsts in networks.items():
for inst in netInsts:
if project_name:
- project = keystone_utils.get_project_by_id(
- keystone, inst['project_id'])
+ if 'project_id' in inst.keys():
+ project = keystone_utils.get_project_by_id(
+ keystone, inst['project_id'])
+ else:
+ project = keystone_utils.get_project_by_id(
+ keystone, inst['tenant_id'])
if project and project.name == project_name:
return __map_network(neutron, inst)
else:
neutron.delete_subnet(subnet.id)
-def get_subnet(neutron, subnet_settings=None, subnet_name=None):
+def get_subnet(neutron, network, subnet_settings=None, subnet_name=None):
"""
Returns the first subnet object that fits the query else None including
if subnet_settings or subnet_name parameters are None.
:param neutron: the client
+ :param network: the associated SNAPS-OO Network domain object
:param subnet_settings: the subnet settings of the object to retrieve
:param subnet_name: the name of the subnet to retrieve
:return: a SNAPS-OO Subnet domain object or None
"""
- sub_filter = dict()
+ sub_filter = {'network_id': network.id}
if subnet_settings:
sub_filter['name'] = subnet_settings.name
sub_filter['cidr'] = subnet_settings.cidr
return Subnet(**subnet)
+def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None):
+ """
+ Returns the first subnet object that fits the query else None including
+ if subnet_settings or subnet_name parameters are None.
+ :param neutron: the Neutron client
+ :param keystone: the Keystone client
+ :param subnet_name: the name of the subnet to retrieve
+ :param project_name: the name of the associated project to the subnet to
+ retrieve
+ :return: a SNAPS-OO Subnet domain object or None
+ """
+ sub_filter = {'name': subnet_name}
+ subnets = neutron.list_subnets(**sub_filter)
+ for subnet in subnets['subnets']:
+ subnet = Subnet(**subnet)
+ if project_name:
+ project = keystone_utils.get_project_by_id(
+ keystone, subnet.project_id)
+ if project and project.name == project_name:
+ return subnet
+ else:
+ return subnet
+
+
def get_subnet_by_id(neutron, subnet_id):
"""
Returns a SNAPS-OO Subnet domain object for a given ID
return __map_router(neutron, router['router'])
-def get_router(neutron, router_settings=None, router_name=None):
+def get_router(neutron, keystone, router_settings=None, router_name=None,
+ project_name=None):
"""
Returns the first router object (dictionary) found the given the settings
values if not None, else finds the first with the value of the router_name
parameter, else None
- :param neutron: the client
+ :param neutron: the Neutron client
+ :param keystone: the Keystone client
:param router_settings: the RouterConfig object
:param router_name: the name of the network to retrieve
+ :param project_name: the name of the router's project
:return: a SNAPS-OO Router domain object
"""
router_filter = dict()
else:
return None
- routers = neutron.list_routers(**router_filter)
-
- for routerInst in routers['routers']:
- return __map_router(neutron, routerInst)
-
- return None
+ os_routers = neutron.list_routers(**router_filter)
+ for os_router in os_routers['routers']:
+ if project_name:
+ if 'project_id' in os_router.keys():
+ project = keystone_utils.get_project_by_id(
+ keystone, os_router['project_id'])
+ else:
+ project = keystone_utils.get_project_by_id(
+ keystone, os_router['tenant_id'])
+ if project and project.name == project_name:
+ return __map_router(neutron, os_router)
def __map_router(neutron, os_router):
project_name = port_settings.project_name
if port_settings.network_name:
network = get_network(
- neutron, keystone, network_name=port_settings.network_name,
- project_name=project_name)
+ neutron, keystone, network_name=port_settings.network_name)
+ if network and not (network.shared or network.external):
+ network = get_network(
+ neutron, keystone, network_name=port_settings.network_name,
+ project_name=project_name)
if network:
port_filter['network_id'] = network.id
elif port_name:
ports = neutron.list_ports(**port_filter)
for port in ports['ports']:
if project_name:
- project = keystone_utils.get_project_by_id(
- keystone, port['project_id'])
+ if 'project_id' in port.keys():
+ project = keystone_utils.get_project_by_id(
+ keystone, port['project_id'])
+ else:
+ project = keystone_utils.get_project_by_id(
+ keystone, port['tenant_id'])
if project and project.name == project_name:
return Port(**port)
else:
return None
groups = neutron.list_security_groups(**sec_grp_filter)
- group = None
for group in groups['security_groups']:
if project_name:
- project = keystone_utils.get_project_by_id(
- keystone, group['tenant_id'])
+ if 'project_id' in group.keys():
+ project = keystone_utils.get_project_by_id(
+ keystone, group['project_id'])
+ else:
+ project = keystone_utils.get_project_by_id(
+ keystone, group['tenant_id'])
if project and project_name == project.name:
- break
+ return __map_os_security_group(neutron, group)
else:
- break
- if group:
- return __map_os_security_group(neutron, group)
+ return __map_os_security_group(neutron, group)
def __map_os_security_group(neutron, os_sec_grp):
return None
+def list_security_groups(neutron):
+
+ """
+ Lists the available security groups
+ :param neutron: the neutron client
+ """
+ logger.info('Listing the available security groups')
+ sec_groups = []
+ response = neutron.list_security_groups()
+ for sg in response['security_groups']:
+ sec_groups.append(__map_os_security_group(neutron, sg))
+
+ return sec_groups
+
+
def create_security_group_rule(neutron, keystone, sec_grp_rule_settings,
proj_name):
"""
"""
logger.debug('Attempting to retrieve existing floating ip with IP - %s',
floating_ip.ip)
- fips = neutron.list_floatingips(ip=floating_ip.id)
+ fips = neutron.list_floatingips(floating_ip_address=floating_ip.ip)
for fip in fips['floatingips']:
if fip['id'] == floating_ip.id: