コード例 #1
0
def _update_security_groups_config(port_config, allow_multiple=False):
    """
    This method will try to update oprt config with securit groups
    configurations using the relationships connected with port node
    :param port_config: The port configuration required in order to
    create the port instance using Openstack API
    :param boolean allow_multiple: This flag to set if it is allowed to have
    security groups configuration from multiple resources relationships + node
    properties
    """

    # Get security groups from port config
    security_groups = port_config.get('security_groups')

    # Get the security groups from relationship if any
    rel_security_groups = \
        find_openstack_ids_of_connected_nodes_by_openstack_type(
            ctx, SECURITY_GROUP_OPENSTACK_TYPE)

    # Check if network config comes from two sources or not
    if rel_security_groups and security_groups and not allow_multiple:
        raise NonRecoverableError('Port can\'t both have the '
                                  '"security_groups" property and be '
                                  'connected to a network via a '
                                  'relationship at the same time')

    port_config['security_groups'] = security_groups or rel_security_groups
コード例 #2
0
def _get_connected_external_network_from_relationship(network_resource):
    """
    This method will lookup external network connected to network using
    relationship
    :param network_resource: Instance of openstack network resource
    :return str: External network id
    """
    # Get networks connected to router
    networks = \
        find_openstack_ids_of_connected_nodes_by_openstack_type(
            ctx,
            NETWORK_OPENSTACK_TYPE)
    # List to save all external networks connected to router
    external_network_ids = []

    for net_id in networks:
        network_resource.resource_id = net_id
        remote_network = network_resource.get()
        if remote_network.is_router_external:
            external_network_ids.append(net_id)

    if len(external_network_ids) > 1:
        raise NonRecoverableError(
            'More than one external network is connected to router {0}'
            ' by a relationship; External network IDs: {0}'.format(
                external_network_ids))

    return external_network_ids[0] if external_network_ids else None
コード例 #3
0
def _update_network_config(port_config, allow_multiple=False):
    """
    This method will try to update oprt config with network configurations
    using the relationships connected with port node
    :param port_config: The port configuration required in order to
    create the port instance using Openstack API
    :param boolean allow_multiple: This flag to set if it is allowed to have
    networks configuration from multiple resources relationships + node
    properties
    """
    # Get network id from port config
    network_id = port_config.get('network_id')

    # Get the network id from relationship if any
    rel_network_ids = find_openstack_ids_of_connected_nodes_by_openstack_type(
        ctx, NETWORK_OPENSTACK_TYPE)

    rel_network_id = rel_network_ids[0] if rel_network_ids else None
    # Check if network config comes from two sources or not
    if network_id and rel_network_id and not allow_multiple:
        raise NonRecoverableError('Port can\'t both have the '
                                  '"network_id" property and be '
                                  'connected to a network via a '
                                  'relationship at the same time')

    port_config['network_id'] = network_id or rel_network_id
コード例 #4
0
def _populate_volume_with_image_id_from_relationship(volume_config):
    """
    This method will try to populate image id for volume if there is a
    relationship between volume & image
    :param volume_config: volume config required in order to create volume
    in openstack
    """

    if volume_config:
        image_ids = find_openstack_ids_of_connected_nodes_by_openstack_type(
            ctx, IMAGE_OPENSTACK_TYPE)

        if image_ids:
            volume_config.update({'imageRef': image_ids[0]})
コード例 #5
0
def _get_subnet_network_id_from_relationship():
    """
    This method will lookup the network id for subnet using relationship
    and will raise error if it returns multiple network
    :return str network_id: Network id
    """
    # Get the network id from relationship if it is existed
    network_ids = find_openstack_ids_of_connected_nodes_by_openstack_type(
        ctx, NETWORK_OPENSTACK_TYPE)
    # Check if subnet is connected to multiple networks
    if len(network_ids) > 1:
        raise NonRecoverableError('Cannot attach subnet to multiple '
                                  'networks {0}'.format(','.join(network_ids)))

    return network_ids[0] if network_ids else None
コード例 #6
0
def _get_floating_network_id_from_relationship(resource_type):
    """
    This method will find if floating ip node is connected to the following
    resource types:
     - Port
     - Network
     - Subnet
    Using relationship and will raise error if it is connected to
    multiple resources
    :param str resource_type: Instance of openstack floating ip
    resource
    :return str floating_network_id: Floating network id
    """
    # Get the network id from relationship if it is existed
    resource_ids = find_openstack_ids_of_connected_nodes_by_openstack_type(
        ctx, resource_type)
    # Check if floating ip is connected to multiple resources
    if len(resource_ids) > 1:
        raise NonRecoverableError('Cannot attach floating ip to multiple '
                                  '{0}s {1}'.format(','.join(resource_ids),
                                                    resource_type))

    return resource_ids[0] if resource_ids else None
コード例 #7
0
def _update_external_port(openstack_resource):
    """
    This method will update external port by attaching new ips to external
    port
    :param openstack_resource: Instance Of OpenstackPort in order to
    use it
    """
    # Get the external port using the resource id provided via port node
    external_port = openstack_resource.get()
    # Check if the current port node has allowed_address_pairs as part of
    # resource_config
    addresses_to_add = openstack_resource.config.get('allowed_address_pairs')
    if addresses_to_add:
        old_addresses = external_port.get('allowed_address_pairs') or []

        # Get the old ips from the each pair
        old_ips = \
            [
                old_address['ip_address']
                for old_address
                in old_addresses if old_address.get('ip_address')
            ]
        # Get the ips need to be added to the external port
        ips_to_add = \
            [
                address_to_add['ip_address']
                for address_to_add
                in addresses_to_add if address_to_add.get('ip_address')
            ]

        # Check if there are a common ips between old ips and the one we
        # should add via node
        common_ips = set(old_ips) & set(ips_to_add)
        if common_ips:
            raise NonRecoverableError(
                'Ips {0} are already assigned to {1}'
                ''.format(common_ips, external_port.id))

        # Update port for allowed paris
        updated_port = openstack_resource.update(
            {'allowed_address_pairs':  addresses_to_add})
        # Update runtime properties
        update_runtime_properties(
            {
                'fixed_ips': updated_port.fixed_ips,
                'mac_address': updated_port.mac_address,
                'allowed_address_pairs': updated_port.allowed_address_pairs,
            }
        )

    # Get the networks from relationships if they are existed
    rel_network_ids = find_openstack_ids_of_connected_nodes_by_openstack_type(
        ctx, NETWORK_OPENSTACK_TYPE)

    rel_network_id = rel_network_ids[0] if rel_network_ids else None
    if rel_network_id:
        port = openstack_resource.get()
        if port['network_id'] != rel_network_id:
            raise NonRecoverableError(
                'Expected external resources port {0} and network {1} '
                'to be connected'.format(port.id, rel_network_id))