1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
defmodule ExOvh.Config do
@moduledoc :false
@default_hackney_opts [connect_timeout: 8000, recv_timeout: 5000]
alias ExOvh.Defaults
@doc "Starts an agent for the storage of credentials in memory"
def start_agent(client, opts) do
Og.log("***logging context***", __ENV__, :debug)
otp_app = Keyword.get(opts, :otp_app, :false) || Og.log_r(__ENV__, :error) |> raise()
Agent.start_link(fn -> config(client, otp_app) end, name: agent_name(client))
end
@doc "Gets all the config.exs environment variables"
def get_config_from_env(client, otp_app) do
config = Application.get_env(otp_app, client)
case otp_app do
:ex_ovh -> Application.get_all_env(otp_app)
_ ->
case config do
:nil ->
temp_client = Module.split(client) |> List.delete_at(-1) |> Enum.join(".") |> String.to_atom()
temp_client = Module.concat(Elixir, temp_client)
Application.get_env(otp_app, temp_client)
config -> config
end
end
end
@doc "Gets the hackney config.exs environment variables"
def get_ovh_config_from_env(client, otp_app) do
try do
get_config_from_env(client, otp_app) |> Keyword.fetch!(:ovh)
|> Keyword.merge(Defaults.ovh(), fn(k, v1, v2) ->
case {k, v1} do
{_, :nil} -> v2
{:endpoint, v1} -> Defaults.endpoints()[v1]
_ -> v1
end
end)
rescue
_error -> Og.log_r("No ovh_config was found. ", __ENV__, :warn) |> raise()
end
end
@doc "Gets the hackney config.exs environment variables"
def get_hackney_opts_from_env(client, otp_app) do
try do
get_config_from_env(client, otp_app) |> Keyword.fetch!(:hackney)
rescue
_error ->
Og.log_r("No hackney_opts was found. " <>
"Falling back to default hackney settings #{inspect(@default_hackney_opts)}", __ENV__, :warn)
@default_hackney_opts
end
end
@doc "Gets all the config variables from a supervised Agent"
def config(client) do
Agent.get(agent_name(client), fn(config) -> config end)
end
@doc "Gets the ovh related config variables from a supervised Agent"
def ovh_config(client) do
Agent.get(agent_name(client), fn(config) -> config[:ovh] end)
end
@doc "Gets the hackney_opts related config variables from a supervised Agent"
def hackney_opts(client) do
Agent.get(agent_name(client), fn(config) -> config[:hackney] end)
end
@doc "Gets the diff"
def get_diff(client) do
Agent.get(agent_name(client), fn(config) -> config[:ovh][:diff] end)
end
defp config(client, otp_app) do
diff = calculate_diff(client, otp_app)
ovh_config = get_ovh_config_from_env(client, otp_app)
|> Keyword.put(:diff, diff)
hackney_opts = get_hackney_opts_from_env(client, otp_app)
connect_timeout = hackney_opts[:connect_timeout] || 30000 # 30 seconds
recv_timeout = hackney_opts[:recv_timeout] || (60000 * 30) # 30 minutes
[
ovh: ovh_config,
hackney:
[
timeout: connect_timeout,
recv_timeout: recv_timeout,
]
]
end
defp agent_name(client) do
Module.concat(__MODULE__, client)
end
defp calculate_diff(client, otp_app) do
api_time = api_time_request(client, otp_app)
os_t = :os.system_time(:seconds)
os_t - api_time
end
defp api_time_request(client, otp_app) do
ovh_config = get_ovh_config_from_env(client, otp_app)
method = :get
url = ovh_config[:endpoint] <> ovh_config[:api_version] <> "/auth/time"
body = ""
headers = [{"Content-Type", "application/json; charset=utf-8"}]
options = get_hackney_opts_from_env(client, otp_app)
conn = %HTTPipe.Conn{
request: %HTTPipe.Request{
method: method,
url: url,
body: body,
headers: headers,
},
adapter_options: options
}
{:ok, conn} = HTTPipe.Conn.execute(conn)
Poison.decode!(conn.response.body)
end
end