<commit_before>module Bosh::Director
  module DeploymentPlan
    class IpProviderV2
      include IpUtil

      def initialize(ip_repo, vip_repo, using_global_networking, logger)
        @logger = logger
        @ip_repo = ip_repo
        @using_global_networking = using_global_networking
        @vip_repo = vip_repo
      end

      def release(reservation)
        return if reservation.network.is_a?(DynamicNetwork)

        if reservation.ip.nil?
          @logger.error("Failed to release IP for manual network '#{reservation.network.name}': IP must be provided")
          raise Bosh::Director::NetworkReservationIpMissing, "Can't release reservation without an IP"
        else
          ip_repo = reservation.network.is_a?(VipNetwork) ? @vip_repo : @ip_repo
          ip_repo.delete(reservation.ip, reservation.network.name)
        end
      end

      def reserve(reservation)
        return if reservation.network.is_a?(DynamicNetwork)

        if reservation.network.is_a?(VipNetwork)
          reservation.validate_type(StaticNetworkReservation)

          @logger.debug("Reserving IP '#{format_ip(reservation.ip)}' for vip network '#{reservation.network.name}'")
          @vip_repo.add(reservation)
          reservation.mark_reserved_as(StaticNetworkReservation)
          return
        end

        if reservation.ip.nil?
          if reservation.is_a?(DynamicNetworkReservation)
            @logger.debug("Allocating dynamic ip for manual network '#{reservation.network.name}'")

            filter_subnet_by_instance_az(reservation).each do |subnet|
              @logger.debug("Trying to allocate a dynamic IP in subnet'#{subnet.inspect}'")
              if @using_global_networking
                ip = @ip_repo.allocate_dynamic_ip(reservation, subnet)
                if ip
                  @logger.debug("Reserving dynamic IP '#{format_ip(ip)}' for manual network '#{reservation.network.name}'")
                  reservation.resolve_ip(ip)
                  reservation.mark_reserved_as(DynamicNetworkReservation)
                  return
                end
              else
                ip = @ip_repo.get_dynamic_ip(subnet)
                if ip
                  @logger.debug("Reserving dynamic IP '#{format_ip(ip)}' for manual network '#{reservation.network.name}'")

                  reservation.resolve_ip(ip)
                  @ip_repo.add(reservation)
                  reservation.mark_reserved_as(DynamicNetworkReservation)
                  return
                end
              end
            end

            raise NetworkReservationNotEnoughCapacity,
              "Failed to reserve IP for '#{reservation.instance}' for manual network '#{reservation.network.name}': no more available"
          else
            # TODO: is this case even possible?
          end
        end

        if reservation.ip
          cidr_ip = format_ip(reservation.ip)
          @logger.debug("Reserving #{reservation.desc} for manual network '#{reservation.network.name}'")

          subnet = find_subnet_containing(reservation)<commit_msg>Removing unneeded conditional in the IPProviderV2.<commit_after>module Bosh::Director
  module DeploymentPlan
    class IpProviderV2
      include IpUtil

      def initialize(ip_repo, vip_repo, using_global_networking, logger)
        @logger = logger
        @ip_repo = ip_repo
        @using_global_networking = using_global_networking
        @vip_repo = vip_repo
      end

      def release(reservation)
        return if reservation.network.is_a?(DynamicNetwork)

        if reservation.ip.nil?
          @logger.error("Failed to release IP for manual network '#{reservation.network.name}': IP must be provided")
          raise Bosh::Director::NetworkReservationIpMissing, "Can't release reservation without an IP"
        else
          ip_repo = reservation.network.is_a?(VipNetwork) ? @vip_repo : @ip_repo
          ip_repo.delete(reservation.ip, reservation.network.name)
        end
      end

      def reserve(reservation)
        return if reservation.network.is_a?(DynamicNetwork)

        if reservation.network.is_a?(VipNetwork)
          reservation.validate_type(StaticNetworkReservation)

          @logger.debug("Reserving IP '#{format_ip(reservation.ip)}' for vip network '#{reservation.network.name}'")
          @vip_repo.add(reservation)
          reservation.mark_reserved_as(StaticNetworkReservation)
          return
        end

        if reservation.ip.nil?
          @logger.debug("Allocating dynamic ip for manual network '#{reservation.network.name}'")

          filter_subnet_by_instance_az(reservation).each do |subnet|
            @logger.debug("Trying to allocate a dynamic IP in subnet'#{subnet.inspect}'")
            if @using_global_networking
              ip = @ip_repo.allocate_dynamic_ip(reservation, subnet)
              if ip
                @logger.debug("Reserving dynamic IP '#{format_ip(ip)}' for manual network '#{reservation.network.name}'")
                reservation.resolve_ip(ip)
                reservation.mark_reserved_as(DynamicNetworkReservation)
                return
              end
            else
              ip = @ip_repo.get_dynamic_ip(subnet)
              if ip
                @logger.debug("Reserving dynamic IP '#{format_ip(ip)}' for manual network '#{reservation.network.name}'")

                reservation.resolve_ip(ip)
                @ip_repo.add(reservation)
                reservation.mark_reserved_as(DynamicNetworkReservation)
                return
              end
            end
          end

          raise NetworkReservationNotEnoughCapacity,
            "Failed to reserve IP for '#{reservation.instance}' for manual network '#{reservation.network.name}': no more available"
        end

        if reservation.ip
          cidr_ip = format_ip(reservation.ip)
          @logger.debug("Reserving #{reservation.desc} for manual network '#{reservation.network.name}'")

          subnet = find_subnet_containing(reservation)
{'fraction_non_alphanumeric': 0.08563812062645784, 'fraction_numerical': 0.0004998333888703765, 'mean_word_length': 2.348020078081428, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2373238', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1591, 'n_words': 342}