Commit 67ac5ee54759e1ce196dc4b07302901013eea8a8

Stephen Moloney 2016-02-29T00:47:19

bug fixes and update readme

diff --git a/README.md b/README.md
index 534773e..c90654f 100644
--- a/README.md
+++ b/README.md
@@ -113,6 +113,17 @@ Shell Output
          }
   ```
 
+
+### Add :httpotion to applications on startup (httpotion is used for http requests) 
+
+```elixir
+  def application do 
+    [ 
+      applications: [:httpotion]
+    ]
+  end
+```
+
 ### Starting the supervisor
 
 Add the supervisor to your supervision tree:
@@ -166,11 +177,12 @@ Get the count of containers again
   container_count2 = resp.body |> Enum.count()
   container_count1 + 1 == container_count2
   ```
-  
-  
+ 
+ 
 ### Example 2: 
 
-Adding an object to the "default" container in OVH Webstorage
+
+Adding an object to the "default" container in [OVH CDN Webstorage](https://www.ovh.ie/cdn/webstorage/)
 
     import ExOvh.Query.Openstack.Swift
     alias ExOvh.Ovh.OpenstackApi.Webstorage.Cache, as: OpenCache
@@ -187,7 +199,7 @@ Adding an object to the "default" container in OVH Webstorage
     ExOvh.ovh_request(create_file_request, %{ openstack: :true, webstorage: service })
 
 
-Listing all objects for "default" container to see if the new `server_object` is there.
+Listing all objects for "default" container to see if the new `server_object` is there in [OVH CDN Webstorage](https://www.ovh.ie/cdn/webstorage/)
 
     import ExOvh.Query.Openstack.Swift
     alias ExOvh.Ovh.OpenstackApi.Webstorage.Cache, as: OpenCache
@@ -228,6 +240,7 @@ Listing all objects for "default" container to see if the new `server_object` is
 - [ ] *Needed* - generate release and publish to hex packages
 - [ ] *Needed* - *Tests* - add basic tests for most api calls.
 - [ ] *Needed* - *Tests* - verify the supervisor chain, genservers and genserver naming is working ok.
+- [ ] *Needed* - *New functions* - ovh and hubic functions with !.
 
  
 - [ ] *Maybe* - investigate ways to add sensitive keys, secrets, etc to system env and allow the config.exs to get variables from `System`.
diff --git a/lib/ovh/auth.ex b/lib/ovh/auth.ex
index 476ddd6..00b1163 100644
--- a/lib/ovh/auth.ex
+++ b/lib/ovh/auth.ex
@@ -32,7 +32,7 @@ defmodule ExOvh.Ovh.Auth do
   3. Returning `ExOvh.Client.query_t` which is a tuple of the format {method, uri, options} which
      can then be easily used to make requests using HTTPpotion, `ovh_request` or `hubic_request`.
   """
-  alias ExOvh.Ovh.Openstack.Auth, as: OpenstackAuth
+  alias ExOvh.Ovh.OpenstackApi.Webstorage.Auth, as: Webstorage
   alias ExOvh.Ovh.OvhApi.Auth, as: OvhAuth
 
 
@@ -57,8 +57,8 @@ defmodule ExOvh.Ovh.Auth do
   """
   @spec prepare_request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map())
                      :: ExOvh.Client.query_t
-  def prepare_request(client, {method, uri, params} = query, %{openstack: :true} = opts) do
-    OpenstackAuth.prepare_request(client, query)
+  def prepare_request(client, {method, uri, params} = query, %{ openstack: :true, webstorage: service } = opts) do
+    Webstorage.prepare_request(client, query, service)
   end
 
   def prepare_request(client, {method, uri, params} = query, opts) do
diff --git a/lib/ovh/openstack_api/webstorage/cache.ex b/lib/ovh/openstack_api/webstorage/cache.ex
index 39b77ca..3f9620c 100644
--- a/lib/ovh/openstack_api/webstorage/cache.ex
+++ b/lib/ovh/openstack_api/webstorage/cache.ex
@@ -2,9 +2,9 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
   @moduledoc :false
   use GenServer
   alias ExOvh.Ovh.OpenstackApi.Webstorage.Supervisor, as: WebStorageSupervisor
-  import ExOvh.Query.Ovh.Webstorage, only: [get_webstorage_credentials: 1]
+  import ExOvh.Query.Ovh.Webstorage, only: [get_webstorage_credentials: 1, get_webstorage_service: 1]
   @get_credentials_retries 10
-  @get_credentials_sleep_interval 150
+  @get_credentials_sleep_interval 450
 
 
   #####################
@@ -40,7 +40,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
   end
 
 
-  def get_account(service), do: get_account(ExOvh)
+  def get_account(service), do: get_account(ExOvh, service)
   def get_account(client, service) do
     credentials = get_credentials(client, service)
     path = URI.parse(credentials.swift_endpoint) |> Map.get(:path)
@@ -119,7 +119,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
     {:ok, resp} = ExOvh.ovh_request(get_webstorage_service(service), %{})
 
     %{
-      "server" => public_url,
+      "server" => domain,
       "storageLimit" => storage_limit
     } = resp.body
 
@@ -199,29 +199,23 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
             swift_endpoint: swift_endpoint,
             identity_endpoint: identity_endpoint,
             service: service,
-            public_url: public_url,
+            public_url: public_url(domain, swift_endpoint),
             storage_limit: storage_limit
           }
       }
 
   end
 
