Commit d12cc13223ae2bff8648ea91d19a56748ca48022

Stephen Moloney 2016-05-14T12:47:43

setting up ExOvh client

diff --git a/lib/auth/openstack/swift/auth.ex b/lib/auth/openstack/swift/auth.ex
deleted file mode 100644
index fad4b5a..0000000
--- a/lib/auth/openstack/swift/auth.ex
+++ /dev/null
@@ -1,51 +0,0 @@
-defimpl Openstex.Auth, for: Openstex.Openstack.Swift.Query do
-  @moduledoc :false
-  alias Openstex.Openstack.Swift.Query
-  @default_headers [{"Content-Type", "application/json; charset=utf-8"}]
-
-
-  # Public
-
-
-  @spec prepare_request(Query.t, Keyword.t, atom) :: Openstex.HttpQuery.t
-  def prepare_request(query, httpoison_opts, client)
-
-  def prepare_request(%Query{method: method, uri: uri, params: params}, httpoison_opts, client)
-                                  when method in [:get, :head, :delete] do
-    cache = client.cache()
-    uri = cache.get_swift_endpoint(client) <> uri
-    body = ""
-    headers =  headers(client)
-    default_httpoison_opts = client.httpoison_config()
-    options = Keyword.merge(default_httpoison_opts, httpoison_opts)
-    if params !== :nil and params !== "", do: uri = uri <> "?" <> URI.encode_query(params)
-    %Openstex.HttpQuery{method: method, uri: uri, body: body, headers: headers, options: options, service: :openstack}
-  end
-
-  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_swift_endpoint(client) <> uri
-    headers =  headers(client)
-    default_httpoison_opts = client.httpoison_config()
-    options = Keyword.merge(default_httpoison_opts, httpoison_opts)
-    if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
-    body = params || ""
-    %Openstex.HttpQuery{method: method, uri: uri, body: body, headers: headers, options: options, service: :openstack}
-  end
-
-
-  # Private
-
-
-  defp headers(client) do
-    @default_headers ++
-    [
-      {
-        "X-Auth-Token", client.cache().get_xauth_token(client)
-      }
-    ]
-  end
-
-
-end
diff --git a/lib/auth/openstack/swift/cache.ex b/lib/auth/openstack/swift/cache.ex
index b0c2436..a30e655 100644
--- a/lib/auth/openstack/swift/cache.ex
+++ b/lib/auth/openstack/swift/cache.ex
@@ -4,8 +4,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   use Openstex.Cache
   alias ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage
   alias ExOvh.Auth.Openstack.Swift.Cache.Webstorage
-  alias Openstex.Helpers.V2.Keystone
-  alias Openstex.Helpers.V2.Keystone.Identity
+  alias Openstex.Keystone.V2.Helpers, as: Keystone
+  alias Openstex.Keystone.V2.Helpers.Identity
   import ExOvh.Utils, only: [ets_tablename: 1]
   @get_identity_retries 5
   @get_identity_interval 1000
diff --git a/lib/auth/openstack/swift/cloudstorage.ex b/lib/auth/openstack/swift/cloudstorage.ex
index 88cad3a..6352da3 100644
--- a/lib/auth/openstack/swift/cloudstorage.ex
+++ b/lib/auth/openstack/swift/cloudstorage.ex
@@ -1,7 +1,7 @@
 defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
   @moduledoc :false
-  alias Openstex.Helpers.V2.Keystone
-  alias Openstex.Helpers.V2.Keystone.Identity
+  alias Openstex.Keystone.V2.Helpers, as: Keystone
+  alias Openstex.Keystone.V2.Helpers.Identity
 
 
   @doc :false
diff --git a/lib/auth/openstack/swift/webstorage.ex b/lib/auth/openstack/swift/webstorage.ex
index d69601d..005307e 100644
--- a/lib/auth/openstack/swift/webstorage.ex
+++ b/lib/auth/openstack/swift/webstorage.ex
@@ -1,7 +1,7 @@
 defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
   @moduledoc :false
