def _save_configuration(gateway, vca_client, operation, public_ip):
    """
        save/refresh nat rules on gateway
    """
    ctx.logger.info("Save NAT configuration.")
    success = save_gateway_configuration(gateway, vca_client)
    if not success:
        return False
    ctx.logger.info("NAT configuration has been saved.")
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip
    else:
        service_type = get_vcloud_config().get('service_type')
        if is_ondemand(service_type):
            if not ctx.target.node.properties['nat'].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 PORT_REPLACEMENT in ctx.target.instance.runtime_properties:
            del ctx.target.instance.runtime_properties[PORT_REPLACEMENT]
    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
        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)
    if not save_gateway_configuration(gateway, vca_client):
        return ctx.operation.retry(message='Waiting for gateway.',
                                   retry_after=10)

    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = 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)
        del ctx.target.instance.runtime_properties[PUBLIC_IP]
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'])
    if gateway.is_busy():
        return False
    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
        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)
    if not success:
        return False
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = 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)
        del ctx.target.instance.runtime_properties[PUBLIC_IP]
    return True
def _save_configuration(gateway, vca_client, operation, public_ip):
    if not save_gateway_configuration(gateway, vca_client):
        return ctx.operation.retry(message='Waiting for gateway.',
                                   retry_after=10)
    ctx.logger.info("NAT configuration has been saved")
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip
    else:
        service_type = get_vcloud_config().get('service_type')
        if is_ondemand(service_type):
            if not ctx.target.node.properties['nat'].get(PUBLIC_IP):
                del_ondemand_public_ip(
                    vca_client, gateway,
                    ctx.target.instance.runtime_properties[PUBLIC_IP], ctx)
        del ctx.target.instance.runtime_properties[PUBLIC_IP]
def _save_configuration(gateway, vca_client, operation, public_ip):
    """
        save/refresh nat rules on gateway
    """
    if not save_gateway_configuration(gateway, vca_client):
        return ctx.operation.retry(message='Waiting for gateway.',
                                   retry_after=10)
    ctx.logger.info("NAT configuration has been saved")
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip
    else:
        service_type = get_vcloud_config().get('service_type')
        if is_ondemand(service_type):
            if not ctx.target.node.properties['nat'].get(PUBLIC_IP):
                del_ondemand_public_ip(
                    vca_client, gateway,
                    ctx.target.instance.runtime_properties[PUBLIC_IP], ctx)
        del ctx.target.instance.runtime_properties[PUBLIC_IP]
 def test_del_ondemand_public_ip(self):
     """
         test release public ip
     """
     fake_client = self.generate_client()
     gateway = self.generate_gateway()
     fake_ctx = self.generate_node_context()
     # can't deallocate ip
     gateway.deallocate_public_ip = mock.MagicMock(return_value=None)
     with self.assertRaises(cfy_exc.NonRecoverableError):
         network_plugin.del_ondemand_public_ip(fake_client, gateway,
                                               '127.0.0.1', fake_ctx)
     gateway.deallocate_public_ip.assert_called_with('127.0.0.1')
     # successfully dropped public ip
     gateway.deallocate_public_ip = mock.MagicMock(
         return_value=self.generate_task(
             vcloud_plugin_common.TASK_STATUS_SUCCESS))
     network_plugin.del_ondemand_public_ip(fake_client, gateway,
                                           '127.0.0.1', fake_ctx)
 def test_del_ondemand_public_ip(self):
     vca_client = self.generate_client()
     gateway = self.generate_gateway()
     fake_ctx = self.generate_node_context()
     # cant deallocate ip
     gateway.deallocate_public_ip = mock.MagicMock(return_value=None)
     with self.assertRaises(cfy_exc.NonRecoverableError):
         network_plugin.del_ondemand_public_ip(
             vca_client, gateway, '127.0.0.1', fake_ctx
         )
     gateway.deallocate_public_ip.assert_called_with('127.0.0.1')
     # successfully dropped public ip
     gateway.deallocate_public_ip = mock.MagicMock(
         return_value=self.generate_task(
             vcloud_plugin_common.TASK_STATUS_SUCCESS
         )
     )
     network_plugin.del_ondemand_public_ip(
         vca_client, gateway, '127.0.0.1', fake_ctx
     )
Exemple #8
0
def _save_configuration(gateway, vca_client, operation, public_ip):
    """
        save/refresh nat rules on gateway
    """
    ctx.logger.info("Save NAT configuration.")
    success = save_gateway_configuration(gateway, vca_client)
    if not success:
        return False
    ctx.logger.info("NAT configuration has been saved.")
    if operation == CREATE:
        ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip
    else:
        service_type = get_vcloud_config().get('service_type')
        if is_ondemand(service_type):
            if not ctx.target.node.properties['nat'].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 PORT_REPLACEMENT in ctx.target.instance.runtime_properties:
            del ctx.target.instance.runtime_properties[PORT_REPLACEMENT]
    return True