Commit 4fcda5537a774fd0090b8af34fd3cad695525e98

Stephen Moloney 2016-05-02T10:33:55

Supervision tree working

diff --git a/config/dev.exs b/config/dev.exs
index 9b1b544..d828ad5 100644
--- a/config/dev.exs
+++ b/config/dev.exs
@@ -21,14 +21,14 @@ config :ex_ovh,
     api_version: System.get_env("EX_OVH_API_VERSION") || "1.0"
   ],
   swift: [
-          webstorage: [ #  <-- :webstorage will be the config_id
+          webstorage: [
                         cdn_name: System.get_env("EX_OVH_WEBSTORAGE_CDN_NAME"),
                         type: :webstorage
                       ],
-          cloudstorage: [ #  <-- :cloudstorage will be the config_id
+          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.
-                          endpoint: "https://auth.cloud.ovh.net/v2.0",
+                          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
                         ]
@@ -37,16 +37,16 @@ config :ex_ovh,
 
 
 
-#config :my_app, MyApp.ExOvhClient1,
+#config :my_app, MyApp.OvhClient,
 #  ... then as above
 
 # SAMPLE CONFIGURATIONS ON A PER APP AND PER API BASIS FOR OPENSTEX
 
-#config :my_app, MyApp.ExOvhClient1.Ovh, <-- For OVH part of the api
+#config :my_app, MyApp.OvhClient.Ovh, <-- For OVH part of the api
 #  httpoison: ... as above
 
-#config :my_app, MyApp.ExOvhClient1.Swift.Webstorage, <-- For Openstack Webstorage part of the api
+#config :my_app, MyApp.OvhClient.Swift.Webstorage, <-- For Openstack Webstorage part of the api
 #  httpoison: ... as above
 
-#config :my_app, MyApp.ExOvhClient1.Swift.Cloudstorage, <-- <-- For Openstack Cloudstorage part of the api
+#config :my_app, MyApp.OvhClient.Swift.Cloudstorage, <-- <-- For Openstack Cloudstorage part of the api
 #  httpoison: ... as above
diff --git a/lib/auth/openstack/swift/auth.ex b/lib/auth/openstack/swift/auth.ex
index 6aca259..fad4b5a 100644
--- a/lib/auth/openstack/swift/auth.ex
+++ b/lib/auth/openstack/swift/auth.ex
@@ -25,7 +25,7 @@ defimpl Openstex.Auth, for: Openstex.Openstack.Swift.Query do
   def prepare_request(%Query{method: method, uri: uri, params: params}, httpoison_opts, client)
                                   when method in [:post, :put] do
     cache = client.cache()
-    uri = cache.get_endpoint(client) <> uri
+    uri = cache.get_swift_endpoint(client) <> uri
     headers =  headers(client)
     default_httpoison_opts = client.httpoison_config()
     options = Keyword.merge(default_httpoison_opts, httpoison_opts)
diff --git a/lib/auth/openstack/swift/cache.ex b/lib/auth/openstack/swift/cache.ex
index 4be28fb..b0c2436 100644
--- a/lib/auth/openstack/swift/cache.ex
+++ b/lib/auth/openstack/swift/cache.ex
@@ -26,7 +26,7 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     |> Map.get(:service_catalog)
     |> Enum.find(fn(%Identity.Service{} = service) ->  service.name == "swift" end)
     |> Map.get(:endpoints)
-    |> List.first()
+    |> Enum.find(fn(%Identity.Endpoint{} = endpoint) ->  endpoint.region == swift_client.config()[:region] end)
     |> Map.get(:public_url)
 
     path = URI.parse(public_url) |> Map.get(:path)
@@ -39,7 +39,7 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     |> Map.get(:service_catalog)
     |> Enum.find(fn(%Identity.Service{} = service) ->  service.name == "swift" end)
     |> Map.get(:endpoints)
-    |> List.first()
+    |> Enum.find(fn(%Identity.Endpoint{} = endpoint) ->  endpoint.region == swift_client.config()[:region] end)
     |> Map.get(:public_url)
 
     path = URI.parse(public_url) |> Map.get(:path)
@@ -62,12 +62,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     Og.context(__ENV__, :debug)
     :erlang.process_flag(:trap_exit, :true)
     create_ets_table(swift_client)
-
-    Og.context(__ENV__, :debug)
     config = swift_client.config()
-    |> Og.log_return(__ENV__, :warn)
-
-    {:ok, identity} = create_identity({ovh_client, swift_client}, config, config[:type])
+    identity = create_identity({ovh_client, swift_client}, config, config[:type])
     Og.context(__ENV__, :debug)
 
     identity = Map.put(identity, :lock, :false)
diff --git a/lib/auth/openstack/swift/cloudstorage.ex b/lib/auth/openstack/swift/cloudstorage.ex
index 1219173..88cad3a 100644
--- a/lib/auth/openstack/swift/cloudstorage.ex
+++ b/lib/auth/openstack/swift/cloudstorage.ex
@@ -1,10 +1,14 @@
 defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
   @moduledoc :false
+  alias Openstex.Helpers.V2.Keystone
   alias Openstex.Helpers.V2.Keystone.Identity
 
+
   @doc :false
-  @spec create_identity({atom, atom}, atom) :: Identity.t | no_return
+  @spec create_identity({atom, atom}, Keyword.t) :: Identity.t | no_return
   def create_identity({ovh_client, swift_client}, config) do
+    Og.context(__ENV__, :debug)
+
     tenant_id = Keyword.fetch!(config, :tenant_id)
     user_id = Keyword.get(config, :user_id, :nil)
     region = Keyword.get(config, :region, "SBG1")
@@ -32,23 +36,13 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
     resp = ExOvh.Ovh.V1.Cloud.Query.regenerate_credentials(tenant_id, user_id) |> ovh_client.request!()
     password = resp.body["password"]
     username = resp.body["username"]
-    endpoint = config[:endpoint] || "https://auth.cloud.ovh.net/v2.0"
+    endpoint = config[:keystone_endpoint] || "https://auth.cloud.ovh.net/v2.0"
 
     # make sure the regenerate credentials (in the external ovh api) had a chance to take effect
     :timer.sleep(1000)
 
-    identity = Module.concat(swift_client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_id: tenant_id])
+    identity = Keystone.authenticate!(endpoint, username, password, [tenant_id: tenant_id])
   end
 
-#    token = Openstex.Keystone.V2.Query.get_token(endpoint, username, password)
-#    |> client.request!()
-#    |> Map.get(:body)
-#    |> Map.get("access")
-#    |> Map.get("token")
-#    |> Map.get("id")
-
-#    identity = Openstex.Keystone.V2.Query.get_identity(token, endpoint, tenant)
-#    |> client.request!()
-#    |> Map.get(:body)
 
 end
\ No newline at end of file
diff --git a/lib/auth/openstack/swift/webstorage.ex b/lib/auth/openstack/swift/webstorage.ex
index eab4640..d69601d 100644
--- a/lib/auth/openstack/swift/webstorage.ex
+++ b/lib/auth/openstack/swift/webstorage.ex
@@ -1,5 +1,6 @@
 defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
   @moduledoc :false
+  alias Openstex.Helpers.V2.Keystone
   alias Openstex.Helpers.V2.Keystone.Identity
   defstruct [ :domain, :storage_limit, :server, :endpoint, :username, :password, :tenant_name ]
   @type t :: %__MODULE__{domain: String.t, storage_limit: String.t, server: String.t, endpoint: String.t,
@@ -31,33 +32,17 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
     webstorage = __MODULE__.new(webstorage)
   end
 
+
   @doc :false
-  @spec create_identity({atom, atom}, atom) :: Identity.t | no_return
+  @spec create_identity({atom, atom}, Keyword.t) :: Identity.t | no_return
   def create_identity({ovh_client, swift_client}, config) do
     Og.context(__ENV__, :debug)
 
     cdn_name = Keyword.fetch!(config, :cdn_name)
     webstorage = webstorage(ovh_client, cdn_name)
     %{endpoint: endpoint, username: username, password: password, tenant_name: tenant_name} = webstorage
-    identity = Module.concat(swift_client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_name: tenant_name])
+    identity = Keystone.authenticate!(endpoint, username, password, [tenant_name: tenant_name])
   end
 
-#  token = Openstex.Keystone.V2.Query.get_token(endpoint, username, password)
-#  |> Og.log_return(:warn)
-#  |> client.request!()
-#  |> Og.log_return(:warn)
-#  |> Map.get(:body)
-#  |> Map.get("access")
-#  |> Map.get("token")
-#  |> Map.get("id")
-#  # |> Map.get("body")["access"]["token"]["id"]
-#  # |> Map.get(:body)  |> Og.log_return(:debug) |> Map.get("access") |> Og.log_return(:debug) |> Map.get("token") |> Og.log_return(:debug) |> Map.get("id")
-#
-#  {token, endpoint, tenant} |> Og.log()
-#  identity = Openstex.Keystone.V2.Query.get_identity(token, endpoint, tenant)
-#  |> Og.log_return(:debug)
-#  |> client.request!()
-#  |> Og.log_return(:debug)
-#  |> Keystone.parse_nested_map_into_identity_struct()
 
 end
\ No newline at end of file
diff --git a/lib/client.ex b/lib/client.ex
index eb87920..c46ccfa 100644
--- a/lib/client.ex
+++ b/lib/client.ex
@@ -26,7 +26,16 @@ defmodule ExOvh.Client do
       defmodule Ovh do
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Ovh.Cache
-        def config(), do: List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:ovh) |> Keyword.merge(Defaults.ovh())
+        def config() do
+          List.last(__ENV__.context_modules).config() |> 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)
+        end
       end
 
       defmodule Swift.Webstorage do
