def _floatingip_operation(operation, vca_client, ctx):
    """
        create/release floating ip by nat rules for this ip with
        relation to internal ip for current node,
        save selected public_ip in runtime properties
    """
    service_type = get_vcloud_config().get('service_type')
    gateway = get_gateway(
        vca_client, ctx.target.node.properties['floatingip']['edge_gateway'])
    internal_ip = get_vm_ip(vca_client, ctx, gateway)

    nat_operation = None
    public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP)
                 or ctx.target.node.properties['floatingip'].get(PUBLIC_IP))
    if operation == CREATE:
        CheckAssignedInternalIp(internal_ip, gateway)
        if public_ip:
            CheckAssignedExternalIp(public_ip, gateway)
        else:
            public_ip = get_public_ip(vca_client, gateway, service_type, ctx)

        nat_operation = _add_nat_rule
    elif operation == DELETE:
        if not public_ip:
            ctx.logger.info("Can't get external IP".format(public_ip))
            return True
        nat_operation = _del_nat_rule
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown operation {0}".format(operation)
        )

    external_ip = check_ip(public_ip)

    nat_operation(gateway, "SNAT", internal_ip, external_ip)
    nat_operation(gateway, "DNAT", external_ip, internal_ip)
    success = save_gateway_configuration(gateway, vca_client, ctx)
    if not success:
        return False
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip
        save_ssh_parameters(ctx, '22', external_ip)
    else:
        if is_ondemand(service_type):
            if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP):
                del_ondemand_public_ip(
                    vca_client,
                    gateway,
                    ctx.target.instance.runtime_properties[PUBLIC_IP],
                    ctx)
        if PUBLIC_IP in ctx.target.instance.runtime_properties:
            del ctx.target.instance.runtime_properties[PUBLIC_IP]
        if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties:
            del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP]
        if SSH_PORT in ctx.target.instance.runtime_properties:
            del ctx.source.instance.runtime_properties[SSH_PORT]
    return True
def _floatingip_operation(operation, vca_client, ctx):
    """
        create/release floating ip by nat rules for this ip with
        relation to internal ip for current node,
        save selected public_ip in runtime properties
    """
    service_type = get_vcloud_config().get('service_type')
    gateway = get_gateway(
        vca_client, ctx.target.node.properties['floatingip']['edge_gateway'])
    internal_ip = get_vm_ip(vca_client, ctx, gateway)

    nat_operation = None
    public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP)
                 or ctx.target.node.properties['floatingip'].get(PUBLIC_IP))
    if operation == CREATE:
        CheckAssignedInternalIp(internal_ip, gateway)
        if public_ip:
            CheckAssignedExternalIp(public_ip, gateway)
        else:
            public_ip = get_public_ip(vca_client, gateway, service_type, ctx)

        nat_operation = _add_nat_rule
    elif operation == DELETE:
        if not public_ip:
            ctx.logger.info("Can't get external IP".format(public_ip))
            return True
        nat_operation = _del_nat_rule
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown operation {0}".format(operation))

    external_ip = check_ip(public_ip)

    nat_operation(gateway, "SNAT", internal_ip, external_ip)
    nat_operation(gateway, "DNAT", external_ip, internal_ip)
    success = save_gateway_configuration(gateway, vca_client, ctx)
    if not success:
        return False
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip
        save_ssh_parameters(ctx, '22', external_ip)
    else:
        if is_ondemand(service_type):
            if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP):
                del_ondemand_public_ip(
                    vca_client, gateway,
                    ctx.target.instance.runtime_properties[PUBLIC_IP], ctx)
        if PUBLIC_IP in ctx.target.instance.runtime_properties:
            del ctx.target.instance.runtime_properties[PUBLIC_IP]
        if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties:
            del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP]
        if SSH_PORT in ctx.source.instance.runtime_properties:
            del ctx.source.instance.runtime_properties[SSH_PORT]
    return True
def nat_network_operation(vca_client, gateway, operation, rule_type, public_ip,
                          private_ip, original_port, translated_port,
                          protocol):
    """
        create/drop nat rule for current network
    """
    if operation == CREATE:
        new_original_port = _get_original_port_for_create(
            gateway, rule_type, public_ip, original_port,
            private_ip, translated_port, protocol)
        function = gateway.add_nat_rule
        message = "Add"
        if _is_dnat(rule_type) and str(translated_port) == DEFAULT_SSH_PORT:
            save_ssh_parameters(ctx, str(new_original_port), public_ip)
    elif operation == DELETE:
        new_original_port = _get_original_port_for_delete(
            public_ip, original_port)
        function = gateway.del_nat_rule
        message = "Remove"
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown operation: {0}".format(operation))

    info_message = ("{6} NAT rule: rule type '{2}', original_ip '{0}', "
                    "translated_ip '{1}',protocol '{3}', "
                    "original_port '{4}', translated_port '{5}'")
    if _is_snat(rule_type):
        # for SNAT type ports and protocol must by "any",
        #  because they are not configurable
        ctx.logger.info(
            info_message.format(private_ip, public_ip, rule_type, protocol,
                                new_original_port, translated_port,
                                message))
        function(
            rule_type, private_ip, "any", public_ip, "any", "any")
    elif _is_dnat(rule_type):
        ctx.logger.info(
            info_message.format(public_ip, private_ip, rule_type, protocol,
                                new_original_port, translated_port,
                                message))
        function(rule_type, public_ip, str(new_original_port), private_ip,
                 str(translated_port), protocol)
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown rule type: {0}".format(rule_type))
示例#4
0
def nat_network_operation(vca_client, gateway, operation, rule_type, public_ip,
                          private_ip, original_port, translated_port,
                          protocol):
    """
        create/drop nat rule for current network
    """
    if operation == CREATE:
        new_original_port = _get_original_port_for_create(
            gateway, rule_type, public_ip, original_port, private_ip,
            translated_port, protocol)
        function = gateway.add_nat_rule
        message = "Add"
        if _is_dnat(rule_type) and str(translated_port) == DEFAULT_SSH_PORT:
            save_ssh_parameters(ctx, str(new_original_port), public_ip)
    elif operation == DELETE:
        new_original_port = _get_original_port_for_delete(
            public_ip, original_port)
        function = gateway.del_nat_rule
        message = "Remove"
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown operation: {0}".format(operation))

    info_message = ("{6} NAT rule: rule type '{2}', original_ip '{0}', "
                    "translated_ip '{1}',protocol '{3}', "
                    "original_port '{4}', translated_port '{5}'")
    if _is_snat(rule_type):
        # for SNAT type ports and protocol must by "any",
        #  because they are not configurable
        ctx.logger.info(
            info_message.format(private_ip, public_ip, rule_type, protocol,
                                new_original_port, translated_port, message))
        function(rule_type, private_ip, "any", public_ip, "any", "any")
    elif _is_dnat(rule_type):
        ctx.logger.info(
            info_message.format(public_ip, private_ip, rule_type, protocol,
                                new_original_port, translated_port, message))
        function(rule_type, public_ip, str(new_original_port), private_ip,
                 str(translated_port), protocol)
    else:
        raise cfy_exc.NonRecoverableError(
            "Unknown rule type: {0}".format(rule_type))