defmodule Wabanex.DevDNSClusterResolver do
  @moduledoc """
  Local DNS resolver for cluster query.
  """
  require Record

  Record.defrecord(:hostent, Record.extract(:hostent, from_lib: "kernel/include/inet.hrl"))

  def basename(node_name) when is_atom(node_name) do
    [basename, _] = node_name |> to_string() |> String.split("@")
    basename
  end

  def connect_node(node_name) when is_atom(node_name) do
    Node.connect(node_name)
  end

  def list_nodes, do: Node.list(:visible)

  def lookup(query, type) when is_binary(query) and type in [:a, :aaaa] do
    case :inet_res.getbyname(~c"#{query}", type) do
      {:ok, hostent(h_addr_list: addr_list)} -> addr_list
      {:error, _} -> []
    end
  end
end
