Supervision tree working
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
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