Ejemplo n.º 1
0
def get_port(neutron,
             keystone,
             port_settings=None,
             port_name=None,
             project_name=None):
    """
    Returns the first port object (dictionary) found for the given query
    :param neutron: the Neutron client
    :param keystone: the Keystone client
    :param port_settings: the PortConfig object used for generating the query
    :param port_name: if port_settings is None, this name is the value to place
                      into the query
    :param project_name: the associated project name
    :return: a SNAPS-OO Port domain object
    """
    port_filter = dict()

    if port_settings:
        if port_settings.name and len(port_settings.name) > 0:
            port_filter['name'] = port_settings.name
        if port_settings.admin_state_up:
            port_filter['admin_state_up'] = port_settings.admin_state_up
        if port_settings.device_id:
            port_filter['device_id'] = port_settings.device_id
        if port_settings.mac_address:
            port_filter['mac_address'] = port_settings.mac_address
        if port_settings.project_name:
            project_name = port_settings.project_name
        if port_settings.network_name:
            network = get_network(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:
        port_filter['name'] = port_name

    ports = neutron.list_ports(**port_filter)
    for port in ports['ports']:
        if project_name:
            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 Port(**port)
    return None
Ejemplo n.º 2
0
def get_security_group(neutron,
                       keystone,
                       sec_grp_settings=None,
                       sec_grp_name=None,
                       project_name=None):
    """
    Returns the first security group for a given query. The query gets built
    from the sec_grp_settings parameter if not None, else only the name of
    the security group will be used, else if the query parameters are None then
    None will be returned
    :param neutron: the neutron client
    :param keystone: the keystone client
    :param sec_grp_settings: an instance of SecurityGroupConfig object
    :param sec_grp_name: the name of security group object to retrieve
    :param project_name: the name of the project/tentant object that owns the
                       secuity group to retrieve
    :return: a SNAPS-OO SecurityGroup domain object or None if not found
    """

    sec_grp_filter = dict()

    if sec_grp_settings:
        sec_grp_filter['name'] = sec_grp_settings.name

        if sec_grp_settings.description:
            sec_grp_filter['description'] = sec_grp_settings.description
        if sec_grp_settings.project_name:
            project_name = sec_grp_settings.project_name
    elif sec_grp_name:
        sec_grp_filter['name'] = sec_grp_name
    else:
        return None

    groups = neutron.list_security_groups(**sec_grp_filter)
    for group in groups['security_groups']:
        if project_name:
            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:
                return __map_os_security_group(neutron, group)
        else:
            return __map_os_security_group(neutron, group)
Ejemplo n.º 3
0
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 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()
    if router_settings:
        router_filter['name'] = router_settings.name
        if router_settings.admin_state_up is not None:
            router_filter['admin_state_up'] = router_settings.admin_state_up
    elif router_name:
        router_filter['name'] = router_name
    else:
        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)
Ejemplo n.º 4
0
def get_network(neutron,
                keystone,
                network_settings=None,
                network_name=None,
                project_name=None):
    """
    Returns Network SNAPS-OO domain object the first network found with
    either the given attributes from the network_settings object if not None,
    else the query will use just the name from the network_name parameter.
    When the project_name is included, that will be added to the query filter.
    :param neutron: the Neutron client
    :param keystone: the Keystone client
    :param network_settings: the NetworkConfig object used to create filter
    :param network_name: the name of the network to retrieve
    :param project_name: the name of the network's project
    :return: a SNAPS-OO Network domain object
    """
    net_filter = dict()
    if network_settings:
        net_filter['name'] = network_settings.name
    elif network_name:
        net_filter['name'] = network_name

    networks = neutron.list_networks(**net_filter)
    for network, netInsts in networks.items():
        for inst in netInsts:
            if project_name:
                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:
                return __map_network(neutron, inst)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def get_volume(cinder,
               keystone=None,
               volume_name=None,
               volume_settings=None,
               project_name=None):
    """
    Returns an OpenStack volume object for a given name
    :param cinder: the Cinder client
    :param keystone: the Keystone client (required if project_name or
                     volume_settings.project_name is not None
    :param volume_name: the volume name to lookup
    :param volume_settings: the volume settings used for lookups
    :param project_name: the name of the project associated with the volume
    :return: the volume object or None
    """
    if volume_settings:
        volume_name = volume_settings.name

    volumes = cinder.volumes.list()
    for os_volume in volumes:
        if os_volume.name == volume_name:
            project_id = None
            if hasattr(os_volume, 'os-vol-tenant-attr:tenant_id'):
                project_id = getattr(os_volume, 'os-vol-tenant-attr:tenant_id')

            if volume_settings and volume_settings.project_name:
                project_name = volume_settings.project_name

            if project_name:
                project = keystone_utils.get_project_by_id(
                    keystone, project_id)

                if project and project.name == project_name:
                    return __map_os_volume_to_domain(os_volume)
            else:
                return __map_os_volume_to_domain(os_volume)