Commit 37f045e332363a0a9404a57b7322e174e81785a6

Stephen Moloney 2016-05-01T21:07:31

restructure supervision tree and genserver naming

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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
diff --git a/lib/application.ex b/lib/application.ex
index 317ce3d..58c294b 100644
--- a/lib/application.ex
+++ b/lib/application.ex
@@ -1,13 +1,10 @@
 defmodule ExOvh.Application do
   @moduledoc :false
   use Application
-  @ex_ovh_config Application.get_all_env(:ex_ovh) |> Keyword.get(:ovh, :nil)
 
   # Start the ex_ovh client only if a config :ex_ovh, ex_ovh: %{...} configuration file has been set.
-  unless @ex_ovh_config in [%{}, :nil] do
-    def start(_type, _args) do
-      ExOvh.Supervisor.start_link(ExOvh, @ex_ovh_config, :ex_ovh)
-    end
+  def start(_type, _args) do
+    ExOvh.start_link(__MODULE__)
   end
 
 end
diff --git a/lib/auth/openstack/supervisor.ex b/lib/auth/openstack/supervisor.ex
deleted file mode 100644
index 3595ce8..0000000
--- a/lib/auth/openstack/supervisor.ex
+++ /dev/null
@@ -1,29 +0,0 @@
-defmodule ExOvh.Auth.Openstack.Supervisor do
-  @moduledoc :false
-
-  use Supervisor
-  alias ExOvh.Auth.Openstack.Swift.Cache
-
-
-  #  Public
-
-
-  def start_link(client \\ []) do
-    Og.context(__ENV__, :debug)
-    Supervisor.start_link(__MODULE__, client, [name: __MODULE__])
-  end
-
-
-  #  Callbacks
-
-
-  def init(client) do
-    Og.context(__ENV__, :debug)
-    tree = [
-            {Cache, {Cache, :start_link, []}, :transient, 10_000, :worker, []}
-           ]
-    supervise(tree, strategy: :simple_one_for_one)
-  end
-
-
-end
\ No newline at end of file
diff --git a/lib/auth/openstack/swift/cache.ex b/lib/auth/openstack/swift/cache.ex
index 40232ae..4be28fb 100644
--- a/lib/auth/openstack/swift/cache.ex
+++ b/lib/auth/openstack/swift/cache.ex
@@ -4,11 +4,9 @@ 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 ExOvh.Auth.Openstack.Supervisor, as: OpenstackSupervisor
-  alias ExOvh.Utils
   alias Openstex.Helpers.V2.Keystone
   alias Openstex.Helpers.V2.Keystone.Identity
-  import ExOvh.Utils, only: [gen_server_name: 1, ets_tablename: 1]
+  import ExOvh.Utils, only: [ets_tablename: 1]
   @get_identity_retries 5
   @get_identity_interval 1000
 
@@ -16,15 +14,15 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   # Public
 
 
-  def start_link(client) do
+  def start_link({ovh_client, swift_client}) do
     Og.context(__ENV__, :debug)
-    GenServer.start_link(__MODULE__, client, [name: gen_server_name(client)])
+    GenServer.start_link(__MODULE__, {ovh_client, swift_client}, [name: swift_client])
   end
 
   # Pulic Opestex.Cache callbacks (public 'interface' to the Cache module)
 
-  def get_swift_account(client) do
-    public_url = get_identity(client)
+  def get_swift_account(swift_client) do
+    public_url = get_identity(swift_client)
     |> Map.get(:service_catalog)
     |> Enum.find(fn(%Identity.Service{} = service) ->  service.name == "swift" end)
     |> Map.get(:endpoints)
@@ -36,8 +34,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     account
   end
 
-  def get_swift_endpoint(client) do
-    public_url = get_identity(client)
+  def get_swift_endpoint(swift_client) do
+    public_url = get_identity(swift_client)
     |> Map.get(:service_catalog)
     |> Enum.find(fn(%Identity.Service{} = service) ->  service.name == "swift" end)
     |> Map.get(:endpoints)
@@ -50,8 +48,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     endpoint
   end
 
-  def get_xauth_token(client) do
-    get_identity(client) |> Map.get(:token) |> Map.get(:id)
+  def get_xauth_token(swift_client) do
+    get_identity(swift_client) |> Map.get(:token) |> Map.get(:id)
   end
 
 
@@ -60,48 +58,45 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
 
   # trap exits so that terminate callback is invoked
   # the :lock key is to allow for locking during the brief moment that the access token is being refreshed