@@ -34,18 +43,21 @@ defmodule ExOvh.Client do
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
         def config(), do: List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:swift) |> Keyword.fetch!(:webstorage)
-        # use Openstex.Swift.V1.Helpers, client: __MODULE__
+        use Openstex.Swift.V1.Helpers, client: __MODULE__
       end
 
       defmodule Swift.Cloudstorage do
         defstruct []
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
-        def config(), do: List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:swift) |> Keyword.fetch!(:cloudstorage)
-        # use Openstex.Swift.V1.Helpers, client: __MODULE__
+        def config() do
+          List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:swift) |> Keyword.fetch!(:cloudstorage)
+          |> Keyword.merge(Defaults.cloudstorage(), fn(_k, v1, v2) -> if v1 == :nil, do: v2, else: v1 end)
+        end
+        use Openstex.Swift.V1.Helpers, client: __MODULE__
       end
 
-#      defmodule Helpers.Keystone do
+#      defmodule Keystone do
 #        use Openstex.Helpers.V2.Keystone, client: Keyword.fetch!(opts, :client)
 #      end
 
diff --git a/lib/defaults.ex b/lib/defaults.ex
index 34d7c71..5b2ad25 100644
--- a/lib/defaults.ex
+++ b/lib/defaults.ex
@@ -1,8 +1,7 @@
 defmodule ExOvh.Ovh.Defaults do
   @moduledoc :false
 
