setting up ExOvh client
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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