-  def init(client) do
+  def init({ovh_client, swift_client}) do
     Og.context(__ENV__, :debug)
     :erlang.process_flag(:trap_exit, :true)
-    create_ets_table(client)
+    create_ets_table(swift_client)
 
-
-
-    ## Get the client id from the config ??
     Og.context(__ENV__, :debug)
-    config = get_config(client)
+    config = swift_client.config()
     |> Og.log_return(__ENV__, :warn)
 
-    {:ok, identity} = create_identity(client, config, config[:type])
+    {:ok, identity} = create_identity({ovh_client, swift_client}, config, config[:type])
     Og.context(__ENV__, :debug)
 
     identity = Map.put(identity, :lock, :false)
-    :ets.insert(ets_tablename(client), {:identity, identity})
+    :ets.insert(ets_tablename(swift_client), {:identity, identity})
     expiry = to_seconds(identity)
     Task.start_link(fn -> monitor_expiry(expiry) end)
-    {:ok, {client, identity}}
+    {:ok, {swift_client, identity}}
   end
 
-  def handle_call(:add_lock, _from, {client, identity}) do
+  def handle_call(:add_lock, _from, {swift_client, identity}) do
     Og.context(__ENV__, :debug)
     new_identity = Map.put(identity, :lock, :true)
-    :ets.insert(ets_tablename(client), {:identity, new_identity})
-    {:reply, :ok, {client, new_identity}}
+    :ets.insert(ets_tablename(swift_client), {:identity, new_identity})
+    {:reply, :ok, {swift_client, new_identity}}
   end
 
-  def handle_call(:remove_lock, _from, {client, identity}) do
+  def handle_call(:remove_lock, _from, {swift_client, identity}) do
     Og.context(__ENV__, :debug)
     new_identity = Map.put(identity, :lock, :false)
-    :ets.insert(ets_tablename(client), {:identity, new_identity})
-    {:reply, :ok, {client, new_identity}}
+    :ets.insert(ets_tablename(swift_client), {:identity, new_identity})
+    {:reply, :ok, {swift_client, new_identity}}
   end
 
-  def handle_call(:update_identity, _from, {client, identity}) do
+  def handle_call(:update_identity, _from, {swift_client, identity}) do
     Og.context(__ENV__, :debug)
-    {:ok, new_identity} = get_identity(client)
+    {:ok, new_identity} = get_identity(swift_client)
     |> Map.put(identity, :lock, :false)
-    :ets.insert(ets_tablename(client), {:identity, new_identity})
-    {:reply, :ok, {client, new_identity}}
+    :ets.insert(ets_tablename(swift_client), {:identity, new_identity})
+    {:reply, :ok, {swift_client, new_identity}}
   end
 
   def handle_call(:stop, _from, state) do
@@ -109,9 +104,9 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
     {:stop, :shutdown, :ok, state}
   end
 
-  def terminate(:shutdown, {client, identity}) do
+  def terminate(:shutdown, {swift_client, identity}) do
     Og.context(__ENV__, :debug)
-    :ets.delete(ets_tablename(client)) # explicilty remove
+    :ets.delete(ets_tablename(swift_client)) # explicilty remove
     :ok
   end
 
@@ -119,60 +114,48 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   # private
 
 
-  defp create_identity(client, config, :webstorage) do
+  defp create_identity({ovh_client, swift_client}, config, :webstorage) do
     Og.context(__ENV__, :debug)
-    Webstorage.create_identity(client, config)
+    Webstorage.create_identity({ovh_client, swift_client}, config)
   end
-  defp create_identity(client, config, :cloudstorage) do
+  defp create_identity({ovh_client, swift_client}, config, :cloudstorage) do
     Og.context(__ENV__, :debug)
-    Cloudstorage.create_identity(client, config)
+    Cloudstorage.create_identity({ovh_client, swift_client}, config)
   end
-  defp create_identity(client, config, type) do
+  defp create_identity({ovh_client, swift_client}, config, type) do
     Og.context(__ENV__, :debug)
     raise "create_identity/3 is only supported for the :webstorage and :cloudstorage types, #{inspect(type)}"
   end
 
 
-  defp get_identity(client) do
-    if supervisor_exists?(client) do
-      get_identity(client, 0)
-    else
-      case Supervisor.start_child(OpenstackSupervisor, [client]) do
-        {:error, error} -> raise inspect(error)
-        {:ok, _} ->
-          if supervisor_exists?(client) do
-            get_identity(client, 0)
-          else
-            raise Og.log_return("", __ENV__, :error) |> inspect()
-          end
-      end
-    end
+  defp get_identity(swift_client) do
+      get_identity(swift_client, 0)
   end