-  alias Openstex.Helpers.V2.Keystone
-  alias Openstex.Helpers.V2.Keystone.Identity
+  alias Openstex.Keystone.V2.Helpers, as: Keystone
+  alias Openstex.Keystone.V2.Helpers.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,
                          username: String.t, password: String.t, tenant_name: String.t}
@@ -14,7 +14,9 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
     Og.context(__ENV__, :debug)
 
     properties = ExOvh.Ovh.V1.Webstorage.Query.get_service(cdn_name) |> ovh_client.request!() |> Map.fetch!(:body)
+    |> Og.log_return(__ENV__, :debug)
     credentials = ExOvh.Ovh.V1.Webstorage.Query.get_credentials(cdn_name) |> ovh_client.request!() |> Map.fetch!(:body)
+    |> Og.log_return(__ENV__, :debug)
 
     webstorage =
     %{
@@ -40,8 +42,11 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
 
     cdn_name = Keyword.fetch!(config, :cdn_name)
     webstorage = webstorage(ovh_client, cdn_name)
+    |> Og.log_return(__ENV__, :debug)
     %{endpoint: endpoint, username: username, password: password, tenant_name: tenant_name} = webstorage
+    |> Og.log_return(__ENV__, :debug)
     identity = Keystone.authenticate!(endpoint, username, password, [tenant_name: tenant_name])
+    |> Og.log_return(__ENV__, :debug)
   end
 
 
diff --git a/lib/auth/ovh/cache.ex b/lib/auth/ovh/cache.ex
index 9059ba7..986fdce 100644
--- a/lib/auth/ovh/cache.ex
+++ b/lib/auth/ovh/cache.ex
@@ -25,12 +25,14 @@ 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
diff --git a/lib/client.ex b/lib/client.ex
index ad58d1a..10e256c 100644
--- a/lib/client.ex
+++ b/lib/client.ex
@@ -1,5 +1,90 @@
 defmodule ExOvh.Client do
-  @moduledoc :false
+  @moduledoc ~s"""
+  A behaviour for setting up an OVH client.
+
+  ## Example: Setting up the OVH client
+
+  This client is only setup to use the Ovh api. So only the `ExOvh.Ovh` client will be available as the
+  configuration settings for anything else are missing.
+
+  Defining a client:
+
+      defmodule ExOvh do
+        @moduledoc :false
+        use ExOvh.Client, otp_app: :ex_ovh
+      end
+
+  Configuring a client:
+
+      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: [
+                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
+                              ]
+               ]
+
+  ## Example http calls to the OVH api using the `ExOvh.Ovh` client
+
+      # Using the Query functions built from scratch
+      %ExOvh.Ovh.Query{ method: :get, uri: "/me", params: :nil} |> ExOvh.Ovh.request!() |> Map.get(:body)
+      %ExOvh.Ovh.Query{ method: :get, uri: "/cloud/project", params: :nil} |> ExOvh.Ovh.request!() |> Map.get(:body)
+
+      # Using the Query functions build using a helper function
+      ExOvh.Ovh.V1.Webstorage.Query.get_services() |> ExOvh.Ovh.request!()
+
+  ## Example: Setting up the Ovh client and the Swift Cloudstorage client
+
+  This client is only setup to use the Ovh api and the cloudstorage swift api. So the `MyApp.MyClient.Ovh` client and the `MyApp.MyClient.Cloudstorage` client
+  will be available as the configuration settings for anything else are missing.
+
+
+      defmodule MyApp.MyClient do
+        @moduledoc :false
+        use ExOvh.Client, otp_app: :my_app
+      end
+
+
+      config :my_app, MyApp.MyClient,
+        ovh: [
+           application_key: System.get_env("MY_APP_MY_CLIENT_APPLICATION_KEY"),
+           application_secret: System.get_env("MY_APP_MY_CLIENT_APPLICATION_SECRET"),
+           consumer_key: System.get_env("MY_APP_MY_CLIENT_CONSUMER_KEY"),
+           endpoint: System.get_env("MY_APP_MY_CLIENT_ENDPOINT"),
+           api_version: System.get_env("MY_APP_MY_CLIENT_API_VERSION") || "1.0"
+        ],
+        swift: [
+                cloudstorage: [
+                                tenant_id: System.get_env("MY_APP_MY_CLIENT_CLOUDSTORAGE_TENANT_ID"), # mandatory, corresponds to a project id
+                                user_id: System.get_env("MY_APP_MY_CLIENT_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
+                              ]
+               ]
+
+   ## Example http calls to a Swift compatible service using the `MyApp.MyClient` client
+
+      # Using Query functions
+      account = MyApp.MyClient.Swift.Cloudstorage.account()
+      container = "ex_ovh_private_test_container"
+      Openstex.Swift.V1.Query.get_objects_in_folder("", container, account) |> MyApp.MyClient.Swift.Cloudstorage.request!() |> Map.get(:body)
+      Openstex.Swift.V1.Query.container_info(container, account) |> MyApp.MyClient.Swift.Cloudstorage.request!()
+      Openstex.Swift.V1.Query.account_info(account) |> MyApp.MyClient.Swift.Cloudstorage.request!()
+
+      # Using Helper functions
+      MyApp.MyClient.Swift.Cloudstorage.list_objects!(container)
+  """
 
   defmacro __using__(opts) do
     quote bind_quoted: [opts: opts] do
@@ -7,6 +92,7 @@ defmodule ExOvh.Client do
 
       # public functions
 
+
       def config() do
         otp_app = unquote(opts) |> Keyword.fetch!(:otp_app)
         if (otp_app != :ex_ovh)  do
@@ -16,7 +102,7 @@ defmodule ExOvh.Client do
         end
       end
 
-      # IS THIS SUPERVISOR NEEDED --> START 2 SEPARATE SUPERVISORS ??
+
       def start_link(sup_opts \\ []) do
         ExOvh.Supervisor.start_link(__MODULE__, sup_opts)
       end
@@ -24,8 +110,10 @@ defmodule ExOvh.Client do
       # client definitions
 
       defmodule Ovh do
+        @moduledoc ExOvh.Client.ovh_docs()
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Ovh.Cache
+        @doc :false
         def config() do
           List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:ovh)
           |> Keyword.merge(Defaults.ovh(), fn(k, v1, v2) ->
@@ -39,26 +127,53 @@ defmodule ExOvh.Client do
       end
 
       defmodule Swift.Webstorage do
-        defstruct []
+        @moduledoc ExOvh.Client.swift_docs()
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
+        @doc :false
         def config(), do: List.last(__ENV__.context_modules).config() |> Keyword.fetch!(:swift) |> Keyword.fetch!(:webstorage)
         use Openstex.Swift.V1.Helpers, client: __MODULE__
+        @doc "Returns the swift account string."
+        @spec account() :: String.t
+        def account(), do: __MODULE__.cache().get_swift_account(__MODULE__)
       end
 
       defmodule Swift.Cloudstorage do
-        defstruct []
+        @moduledoc ExOvh.Client.swift_docs()
         use Openstex.Client, client: __MODULE__
         def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
+        @doc :false
         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__
+        @doc "Returns the swift account string."
+        @spec account() :: String.t
+        def account(), do: __MODULE__.cache().get_swift_account(__MODULE__)
       end
 
 
     end
   end
 
+  @doc :false
+  def ovh_docs() do
+    ~s"""
+    A default client for sending request to the [OVH API](https://api.ovh.com/console/).
+
+    `ExOvh.Ovh` is the default client. `MyApp.MyClient.Ovh` (or similar) can alternatively be the OVH client provided that the client
+    has been created by implementing the `ExOvh.Client` behavior.
+    """
+  end
+
+  @doc :false
+  def swift_docs() do
+    ~s"""
+    A default client for sending request to the [Swift Compatible API](http://developer.openstack.org/api-ref-objectstorage-v1.html).
+
+    Incorporates the behaviours from the `Openstex.Swift.V1.Helpers` module.
+    """
+  end
+
 end
\ No newline at end of file