-  @doc "Returns ovh default configuration settings"
-  @spec ovh() :: map
+
   def ovh() do
     [
      endpoint: endpoints()["ovh-eu"],
@@ -11,8 +10,14 @@ defmodule ExOvh.Ovh.Defaults do
   end
 
 
-  @doc "Returns a map of ovh endpoints"
-  @spec endpoints() :: map
+  def cloudstorage() do
+    [
+     keystone_endpoint: "https://auth.cloud.ovh.net/v2.0", # default endpoint for keystone (identity) auth
+     region: "SBG1"
+    ]
+  end
+
+
   def endpoints() do
     %{
       "ovh-eu"        => "https://api.ovh.com/",
@@ -25,18 +30,13 @@ defmodule ExOvh.Ovh.Defaults do
     }
   end
 
-  @doc "Returns the default suffix for creating a new application in ovh"
-  @spec create_app_uri_suffix() :: String.t
+
   def create_app_uri_suffix(), do: "createApp/"
 
 
-  @doc "Returns the default suffix for getting the consumer key in ovh"
-  @spec consumer_key_suffix() :: String.t
   def consumer_key_suffix(), do: "/auth/credential/"
 
 
-  @doc "Returns the default access rules (all methods and paths by default)"
-  @spec access_rules() :: [map]
   def access_rules() do
      [
         %{
diff --git a/lib/supervisor.ex b/lib/supervisor.ex
index 46fb17c..86b775a 100644
--- a/lib/supervisor.ex
+++ b/lib/supervisor.ex
@@ -23,9 +23,19 @@ defmodule ExOvh.Supervisor do
   def init(client) do
     Og.context(__ENV__, :debug)
 
-    ovh_config = Keyword.merge(Defaults.ovh(), Keyword.fetch!(client.config(), :ovh))
+    ovh_config = Keyword.fetch!(client.config(), :ovh)
+                           |> Keyword.merge(Defaults.ovh(), fn(k, v1, v2) ->
+                             case {k, v1} do
+                              {_, :nil} -> v2
+                              {:endpoint, v1} -> Defaults.endpoints()[v1]
+                              _ -> v1
+                             end
+                           end)
+                           |> Og.log_return(__ENV__)
     webstorage_config = Keyword.get(client.config(), :swift, []) |> Keyword.get(:webstorage, :nil)
     cloudstorage_config = Keyword.get(client.config(), :swift, []) |> Keyword.get(:cloudstorage, :nil)
+                          |> Keyword.merge(Defaults.cloudstorage(), fn(_k, v1, v2) -> if v1 == :nil, do: v2, else: v1 end)
+                          |> Og.log_return(__ENV__)
 
     ovh_client = Module.concat(client, Ovh)
     sup_tree = [
@@ -51,7 +61,7 @@ defmodule ExOvh.Supervisor do
       cloudstorage_config ->
         cloudstorage_client = Module.concat(client, Swift.Cloudstorage)
         sup_tree ++
-        [{cloudstorage_client, {Swift, :start_link, [{ovh_client, cloudstorage_client}]}, :permanent, 10_000, :worker, [Swift]}]
+        [{cloudstorage_client, {SwiftCache, :start_link, [{ovh_client, cloudstorage_client}]}, :permanent, 10_000, :worker, [SwiftCache]}]
     end
 
     if sup_tree === [] do
diff --git a/lib/utils/utils.ex b/lib/utils/utils.ex
index 1fb0f60..9d72d35 100644
--- a/lib/utils/utils.ex
+++ b/lib/utils/utils.ex
@@ -1,60 +1,11 @@
 defmodule ExOvh.Utils do
   @moduledoc false
-
   alias ExOvh.Auth.Ovh.Cache, as: OvhCache
   alias ExOvh.Auth.Openstack.Swift.Cache, as: SwiftCache
   alias ExOvh.Ovh.Defaults
 
 
   @doc """
-  For naming a supervisor to incorporate the name of the client.
-
-  The client name is required so that when a client makes a request, the correct supervisor
-  is called if there are multiple clients in use.
-  """
-  defmacro supervisor_name(client) do
-    caller = __CALLER__.module
-    quote do
-      (
-        (
-         Atom.to_string(unquote(client))
-         <>
-         "."
-         )
-         <>
-         Atom.to_string(unquote(caller))
-       )
-       |> String.replace("Elixir.", "")
-       |> String.to_atom()
-     end
-  end
-
-
-  @doc """
-  For naming a genserver to incorporate the name of the client.
-
-  The client name is required so that when a client makes a request, the correct genserver
-  is called if there are multiple clients in use.
-  """
-  defmacro gen_server_name(client) do
-    caller = __CALLER__.module
-    quote do
-      (
-        (
-         Atom.to_string(unquote(client))
-         <>
-         "."
-         )
-         <>
-         Atom.to_string(unquote(caller))
-      )
-      |> String.replace("Elixir.", "")
-      |> String.to_atom()
-    end
-  end
-
-
-  @doc """
   For naming an ets table to incorporate the name of the client.
 
   The client name is required so that when a client makes a request, the correct ets table