-  defp get_identity(client, index) do
+  defp get_identity(swift_client, index) do
     Og.context(__ENV__, :debug)
 
-    retry = fn(client, index) ->
+    retry = fn(swift_client, index) ->
       if index > @get_identity_retries do
-        raise "Cannot retrieve openstack identity, #{__ENV__.module}, #{__ENV__.line}, client: #{client}"
+        raise "Cannot retrieve openstack identity, #{__ENV__.module}, #{__ENV__.line}, client: #{swift_client}"
       else
         :timer.sleep(@get_identity_interval)
-        get_identity(client, index + 1)
+        get_identity(swift_client, index + 1)
       end
     end
 
-    if ets_tablename(client) in :ets.all() do
-      table = :ets.lookup(ets_tablename(client), :identity)
+    if ets_tablename(swift_client) in :ets.all() do
+      table = :ets.lookup(ets_tablename(swift_client), :identity)
       case table do
         [identity: identity] ->
           if identity.lock === :true do
-            retry.(client, index)
+            retry.(swift_client, index)
           else
             identity
           end
-        [] -> retry.(client, index)
+        [] -> retry.(swift_client, index)
       end
     else
-      retry.(client, index)
+      retry.(swift_client, index)
     end
 
   end
@@ -191,7 +174,7 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   end
 
 
-  defp create_ets_table(client) do
+  defp create_ets_table(swift_client) do
     Og.context(__ENV__, :debug)
     ets_options = [
                    :set, # type
@@ -201,8 +184,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
                    {:write_concurrency, :false},
                    {:read_concurrency, :true}
                   ]
-    unless ets_tablename(client) in :ets.all() do
-      :ets.new(ets_tablename(client), ets_options)
+    unless ets_tablename(swift_client) in :ets.all() do
+      :ets.new(ets_tablename(swift_client), ets_options)
     end
   end
 
@@ -227,32 +210,4 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache do
   end
 
 
-  defp supervisor_exists?(client) do
-    registered_name = gen_server_name(client)
-    |> Og.log_return(__ENV__, :debug)
-    case Process.whereis(registered_name) do
-      :nil -> :false
-      _pid -> :true
-    end
-  end
-
-
-  defp get_config(client) do
-    str = Atom.to_string(client) |> String.downcase()
-    config =
-    cond do
-      String.ends_with?(str, "webstorage") ->
-        client.swift_config() |> Keyword.fetch!(:webstorage)
-      String.ends_with?(str, "cloudstorage") ->
-        client.swift_config() |> Keyword.fetch!(:cloudstorage)
-      true ->
-        raise "config not found, #{Og.context(__ENV__, :error)}"
-    end
-    config
-  end
-
-  # defp gen_server_name(client, config_id), do:  String.to_atom(Atom.to_string(config_id) <>  "-" <> Atom.to_string(client))
-  # def ets_tablename(client, config_id), do: String.to_atom(Atom.to_string(config_id) <>  "-" <> Atom.to_string(client))
-
-
 end
\ No newline at end of file
diff --git a/lib/auth/openstack/swift/cloudstorage.ex b/lib/auth/openstack/swift/cloudstorage.ex
index 18438c6..1219173 100644
--- a/lib/auth/openstack/swift/cloudstorage.ex
+++ b/lib/auth/openstack/swift/cloudstorage.ex
@@ -3,8 +3,8 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
   alias Openstex.Helpers.V2.Keystone.Identity
 
   @doc :false
-  @spec create_identity(atom, atom) :: Identity.t | no_return
-  def create_identity(client, config) do
+  @spec create_identity({atom, atom}, atom) :: Identity.t | no_return
+  def create_identity({ovh_client, swift_client}, config) do
     tenant_id = Keyword.fetch!(config, :tenant_id)
     user_id = Keyword.get(config, :user_id, :nil)
     region = Keyword.get(config, :region, "SBG1")
@@ -13,7 +13,7 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
     case user_id do
       :nil ->
         user = ExOvh.Ovh.V1.Cloud.Query.get_users(tenant_id)
