Exemplo n.º 1
0
def create(openstack_resource):
    """
    Create openstack port instance
    :param openstack_resource: instance of openstack port resource
    """
    # Update port config before create port
    _update_port_config(openstack_resource.config)

    # Create port
    created_resource = openstack_resource.create()

    # Handle runtime properties
    update_runtime_properties(
        {
            RESOURCE_ID: created_resource.id,
            'fixed_ips': created_resource.fixed_ips,
            'mac_address': created_resource.mac_address,
            'allowed_address_pairs': created_resource.allowed_address_pairs,
        }
    )
Exemplo n.º 2
0
def create(openstack_resource):
    """
    Create openstack port instance
    :param openstack_resource: instance of openstack port resource
    """
    # Update port config before create port
    _update_port_config(openstack_resource.config)

    # Create port
    created_resource = openstack_resource.create()
    ipv4_list, ipv6_list = _get_fixed_ips_from_port(created_resource)
    fixed_ips = ipv4_list + ipv6_list
    _export_ips_to_port_instance(ipv4_list, ipv6_list)

    # Handle runtime properties
    update_runtime_properties(
        {
            RESOURCE_ID: created_resource.id,
            'fixed_ips': fixed_ips,
            'mac_address': created_resource.mac_address,
            'allowed_address_pairs': created_resource.allowed_address_pairs,
        }
    )
Exemplo n.º 3
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))