<commit_before>module Cyoi::Cli::Addresses; end
class Cyoi::Cli::Addresses::AddressCliOpenstack
  attr_reader :provider_client
  attr_reader :attributes
  attr_reader :hl

  def initialize(provider_client, attributes, highline)
    @provider_client = provider_client
    @hl = highline
    @attributes = attributes.is_a?(Hash) ? ReadWriteSettings.new(attributes) : attributes
    raise "@attributes must be ReadWriteSettings (or Hash)" unless @attributes.is_a?(ReadWriteSettings)
  end

  def perform_and_return_attributes
    unless valid_address?
      if networks?
        subnet = select_subnet
        choose_address_from_subnet(subnet)
      else
        provision_address
      end
    end
    export_attributes
  end

  # helper to export the complete nested attributes.
  def export_attributes
    attributes.to_nested_hash
  end


  def valid_address?
    attributes["ip"]
  end

  def display_confirmation
    puts "\n"
    puts "Confirming: Using address #{attributes.ip}"
  end

  protected
  def provision_address
    print "Acquiring a public IP address... "
    attributes["ip"] = provider_client.provision_public_ip_address
    puts attributes.ip
  end

  def networks?
    provider_client.networks?
  end

  def select_subnet
    subnets = provider_client.subnets
    subnet = if subnets.size == 0
      $stderr.puts "ERROR: Your OpenStack is configured for Neutron networking but you have not yet created any subnets."
      exit 1
    elsif subnets.size == 1
      subnets.first
    else
      hl.choose do |menu|
        menu.prompt = "Choose a subnet: "
        # menu.choice("AWS") { "aws" }
        subnets.each do |subnet|
          menu.choice("#{pretty_subnet_name(subnet)}") { subnet }
        end
      end
    end
    attributes["subnet_id"] = subnet.id
    subnet
  end

  def choose_address_from_subnet(subnet)
    ip = provider_client.next_available_ip_in_subnet(subnet)
    puts "Auto-selected IP #{ip} from subnet for Micro BOSH"
    attributes["ip"] = ip
  end

  def pretty_ip_pool_ranges(subnet)
    ranges = subnet.allocation_pools.map do |pool|
      "#{pool['start']}-#{pool['end']}"
    end
    ranges.join(',')
  end

  def pretty_subnet_name(subnet)
    "#{subnet.name} (#{subnet.cidr})"
  end
end

Cyoi::Cli::Address.register_address_cli("openstack", Cyoi::Cli::Addresses::AddressCliOpenstack)<commit_msg>remove 'Micro BOSH' reference from this common library<commit_after>module Cyoi::Cli::Addresses; end
class Cyoi::Cli::Addresses::AddressCliOpenstack
  attr_reader :provider_client
  attr_reader :attributes
  attr_reader :hl

  def initialize(provider_client, attributes, highline)
    @provider_client = provider_client
    @hl = highline
    @attributes = attributes.is_a?(Hash) ? ReadWriteSettings.new(attributes) : attributes
    raise "@attributes must be ReadWriteSettings (or Hash)" unless @attributes.is_a?(ReadWriteSettings)
  end

  def perform_and_return_attributes
    unless valid_address?
      if networks?
        subnet = select_subnet
        choose_address_from_subnet(subnet)
      else
        provision_address
      end
    end
    export_attributes
  end

  # helper to export the complete nested attributes.
  def export_attributes
    attributes.to_nested_hash
  end


  def valid_address?
    attributes["ip"]
  end

  def display_confirmation
    puts "\n"
    puts "Confirming: Using address #{attributes.ip}"
  end

  protected
  def provision_address
    print "Acquiring a public IP address... "
    attributes["ip"] = provider_client.provision_public_ip_address
    puts attributes.ip
  end

  def networks?
    provider_client.networks?
  end

  def select_subnet
    subnets = provider_client.subnets
    subnet = if subnets.size == 0
      $stderr.puts "ERROR: Your OpenStack is configured for Neutron networking but you have not yet created any subnets."
      exit 1
    elsif subnets.size == 1
      subnets.first
    else
      hl.choose do |menu|
        menu.prompt = "Choose a subnet: "
        # menu.choice("AWS") { "aws" }
        subnets.each do |subnet|
          menu.choice("#{pretty_subnet_name(subnet)}") { subnet }
        end
      end
    end
    attributes["subnet_id"] = subnet.id
    subnet
  end

  def choose_address_from_subnet(subnet)
    ip = provider_client.next_available_ip_in_subnet(subnet)
    puts "Auto-selected IP #{ip} from subnet"
    attributes["ip"] = ip
  end

  def pretty_ip_pool_ranges(subnet)
    ranges = subnet.allocation_pools.map do |pool|
      "#{pool['start']}-#{pool['end']}"
    end
    ranges.join(',')
  end

  def pretty_subnet_name(subnet)
    "#{subnet.name} (#{subnet.cidr})"
  end
end

Cyoi::Cli::Address.register_address_cli("openstack", Cyoi::Cli::Addresses::AddressCliOpenstack)
{'fraction_non_alphanumeric': 0.09183241641980533, 'fraction_numerical': 0.0012695725772323319, 'mean_word_length': 3.3728029602220166, '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': '1214165', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1485, 'n_words': 359}