-        |> ExOvh.request!()
+        |> ovh_client.request!()
         |> Map.get(:body)
         |> Enum.find(:nil,
           fn(user) -> %{"description" => "ex_ovh"} = user end
@@ -21,7 +21,7 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
         if user == :nil do
           # create user for "ex_ovh" description
           ExOvh.Ovh.V1.Cloud.Query.create_user(tenant_id, "ex_ovh")
-          |> ExOvh.request!()
+          |> ovh_client.request!()
           |> Map.get("id")
         else
           user["id"]
@@ -29,15 +29,15 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Cloudstorage do
       user_id -> user_id
     end
 
-    resp = ExOvh.Ovh.V1.Cloud.Query.regenerate_credentials(tenant_id, user_id) |> ExOvh.request!()
+    resp = ExOvh.Ovh.V1.Cloud.Query.regenerate_credentials(tenant_id, user_id) |> ovh_client.request!()
     password = resp.body["password"]
     username = resp.body["username"]
-    endpoint = client.ovh_config()[:cloudstorage_endpoint]
+    endpoint = config[:endpoint] || "https://auth.cloud.ovh.net/v2.0"
 
-    # make sure the regenerate credentials had a chance to take effect
+    # make sure the regenerate credentials (in the external ovh api) had a chance to take effect
     :timer.sleep(1000)
 
-    identity = Module.concat(client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_id: tenant_id])
+    identity = Module.concat(swift_client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_id: tenant_id])
   end
 
 #    token = Openstex.Keystone.V2.Query.get_token(endpoint, username, password)
diff --git a/lib/auth/openstack/swift/webstorage.ex b/lib/auth/openstack/swift/webstorage.ex
index 6592bed..eab4640 100644
--- a/lib/auth/openstack/swift/webstorage.ex
+++ b/lib/auth/openstack/swift/webstorage.ex
@@ -8,11 +8,12 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
 
 
   @doc :false
-  @spec webstorage(atom, String.t) :: __MODULE__.t | no_return
-  def webstorage(client, config) do
-    cdn_name = Keyword.fetch!(config, :cdn_name)
-    properties = ExOvh.Ovh.V1.Webstorage.Query.get_service(cdn_name) |> client.request!() |> Map.fetch!(:body)
-    credentials = ExOvh.Ovh.V1.Webstorage.Query.get_credentials(cdn_name) |> client.request!() |> Map.fetch!(:body)
+  @spec webstorage({atom, atom}, String.t) :: __MODULE__.t | no_return
+  def webstorage(ovh_client, cdn_name) do
+    Og.context(__ENV__, :debug)
+
+    properties = ExOvh.Ovh.V1.Webstorage.Query.get_service(cdn_name) |> ovh_client.request!() |> Map.fetch!(:body)
+    credentials = ExOvh.Ovh.V1.Webstorage.Query.get_credentials(cdn_name) |> ovh_client.request!() |> Map.fetch!(:body)
 
     webstorage =
     %{
@@ -31,13 +32,14 @@ defmodule ExOvh.Auth.Openstack.Swift.Cache.Webstorage do
   end
 
   @doc :false
-  @spec create_identity(atom, atom) :: Identity.t | no_return
-  def create_identity(client, config_id) do
-    config = client.swift_config() |> Keyword.fetch!(config_id)
+  @spec create_identity({atom, atom}, atom) :: 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(client, cdn_name)
+    webstorage = webstorage(ovh_client, cdn_name)
     %{endpoint: endpoint, username: username, password: password, tenant_name: tenant_name} = webstorage
-    identity = Module.concat(client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_name: tenant_name])
+    identity = Module.concat(swift_client, Helpers.Keystone).authenticate!(endpoint, username, password, [tenant_name: tenant_name])
   end
 
 #  token = Openstex.Keystone.V2.Query.get_token(endpoint, username, password)
diff --git a/lib/auth/ovh/auth.ex b/lib/auth/ovh/auth.ex
index 0044826..941604e 100644
--- a/lib/auth/ovh/auth.ex
+++ b/lib/auth/ovh/auth.ex
@@ -16,7 +16,7 @@ defimpl Openstex.Auth, for: ExOvh.Ovh.Query do
   def prepare_request(%Query{method: method, uri: uri, params: params}, httpoison_opts, client) when method in [:get, :head, :delete] do
     uri = if params !== :nil and params !== "" and is_map(params), do: uri <> "?" <> URI.encode_query(params), else: uri
     uri = if params !== :nil and params !== "" and is_map(params) === :false, do: uri <> URI.encode_www_form(params), else: uri
