Esempio n. 1
0
def make_port_public(client, port_id, project_id):
    """Associate floating IP with the port."""
    public_network_id = get_public_network(client)
    if not public_network_id:
        raise exception.PublicNetworkNotFound()

    fip_body = {
        "floatingip": {
            'floating_network_id': public_network_id,
            'port_id': port_id,
        }
    }
    if project_id:
        if check_extension_enabled(client, PROJECT_ID_EXT_ALIAS):
            project_id_key = 'project_id'
        else:
            project_id_key = 'tenant_id'
        fip_body['floatingip'][project_id_key] = project_id

    try:
        LOG.debug(f"Creating floating IP for the port {port_id}, "
                  f"request body: {fip_body}")
        ret = client.create_floatingip(fip_body)
        LOG.info(f"Successfully created floating IP "
                 f"{ret['floatingip']['floating_ip_address']} for port "
                 f"{port_id}")
    except Exception as e:
        LOG.error(f"Failed to create public IP with port {port_id}: "
                  f"{str(e)}")
        raise exception.TroveError('Failed to expose instance port to public.')
Esempio n. 2
0
def create_port(client, name, description, network_id, security_groups,
                is_public=False):
    port_body = {
        "port": {
            "name": name,
            "description": description,
            "network_id": network_id,
            "security_groups": security_groups
        }
    }
    port = client.create_port(body=port_body)
    port_id = port['port']['id']

    if is_public:
        public_network_id = get_public_network(client)
        if not public_network_id:
            raise exception.PublicNetworkNotFound()

        fip_body = {
            "floatingip": {
                'floating_network_id': public_network_id,
                'port_id': port_id,
            }
        }
        client.create_floatingip(fip_body)

    return port_id
Esempio n. 3
0
def create_port(client, name, description, network_id, security_groups,
                is_public=False, subnet_id=None, ip=None):
    port_body = {
        "port": {
            "name": name,
            "description": description,
            "network_id": network_id,
            "security_groups": security_groups
        }
    }

    if subnet_id:
        fixed_ips = {
            "fixed_ips": [{"subnet_id": subnet_id}]
        }
        if ip:
            fixed_ips['fixed_ips'][0].update({'ip_address': ip})
        port_body['port'].update(fixed_ips)

    port = client.create_port(body=port_body)
    port_id = port['port']['id']

    if is_public:
        public_network_id = get_public_network(client)
        if not public_network_id:
            raise exception.PublicNetworkNotFound()

        fip_body = {
            "floatingip": {
                'floating_network_id': public_network_id,
                'port_id': port_id,
            }
        }
        client.create_floatingip(fip_body)

    return port_id