<commit_before>########
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.


from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError

from cloudstack_plugin.cloudstack_common import (
    get_cloud_driver,
    get_floating_ip_by_id,
    get_node_by_id,
    get_network_by_id,
    get_nic_by_node_and_network_id,
    get_public_ip_by_id,
    get_portmaps_by_node_id,
    USE_EXTERNAL_RESOURCE_PROPERTY,
    CLOUDSTACK_ID_PROPERTY,
    CLOUDSTACK_TYPE_PROPERTY,
    CLOUDSTACK_NAME_PROPERTY,
    COMMON_RUNTIME_PROPERTIES_KEYS,
    get_resource_id

)

FLOATINGIP_CLOUDSTACK_TYPE = 'floatingip'

# Runtime properties
IP_ADDRESS_PROPERTY = 'floating_ip_address'
RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS + \
    [IP_ADDRESS_PROPERTY]

__author__ = 'boul'


@operation
def connect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    network = get_network_by_id(ctx, cloud_driver, network_id)
    #firewall_rules = ctx.node.target.properties.get(['firewall'][0], None)

    if network.extra['vpc_id'] is not None:

        ctx.logger.info('Acquiring IP for VPC with id: {0}'
                        .format(network.extra['vpc_id']))

        fip = cloud_driver.ex_allocate_public_ip(vpc_id=
                                                 network.extra['vpc_id'])

    elif network.id is not None:

        ctx.logger.info('Acquiring IP for network with id: {0}'.
                        format(network.id))

        fip = cloud_driver.ex_allocate_public_ip(network_id=network.id)

        if 'firewall' in ctx.target.node.properties:
                firewall_config = ctx.target.node.properties['firewall']

                ingress_rules = [rule for rule in firewall_config if
                                 rule['type'] == 'ingress']

                for rule in ingress_rules:
                    rule_cidr = rule.get('cidr')
                    rule_protocol = rule.get('protocol')
                    rule_ports = rule.get('ports')

                    for port in rule_ports:
                        cloud_driver.ex_create_firewall_rule(
                            address=fip,
                            cidr_list=rule_cidr,
                            protocol=rule_protocol,
                            start_port=port,
                            end_port=port
                        )

    else:
        raise NonRecoverableError('Cannot resolve network or vpc id')

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = fip.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        FLOATINGIP_CLOUDSTACK_TYPE
    ctx.source.instance.runtime_properties[IP_ADDRESS_PROPERTY] = fip.address


@operation
def disconnect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    fip_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    fip = get_floating_ip_by_id(ctx, cloud_driver, fip_id)

    firewall_rules = [rule for rule in cloud_driver.ex_list_firewall_rules() if
                      rule.address == fip]

    for rule in firewall_rules:

        ctx.logger.info('Deleting fw rule: {0}'.format(rule))
        cloud_driver.ex_delete_firewall_rule(rule.id)



    ctx.logger.info('Deleting floating ip: {0}'.format(fip))

    cloud_driver.ex_release_public_ip(address=fip)
<commit_msg>compare id<commit_after>########
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.


from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError

from cloudstack_plugin.cloudstack_common import (
    get_cloud_driver,
    get_floating_ip_by_id,
    get_node_by_id,
    get_network_by_id,
    get_nic_by_node_and_network_id,
    get_public_ip_by_id,
    get_portmaps_by_node_id,
    USE_EXTERNAL_RESOURCE_PROPERTY,
    CLOUDSTACK_ID_PROPERTY,
    CLOUDSTACK_TYPE_PROPERTY,
    CLOUDSTACK_NAME_PROPERTY,
    COMMON_RUNTIME_PROPERTIES_KEYS,
    get_resource_id

)

FLOATINGIP_CLOUDSTACK_TYPE = 'floatingip'

# Runtime properties
IP_ADDRESS_PROPERTY = 'floating_ip_address'
RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS + \
    [IP_ADDRESS_PROPERTY]

__author__ = 'boul'


@operation
def connect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    network_id = ctx.target.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    network = get_network_by_id(ctx, cloud_driver, network_id)
    #firewall_rules = ctx.node.target.properties.get(['firewall'][0], None)

    if network.extra['vpc_id'] is not None:

        ctx.logger.info('Acquiring IP for VPC with id: {0}'
                        .format(network.extra['vpc_id']))

        fip = cloud_driver.ex_allocate_public_ip(vpc_id=
                                                 network.extra['vpc_id'])

    elif network.id is not None:

        ctx.logger.info('Acquiring IP for network with id: {0}'.
                        format(network.id))

        fip = cloud_driver.ex_allocate_public_ip(network_id=network.id)

        if 'firewall' in ctx.target.node.properties:
                firewall_config = ctx.target.node.properties['firewall']

                ingress_rules = [rule for rule in firewall_config if
                                 rule['type'] == 'ingress']

                for rule in ingress_rules:
                    rule_cidr = rule.get('cidr')
                    rule_protocol = rule.get('protocol')
                    rule_ports = rule.get('ports')

                    for port in rule_ports:
                        cloud_driver.ex_create_firewall_rule(
                            address=fip,
                            cidr_list=rule_cidr,
                            protocol=rule_protocol,
                            start_port=port,
                            end_port=port
                        )

    else:
        raise NonRecoverableError('Cannot resolve network or vpc id')

    ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY] = fip.id
    ctx.source.instance.runtime_properties[CLOUDSTACK_TYPE_PROPERTY] = \
        FLOATINGIP_CLOUDSTACK_TYPE
    ctx.source.instance.runtime_properties[IP_ADDRESS_PROPERTY] = fip.address


@operation
def disconnect_network(ctx, **kwargs):

    cloud_driver = get_cloud_driver(ctx)

    fip_id = ctx.source.instance.runtime_properties[CLOUDSTACK_ID_PROPERTY]
    fip = get_floating_ip_by_id(ctx, cloud_driver, fip_id)

    firewall_rules = [rule for rule in cloud_driver.ex_list_firewall_rules() if
                      fip.id == rule.address.id]

    for rule in firewall_rules:

        ctx.logger.info('Deleting fw rule: {0}'.format(rule))
        cloud_driver.ex_delete_firewall_rule(rule.id)



    ctx.logger.info('Deleting floating ip: {0}'.format(fip))

    cloud_driver.ex_release_public_ip(address=fip)

{'fraction_non_alphanumeric': 0.08132568514977692, 'fraction_numerical': 0.002294455066921606, 'mean_word_length': 2.6904985888993416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '221582', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2379, 'n_words': 489}