-    ovh_config = client.ovh_config()
+    ovh_config = client.config()
     uri = ovh_config[:endpoint] <> ovh_config[:api_version] <> uri
     body = params || ""
     headers = headers([ovh_config[:application_secret], ovh_config[:application_key], ovh_config[:consumer_key], Atom.to_string(method), uri, ""], client)
@@ -27,7 +27,7 @@ defimpl Openstex.Auth, for: ExOvh.Ovh.Query do
 
   def prepare_request(%Query{method: method, uri: uri, params: params}, httpoison_opts, client) when method in [:post, :put] do
     if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
-    ovh_config = client.ovh_config()
+    ovh_config = client.config()
     uri = ovh_config[:endpoint] <> ovh_config[:api_version] <> uri
     body = params || ""
     headers = headers([ovh_config[:application_secret], ovh_config[:application_key], ovh_config[:consumer_key], Atom.to_string(method), uri, ""], client)
diff --git a/lib/auth/ovh/cache.ex b/lib/auth/ovh/cache.ex
index dae9031..9059ba7 100644
--- a/lib/auth/ovh/cache.ex
+++ b/lib/auth/ovh/cache.ex
@@ -1,51 +1,36 @@
 defmodule ExOvh.Auth.Ovh.Cache do
+  # Stores the time diff in state of the gen_server - later used in authentication headers for every request
   @moduledoc :false
   use GenServer
-  import ExOvh.Utils, only: [gen_server_name: 1]
-  alias ExOvh.Utils
 
 
   # Public
 
 
-  def start_link({client, ovh_config, opts}) do
+  def start_link(client) do
     Og.context(__ENV__, :debug)
-    GenServer.start_link(__MODULE__, {client, ovh_config}, [name: gen_server_name(client)])
+    GenServer.start_link(__MODULE__, client, [name: client])
   end
 
 
   @doc "Retrieves the ovh api time diff from the state"
   def get_time_diff(client) do
-    GenServer.call(gen_server_name(client), :get_diff)
-  end
-  @doc "Retrieves the ovh config map"
-  def get_ovh_config(client) do
-    GenServer.call(gen_server_name(client), :get_config)
+    GenServer.call(client, :get_diff)
   end
 
 
   # Genserver Callbacks
 
 
-  def init({client, ovh_config}) do
-    Og.context(__ENV__, :debug)
-    diff = calculate_diff(client, ovh_config)
-    {:ok, {ovh_config, diff}}
-  end
-
-  def handle_call(:get_diff, _from, {ovh_config, diff}) do
+  def init(client) do
     Og.context(__ENV__, :debug)
-    {:reply, diff, {ovh_config, diff}}
+    diff = calculate_diff(client)
+    {:ok, diff}
   end
 
-  def handle_call(:get_config, _from, {ovh_config, diff}) do
+  def handle_call(:get_diff, _from, diff) do
     Og.context(__ENV__, :debug)
-    {:reply, ovh_config, {ovh_config, diff}}
-  end
-
-  def handle_cast({:set_diff, new_diff}, {ovh_config, diff}) do
-    Og.context(__ENV__, :debug)
-    {:noreply, {ovh_config, new_diff}}
+    {:reply, diff, diff}
   end
 
   def terminate(:shutdown, state) do
@@ -58,35 +43,29 @@ defmodule ExOvh.Auth.Ovh.Cache do
   # Private
 
 
-  defp api_time_request(client, ovh_config) do
+  defp calculate_diff(client) do
+    Og.context(__ENV__, :debug)
+    api_time = api_time_request(client)
+    os_t = :os.system_time(:seconds)
+    os_t - api_time
+  end
+
+
+  defp api_time_request(client) do
     Og.context(__ENV__, :debug)
+    client |> Og.log_return(__ENV__)
+    ovh_config = client.config() |> Og.log_return(__ENV__)
     method = :get
     uri = ovh_config[:endpoint] <> ovh_config[:api_version] <> "/auth/time"
     body = ""
     headers = [{"Content-Type", "application/json; charset=utf-8"}]
-    httpoison_config = client.httpoison_config()
-    options = httpoison_config
+    client |> Og.log_return(__ENV__)
+    httpoison_opts = client.httpoison_config()
+    options = httpoison_opts
+    {method, uri, body, headers, options} |> Og.log_return(__ENV__, :warn)
     resp = HTTPoison.request!(method, uri, body, headers, options)
     api_time = Poison.decode!(resp.body)
   end
 
 
