<commit_before>from cloudify import ctx
from cloudify import exceptions as cfy_exc
from cloudify.decorators import operation
from vcloud_plugin_common import with_vca_client, get_vcloud_config
from network_plugin import check_ip, save_gateway_configuration, get_vm_ip, isExternalIpAssigned, getFreeIP, CREATE, DELETE, PUBLIC_IP
from network_plugin.network import VCLOUD_NETWORK_NAME


@operation
@with_vca_client
def net_connect_to_nat(vca_client, **kwargs):
    prepare_network_operation(vca_client, CREATE)


@operation
@with_vca_client
def net_disconnect_from_nat(vca_client, **kwargs):
    prepare_network_operation(vca_client, DELETE)


@operation
@with_vca_client
def server_connect_to_nat(vca_client, **kwargs):
    prepare_vm_operation(vca_client, CREATE)


@operation
@with_vca_client
def server_disconnect_from_nat(vca_client, **kwargs):
    prepare_vm_operation(vca_client, DELETE)


def prepare_network_operation(vca_client, operation):
    try:
        network_name = ctx.source.instance.runtime_properties[VCLOUD_NETWORK_NAME]
        vdc_name = get_vcloud_config()['vdc']
        rule_type = ctx.target.node.properties['rules']['type']
        gateway = vca_client.get_gateway(vdc_name,
                                         ctx.target.node.properties['nat']['edge_gateway'])
        public_ip = _get_public_ip(ctx, gateway, operation)
    except KeyError as e:
        raise cfy_exc.NonRecoverableError("Parameter not found: {0}".format(e))
    ip_ranges = _get_network_ip_range(vca_client, vdc_name, network_name)
    ip_ranges.extend(_get_gateway_ip_range(gateway, network_name))
    nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, ip_ranges, "any", "any", "any")


def prepare_vm_operation(vca_client, operation):
    try:
        vdc_name = get_vcloud_config()['vdc']
        rule_type = ctx.target.node.properties['rules']['type']
        gateway = vca_client.get_gateway(vdc_name,
                                         ctx.target.node.properties['nat']['edge_gateway'])
        public_ip = _get_public_ip(ctx, gateway, operation)
        rule_type = ctx.target.node.properties['rules']['type']
        protocol = ctx.target.node.properties['rules']['protocol'] if 'protocol' in ctx.target.node.properties['rules'] else "any"
        original_port = ctx.target.node.properties['rules']['original_port'] if 'original_port' in ctx.target.node.properties['rules'] else "any"
        translated_port = ctx.target.node.properties['rules']['translated_port'] if 'translated_port' in ctx.target.node.properties['rules'] else "any"
    except KeyError as e:
        raise cfy_exc.NonRecoverableError("Parameter not found: {0}".format(e))
    private_ip = check_ip(get_vm_ip(vca_client, ctx))
    nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, [private_ip], original_port, translated_port, protocol)


def nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, private_ip, original_port, translated_port, protocol):
    ctx.logger.info("Public Ip {0}".format(public_ip))
    function = None
    message = None
    if operation == CREATE:
        if isExternalIpAssigned(public_ip, gateway):
            raise cfy_exc.NonRecoverableError("Public IP {0} is already allocated".format(public_ip))
        function = gateway.add_nat_rule
        message = "Create"
    elif operation == DELETE:
        function = gateway.del_nat_rule
        message = "Delete"
    else:
        raise cfy_exc.NonRecoverableError("Unknown operation: {0}".format(operation))

    for rule in rule_type:
        for ip in private_ip:
            ctx.logger.info("{6} NAT rule: original_ip '{0}',"
                            " translated_ip '{1}', rule type '{2},"
                            " protocol {3}, original_port {4}, translated_port {5}'"
                            .format(ip, public_ip, rule, protocol, original_port, translated_port, message))
            if rule == "SNAT":
                # for SNAT type ports and protocol must by "any", because they are not configurable
                function(<commit_msg>Refactoring code<commit_after>from cloudify import ctx
from cloudify import exceptions as cfy_exc
from cloudify.decorators import operation
from vcloud_plugin_common import with_vca_client, get_vcloud_config
from network_plugin import check_ip, save_gateway_configuration, get_vm_ip, isExternalIpAssigned, getFreeIP, CREATE, DELETE, PUBLIC_IP
from network_plugin.network import VCLOUD_NETWORK_NAME


@operation
@with_vca_client
def net_connect_to_nat(vca_client, **kwargs):
    prepare_network_operation(vca_client, CREATE)


@operation
@with_vca_client
def net_disconnect_from_nat(vca_client, **kwargs):
    prepare_network_operation(vca_client, DELETE)


@operation
@with_vca_client
def server_connect_to_nat(vca_client, **kwargs):
    prepare_vm_operation(vca_client, CREATE)


@operation
@with_vca_client
def server_disconnect_from_nat(vca_client, **kwargs):
    prepare_vm_operation(vca_client, DELETE)


def prepare_network_operation(vca_client, operation):
    try:
        network_name = ctx.source.instance.runtime_properties[VCLOUD_NETWORK_NAME]
        vdc_name = get_vcloud_config()['vdc']
        rule_type = ctx.target.node.properties['rules']['type']
        gateway = vca_client.get_gateway(vdc_name,
                                         ctx.target.node.properties['nat']['edge_gateway'])
        public_ip = _get_public_ip(ctx, gateway, operation)
    except KeyError as e:
        raise cfy_exc.NonRecoverableError("Parameter not found: {0}".format(e))
    ip_ranges = _get_network_ip_range(vca_client, vdc_name, network_name)
    ip_ranges.extend(_get_gateway_ip_range(gateway, network_name))
    nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, ip_ranges, "any", "any", "any")


def prepare_vm_operation(vca_client, operation):
    try:
        vdc_name = get_vcloud_config()['vdc']
        rule_type = ctx.target.node.properties['rules']['type']
        gateway = vca_client.get_gateway(vdc_name,
                                         ctx.target.node.properties['nat']['edge_gateway'])
        public_ip = _get_public_ip(ctx, gateway, operation)
        rule_type = ctx.target.node.properties['rules']['type']
        protocol = ctx.target.node.properties['rules'].get('protocol', "any")
        original_port = ctx.target.node.properties['rules'].get('original_port', "any")
        translated_port = ctx.target.node.properties['rules'].get('translated_port', "any")
    except KeyError as e:
        raise cfy_exc.NonRecoverableError("Parameter not found: {0}".format(e))
    private_ip = check_ip(get_vm_ip(vca_client, ctx))
    nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, [private_ip], original_port, translated_port, protocol)


def nat_network_operation(vca_client, gateway, operation, rule_type, public_ip, private_ip, original_port, translated_port, protocol):
    ctx.logger.info("Public Ip {0}".format(public_ip))
    function = None
    message = None
    if operation == CREATE:
        if isExternalIpAssigned(public_ip, gateway):
            raise cfy_exc.NonRecoverableError("Public IP {0} is already allocated".format(public_ip))
        function = gateway.add_nat_rule
        message = "Create"
    elif operation == DELETE:
        function = gateway.del_nat_rule
        message = "Delete"
    else:
        raise cfy_exc.NonRecoverableError("Unknown operation: {0}".format(operation))

    for rule in rule_type:
        for ip in private_ip:
            ctx.logger.info("{6} NAT rule: original_ip '{0}',"
                            " translated_ip '{1}', rule type '{2},"
                            " protocol {3}, original_port {4}, translated_port {5}'"
                            .format(ip, public_ip, rule, protocol, original_port, translated_port, message))
            if rule == "SNAT":
                # for SNAT type ports and protocol must by "any", because they are not configurable
                function(
{'fraction_non_alphanumeric': 0.10346115537848606, 'fraction_numerical': 0.00298804780876494, 'mean_word_length': 3.7901013714967204, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459454', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2438, 'n_words': 505}