Commit e3ef2c1a147296935db2af41e62e447a8171552c

Stephen Moloney 2016-05-15T20:35:57

set the temp_url_key optionally

diff --git a/config/dev.exs b/config/dev.exs
index 4651724..e63841b 100644
--- a/config/dev.exs
+++ b/config/dev.exs
@@ -32,23 +32,9 @@ config :ex_ovh,
           cloudstorage: [
                           tenant_id: System.get_env("EX_OVH_CLOUDSTORAGE_TENANT_ID"), # mandatory, corresponds to a project id
                           user_id: System.get_env("EX_OVH_CLOUDSTORAGE_USER_ID"), # optional, if absent a user will be created using the ovh api.
+                          temp_url_key: System.get_env("EX_OVH_CLOUDSTORAGE_TEMP_URL_KEY"), # defaults to :nil if absent and won't be added if == :nil.
                           keystone_endpoint: "https://auth.cloud.ovh.net/v2.0", # default endpoint for keystone (identity) auth
                           region: :nil, # defaults to "SBG1" if set to :nil
                           type: :cloudstorage
                         ]
-         ]
-
-
-#config :my_app, MyApp.OvhClient,
-#  ... then as above
-
-# SAMPLE CONFIGURATIONS ON A PER APP AND PER API BASIS FOR OPENSTEX
-
-#config :my_app, MyApp.OvhClient.Ovh, <-- For OVH part of the api
-#  httpoison: ... as above
-
-#config :my_app, MyApp.OvhClient.Swift.Webstorage, <-- For Openstack Webstorage part of the api
-#  httpoison: ... as above
-
-#config :my_app, MyApp.OvhClient.Swift.Cloudstorage, <-- <-- For Openstack Cloudstorage part of the api
-#  httpoison: ... as above
+         ]
\ No newline at end of file
diff --git a/config/test.exs b/config/test.exs
deleted file mode 100644
index c3970ca..0000000
--- a/config/test.exs
+++ /dev/null
@@ -1,29 +0,0 @@
-use Mix.Config
-
-
-config :logger,
-  backends: [:console],
-  compile_time_purge_level: :error
-
-
-config :ex_ovh,
-  ovh: [
-    application_key: System.get_env("EX_OVH_APPLICATION_KEY"),
-    application_secret: System.get_env("EX_OVH_APPLICATION_SECRET"),
-    consumer_key: System.get_env("EX_OVH_CONSUMER_KEY"),
-    endpoint: System.get_env("EX_OVH_ENDPOINT"),
-    api_version: System.get_env("EX_OVH_API_VERSION") || "1.0"
-  ],
-  swift: [
-          webstorage: [
-                        cdn_name: System.get_env("EX_OVH_WEBSTORAGE_CDN_NAME"),
-                        type: :webstorage
-                      ],
-          cloudstorage: [
-                          tenant_id: System.get_env("EX_OVH_CLOUDSTORAGE_TENANT_ID"), # mandatory, corresponds to a project id
-                          user_id: System.get_env("EX_OVH_CLOUDSTORAGE_USER_ID"), # optional, if absent a user will be created using the ovh api.
-                          keystone_endpoint: "https://auth.cloud.ovh.net/v2.0", # default endpoint for keystone (identity) auth
-                          region: :nil, # defaults to "SBG1" if set to :nil
-                          type: :cloudstorage
-                        ]
-         ]
\ No newline at end of file
diff --git a/lib/auth/openstack/swift/cache.ex b/lib/auth/openstack/swift/cache.ex
index a30e655..cdc5989 100644
--- a/lib/auth/openstack/swift/cache.ex
+++ b/lib/auth/openstack/swift/cache.ex
@@ -64,10 +64,9 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     create_ets_table(swift_client)
     config = swift_client.config()
     identity = create_identity({ovh_client, swift_client}, config, config[:type])
-    Og.context(__ENV__, :debug)
-
     identity = Map.put(identity, :lock, :false)
     :ets.insert(ets_tablename(swift_client), {:identity, identity})
+    try_to_set_temp_url_key(swift_client)
     expiry = to_seconds(identity)
     Task.start_link(fn -> monitor_expiry(expiry) end)
     {:ok, {swift_client, identity}}
@@ -124,6 +123,24 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   end
 
 
+  defp try_to_set_temp_url_key(swift_client) do
+    if Keyword.has_key?(swift_client.config(), :temp_url_key) do
+      temp_key = Keyword.get(swift_client.config() |> Og.log_return(__ENV__), :temp_url_key, :nil)
+      |> Og.log_return(__ENV__)
+      account = swift_client.account()
+      if temp_key != :nil do
+        resp = Openstex.Swift.V1.Query.account_info(account) |> swift_client.request!()
+        unless Map.has_key?(resp.headers, "X-Account-Meta-Temp-Url-Key") do
+          %Openstex.Openstack.Swift.Query{method: :post, uri: account, params: :nil}
+          |> swift_client.prepare_request()
+          |> Openstex.Utils.put_http_headers(%{"X-Account-Meta-Temp-URL-Key" => temp_key})
+          |> swift_client.request!()
+        end
+      end
+    end
+  end
+
+
   defp get_identity(swift_client) do
       get_identity(swift_client, 0)
   end
diff --git a/lib/auth/ovh/cache.ex b/lib/auth/ovh/cache.ex
index 986fdce..cc43cda 100644
--- a/lib/auth/ovh/cache.ex
+++ b/lib/auth/ovh/cache.ex
@@ -25,19 +25,17 @@ defmodule ExOvh.Auth.Ovh.Cache do
   def init(client) do
     Og.context(__ENV__, :debug)
     diff = calculate_diff(client)
-    |> Og.log_return(:error)
     {:ok, diff}
   end
 
   def handle_call(:get_diff, _from, diff) do
     Og.context(__ENV__, :debug)
     {:reply, diff, diff}
-    |> Og.log_return(__ENV__, :warn)
   end
 
   def terminate(:shutdown, state) do
     Og.context(__ENV__, :warn)
-    Og.log_return("gen_server #{__MODULE__} shutting down", :warn)
+    Og.log_return(__ENV__, "gen_server #{__MODULE__} shutting down", :warn)
     :ok
   end
 
@@ -64,7 +62,6 @@ defmodule ExOvh.Auth.Ovh.Cache do
     client |> Og.log_return(__ENV__)
     httpoison_opts = client.httpoison_config()
     options = httpoison_opts
-    {method, uri, body, headers, options} |> Og.log_return(__ENV__, :warn)
     resp = HTTPoison.request!(method, uri, body, headers, options)
     api_time = Poison.decode!(resp.body)
   end
diff --git a/lib/utils/utils.ex b/lib/utils/utils.ex
index 9d72d35..9971c5c 100644
--- a/lib/utils/utils.ex
+++ b/lib/utils/utils.ex
@@ -24,16 +24,6 @@ defmodule ExOvh.Utils do
   end
 
   @doc """
-  Changes the timeout option for a http_query.
-  """
-  @spec change_http_query_timeout(Openstex.HttpQuery.t, integer) :: Openstex.HttpQuery.t
-  def change_http_query_timeout(%Openstex.HttpQuery{options: options} = http_query, new_timeout) do
-    new_options = Map.merge(options, Map.put(options, :timeout, new_timeout))
-    Map.put(http_query, :options, new_options)
-  end
-
-
-  @doc """
   Returns a string with the formatted date
   """
   @spec formatted_date() :: String.t