-  defp calculate_diff(client, ovh_config) do
-    api_time = api_time_request(client, ovh_config)
-    os_t = :os.system_time(:seconds)
-    os_t - api_time
-  end
-
-
-  #Caches the ovh api time diff
-  defp set_time_diff(client) do
-    ovh_config = get_config(client)
-    set_time_diff(client, ovh_config)
-  end
-  defp set_time_diff(client, ovh_config) when is_list(ovh_config) do
-    diff = calculate_diff(client, ovh_config)
-    GenServer.cast(gen_server_name(client), {:set_diff, diff})
-  end
-
-
 end
\ No newline at end of file
diff --git a/lib/auth/supervisor.ex b/lib/auth/supervisor.ex
deleted file mode 100644
index 6db5329..0000000
--- a/lib/auth/supervisor.ex
+++ /dev/null
@@ -1,35 +0,0 @@
-defmodule ExOvh.Auth.Supervisor do
-  @moduledoc :false
-
-  use Supervisor
-  import ExOvh.Utils, only: [supervisor_name: 1]
-  alias ExOvh.Auth.Ovh.Cache, as: OvhCache
-  alias ExOvh.Auth.Openstack.Supervisor, as: OpenstackSupervisor
-
-
-  #  Public
-
-
-  def start_link(client, ovh_config, opts) do
-    Og.context(__ENV__, :debug)
-    Supervisor.start_link(__MODULE__, {client, ovh_config, opts}, [name: supervisor_name(client)])
-  end
-
-
-  # Supervisor Callbacks
-
-
-  def init({client, ovh_config, opts}) do
-    Og.context(__ENV__, :debug)
-
-    tree = [
-            {OvhCache,
-              {OvhCache, :start_link, [{client, ovh_config, opts}]}, :permanent, 10_000, :worker, [OvhCache]},
-            {OpenstackSupervisor,
-              {OpenstackSupervisor, :start_link, []}, :permanent, 10_000, :supervisor, [OpenstackSupervisor]}
-           ]
-    supervise(tree, strategy: :one_for_one)
-  end
-
-
-end
diff --git a/lib/client.ex b/lib/client.ex
index 6d7e1db..eb87920 100644
--- a/lib/client.ex
+++ b/lib/client.ex
@@ -3,63 +3,10 @@ defmodule ExOvh.Client do
 
   defmacro __using__(opts) do
     quote bind_quoted: [opts: opts] do
-    opts |> Og.log_return(__ENV__, :debug)
-
-      # client definitions
-#
-#      defmodule Ovh do
-#        otp_app = Keyword.fetch!(opts, :otp_app)
-#        use Openstex.Client, otp_app: otp_app, client: __MODULE__
-#        def cache(), do: ExOvh.Auth.Ovh.Cache
-#      end
-
-
-#      defmodule Swift.Webstorage do
-#        defstruct []
-#        otp_app = Keyword.fetch!(opts, :otp_app)
-#        use Openstex.Client, otp_app: otp_app, client: __MODULE__
-#        def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
-#        # use Openstex.Swift.V1.Helpers, client: client, config_id: :webstorage
-#      end
-
-#
-#      defmodule Swift.Cloudstorage do
-#        defstruct []
-#        otp_app = Keyword.fetch!(opts, :otp_app)
-#        use Openstex.Client, otp_app: otp_app, client: __MODULE__
-#        def cache(), do: ExOvh.Auth.Openstack.Swift.Cache
-#        # use Openstex.Swift.V1.Helpers, client: client, config_id: :cloudstorage
-#      end
-
-
-#      swift_mods =
-#      if (otp_app != :ex_ovh)  do
-#        Application.get_env(otp_app, __MODULE__)
-#      else
-#        Application.get_all_env(otp_app)
-#      end
-#      |> Keyword.fetch!(:swift)
-#      |> Keyword.keys()
-
-        # ** THIS IS READY TO GO ONCE SOLVE PROBLEM INSIDE IT **
-#      for mod <- swift_mods do
-#        defmodule Module.concat(Helpers.Swift, Utils.module_name(mod)) do
-#          mod |> Og.log_return(:debug)
-#          Utils.module_name(mod) |> Og.log_return(:debug)
-#          Module.concat(Helpers.Swift, Utils.module_name(mod)) |> Og.log_return(:debug)
-#          use Openstex.Swift.V1.Helpers, client: client, cache: mod
-#        end
-#      end
-
-
-#      defmodule Helpers.Keystone do
-#        use Openstex.Helpers.V2.Keystone, client: Keyword.fetch!(opts, :client)
-#      end
-
+      alias ExOvh.Ovh.Defaults
 
       # public functions
 
