bug fixes and update readme
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
diff --git a/README.md b/README.md
index 534773e..c90654f 100644
--- a/README.md
+++ b/README.md
@@ -113,6 +113,17 @@ Shell Output
}
```
+
+### Add :httpotion to applications on startup (httpotion is used for http requests)
+
+```elixir
+ def application do
+ [
+ applications: [:httpotion]
+ ]
+ end
+```
+
### Starting the supervisor
Add the supervisor to your supervision tree:
@@ -166,11 +177,12 @@ Get the count of containers again
container_count2 = resp.body |> Enum.count()
container_count1 + 1 == container_count2
```
-
-
+
+
### Example 2:
-Adding an object to the "default" container in OVH Webstorage
+
+Adding an object to the "default" container in [OVH CDN Webstorage](https://www.ovh.ie/cdn/webstorage/)
import ExOvh.Query.Openstack.Swift
alias ExOvh.Ovh.OpenstackApi.Webstorage.Cache, as: OpenCache
@@ -187,7 +199,7 @@ Adding an object to the "default" container in OVH Webstorage
ExOvh.ovh_request(create_file_request, %{ openstack: :true, webstorage: service })
-Listing all objects for "default" container to see if the new `server_object` is there.
+Listing all objects for "default" container to see if the new `server_object` is there in [OVH CDN Webstorage](https://www.ovh.ie/cdn/webstorage/)
import ExOvh.Query.Openstack.Swift
alias ExOvh.Ovh.OpenstackApi.Webstorage.Cache, as: OpenCache
@@ -228,6 +240,7 @@ Listing all objects for "default" container to see if the new `server_object` is
- [ ] *Needed* - generate release and publish to hex packages
- [ ] *Needed* - *Tests* - add basic tests for most api calls.
- [ ] *Needed* - *Tests* - verify the supervisor chain, genservers and genserver naming is working ok.
+- [ ] *Needed* - *New functions* - ovh and hubic functions with !.
- [ ] *Maybe* - investigate ways to add sensitive keys, secrets, etc to system env and allow the config.exs to get variables from `System`.
diff --git a/lib/ovh/auth.ex b/lib/ovh/auth.ex
index 476ddd6..00b1163 100644
--- a/lib/ovh/auth.ex
+++ b/lib/ovh/auth.ex
@@ -32,7 +32,7 @@ defmodule ExOvh.Ovh.Auth do
3. Returning `ExOvh.Client.query_t` which is a tuple of the format {method, uri, options} which
can then be easily used to make requests using HTTPpotion, `ovh_request` or `hubic_request`.
"""
- alias ExOvh.Ovh.Openstack.Auth, as: OpenstackAuth
+ alias ExOvh.Ovh.OpenstackApi.Webstorage.Auth, as: Webstorage
alias ExOvh.Ovh.OvhApi.Auth, as: OvhAuth
@@ -57,8 +57,8 @@ defmodule ExOvh.Ovh.Auth do
"""
@spec prepare_request(client :: atom, query :: ExOvh.Client.raw_query_t, opts :: map())
:: ExOvh.Client.query_t
- def prepare_request(client, {method, uri, params} = query, %{openstack: :true} = opts) do
- OpenstackAuth.prepare_request(client, query)
+ def prepare_request(client, {method, uri, params} = query, %{ openstack: :true, webstorage: service } = opts) do
+ Webstorage.prepare_request(client, query, service)
end
def prepare_request(client, {method, uri, params} = query, opts) do
diff --git a/lib/ovh/openstack_api/webstorage/cache.ex b/lib/ovh/openstack_api/webstorage/cache.ex
index 39b77ca..3f9620c 100644
--- a/lib/ovh/openstack_api/webstorage/cache.ex
+++ b/lib/ovh/openstack_api/webstorage/cache.ex
@@ -2,9 +2,9 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
@moduledoc :false
use GenServer
alias ExOvh.Ovh.OpenstackApi.Webstorage.Supervisor, as: WebStorageSupervisor
- import ExOvh.Query.Ovh.Webstorage, only: [get_webstorage_credentials: 1]
+ import ExOvh.Query.Ovh.Webstorage, only: [get_webstorage_credentials: 1, get_webstorage_service: 1]
@get_credentials_retries 10
- @get_credentials_sleep_interval 150
+ @get_credentials_sleep_interval 450
#####################
@@ -40,7 +40,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
end
- def get_account(service), do: get_account(ExOvh)
+ def get_account(service), do: get_account(ExOvh, service)
def get_account(client, service) do
credentials = get_credentials(client, service)
path = URI.parse(credentials.swift_endpoint) |> Map.get(:path)
@@ -119,7 +119,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
{:ok, resp} = ExOvh.ovh_request(get_webstorage_service(service), %{})
%{
- "server" => public_url,
+ "server" => domain,
"storageLimit" => storage_limit
} = resp.body
@@ -199,29 +199,23 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
swift_endpoint: swift_endpoint,
identity_endpoint: identity_endpoint,
service: service,
- public_url: public_url,
+ public_url: public_url(domain, swift_endpoint),
storage_limit: storage_limit
}
}
end
+ defp public_url(domain, swift_endpoint) do
+ path = URI.parse(swift_endpoint) |> Map.get(:path)
+ {version, account} = String.split_at(path, 4)
+ domain <> version <> account
+ end
defp get_credentials(client, service, index) do
Og.context(__ENV__, :debug)
- if ets_tablename(client, service) in :ets.all() do
- [credentials: credentials] = :ets.lookup(ets_tablename(client, service), :credentials)
- if credentials.lock === :true do
- if index > @get_credentials_retries do
- raise "Cannot retrieve openstack credentials from ets table, #{__ENV__.module}, #{__ENV__.line}"
- else
- :timer.sleep(@get_credentials_sleep_interval)
- get_credentials(client, service, index + 1)
- end
- else
- credentials
- end
- else
+
+ retry = fn(client, service, index) ->
if index > @get_credentials_retries do
raise "Cannot retrieve openstack credentials from ets table, #{__ENV__.module}, #{__ENV__.line}"
else
@@ -229,6 +223,21 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
get_credentials(client, service, index + 1)
end
end
+
+ if ets_tablename(client, service) in :ets.all() do
+ table = :ets.lookup(ets_tablename(client, service), :credentials)
+ case table do
+ [credentials: credentials] ->
+ if credentials.lock === :true do
+ retry.(client, service, index)
+ else
+ credentials
+ end
+ [] -> retry.(client,service,index)
+ end
+ else
+ retry.(client, service, index)
+ end
end
@@ -274,7 +283,10 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Cache do
defp supervisor_exists?(client, service) do
- Process.whereis(registered_supervisor_name(client, service))
+ case Process.whereis(registered_supervisor_name(client, service)) do
+ :nil -> :false
+ _pid -> :true
+ end
end
diff --git a/lib/ovh/openstack_api/webstorage/request.ex b/lib/ovh/openstack_api/webstorage/request.ex
index 5cd5e5f..1b47734 100644
--- a/lib/ovh/openstack_api/webstorage/request.ex
+++ b/lib/ovh/openstack_api/webstorage/request.ex
@@ -30,7 +30,7 @@ defmodule ExOvh.Ovh.OpenstackApi.Webstorage.Request do
}
end
else
- {:error, resp}
+ {:error, resp}
end
end
diff --git a/lib/ovh/ovh_api/auth.ex b/lib/ovh/ovh_api/auth.ex
index f88db27..7ffea99 100644
--- a/lib/ovh/ovh_api/auth.ex
+++ b/lib/ovh/ovh_api/auth.ex
@@ -37,7 +37,8 @@ defmodule ExOvh.Ovh.OvhApi.Auth do
config = config(client)
consumer_key = get_consumer_key(config)
if params !== "" and params !== :nil and is_map(params), do: params = Poison.encode!(params)
- opts = [app_secret(config), consumer_key, Atom.to_string(method), uri, params]
+ opts = [app_secret(config), app_key(config), consumer_key, Atom.to_string(method), uri, params]
+ #opts = [app_secret(config), consumer_key, Atom.to_string(method), uri, params]
options = %{ body: params, headers: headers(opts, client), timeout: @timeout }
{method, uri, options}
end
diff --git a/lib/query/openstack/swift/query.ex b/lib/query/openstack/swift/query.ex
index 49a6635..db0892d 100644
--- a/lib/query/openstack/swift/query.ex
+++ b/lib/query/openstack/swift/query.ex
@@ -72,6 +72,24 @@ defmodule ExOvh.Query.Openstack.Swift do
def delete_container(account, container), do: {:delete, account <> "/" <> container, %{ "format" => "json" }}
+ @doc ~S"""
+ DELETE /v1/{account}/{container}, Delete a container
+
+ ### Example usage
+
+ ```elixir
+ import ExOvh.Query.Openstack.Swift
+ alias ExOvh.Hubic.OpenstackApi.Cache, as: OpenCache
+ client = ExOvh
+ account = OpenCache.get_account(client)
+ ExOvh.hubic_request(container_info(account, "new_container"), %{ openstack: :true })
+ ```
+ """
+ @spec container_info(account :: String.t, container :: String.t)
+ :: ExOvh.Client.raw_query_t
+ def container_info(account, container), do: {:head, account <> "/" <> container, %{ "format" => "json" }}
+
+
##########################
# OBJECT RELATED REQUESTS
##########################