+  defp public_url(domain, swift_endpoint) do
+    path = URI.parse(swift_endpoint) |> Map.get(:path)
+    {version, account} = String.split_at(path, 4)
+    domain <> version <> account
+  end
 
   defp get_credentials(client, service, index) do
     Og.context(__ENV__, :debug)
-    if ets_tablename(client, service) in :ets.all() do
-      [credentials: credentials] = :ets.lookup(ets_tablename(client, service), :credentials)
-      if credentials.lock === :true do
-        if index > @get_credentials_retries do
-          raise "Cannot retrieve openstack credentials from ets table, #{__ENV__.module}, #{__ENV__.line}"
-        else
-          :timer.sleep(@get_credentials_sleep_interval)
-          get_credentials(client, service, index + 1)
-        end
-      else
-        credentials
-      end
-    else
+
+    retry = fn(client, service, index) ->
       if index > @get_credentials_retries do
         raise "Cannot retrieve openstack credentials from ets table, #{__ENV__.module}, #{__ENV__.line}"
       else
@@ -229,6 +223,21 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
         get_credentials(client, service, index + 1)
       end
     end
+
+    if ets_tablename(client, service) in :ets.all() do
+      table = :ets.lookup(ets_tablename(client, service), :credentials)
+      case table do
+        [credentials: credentials] ->
+          if credentials.lock === :true do
+            retry.(client, service, index)
+          else
+            credentials
+          end
+        [] -> retry.(client,service,index)
+      end
+    else
+      retry.(client, service, index)
+    end
   end
 
 
@@ -274,7 +283,10 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
 
 
   defp supervisor_exists?(client, service) do
-    Process.whereis(registered_supervisor_name(client, service))
+    case Process.whereis(registered_supervisor_name(client, service)) do
+      :nil -> :false
+      _pid -> :true
+    end
   end
 
 
diff --git a/lib/ovh/openstack_api/webstorage/request.ex b/lib/ovh/openstack_api/webstorage/request.ex
index 5cd5e5f..1b47734 100644
--- a/lib/ovh/openstack_api/webstorage/request.ex
+++ b/lib/ovh/openstack_api/webstorage/request.ex
@@ -30,7 +30,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Request do
         }
       end
     else
-     {:error, resp}
+      {:error, resp}
     end
 
   end
diff --git a/lib/ovh/ovh_api/auth.ex b/lib/ovh/ovh_api/auth.ex
index f88db27..7ffea99 100644
--- a/lib/ovh/ovh_api/auth.ex
+++ b/lib/ovh/ovh_api/auth.ex
@@ -37,7 +37,8 @@ defmodule ExOvh.Ovh.OvhApi.Auth do
     config = config(client)
     consumer_key = get_consumer_key(config)
     if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
-    opts = [app_secret(config), consumer_key, Atom.to_string(method), uri, params]
+    opts = [app_secret(config), app_key(config), consumer_key, Atom.to_string(method), uri, params]
+    #opts = [app_secret(config), consumer_key, Atom.to_string(method), uri, params]
     options = %{ body: params, headers: headers(opts, client), timeout: @timeout }
     {method, uri, options}
   end
diff --git a/lib/query/openstack/swift/query.ex b/lib/query/openstack/swift/query.ex
index 49a6635..db0892d 100644
--- a/lib/query/openstack/swift/query.ex
+++ b/lib/query/openstack/swift/query.ex
@@ -72,6 +72,24 @@ defmodule ExOvh.Query.Openstack.Swift do
   def delete_container(account, container), do: {:delete, account <> "/" <> container, %{ "format" => "json" }}
 
 
+  @doc ~S"""
+  DELETE /v1/​{account}/{container}​, Delete a container
+
+  ### Example usage
+
+      ```elixir
+      import ExOvh.Query.Openstack.Swift
+      alias ExOvh.Hubic.OpenstackApi.Cache, as: OpenCache
+      client = ExOvh
+      account = OpenCache.get_account(client)
+      ExOvh.hubic_request(container_info(account, "new_container"), %{ openstack: :true })
+      ```
+  """
+  @spec container_info(account :: String.t, container :: String.t)
+                         :: ExOvh.Client.raw_query_t
+  def container_info(account, container), do: {:head, account <> "/" <> container, %{ "format" => "json" }}
+
+
   ##########################
   # OBJECT RELATED REQUESTS
   ##########################