-
       def config() do
         otp_app = unquote(opts) |> Keyword.fetch!(:otp_app)
         if (otp_app != :ex_ovh)  do
@@ -69,47 +16,41 @@ defmodule ExOvh.Client do
         end
       end
 
-      def ovh_config() do
-        config() |> Keyword.fetch!(:ovh)
+      # IS THIS SUPERVISOR NEEDED --> START 2 SEPARATE SUPERVISORS ??
+      def start_link(sup_opts \\ []) do
+        ExOvh.Supervisor.start_link(__MODULE__, sup_opts)
       end
 
-      def swift_config() do
-        config() |> Keyword.fetch!(:swift)
+      # client definitions
+
+      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())
       end
 
+      defmodule Swift.Webstorage 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!(:webstorage)
+        # use Openstex.Swift.V1.Helpers, client: __MODULE__
+      end
 
-      def start_link(sup_opts \\ []) do
-        ExOvh.Supervisor.start_link(__MODULE__, ovh_config(), sup_opts)
+      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__
       end
 
+#      defmodule Helpers.Keystone do
+#        use Openstex.Helpers.V2.Keystone, client: Keyword.fetch!(opts, :client)
+#      end
+
 
     end
   end
 
-
-  @callback start_link() :: :ok | {:error, {:already_started, pid}} | {:error, term}
-  @callback config() :: :nil | Keyword.t
-  @callback ovh_config() :: :nil | map
-  @callback swift_config() :: :nil | map
-
-end
-
-#
-#def ovh_config() do
-#  otp_app = Keyword.get(unquote(opts), :otp_app, :ex_ovh)
-#  if (otp_app != :ex_ovh)  do
-#    Application.get_env(otp_app, __MODULE__) |> Keyword.fetch!(:ovh)
-#  else
-#    Application.get_all_env(otp_app) |> Keyword.fetch!(:ovh)
-#  end
-#end
-#
-#def swift_config() do
-#  otp_app = Keyword.get(unquote(opts), :otp_app, :ex_ovh)
-#  if (otp_app != :ex_ovh)  do
-#    Application.get_env(otp_app, __MODULE__) |> Keyword.fetch!(:swift)
-#  else
-#    Application.get_all_env(otp_app) |> Keyword.fetch!(:swift)
-#  end
-#  config() |> Keyword.fetch!(:swift)
-#end
\ No newline at end of file
+end
\ No newline at end of file
diff --git a/lib/defaults.ex b/lib/defaults.ex
index d0bcdf7..34d7c71 100644
--- a/lib/defaults.ex
+++ b/lib/defaults.ex
@@ -1,14 +1,13 @@
-defmodule ExOvh.Defaults do
+defmodule ExOvh.Ovh.Defaults do
   @moduledoc :false
 
   @doc "Returns ovh default configuration settings"
   @spec ovh() :: map
   def ovh() do
-    %{
-      endpoint: "ovh-eu",
-      api_version: "1.0",
-      cloudstorage_endpoint: "https://auth.cloud.ovh.net/v2.0"
-    }
+    [
+     endpoint: endpoints()["ovh-eu"],
+     api_version: "1.0"
+    ]
   end
 
 
diff --git a/lib/mix/tasks/ovh.ex b/lib/mix/tasks/ovh.ex
index ae18dec..3519b90 100644
--- a/lib/mix/tasks/ovh.ex
+++ b/lib/mix/tasks/ovh.ex
@@ -70,7 +70,7 @@ defmodule Mix.Tasks.Ovh do
   """
   use Mix.Task
   alias ExOvh.Utils
-  alias ExOvh.Defaults
+  alias ExOvh.Ovh.Defaults
 
 
   @default_headers [{"Content-Type", "application/json; charset=utf-8"}]
diff --git a/lib/ovh/v1/webstorage/query.ex b/lib/ovh/v1/webstorage/query.ex
index d80cd75..2d3a5d4 100644
--- a/lib/ovh/v1/webstorage/query.ex
+++ b/lib/ovh/v1/webstorage/query.ex
@@ -17,9 +17,7 @@ defmodule ExOvh.Ovh.V1.Webstorage.Query do
 
   ### Example usage
 
-      alias ExOvh.Ovh.V1.Webstorage.Query
-      query = Query.get_services()
-      ExOvh.request(query)
+      ExOvh.Ovh.V1.Webstorage.Query. get_services() |> ExOvh.request()
   """
   @spec get_services() :: Query.t
   def get_services() do
diff --git a/lib/supervisor.ex b/lib/supervisor.ex
index bdd4bb2..46fb17c 100644
--- a/lib/supervisor.ex
+++ b/lib/supervisor.ex
@@ -2,47 +2,63 @@ defmodule ExOvh.Supervisor do
   @moduledoc :false
 
   use Supervisor
-  alias ExOvh.Defaults
-  alias ExOvh.Auth.Supervisor, as: AuthSupervisor
+  alias ExOvh.Ovh.Defaults
+  alias ExOvh.Auth.Ovh.Cache, as: OvhCache
+  alias ExOvh.Auth.Openstack.Swift.Cache, as: SwiftCache
+
 
 
   #  Public
 
 
-  def start_link(client, ovh_config, opts) do
+  def start_link(client, opts) do
     Og.context(__ENV__, :debug)
-    Supervisor.start_link(__MODULE__, {client, ovh_config, opts}, [name: client])
+    Supervisor.start_link(__MODULE__, client, [name: client])
   end
 
 
   #  Callbacks
 
 
-  def init({client, ovh_config, opts}) do
+  def init(client) do
     Og.context(__ENV__, :debug)
+
+    ovh_config = Keyword.merge(Defaults.ovh(), Keyword.fetch!(client.config(), :ovh))
+    webstorage_config = Keyword.get(client.config(), :swift, []) |> Keyword.get(:webstorage, :nil)
+    cloudstorage_config = Keyword.get(client.config(), :swift, []) |> Keyword.get(:cloudstorage, :nil)
+
+    ovh_client = Module.concat(client, Ovh)
+    sup_tree = [
+                {ovh_client, {OvhCache, :start_link, [ovh_client]}, :permanent, 10_000, :worker, [OvhCache]}
+               ]
+
     sup_tree =
-    case ovh_config(ovh_config, client) do
-      {:error, :config_not_found} ->
-        Og.log("No ovh config found. Ovh supervisor will not be started for client #{client}", :error)
-        []
-      valid_ovh_config ->
-        [{AuthSupervisor,
-         {AuthSupervisor, :start_link, [client, valid_ovh_config, opts]}, :permanent, 10_000, :supervisor, [AuthSupervisor]}]
+    case webstorage_config do
+      :nil ->
+        Og.log("No webstorage config found. Skipping initiation of OVH webstorage cdn service", :debug)
+        sup_tree
+      webstorage_config ->
+        webstorage_client = Module.concat(client, Swift.Webstorage)
+        sup_tree ++
+        [{webstorage_client, {SwiftCache, :start_link, [{ovh_client, webstorage_client}]}, :permanent, 10_000, :worker, [SwiftCache]}]
     end
+
+    sup_tree =
+    case cloudstorage_config do
+      :nil ->
+        Og.log("No cloudstorage config found. Skipping initiation of OVH cloudstorage service", :debug)
+        sup_tree
+      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]}]
+    end
+
     if sup_tree === [] do
         raise "No configuration found for ovh."
     end
-    supervise(sup_tree, strategy: :one_for_one, max_restarts: 20)
-  end
 
-
-  @doc "Gets the ovh config settings."
-  @spec ovh_config(map, atom) :: map | {:error, :config_not_found}
-  def ovh_config(ovh_config, client) do
-    case ovh_config do
-      :nil -> {:error, :config_not_found}
-      _ -> Map.merge(Defaults.ovh(), client.ovh_config())
-    end
+    supervise(sup_tree, strategy: :one_for_one, max_restarts: 30)
   end
 
 
diff --git a/lib/utils/utils.ex b/lib/utils/utils.ex
index c74cb97..1fb0f60 100644
--- a/lib/utils/utils.ex
+++ b/lib/utils/utils.ex
@@ -3,7 +3,7 @@ defmodule ExOvh.Utils do
 
   alias ExOvh.Auth.Ovh.Cache, as: OvhCache
   alias ExOvh.Auth.Openstack.Swift.Cache, as: SwiftCache
-  alias ExOvh.Defaults
+  alias ExOvh.Ovh.Defaults
 
 
   @doc """
@@ -66,8 +66,7 @@ defmodule ExOvh.Utils do
       "Ets."
       <>
       (
-        gen_server_name(unquote(client))
-        |> Atom.to_string()
+        unquote(client) |> Atom.to_string()
       )
       |> String.to_atom()
     end