diff --git a/lib/auth.ex b/lib/auth.ex index 56294ec..ba6bb4d 100644 --- a/lib/auth.ex +++ b/lib/auth.ex @@ -20,7 +20,7 @@ defmodule GCloudex.Auth do # Compute Engine scopes @compute_read_only "https://www.googleapis.com/auth/compute.readonly" - @compute "https://www.googleapis.com/auth/compute" + @compute "https://www.googleapis.com/auth/compute" @doc """ Retrieves an authentication token for the Google Cloud Storage service. @@ -49,7 +49,7 @@ defmodule GCloudex.Auth do get_token_response |> Map.get(:token) :sql_admin -> - {:ok, get_token_response} = + {:ok, get_token_response} = GoogleAuth.for_scope @sql_scope_admin get_token_response |> Map.get(:token) @@ -66,16 +66,16 @@ defmodule GCloudex.Auth do get_token_response |> Map.get(:token) :compute_read_only -> - {:ok, get_token_response} = + {:ok, get_token_response} = GoogleAuth.for_scope @compute_read_only get_token_response |> Map.get(:token) :compute -> - {:ok, get_token_response} = + {:ok, get_token_response} = GoogleAuth.for_scope @compute get_token_response |> Map.get(:token) end end -end +end \ No newline at end of file diff --git a/lib/cloud_speech/request.ex b/lib/cloud_speech/request.ex index 053ea5f..d5ae8a6 100644 --- a/lib/cloud_speech/request.ex +++ b/lib/cloud_speech/request.ex @@ -11,7 +11,6 @@ defmodule GCloudex.CloudSpeech.Request do quote do @endpoint "speech.googleapis.com" - @project GCloudex.get_project_id @doc""" Sends an HTTP request without any query parameters. @@ -23,7 +22,7 @@ defmodule GCloudex.CloudSpeech.Request do "https://" <> Path.join(@endpoint, path), body, headers ++ [{"Authorization", "Bearer #{Auth.get_token_storage(:cs)}"}, - {"x-goog-project-id", @project}], + {"x-goog-project-id", GCloudex.get_project_id()}], [timeout: 50_000, recv_timeout: 50_000] ) end diff --git a/lib/cloud_sql/impl.ex b/lib/cloud_sql/impl.ex index 6b3892a..b34b46d 100644 --- a/lib/cloud_sql/impl.ex +++ b/lib/cloud_sql/impl.ex @@ -1,18 +1,14 @@ defmodule GCloudex.CloudSQL.Impl do @moduledoc """ - + """ - defmacro __using__(:cloud_sql) do - quote do + defmacro __using__(:cloud_sql) do + quote do use GCloudex.CloudSQL.Request - @project_id GCloudex.get_project_id - @instance_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/instances" @flag_ep "https://www.googleapis.com/sql/v1beta4/flags" - @operation_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/operations" - @tiers_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/tiers" ################# ### Instances ### @@ -23,18 +19,18 @@ defmodule GCloudex.CloudSQL.Impl do """ @spec list_instances() :: HTTPResponse.t def list_instances do - request :get, @instance_ep, [], "" + request :get, instance_ep(), [], "" end @doc """ - Retrieves a resource containing information about the given + Retrieves a resource containing information about the given Cloud SQL 'instance'. """ @spec get_instance(instance :: binary) :: HTTPResponse.t def get_instance(instance) do - request_query :get, @instance_ep, [], "", instance + request_query :get, instance_ep(), [], "", instance end - + @doc """ Creates a new Cloud SQL instance with the specified 'name', 'settings', 'optional_properties' and with the given 'tier' unless already passed through @@ -42,11 +38,11 @@ defmodule GCloudex.CloudSQL.Impl do The 'settings' parameter is meant to have the settings JSON nested object while 'optional_properties' is meant to have other non-required request - fields like the 'replicaConfiguration' JSON nested object field. The - 'settings' map will be nested into 'optional_properties' to match the + fields like the 'replicaConfiguration' JSON nested object field. The + 'settings' map will be nested into 'optional_properties' to match the API's request structure like in the following example: - optional_properties = + optional_properties = %{ name: 'name', region: someRegion, @@ -71,15 +67,15 @@ defmodule GCloudex.CloudSQL.Impl do |> Map.merge(optional_properties) |> Poison.encode! - request :post, @instance_ep, [{"Content-Type", "application/json"}], body + request :post, instance_ep(), [{"Content-Type", "application/json"}], body end @doc """ Deletes the given 'instance' from the project. """ @spec delete_instance(instance :: binary) :: HTTPResponse.t - def delete_instance(instance) do - request_query :delete, @instance_ep, [], "", instance + def delete_instance(instance) do + request_query :delete, instance_ep(), [], "", instance end @doc """ @@ -87,14 +83,14 @@ defmodule GCloudex.CloudSQL.Impl do and the given 'bin_log_file' and 'bin_log_pos'. """ @spec clone_instance(instance :: binary, dest_name :: binary, bin_log_file :: binary, bin_log_pos :: binary) :: HTTPResponse.t - def clone_instance(instance, dest_name, bin_log_file, bin_log_pos) do + def clone_instance(instance, dest_name, bin_log_file, bin_log_pos) do bin_log_coords = %{ "kind" => "sql#binLogCoordinates", "binLogFileName" => bin_log_file, "binLogPosition" => bin_log_pos } - + clone_context = %{ "kind" => "sql#cloneContext", "destinationInstanceName" => dest_name, @@ -105,12 +101,12 @@ defmodule GCloudex.CloudSQL.Impl do "cloneContext" => clone_context } |> Poison.encode! - + request_query( :post, - @instance_ep, - [{"Content-Type", "application/json"}], - body, + instance_ep(), + [{"Content-Type", "application/json"}], + body, instance <> "/clone" ) end @@ -119,12 +115,12 @@ defmodule GCloudex.CloudSQL.Impl do Restarts the given 'instance'. """ @spec restart_instance(instance :: binary) :: HTTPResponse.t - def restart_instance(instance) do + def restart_instance(instance) do request_query( - :post, - @instance_ep, - [{"Content-Type", "application/json"}], - "", + :post, + instance_ep(), + [{"Content-Type", "application/json"}], + "", instance <> "/" <> "restart" ) end @@ -133,10 +129,10 @@ defmodule GCloudex.CloudSQL.Impl do Starts the replication in the read replica 'instance'. """ @spec start_replica(instance :: binary) :: HTTPResponse.t - def start_replica(instance) do + def start_replica(instance) do request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], "", instance <> "/" <> "startReplica" @@ -146,11 +142,11 @@ defmodule GCloudex.CloudSQL.Impl do @doc """ Stops the replication in the read replica 'instance'. """ - @spec stop_replica(instance :: binary) :: HTTPResponse.t - def stop_replica(instance) do + @spec stop_replica(instance :: binary) :: HTTPResponse.t + def stop_replica(instance) do request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], "", instance <> "/" <> "stopReplica" @@ -161,10 +157,10 @@ defmodule GCloudex.CloudSQL.Impl do Promotes the read replica 'instance' to be a stand-alone Cloud SQL instance. """ @spec promote_replica(instance :: binary) :: HTTPResponse.t - def promote_replica(instance) do + def promote_replica(instance) do request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], "", instance <> "/" <> "promoteReplica" @@ -172,11 +168,11 @@ defmodule GCloudex.CloudSQL.Impl do end @doc """ - Failover the 'instance' to its failover replica instance with the + Failover the 'instance' to its failover replica instance with the specified 'settings_version'. """ @spec failover_instance(instance :: binary, settings_version :: number) :: HTTPResponse.t - def failover_instance(instance, settings_version) do + def failover_instance(instance, settings_version) do failover = %{ "kind" => "sql#failoverContext", "settingsVersion" => settings_version @@ -188,7 +184,7 @@ defmodule GCloudex.CloudSQL.Impl do request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/" <> "failover" @@ -204,10 +200,10 @@ defmodule GCloudex.CloudSQL.Impl do immediate; all existing connections to the instance are broken. """ @spec reset_ssl_config(instance :: binary) :: HTTPResponse.t - def reset_ssl_config(instance) do + def reset_ssl_config(instance) do request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], "", instance <> "/" <> "resetSslConfig" @@ -222,12 +218,12 @@ defmodule GCloudex.CloudSQL.Impl do Lists databases in the specified Cloud SQL 'instance'. """ @spec list_databases(instance :: binary) :: HTTPResponse.t - def list_databases(instance) do - request_query :get, @instance_ep, [], "", instance <> "/databases" + def list_databases(instance) do + request_query :get, instance_ep(), [], "", instance <> "/databases" end @doc """ - Creates a new database inside the specified Cloud SQL 'instance' with the + Creates a new database inside the specified Cloud SQL 'instance' with the given 'name'. """ @spec insert_database(instance :: binary, name :: binary) :: HTTPResponse.t @@ -235,12 +231,12 @@ defmodule GCloudex.CloudSQL.Impl do body = %{ "instance" => instance, "name" => name, - "project" => @project_id + "project" => project_id() } |> Poison.encode! - + request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/databases" @@ -252,12 +248,12 @@ defmodule GCloudex.CloudSQL.Impl do Cloud SQL 'instance'. """ @spec get_database(instance :: binary, database :: binary) :: HTTPResponse.t - def get_database(instance, database) do + def get_database(instance, database) do request_query( - :get, - @instance_ep, - [], - "", + :get, + instance_ep(), + [], + "", instance <> "/databases" <> "/" <> database ) end @@ -266,18 +262,18 @@ defmodule GCloudex.CloudSQL.Impl do Deletes the 'database' from the Cloud SQL 'instance'. """ @spec delete_database(instance :: binary, database :: binary) :: HTTPResponse.t - def delete_database(instance, database) do + def delete_database(instance, database) do request_query( - :delete, - @instance_ep, - [], - "", + :delete, + instance_ep(), + [], + "", instance <> "/databases" <> "/" <> database ) end @doc """ - Updates a resource containing information about a 'database' inside a + Updates a resource containing information about a 'database' inside a Cloud SQL 'instance' using patch semantics. The 'db_resource' must follow the description of Database Resources in: https://cloud.google.com/sql/docs/admin-api/v1beta4/databases#resource @@ -288,24 +284,43 @@ defmodule GCloudex.CloudSQL.Impl do request_query( :patch, - @instance_ep, - [{"Content-Type", "application/json"}], + instance_ep(), + [{"Content-Type", "application/json"}], body, instance <> "/databases" <> "/" <> database ) end @doc """ - Updates a resource containing information about a 'database' inside a + Updates a resource containing information about a 'database' inside a + Cloud SQL 'instance' using patch semantics. The 'db_resource' must follow + the description of Database Resources in: + https://cloud.google.com/sql/docs/admin-api/v1beta4/databases#resource + """ + @spec patch_instance(instance :: binary, instance_resource :: Map.t) :: HTTPResponse.t + def patch_instance(instance, instance_resource) do + body = instance_resource |> Poison.encode! + + request_query( + :patch, + instance_ep(), + [{"Content-Type", "application/json"}], + body, + instance + ) + end + + @doc """ + Updates a resource containing information about a 'database' inside a Cloud SQL 'instance'. The 'update_map' must be a Map. """ @spec update_database(instance :: binary, database :: binary, db_resource :: Map.t) :: HTTPResponse.t - def update_database(instance, database, db_resource) do + def update_database(instance, database, db_resource) do body = db_resource |> Poison.encode! request_query( :put, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/databases" <> "/" <> database @@ -320,8 +335,8 @@ defmodule GCloudex.CloudSQL.Impl do List all available database flags for Google the Cloud SQL 'instance'. """ @spec list_flags :: HTTPResponse.t - def list_flags do - request :get, @flag_ep, [], "" + def list_flags do + request :get, @flag_ep, [], "" end ################## @@ -329,28 +344,28 @@ defmodule GCloudex.CloudSQL.Impl do ################## @doc """ - Lists all instance operations that have been performed on the given + Lists all instance operations that have been performed on the given Cloud SQL 'instance' in the reverse chronological order of the start time. """ @spec list_operations(instance :: binary) :: HTTPResponse.t - def list_operations(instance) do + def list_operations(instance) do request( :get, - @operation_ep <> "?" <> "instance=#{instance}", + operation_ep() <> "?" <> "instance=#{instance}", [], "" ) end @doc """ - Retrieves the instance operation with 'operation_id' that has been + Retrieves the instance operation with 'operation_id' that has been performed on an instance. """ @spec get_operation(operation_id :: binary) :: HTTPResponse.t - def get_operation(operation_id) do + def get_operation(operation_id) do request_query( :get, - @operation_ep, + operation_ep(), [], "", operation_id @@ -362,25 +377,25 @@ defmodule GCloudex.CloudSQL.Impl do ############# @doc """ - Lists all available service tiers for Google Cloud SQL, for example D1, D2. + Lists all available service tiers for Google Cloud SQL, for example D1, D2. """ @spec list_tiers :: HTTPResponse.t - def list_tiers do - request :get, @tiers_ep, [], "" - end + def list_tiers do + request :get, tiers_ep(), [], "" + end ############# ### Users ### - ############# + ############# @doc """ Lists users in the specified Cloud SQL 'instance'. """ @spec list_users(instance :: binary) :: HTTPResponse.t - def list_users(instance) do + def list_users(instance) do request_query( - :get, - @instance_ep, + :get, + instance_ep(), [], "", instance <> "/" <> "users" @@ -393,18 +408,18 @@ defmodule GCloudex.CloudSQL.Impl do (defaults to '% (any host)'). """ @spec insert_user(instance :: binary, name :: binary, password :: binary, host :: binary) :: HTTPResponse.t - def insert_user(instance, name, password, host \\ "%") do + def insert_user(instance, name, password, host \\ "%") do body = %{ "name" => name, "password" => password, "host" => host, - "project" => @project_id, + "project" => project_id(), "instance" => instance } |> Poison.encode! request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/" <> "users" @@ -416,13 +431,13 @@ defmodule GCloudex.CloudSQL.Impl do 'name' and 'password'. """ @spec update_user(instance :: binary, host :: binary, name :: binary, password :: binary) :: HTTPResponse.t - def update_user(instance, host, name, password) do + def update_user(instance, host, name, password) do body = %{"password" => password} |> Poison.encode! query = "#{instance}/users?host=#{host}&name=#{name}" request_query( :put, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, query @@ -433,12 +448,12 @@ defmodule GCloudex.CloudSQL.Impl do Deletes a user with the given 'host' and 'name' from the Cloud SQL 'instance'. """ @spec delete_user(instance :: binary, host :: binary, name :: binary) :: HTTPResponse.t - def delete_user(instance, host, name) do + def delete_user(instance, host, name) do query = "#{instance}/users?host=#{host}&name=#{name}" request_query( :delete, - @instance_ep, + instance_ep(), [], "", query @@ -450,14 +465,14 @@ defmodule GCloudex.CloudSQL.Impl do ################### @doc """ - Lists all backup runs associated with a given 'instance' and configuration + Lists all backup runs associated with a given 'instance' and configuration in the reverse chronological order of the backup initiation time. """ @spec list_backup_runs(instance :: binary) :: HTTPResponse.t - def list_backup_runs(instance) do + def list_backup_runs(instance) do request_query( :get, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "backupRuns" @@ -469,24 +484,24 @@ defmodule GCloudex.CloudSQL.Impl do of 'run_id' and belonging to the given 'instance'. """ @spec get_backup_run(instance :: binary, run_id :: binary | number) :: HTTPResponse.t - def get_backup_run(instance, run_id) do + def get_backup_run(instance, run_id) do gbr instance, run_id end - defp gbr(instance, run_id) when is_integer(run_id) do + defp gbr(instance, run_id) when is_integer(run_id) do request_query( :get, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "backupRuns" <> "/" <> Integer.to_string(run_id) ) end - defp gbr(instance, run_id) do + defp gbr(instance, run_id) do request_query( :get, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "backupRuns" <> "/" <> run_id @@ -494,28 +509,28 @@ defmodule GCloudex.CloudSQL.Impl do end @doc """ - Deletes the backup taken by a backup run with ID 'run_id' and belonging to - the given 'instance'. + Deletes the backup taken by a backup run with ID 'run_id' and belonging to + the given 'instance'. """ @spec delete_backup_run(instance :: binary, run_id :: binary | number) :: HTTPResponse.t - def delete_backup_run(instance, run_id) do + def delete_backup_run(instance, run_id) do dbr instance, run_id end - defp dbr(instance, run_id) when is_integer(run_id) do + defp dbr(instance, run_id) when is_integer(run_id) do request_query( :delete, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "backupRuns" <> "/" <> Integer.to_string(run_id) ) end - defp dbr(instance, run_id) do + defp dbr(instance, run_id) do request_query( :delete, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "backupRuns" <> "/" <> run_id @@ -530,10 +545,10 @@ defmodule GCloudex.CloudSQL.Impl do Lists all of the current SSL certificates for the 'instance'. """ @spec list_ssl_certs(instance :: binary) :: HTTPResponse.t - def list_ssl_certs(instance) do + def list_ssl_certs(instance) do request_query( :get, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "sslCerts" @@ -541,16 +556,16 @@ defmodule GCloudex.CloudSQL.Impl do end @doc """ - Retrieves a particular SSL certificate belonging to the given 'instance' and - with the provided 'sh1_fingerprint'. Does not include the private key - (required for usage). The private key must be saved from the response + Retrieves a particular SSL certificate belonging to the given 'instance' and + with the provided 'sh1_fingerprint'. Does not include the private key + (required for usage). The private key must be saved from the response to initial creation. """ @spec get_ssl_cert(instance :: binary, sh1_fingerprint :: binary) :: HTTPResponse.t - def get_ssl_cert(instance, sha1_fingerprint) do + def get_ssl_cert(instance, sha1_fingerprint) do request_query( :get, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "sslCerts" <> "/" <> sha1_fingerprint @@ -559,19 +574,19 @@ defmodule GCloudex.CloudSQL.Impl do @doc """ Creates an SSL certificate for the given 'instance' and gives it the name - specified in 'common_name' and returns it along with the private key and - server certificate authority. + specified in 'common_name' and returns it along with the private key and + server certificate authority. - For First Generation instances, the new certificate does not take effect + For First Generation instances, the new certificate does not take effect until the instance is restarted. """ @spec insert_ssl_cert(instance :: binary, common_name :: binary) :: HTTPResponse.t - def insert_ssl_cert(instance, common_name) do + def insert_ssl_cert(instance, common_name) do body = %{"commonName" => common_name} |> Poison.encode! request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/" <> "sslCerts" @@ -579,14 +594,14 @@ defmodule GCloudex.CloudSQL.Impl do end @doc """ - Deletes the SSL certificate with the given 'sha1_fingerprint' from the + Deletes the SSL certificate with the given 'sha1_fingerprint' from the specified 'instance'. """ @spec delete_ssl_cert(instance :: binary, sha1_fingerprint :: binary) :: HTTPResponse.t - def delete_ssl_cert(instance, sha1_fingerprint) do + def delete_ssl_cert(instance, sha1_fingerprint) do request_query( :delete, - @instance_ep, + instance_ep(), [], "", instance <> "/" <> "sslCerts" <> "/" <> sha1_fingerprint @@ -594,22 +609,30 @@ defmodule GCloudex.CloudSQL.Impl do end @doc """ - Generates a short-lived X509 certificate containing the provided 'public_key' - and signed by a private key specific to the target 'instance'. Users may use + Generates a short-lived X509 certificate containing the provided 'public_key' + and signed by a private key specific to the target 'instance'. Users may use the certificate to authenticate as themselves when connecting to the database. """ @spec create_ephemeral_ssl_cert(instance :: binary, public_key :: binary) :: HTTPResponse.t - def create_ephemeral_ssl_cert(instance, public_key) do + def create_ephemeral_ssl_cert(instance, public_key) do body = %{"public_key" => public_key} |> Poison.encode! request_query( :post, - @instance_ep, + instance_ep(), [{"Content-Type", "application/json"}], body, instance <> "/" <> "createEphemeral" ) end + + defp project_id, do: GCloudex.get_project_id() + + defp instance_ep, do: "https://www.googleapis.com/sql/v1beta4/projects/#{project_id()}/instances" + + defp operation_ep, do: "https://www.googleapis.com/sql/v1beta4/projects/#{project_id()}/operations" + + defp tiers_ep, do: "https://www.googleapis.com/sql/v1beta4/projects/#{project_id()}/tiers" end end -end \ No newline at end of file +end diff --git a/lib/cloud_sql/request.ex b/lib/cloud_sql/request.ex index 9931a64..b364352 100644 --- a/lib/cloud_sql/request.ex +++ b/lib/cloud_sql/request.ex @@ -7,23 +7,21 @@ defmodule GCloudex.CloudSQL.Request do Offers HTTP requests to be used in by the Google Cloud SQL wrapper. """ - defmacro __using__(_opts) do - quote do - - @project_id GCloudex.get_project_id + defmacro __using__(_opts) do + quote do @doc """ - Sends a HTTP request with the given 'verb', 'headers' and 'body' to the - specified 'endpoint'. The authorization and google project headers are + Sends a HTTP request with the given 'verb', 'headers' and 'body' to the + specified 'endpoint'. The authorization and google project headers are added automatically. """ @spec request(verb :: atom, endpoint :: binary, headers :: [{key :: binary, value :: binary}], body :: binary) :: HTTPResponse.t - def request(verb, endpoint, headers \\ [], body \\ "") do + def request(verb, endpoint, headers \\ [], body \\ "") do HTTP.request( verb, endpoint, body, - headers ++ [{"x-goog-project-id", @project_id}, + headers ++ [{"x-goog-project-id", project_id()}, {"Authorization", "Bearer #{Auth.get_token_storage(:sql_admin)}"}], [] ) @@ -31,16 +29,16 @@ defmodule GCloudex.CloudSQL.Request do @doc """ Sends a HTTP request with the given 'verb', 'headers', 'body' and query - 'parameters' to the specified 'endpoint'. The authorization and google + 'parameters' to the specified 'endpoint'. The authorization and google project headers are added automatically. """ @spec request_query(verb :: atom, endpoint :: binary, headers :: [{key :: binary, value :: binary}], body :: binary, parameters :: binary) :: HTTPResponse.t - def request_query(verb, endpoint, headers \\ [], body \\ "", parameters) do + def request_query(verb, endpoint, headers \\ [], body \\ "", parameters) do HTTP.request( verb, endpoint <> "/" <> parameters, body, - headers ++ [{"x-goog-project-id", @project_id}, + headers ++ [{"x-goog-project-id", project_id()}, {"Authorization", "Bearer #{Auth.get_token_storage(:sql_admin)}"}], [] ) @@ -51,6 +49,8 @@ defmodule GCloudex.CloudSQL.Request do request: 4, request_query: 5 ] + + defp project_id, do: GCloudex.get_project_id() end end -end \ No newline at end of file +end diff --git a/lib/cloud_storage/impl.ex b/lib/cloud_storage/impl.ex index f0bc4b8..4d4d225 100644 --- a/lib/cloud_storage/impl.ex +++ b/lib/cloud_storage/impl.ex @@ -3,12 +3,11 @@ defmodule GCloudex.CloudStorage.Impl do @moduledoc """ Wrapper for Google Cloud Storage API. """ - defmacro __using__(:cloud_storage) do - quote do + defmacro __using__(:cloud_storage) do + quote do use GCloudex.CloudStorage.Request @endpoint "storage.googleapis.com" - @project GCloudex.get_project_id ################### ### GET Service ### @@ -145,7 +144,7 @@ defmodule GCloudex.CloudStorage.Impl do """ @spec create_bucket(bucket :: binary) :: HTTPResponse.t def create_bucket(bucket) do - headers = [{"x-goog-project-id", @project}] + headers = [{"x-goog-project-id", project_id()}] request :put, bucket, headers, "" end @@ -157,7 +156,7 @@ defmodule GCloudex.CloudStorage.Impl do """ @spec create_bucket(bucket :: binary, region :: binary) :: HTTPResponse.t def create_bucket(bucket, region) do - headers = [{"x-goog-project-id", @project}] + headers = [{"x-goog-project-id", project_id()}] body = """ @@ -174,7 +173,7 @@ defmodule GCloudex.CloudStorage.Impl do """ @spec create_bucket(bucket :: binary, region :: binary, class :: binary) :: HTTPResponse.t def create_bucket(bucket, region, class) do - headers = [{"x-goog-project-id", @project}] + headers = [{"x-goog-project-id", project_id()}] body = """ @@ -384,7 +383,9 @@ defmodule GCloudex.CloudStorage.Impl do defp parse_query_params([{param, val} = _head | []], query), do: query <> param <> "=" <> val defp parse_query_params([{param, val} = _head | tail], query) do parse_query_params tail, query <> param <> "=" <> val <> "&" - end + end + + defp project_id, do: GCloudex.get_project_id() end - end + end end diff --git a/lib/cloud_storage/request.ex b/lib/cloud_storage/request.ex index bb1eff2..64173bc 100644 --- a/lib/cloud_storage/request.ex +++ b/lib/cloud_storage/request.ex @@ -7,11 +7,10 @@ defmodule GCloudex.CloudStorage.Request do Offers HTTP requests to be used in by the Google Cloud Storage wrapper. """ - defmacro __using__(_opts) do + defmacro __using__(_opts) do quote do @endpoint "storage.googleapis.com" - @project_id GCloudex.get_project_id @doc""" Sends an HTTP request according to the Service resource in the Google Cloud @@ -24,7 +23,7 @@ defmodule GCloudex.CloudStorage.Request do @endpoint, "", [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", project_id()}, {"Authorization", "Bearer #{Auth.get_token_storage(:full_control)}"} ], [] @@ -57,16 +56,18 @@ defmodule GCloudex.CloudStorage.Request do body, headers ++ [{"Authorization", "Bearer #{Auth.get_token_storage(:full_control)}"}], - [timeout: 50_000, recv_timeout: 50_000] + [] ) end defoverridable [ - request_service: 0, + request_service: 0, request: 3, - request: 4, + request: 4, request_query: 5 ] + + defp project_id, do: GCloudex.get_project_id() end end end diff --git a/lib/compute_engine/impl.ex b/lib/compute_engine/impl.ex index d34df92..a65144c 100644 --- a/lib/compute_engine/impl.ex +++ b/lib/compute_engine/impl.ex @@ -1,30 +1,26 @@ defmodule GCloudex.ComputeEngine.Impl do @moduledoc """ - + """ - defmacro __using__(:compute_engine) do - quote do + defmacro __using__(:compute_engine) do + quote do use GCloudex.ComputeEngine.Request - @project_id GCloudex.get_project_id - @instance_ep "https://www.googleapis.com/compute/v1/projects/#{@project_id}/zones" - @no_zone_ep "https://www.googleapis.com/compute/v1/projects/#{@project_id}" - ################### ### Autoscalers ### ################### @doc """ - Retrieves a list of autoscalers contained within the specified 'zone' and + Retrieves a list of autoscalers contained within the specified 'zone' and according to the 'query_params' if provided. """ @spec list_autoscalers(zone :: binary, query_params :: map) :: HTTPResponse.t def list_autoscalers(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/zones/#{zone}/autoscalers", [], "", query + request :get, no_zone_ep() <> "/zones/#{zone}/autoscalers", [], "", query end @doc """ @@ -34,11 +30,11 @@ defmodule GCloudex.ComputeEngine.Impl do def get_autoscaler(zone, autoscaler, fields \\ "") do query = fields_binary_to_map fields - request :get, @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}", [], "", query + request :get, no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}", [], "", query end @doc """ - Creates an autoscaler in the given 'zone' using the data provided in + Creates an autoscaler in the given 'zone' using the data provided in 'autoscaler_resource'. For the properties and structure of the 'autoscaler_resource' check @@ -50,10 +46,10 @@ defmodule GCloudex.ComputeEngine.Impl do body = autoscaler_resource |> Poison.encode! request( - :post, - @no_zone_ep <> "/zones/#{zone}/autoscalers", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/zones/#{zone}/autoscalers", + [{"Content-Type", "application/json"}], + body, query) end @@ -62,9 +58,9 @@ defmodule GCloudex.ComputeEngine.Impl do the 'autoscaler_resource'. This function supports patch semantics. """ @spec patch_autoscaler(zone :: binary, autoscaler_name :: binary, autoscaler_resource :: Map.t, fields :: binary) :: HTTPResponse.t - def patch_autoscaler(zone, autoscaler_name, autoscaler_resource, fields \\ "") do - query = - if fields == "" do + def patch_autoscaler(zone, autoscaler_name, autoscaler_resource, fields \\ "") do + query = + if fields == "" do %{"autoscaler" => autoscaler_name} |> URI.encode_query else %{"autoscaler" => autoscaler_name, "fields" => fields} |> URI.encode_query @@ -73,23 +69,23 @@ defmodule GCloudex.ComputeEngine.Impl do body = autoscaler_resource |> Poison.encode! request( - :patch, - @no_zone_ep <> "/zones/#{zone}/autoscalers", + :patch, + no_zone_ep() <> "/zones/#{zone}/autoscalers", [{"Content-Type", "application/json"}], body, query) end @doc """ - Updates an autoscaler in the specified 'zone' using the data included in + Updates an autoscaler in the specified 'zone' using the data included in the 'autoscaler_resource'. The 'autoscaler_name' may be provided but it's optional. """ @spec update_autoscaler(zone :: binary, autoscaler_name :: binary, autoscaler_resource :: Map.t, fields :: binary) :: HTTPResponse.t def update_autoscaler(zone, autoscaler_name \\ "", autoscaler_resource, fields \\ "") do body = autoscaler_resource |> Poison.encode! - query = - case {autoscaler_name == "", fields == ""} do + query = + case {autoscaler_name == "", fields == ""} do {true, true} -> "" {true, false} -> @@ -98,11 +94,11 @@ defmodule GCloudex.ComputeEngine.Impl do %{"autoscaler" => autoscaler_name} |> URI.encode_query {false, false} -> %{"autoscaler" => autoscaler_name, "fields" => fields} |> URI.encode_query - end + end request( - :put, - @no_zone_ep <> "/zones/#{zone}/autoscalers", + :put, + no_zone_ep() <> "/zones/#{zone}/autoscalers", [{"Content-Type", "application/json"}], body, query) @@ -115,20 +111,20 @@ defmodule GCloudex.ComputeEngine.Impl do def delete_autoscaler(zone, autoscaler, fields \\ "") do query = fields_binary_to_map fields - request(:delete, @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}", [], "", query) + request(:delete, no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}", [], "", query) end @doc """ - Retrieves an aggregated list of autoscalers according to the given + Retrieves an aggregated list of autoscalers according to the given 'query_params' if provided. """ @spec aggregated_list_of_autoscalers(query_params :: Map.t) :: HTTPResponse.t def aggregated_list_of_autoscalers(query_params \\ %{}) do query = query_params |> URI.encode_query - request(:get, @no_zone_ep <> "/aggregated/autoscalers", [], "", query) - end - + request(:get, no_zone_ep() <> "/aggregated/autoscalers", [], "", query) + end + ################# ### DiskTypes ### ################# @@ -138,12 +134,12 @@ defmodule GCloudex.ComputeEngine.Impl do to the given 'query_params' if provided. """ @spec list_disk_types(zone :: binary, query_params :: Map.t) :: HTTPResponse.t - def list_disk_types(zone, query_params \\ %{}) do + def list_disk_types(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/zones/#{zone}/diskTypes", [], "", query + request :get, no_zone_ep() <> "/zones/#{zone}/diskTypes", [], "", query end - + @doc """ Returns the specified 'disk_type' if it exists in the given 'zone'. """ @@ -152,8 +148,8 @@ defmodule GCloudex.ComputeEngine.Impl do query = fields_binary_to_map fields request( - :get, - @no_zone_ep <> "/zones/#{zone}/diskTypes/#{disk_type}", + :get, + no_zone_ep() <> "/zones/#{zone}/diskTypes/#{disk_type}", [], "", query) @@ -167,8 +163,8 @@ defmodule GCloudex.ComputeEngine.Impl do def aggregated_list_of_disk_types(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/aggregated/diskTypes", [], "", query - end + request :get, no_zone_ep() <> "/aggregated/diskTypes", [], "", query + end ############# ### Disks ### @@ -181,8 +177,8 @@ defmodule GCloudex.ComputeEngine.Impl do @spec list_disks(zone :: binary, query_params :: Map.t) :: HTTPResponse.t def list_disks(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - - request :get, @no_zone_ep <> "/zones/#{zone}/disks", [], "", query + + request :get, no_zone_ep() <> "/zones/#{zone}/disks", [], "", query end @doc """ @@ -191,27 +187,27 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec get_disk(zone :: binary, disk :: binary, fields :: binary) :: HTTPResponse.t def get_disk(zone, disk, fields \\ "") do - query = fields_binary_to_map fields + query = fields_binary_to_map fields - request :get, @no_zone_ep <> "/zones/#{zone}/disks/#{disk}", [], "", query + request :get, no_zone_ep() <> "/zones/#{zone}/disks/#{disk}", [], "", query end @doc """ - Creates a persistent disk in the specified 'zone' using the data in the - 'disk_resource'. You can create a disk with a 'source_image' or a - sourceSnapshot (provided in the 'disk_resource'), or create an empty 500 GB - data disk by omitting all properties. You can also create a disk that is - larger than the default size by specifying the sizeGb property in the + Creates a persistent disk in the specified 'zone' using the data in the + 'disk_resource'. You can create a disk with a 'source_image' or a + sourceSnapshot (provided in the 'disk_resource'), or create an empty 500 GB + data disk by omitting all properties. You can also create a disk that is + larger than the default size by specifying the sizeGb property in the 'disk_resource'. """ @spec insert_disk(zone :: binary, disk_resource :: map, source_image :: binary, fields :: binary) :: HTTPResponse.t - def insert_disk(zone, disk_resource, source_image \\ "", fields \\ "") do - if not Map.has_key?(disk_resource, "name") do + def insert_disk(zone, disk_resource, source_image \\ "", fields \\ "") do + if not Map.has_key?(disk_resource, "name") do raise ArgumentError, message: "The Disk Resource must contain at least the 'name' key." end - query = - case {source_image != "", fields != ""} do + query = + case {source_image != "", fields != ""} do {true, true} -> fields_binary_to_map(fields) <> "&sourceImage=#{source_image}" {true, false} -> @@ -226,7 +222,7 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @no_zone_ep <> "/zones/#{zone}/disks", + no_zone_ep() <> "/zones/#{zone}/disks", [{"Content-Type", "application/json"}], body, query) @@ -239,23 +235,23 @@ defmodule GCloudex.ComputeEngine.Impl do def delete_disk(zone, disk, fields \\ "") do query = fields_binary_to_map fields - request :delete, @no_zone_ep <> "/zones/#{zone}/disks/#{disk}", [], "", query + request :delete, no_zone_ep() <> "/zones/#{zone}/disks/#{disk}", [], "", query end @doc """ - Resizes the specified persistent 'disk' if it exists in the given 'zone' to + Resizes the specified persistent 'disk' if it exists in the given 'zone' to the provided 'size_gb'. """ @spec resize_disk(zone :: binary, disk :: binary, size_gb :: pos_integer, fields :: binary) :: HTTPResponse.t - def resize_disk(zone, disk, size_gb, fields \\ "") when size_gb > 0 do + def resize_disk(zone, disk, size_gb, fields \\ "") when size_gb > 0 do query = fields_binary_to_map fields body = %{"sizeGb" => size_gb} |> Poison.encode! request( - :post, - @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/resize", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/resize", + [{"Content-Type", "application/json"}], + body, query) end @@ -267,13 +263,13 @@ defmodule GCloudex.ComputeEngine.Impl do def aggregated_list_of_disks(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/aggregated/disks", [], "", query + request :get, no_zone_ep() <> "/aggregated/disks", [], "", query end - + @doc """ Creates a snapshot of a specified persistent 'disk' if it exists in the given 'zone' and according to the given 'resource'. The 'request' map must - contain the keys "name" and "description" and "name" must obey the + contain the keys "name" and "description" and "name" must obey the refex '(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'. """ @spec create_snapshot(zone :: binary, disk :: binary, request :: Map.t, fields :: binary) :: HTTPResponse.t @@ -282,26 +278,26 @@ defmodule GCloudex.ComputeEngine.Impl do body = request |> Poison.encode! request( - :post, - @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/createSnapshot", + :post, + no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/createSnapshot", [{"Content-Type", "application/json"}], body, query) - end + end ################# ### Firewalls ### - ################# + ################# @doc """ - Retrieves the list of firewall rules according to the 'query_params' if + Retrieves the list of firewall rules according to the 'query_params' if provided. """ @spec list_firewalls(query_params :: Map.t) :: HTTPResponse.t def list_firewalls(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/global/firewalls", [], "", query + request :get, no_zone_ep() <> "/global/firewalls", [], "", query end @doc """ @@ -311,11 +307,11 @@ defmodule GCloudex.ComputeEngine.Impl do def get_firewall(firewall, fields \\ "") do query = fields_binary_to_map fields - request :get, @no_zone_ep <> "/global/firewalls/#{firewall}", [], "", query + request :get, no_zone_ep() <> "/global/firewalls/#{firewall}", [], "", query end @doc """ - Creates a new firewall using the data included in 'firewall_resource'. For + Creates a new firewall using the data included in 'firewall_resource'. For information about the structure and properties of Firewall Resources check https://cloud.google.com/compute/docs/reference/latest/firewalls#resource """ @@ -325,15 +321,15 @@ defmodule GCloudex.ComputeEngine.Impl do body = firewall_resource |> Poison.encode! request( - :post, - @no_zone_ep <> "/global/firewalls", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/global/firewalls", + [{"Content-Type", "application/json"}], + body, query) end @doc """ - Updates the specified 'firewall' rule with the data included in the + Updates the specified 'firewall' rule with the data included in the 'firewall_resource'. This function supports patch semantics. """ @spec patch_firewall(firewall :: binary, firewall_resource :: Map.t, fields :: binary) :: HTTPResponse.t @@ -342,15 +338,15 @@ defmodule GCloudex.ComputeEngine.Impl do body = firewall_resource |> Poison.encode! request( - :patch, - @no_zone_ep <> "/global/firewalls/#{firewall}", - [{"Content-Type", "application/json"}], - body, + :patch, + no_zone_ep() <> "/global/firewalls/#{firewall}", + [{"Content-Type", "application/json"}], + body, query) end @doc """ - Updates the specified 'firewall' rule with the data included in the + Updates the specified 'firewall' rule with the data included in the 'firewall_resource'. """ @spec update_firewall(firewall :: binary, firewall_resource :: map, fields :: binary) :: HTTPResponse.t @@ -359,11 +355,11 @@ defmodule GCloudex.ComputeEngine.Impl do body = firewall_resource |> Poison.encode! request( - :put, - @no_zone_ep <> "/global/firewalls/#{firewall}", - [{"Content-Type", "application/json"}], - body, - query) + :put, + no_zone_ep() <> "/global/firewalls/#{firewall}", + [{"Content-Type", "application/json"}], + body, + query) end @doc """ @@ -375,11 +371,11 @@ defmodule GCloudex.ComputeEngine.Impl do request( :delete, - @no_zone_ep <> "/global/firewalls/#{firewall}", + no_zone_ep() <> "/global/firewalls/#{firewall}", [], "", query) - end + end ############## ### Images ### @@ -392,22 +388,22 @@ defmodule GCloudex.ComputeEngine.Impl do def list_images(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/global/images", [], "", query + request :get, no_zone_ep() <> "/global/images", [], "", query end @doc """ Returns the specified private 'image'. For public images use 'get_public_image/3'. """ @spec get_image(image :: binary, fields :: binary) :: HTTPResponse.t - def get_image(image, fields \\ "") do - query = - if fields == "" do + def get_image(image, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end - request(:get, @no_zone_ep <> "/global/images/#{image}", [], "", query) + request(:get, no_zone_ep() <> "/global/images/#{image}", [], "", query) end @doc""" @@ -415,14 +411,14 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec get_public_image(image :: binary, project :: binary, fields :: binary) :: HTTPResponse.t def get_public_image(image, project, fields \\ "") do - query = + query = if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end - new_ep = @no_zone_ep |> String.replace(@project_id, project) + new_ep = no_zone_ep() |> String.replace(project_id(), project) request(:get, new_ep <> "/global/images/#{image}", [], "", query) end @@ -431,43 +427,43 @@ defmodule GCloudex.ComputeEngine.Impl do Creates an image with the provided 'image_resource'. """ @spec insert_image_with_resource(image_resource :: Map.t, fields :: binary) :: HTTPResponse.t - def insert_image_with_resource(image_resource, fields \\ "") when is_map(image_resource) do + def insert_image_with_resource(image_resource, fields \\ "") when is_map(image_resource) do body = image_resource |> Poison.encode! - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end request( - :post, - @no_zone_ep <> "/global/images", - [{"Content-Type", "application/json"}], + :post, + no_zone_ep() <> "/global/images", + [{"Content-Type", "application/json"}], body, - query) + query) end @doc """ Creates an image with the given 'name' and 'source_url'. This function uses - the minimal amount of parameters needed to build the image. For a more - detailed image creation use insert_image_with_resource/2 where more complex + the minimal amount of parameters needed to build the image. For a more + detailed image creation use insert_image_with_resource/2 where more complex Image Resources can be passed. """ @spec insert_image(name :: binary, source_url :: binary, fields :: binary) :: HTTPResponse.t def insert_image(name, source_url, fields \\ "") do body = %{"name" => name, "rawDisk" => %{"source" => source_url}} |> Poison.encode! - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end request( - :post, - @no_zone_ep <> "/global/images", - [{"Content-Type", "application/json"}], + :post, + no_zone_ep() <> "/global/images", + [{"Content-Type", "application/json"}], body, query) end @@ -477,25 +473,25 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec delete_image(image :: binary, fields :: binary) :: HTTPResponse.t def delete_image(image, fields \\ "") do - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end - request(:delete, @no_zone_ep <> "/global/images/#{image}", [], "", query) + request(:delete, no_zone_ep() <> "/global/images/#{image}", [], "", query) end @doc """ Sets the deprecation status of an 'image' using the data provided in the - 'request_params'. + 'request_params'. """ @spec deprecate_image(image :: binary, request_params :: Map.t, fields :: binary) :: HTTPResponse.t - def deprecate_image(image, request_params, fields \\ "") do + def deprecate_image(image, request_params, fields \\ "") do body = request_params |> Poison.encode! - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query @@ -503,29 +499,29 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @no_zone_ep <> "/global/images/#{image}/deprecate", - [{"Content-Type", "application/json"}], - body, + no_zone_ep() <> "/global/images/#{image}/deprecate", + [{"Content-Type", "application/json"}], + body, query) - end + end ###################### ### InstanceGroups ### ###################### @doc """ - Retrieves the list of instance groups that are located in the + Retrieves the list of instance groups that are located in the specified 'zone'. """ @spec list_instance_groups(zone :: binary, query_params :: Map.t) :: HTTPResponse.t def list_instance_groups(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/zones/#{zone}/instanceGroups", [], "", query + request :get, no_zone_ep() <> "/zones/#{zone}/instanceGroups", [], "", query end @doc """ - Lists the instances in the specified 'instance_group' if it exists in the + Lists the instances in the specified 'instance_group' if it exists in the given 'zone'. A filter for the state of the instances can be passed through 'instance_state'. """ @@ -536,12 +532,12 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}/listInstances", + no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}/listInstances", [{"Content-Type", "application/json"}], body, query) end - + @doc """ Returns the specified 'instance_group' if it exists in the given 'zone'. """ @@ -549,7 +545,7 @@ defmodule GCloudex.ComputeEngine.Impl do def get_instance_group(zone, instance_group, fields \\ "") do query = fields_binary_to_map fields - request(:get, @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}", [], "", query) + request(:get, no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}", [], "", query) end @doc """ @@ -562,10 +558,10 @@ defmodule GCloudex.ComputeEngine.Impl do def insert_instance_group(zone, instance_group_resource, fields \\ "") when is_map(instance_group_resource) do query = fields_binary_to_map fields body = instance_group_resource |> Poison.encode! - + request( :post, - @no_zone_ep <> "/zones/#{zone}/instanceGroups", + no_zone_ep() <> "/zones/#{zone}/instanceGroups", [{"Content-Type", "application/json"}], body, query) @@ -580,10 +576,10 @@ defmodule GCloudex.ComputeEngine.Impl do request( :delete, - @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}", + no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}", [], "", - query) + query) end @doc """ @@ -593,7 +589,7 @@ defmodule GCloudex.ComputeEngine.Impl do def aggregated_list_of_instance_groups(query_params \\ %{}) do query = query_params |> URI.encode_query - request(:get, @no_zone_ep <> "/aggregated/instanceGroups", [], "", query) + request(:get, no_zone_ep() <> "/aggregated/instanceGroups", [], "", query) end @doc """ @@ -607,10 +603,10 @@ defmodule GCloudex.ComputeEngine.Impl do |> Poison.encode! request( - :post, - @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}/addInstances", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}/addInstances", + [{"Content-Type", "application/json"}], + body, query) end @@ -625,11 +621,11 @@ defmodule GCloudex.ComputeEngine.Impl do |> Poison.encode! request( - :post, - @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}/removeInstances", - [{"Content-Type", "application/json"}], - body, - query) + :post, + no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}/removeInstances", + [{"Content-Type", "application/json"}], + body, + query) end @doc """ @@ -642,18 +638,18 @@ defmodule GCloudex.ComputeEngine.Impl do query = fields_binary_to_map fields body = %{"namedPorts" => build_list_of_ports(ports, [])} - body = - if fingerprint != "" do + body = + if fingerprint != "" do body |> Map.put_new("fingerprint", fingerprint) |> Poison.encode! else body |> Poison.encode! end request( - :post, - @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{instance_group}/setNamedPorts", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{instance_group}/setNamedPorts", + [{"Content-Type", "application/json"}], + body, query) end @@ -663,9 +659,9 @@ defmodule GCloudex.ComputeEngine.Impl do end defp build_list_of_ports([_head = {name, port} | []], state), do: state ++ [%{"name" => name, "port" => port}] - defp build_list_of_ports([_head = {name, port} | tail], state) do + defp build_list_of_ports([_head = {name, port} | tail], state) do build_list_of_ports tail, state ++ [%{"name" => name, "port" => port}] - end + end ################# ### Instances ### @@ -679,7 +675,7 @@ defmodule GCloudex.ComputeEngine.Impl do def list_instances(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @instance_ep <> "/#{zone}/instances", [], "", query + request :get, instance_ep() <> "/#{zone}/instances", [], "", query end @doc """ @@ -687,34 +683,34 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec get_instance(zone :: binary, instance :: binary, fields :: binary) :: HTTPResponse.t def get_instance(zone, instance, fields \\ "") do - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :get, - @instance_ep <> "/#{zone}/instances/#{instance}", - [], + :get, + instance_ep() <> "/#{zone}/instances/#{instance}", + [], "", query) end @doc """ Creates a new Virtual Machine instance on Google Compute Engine in the given - 'zone' and with properties defined in 'instance_resource'. Example of the + 'zone' and with properties defined in 'instance_resource'. Example of the minimal Instance Resource the API will accept: %{ - "disks" => + "disks" => [ %{ "autoDelete" => true, "boot" => true, - "initializeParams" => + "initializeParams" => %{ - "sourceImage" => + "sourceImage" => "projects/debian-cloud/global/images/debian-8-jessie-v20160119" }, "type" => "PERSISTENT" @@ -722,16 +718,16 @@ defmodule GCloudex.ComputeEngine.Impl do ], "machineType" => "zones/europe-west1-d/machineTypes/f1-micro", "name" => "example-instance", - "networkInterfaces" => + "networkInterfaces" => [ %{ - "accessConfigs" => + "accessConfigs" => [ %{ "name" => "External NAT", "type" => "ONE_TO_ONE_NAT" } - ], + ], "network" => "global/networks/default" } ] @@ -744,17 +740,17 @@ defmodule GCloudex.ComputeEngine.Impl do @spec insert_instance(zone :: binary, instance_resource :: Map.t, fields :: binary) :: HTTPResponse.t def insert_instance(zone, instance_resource, fields \\ "") do body = instance_resource |> Poison.encode! - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :post, - @instance_ep <> "/#{zone}/instances", - [{"Content-Type", "application/json"}], + :post, + instance_ep() <> "/#{zone}/instances", + [{"Content-Type", "application/json"}], body, query) end @@ -763,18 +759,18 @@ defmodule GCloudex.ComputeEngine.Impl do Deletes the given 'instance' if it exists in the given 'zone'. """ @spec delete_instance(zone :: binary, instance :: binary, fields :: binary) :: HTTPResponse.t - def delete_instance(zone, instance, fields \\ "") do - query = - if fields == "" do + def delete_instance(zone, instance, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :delete, - @instance_ep <> "/#{zone}/instances/#{instance}", - [], + :delete, + instance_ep() <> "/#{zone}/instances/#{instance}", + [], "", query) end @@ -783,18 +779,18 @@ defmodule GCloudex.ComputeEngine.Impl do Starts a stopped 'instance' if it exists in the given 'zone'. """ @spec start_instance(zone :: binary, instance :: binary, fields :: binary) :: HTTPResponse.t - def start_instance(zone, instance, fields \\ "") do - query = - if fields == "" do + def start_instance(zone, instance, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end - request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/start", - [{"Content-Type", "application/json"}], + request( + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/start", + [{"Content-Type", "application/json"}], "", query) end @@ -803,18 +799,18 @@ defmodule GCloudex.ComputeEngine.Impl do Stops a running 'instance' if it exists in the given 'zone'. """ @spec stop_instance(zone :: binary, instance :: binary, fields :: binary) :: HTTPResponse.t - def stop_instance(zone, instance, fields \\ "") do - query = - if fields == "" do + def stop_instance(zone, instance, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/stop", - [{"Content-Type", "application/json"}], + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/stop", + [{"Content-Type", "application/json"}], "", query) end @@ -823,20 +819,20 @@ defmodule GCloudex.ComputeEngine.Impl do Performs a hard reset on the 'instance' if it exists in the given 'zone'. """ @spec reset_instance(zone :: binary, instance :: binary, fields :: binary) :: HTTPResponse.t - def reset_instance(zone, instance, fields \\ "") do - query = - if fields == "" do + def reset_instance(zone, instance, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/reset", - [{"Content-Type", "application/json"}], + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/reset", + [{"Content-Type", "application/json"}], "", - query) + query) end @doc """ @@ -845,33 +841,33 @@ defmodule GCloudex.ComputeEngine.Impl do values. """ @spec add_access_config(zone :: binary, instance :: binary, network_interface :: binary, name :: binary, nat_ip :: binary, fields :: binary) :: HTTPResponse.t - def add_access_config(zone, instance, network_interface, name, nat_ip \\ "", fields \\ "") do - body = + def add_access_config(zone, instance, network_interface, name, nat_ip \\ "", fields \\ "") do + body = %{ "kind" => "compute#accessConfig", "type" => "ONE_TO_ONE_NAT", "name" => name - } + } - body = - if nat_ip != "" do + body = + if nat_ip != "" do body |> Map.put_new("natIP", nat_ip) else body end - query = - if fields == "" do + query = + if fields == "" do %{"networkInterface" => network_interface} |> URI.encode_query else - %{"networkInterface" => network_interface, "fields" => fields} + %{"networkInterface" => network_interface, "fields" => fields} |> URI.encode_query end request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/addAccessConfig", + instance_ep() <> "/#{zone}/instances/#{instance}/addAccessConfig", [{"Content-Type", "application/json"}], body |> Poison.encode!, query) @@ -882,30 +878,30 @@ defmodule GCloudex.ComputeEngine.Impl do 'instance' exists in the given 'zone'. """ @spec delete_access_config(zone :: binary, instance :: binary, access_config :: binary, network_interface :: binary, fields :: binary) :: HTTPResponse.t - def delete_access_config(zone, instance, access_config, network_interface, fields \\ "") do - query = - if fields == "" do + def delete_access_config(zone, instance, access_config, network_interface, fields \\ "") do + query = + if fields == "" do %{"accessConfig" => access_config, "networkInterface" => network_interface} |> URI.encode_query else %{ - "accessConfig" => access_config, + "accessConfig" => access_config, "networkInterface" => network_interface, "fields" => fields } |> URI.encode_query - end + end request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/deleteAccessConfig", + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/deleteAccessConfig", [{"Content-Type", "application/json"}], "", query) end @doc """ - Retrieves aggregated list of instances according to the specified + Retrieves aggregated list of instances according to the specified 'query_params' if present. """ @spec aggregated_list_of_instances(query_params :: Map.t) :: HTTPResponse.t @@ -913,8 +909,8 @@ defmodule GCloudex.ComputeEngine.Impl do query = query_params |> URI.encode_query request( - :get, - @no_zone_ep <> "/aggregated/instances", + :get, + no_zone_ep() <> "/aggregated/instances", [], "", query) @@ -949,16 +945,16 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec attach_disk(zone :: binary, instance :: binary, disk_resource :: map, fields :: binary) :: HTTPResponse.t def attach_disk(zone, instance, disk_resource, fields \\ "") do - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/attachDisk", + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/attachDisk", [{"Content-Type", "application/json"}], disk_resource |> Poison.encode!, query) @@ -970,34 +966,34 @@ defmodule GCloudex.ComputeEngine.Impl do """ @spec detach_disk(zone :: binary, instance :: binary, device_name :: binary, fields :: binary) :: HTTPResponse.t def detach_disk(zone, instance, device_name, fields \\ "") do - query = - if fields == "" do - %{"deviceName" => device_name} |> URI.encode_query + query = + if fields == "" do + %{"deviceName" => device_name} |> URI.encode_query else %{"deviceName" => device_name, "fields" => fields} |> URI.encode_query - end + end request( - :post, - @instance_ep <> "/#{zone}/instances/#{instance}/detachDisk", + :post, + instance_ep() <> "/#{zone}/instances/#{instance}/detachDisk", [{"Content-Type", "application/json"}], "", query) end @doc """ - Sets the 'auto_delete' flag for the disk with 'device_name' attached to + Sets the 'auto_delete' flag for the disk with 'device_name' attached to 'instance' if it exists in the given 'zone'. """ @spec set_disk_auto_delete(zone :: binary, instance :: binary, auto_delete :: boolean, device_name :: binary, fields :: binary) :: HTTPResponse.t - def set_disk_auto_delete(zone, instance, auto_delete, device_name, fields \\ "") do - query = - if fields == "" do + def set_disk_auto_delete(zone, instance, auto_delete, device_name, fields \\ "") do + query = + if fields == "" do %{"deviceName" => device_name, "autoDelete" => auto_delete} |> URI.encode_query else %{ - "deviceName" => device_name, + "deviceName" => device_name, "autoDelete" => auto_delete, "fields" => fields } @@ -1006,7 +1002,7 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/setDiskAutoDelete", + instance_ep() <> "/#{zone}/instances/#{instance}/setDiskAutoDelete", [{"Content-Type", "application/json"}], "", query) @@ -1014,41 +1010,41 @@ defmodule GCloudex.ComputeEngine.Impl do @doc """ Returns the specified 'instance' serial 'port' output if the 'instance' - exists in the given 'zone'. The 'port' accepted values are from 1 to 4, + exists in the given 'zone'. The 'port' accepted values are from 1 to 4, inclusive. """ @spec get_serial_port_output(zone :: binary, instance :: binary, port :: 1..4, fields :: binary) :: HTTPResponse.t - def get_serial_port_output(zone, instance, port \\ 1, fields \\ "") do - port = - if port == 1 do + def get_serial_port_output(zone, instance, port \\ 1, fields \\ "") do + port = + if port == 1 do port else port end - query = - if fields == "" do + query = + if fields == "" do %{"port" => port} |> URI.encode_query else %{"port" => port, "fields" => fields} |> URI.encode_query end - + request( - :get, - @instance_ep <> "/#{zone}/instances/#{instance}/serialPort", + :get, + instance_ep() <> "/#{zone}/instances/#{instance}/serialPort", [], "", query) end @doc """ - Changes the Machine Type for a stopped 'instance' to the specified + Changes the Machine Type for a stopped 'instance' to the specified 'machine_type' if the 'instance' exists in the given 'zone'. """ @spec set_machine_type(zone :: binary, instance :: binary, machine_type :: binary, fields :: binary) :: HTTPResponse.t - def set_machine_type(zone, instance, machine_type, fields \\ "") do + def set_machine_type(zone, instance, machine_type, fields \\ "") do body = %{"machineType" => machine_type} |> Poison.encode! - query = + query = if fields == "" do fields else @@ -1057,14 +1053,14 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/setMachineType", + instance_ep() <> "/#{zone}/instances/#{instance}/setMachineType", [{"Content-Type", "application/json"}], body, query) end @doc """ - Sets metadata for the specified 'instance' to the data specified in + Sets metadata for the specified 'instance' to the data specified in 'fingerprint' and 'items'. The 'instance' must exist in the given 'zone'. The 'items' should be passed as a list of maps: @@ -1080,22 +1076,22 @@ defmodule GCloudex.ComputeEngine.Impl do ] """ @spec set_metadata(zone :: binary, instance :: binary, fingerprint :: binary, items :: list(map), fields :: binary) :: HTTPResponse.t - def set_metadata(zone, instance, fingerprint, items, fields \\ "") do + def set_metadata(zone, instance, fingerprint, items, fields \\ "") do body = %{"kind" => "compute#metadata"} |> Map.put_new("fingerprint", fingerprint) |> Map.put_new("items", items) |> Poison.encode! - query = + query = if fields == "" do fields else %{"fields" => fields} - end + end request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/setMetadata", + instance_ep() <> "/#{zone}/instances/#{instance}/setMetadata", [{"Content-Type", "application/json"}], body, query) @@ -1106,15 +1102,15 @@ defmodule GCloudex.ComputeEngine.Impl do 'preemptible' option cannot be changed after instance creation. """ @spec set_scheduling(zone :: binary, instance :: binary, {on_host_maintenance :: binary, automatic_restart :: boolean, preemptible :: boolean}, fields :: binary) :: HTTPResponse.t - def set_scheduling(zone, instance, {on_host_maintenance, automatic_restart, preemptible}, fields \\ "") do + def set_scheduling(zone, instance, {on_host_maintenance, automatic_restart, preemptible}, fields \\ "") do body = %{ - "onHostMaintenance" => on_host_maintenance, + "onHostMaintenance" => on_host_maintenance, "automaticRestart" => automatic_restart, "preemptible" => preemptible } |> Poison.encode! - query = + query = if fields == "" do fields else @@ -1123,7 +1119,7 @@ defmodule GCloudex.ComputeEngine.Impl do request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/setScheduling", + instance_ep() <> "/#{zone}/instances/#{instance}/setScheduling", [{"Content-Type", "application/json"}], body, query) @@ -1134,22 +1130,22 @@ defmodule GCloudex.ComputeEngine.Impl do The 'instance' must exist in the given 'zone'. """ @spec set_tags(zone :: binary, instance :: binary, fingerprint :: binary, items :: list(binary), fields :: binary) :: HTTPResponse.t - def set_tags(zone, instance, fingerprint, items, fields \\ "") do + def set_tags(zone, instance, fingerprint, items, fields \\ "") do body = %{"fingerprint" => fingerprint, "items" => items} |> Poison.encode! - query = + query = if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end + end request( :post, - @instance_ep <> "/#{zone}/instances/#{instance}/setTags", + instance_ep() <> "/#{zone}/instances/#{instance}/setTags", [{"Content-Type", "application/json"}], body, query) - end + end ################ ### Licenses ### @@ -1162,68 +1158,68 @@ defmodule GCloudex.ComputeEngine.Impl do def get_license(license, fields \\ "") do query = fields_binary_to_map fields - request :get, @no_zone_ep <> "/global/licenses/#{license}", [], "", query - end + request :get, no_zone_ep() <> "/global/licenses/#{license}", [], "", query + end ##################### ### Machine Types ### ##################### @doc """ - Retrieves a list of machine types available in the specified 'zone' and + Retrieves a list of machine types available in the specified 'zone' and that fit in the given 'query_params' if present. """ @spec list_machine_types(zone :: binary, query_params :: Map.t) :: HTTPResponse.t - def list_machine_types(zone, query_params \\ %{}) do + def list_machine_types(zone, query_params \\ %{}) do query = query_params |> URI.encode_query - request(:get, @instance_ep <> "/#{zone}/machineTypes", [], "", query) + request(:get, instance_ep() <> "/#{zone}/machineTypes", [], "", query) end @doc """ Returns the specified 'machine_type' in the given 'zone'. """ @spec get_machine_type(zone :: binary, machine_type :: binary, fields :: binary) :: HTTPResponse.t - def get_machine_type(zone, machine_type, fields \\ "") do - query = - if fields == "" do + def get_machine_type(zone, machine_type, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end request( - :get, - @instance_ep <> "/#{zone}/machineTypes/#{machine_type}", - [], - "", + :get, + instance_ep() <> "/#{zone}/machineTypes/#{machine_type}", + [], + "", query) end @doc """ - Returns an aggragated list of machine types following the specified + Returns an aggragated list of machine types following the specified 'query_params' if present. """ @spec aggregated_list_of_machine_types(query_params :: Map.t) :: HTTPResponse.t - def aggregated_list_of_machine_types(query_params \\ %{}) do + def aggregated_list_of_machine_types(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/aggregated/machineTypes", [], "", query - end + request :get, no_zone_ep() <> "/aggregated/machineTypes", [], "", query + end ################ ### Networks ### ################ @doc """ - Retrieves the list of networks available according to the given + Retrieves the list of networks available according to the given 'query_params' if provided. """ @spec list_networks(query_params :: Map.t) :: HTTPResponse.t def list_networks(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/global/networks", [], "", query + request :get, no_zone_ep() <> "/global/networks", [], "", query end @doc """ @@ -1233,7 +1229,7 @@ defmodule GCloudex.ComputeEngine.Impl do def get_network(network, fields \\ "") do query = fields_binary_to_map fields - request :get, @no_zone_ep <> "/global/networks/#{network}", [], "", query + request :get, no_zone_ep() <> "/global/networks/#{network}", [], "", query end @doc """ @@ -1247,13 +1243,13 @@ defmodule GCloudex.ComputeEngine.Impl do body = network_resource |> Poison.encode! request( - :post, - @no_zone_ep <> "/global/networks", - [{"Content-Type", "application/json"}], - body, + :post, + no_zone_ep() <> "/global/networks", + [{"Content-Type", "application/json"}], + body, query) end - + @doc """ Deletes the specified 'network'. """ @@ -1263,11 +1259,11 @@ defmodule GCloudex.ComputeEngine.Impl do request( :delete, - @no_zone_ep <> "/global/networks/#{network}", - [], + no_zone_ep() <> "/global/networks/#{network}", + [], "", query) - end + end ############### ### Regions ### @@ -1280,23 +1276,23 @@ defmodule GCloudex.ComputeEngine.Impl do def list_regions(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/regions", [], "", query - end + request :get, no_zone_ep() <> "/regions", [], "", query + end @doc """ Returns the specified 'region' resource. """ @spec get_region(region :: binary, fields :: binary) :: HTTPResponse.t - def get_region(region, fields \\ "") do - query = - if fields == "" do + def get_region(region, fields \\ "") do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query end - request :get, @no_zone_ep <> "/regions/#{region}", [], "", query - end + request :get, no_zone_ep() <> "/regions/#{region}", [], "", query + end ############# ### Zones ### @@ -1307,39 +1303,45 @@ defmodule GCloudex.ComputeEngine.Impl do 'query_params'. """ @spec list_zones(query_params :: Map.t) :: HTTPResponse.t - def list_zones(query_params \\ %{}) do + def list_zones(query_params \\ %{}) do query = query_params |> URI.encode_query - request :get, @no_zone_ep <> "/zones", [], "", query - end + request :get, no_zone_ep() <> "/zones", [], "", query + end @doc """ - Returns the specified 'zone' resource. + Returns the specified 'zone' resource. """ @spec get_zone(zone :: binary, fields :: binary) :: HTTPResponse.t def get_zone(zone, fields \\ "") do - query = - if fields == "" do + query = + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end - - request :get, @no_zone_ep <> "/zones/#{zone}", [], "", query + end + + request :get, no_zone_ep() <> "/zones/#{zone}", [], "", query end ############### ### Helpers ### ############### - defp fields_binary_to_map(fields) do - if fields == "" do + defp fields_binary_to_map(fields) do + if fields == "" do fields else %{"fields" => fields} |> URI.encode_query - end - end + end + end + + defp project_id, do: GCloudex.get_project_id() + + defp no_zone_ep, do: "https://www.googleapis.com/compute/beta/projects/#{project_id()}" + + defp instance_ep, do: "https://www.googleapis.com/compute/beta/projects/#{project_id()}/zones" + end end - end diff --git a/lib/compute_engine/request.ex b/lib/compute_engine/request.ex index aee667c..ca2b718 100644 --- a/lib/compute_engine/request.ex +++ b/lib/compute_engine/request.ex @@ -2,37 +2,38 @@ defmodule GCloudex.ComputeEngine.Request do alias GCloudex.Auth, as: Auth @moduledoc """ - + """ - defmacro __using__(_opts) do - quote do + defmacro __using__(_opts) do + quote do require Logger - @project_id GCloudex.get_project_id @doc """ - + """ @spec request(verb :: atom, endpoint :: binary, headers :: [{binary, binary}], body :: binary, parameters :: binary) :: HTTPResponse.t def request(verb, endpoint, headers \\ [], body \\ "", parameters \\ "") do - endpoint = - if parameters == "" do + endpoint = + if parameters == "" do endpoint else endpoint <> "/" <> "?" <> parameters - end + end HTTPoison.request( verb, endpoint, body, - headers ++ [{"x-goog-project-id", @project_id}, + headers ++ [{"x-goog-project-id", project_id()}, {"Authorization", "Bearer #{Auth.get_token_storage(:compute)}"}], [] ) - end + end defoverridable [request: 4, request: 5] + + defp project_id, do: GCloudex.get_project_id() end end end diff --git a/lib/gcloudex.ex b/lib/gcloudex.ex index d045730..e804940 100644 --- a/lib/gcloudex.ex +++ b/lib/gcloudex.ex @@ -9,9 +9,7 @@ defmodule GCloudex do """ @spec get_project_id :: binary def get_project_id do - :goth - |> Application.get_env(:json) - |> Poison.decode! - |> Map.get("project_id") + {:ok, <<_,_::binary>> = project_id} = Goth.Config.get(:project_id) + project_id end -end \ No newline at end of file +end diff --git a/mix.exs b/mix.exs index afcce17..13b7795 100644 --- a/mix.exs +++ b/mix.exs @@ -44,7 +44,7 @@ defmodule GCloudex.Mixfile do ] end - defp package do + defp package do [ maintainers: ["Sasha Fonseca"], licenses: ["Apache 2.0"], diff --git a/test/cloud_speech/cloud_speech_test.exs b/test/cloud_speech/cloud_speech_test.exs index 91de2aa..bc1b1dd 100644 --- a/test/cloud_speech/cloud_speech_test.exs +++ b/test/cloud_speech/cloud_speech_test.exs @@ -3,7 +3,6 @@ defmodule Test.Dummy.CloudSpeech do use GCloudex.CloudSpeech.Impl, :cloud_speech @endpoint "speech.googleapis.com" - @project_id GCloudex.get_project_id def request(verb, path, body, headers \\ []) do %{ @@ -14,7 +13,7 @@ defmodule Test.Dummy.CloudSpeech do headers ++ [ {"Authorization", "Bearer Dummy Token"}, - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, ], opts: [] } @@ -26,7 +25,7 @@ defmodule CloudSpeechTest do alias Test.Dummy.CloudSpeech, as: API @endpoint "speech.googleapis.com" - @project_id GCloudex.get_project_id + GCloudex.get_project_id() GCloudex.get_project_id ######################### ### POST Speech Tests ### @@ -73,7 +72,7 @@ defmodule CloudSpeechTest do headers: headers ++ [{"Authorization", "Bearer Dummy Token"}, - {"x-goog-project-id", @project_id}], + {"x-goog-project-id", GCloudex.get_project_id()}], body: body, opts: [] } diff --git a/test/cloud_sql/cloud_sql_test.exs b/test/cloud_sql/cloud_sql_test.exs index f8f7f11..a80e4fe 100644 --- a/test/cloud_sql/cloud_sql_test.exs +++ b/test/cloud_sql/cloud_sql_test.exs @@ -2,68 +2,64 @@ defmodule Test.Dummy.CloudSQL do use GCloudex.CloudSQL.Impl, :cloud_sql require Logger - @project_id GCloudex.get_project_id - - def request(verb, endpoint, headers \\ [], body \\ "") do + def request(verb, endpoint, headers \\ [], body \\ "") do %{ verb: verb, host: endpoint, body: body, headers: - headers ++ + headers ++ [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} ], opts: [] } end - def request_query(verb, endpoint, headers, body, parameters) do + def request_query(verb, endpoint, headers, body, parameters) do %{ verb: verb, host: endpoint <> "/" <> parameters, body: body, headers: - headers ++ + headers ++ [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} ], opts: [] - } - end + } + end end -defmodule CloudSQLTest do +defmodule CloudSQLTest do use ExUnit.Case, async: true alias Test.Dummy.CloudSQL, as: API - - @project_id GCloudex.get_project_id - @instance_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/instances" - @flag_ep "https://www.googleapis.com/sql/v1beta4/flags" - @operation_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/operations" - @tiers_ep "https://www.googleapis.com/sql/v1beta4/projects/#{@project_id}/tiers" + def instance_ep(), do: "https://www.googleapis.com/sql/v1beta4/projects/#{GCloudex.get_project_id()}/instances" + def flag_ep(), do: "https://www.googleapis.com/sql/v1beta4/flags" + def operation_ep(), do: "https://www.googleapis.com/sql/v1beta4/projects/#{GCloudex.get_project_id()}/operations" + def tiers_ep(), do: "https://www.googleapis.com/sql/v1beta4/projects/#{GCloudex.get_project_id()}/tiers" ####################### ### Instances Tests ### ####################### - test "list_instances" do - expected = build_expected(:get, @instance_ep, [], "") + test "list_instances" do + expected = build_expected(:get, instance_ep(), [], "") assert expected == API.list_instances - end + end - test "get_instance" do + test "get_instance" do instance = "instance" - expected = build_expected(:get, @instance_ep, [], "", instance) + expected = build_expected(:get, instance_ep(), [], "", instance) assert expected == API.get_instance instance end - test "insert_instance (no opts)" do + test "insert_instance (no opts)" do name = "name" opts = %{} settings = %{settings_1: "abc", settings_2: "def"} @@ -74,12 +70,12 @@ defmodule CloudSQLTest do settings: Map.put(settings, :tier, tier) } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body) + expected = build_expected(:post, instance_ep(), headers, body) assert expected == API.insert_instance name, opts, settings, tier end - test "insert_instance (with opts)" do + test "insert_instance (with opts)" do name = "name" opts = %{first: "first", second: "second"} settings = %{settings_1: "abc", settings_2: "def"} @@ -92,21 +88,21 @@ defmodule CloudSQLTest do second: "second" } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body) + expected = build_expected(:post, instance_ep(), headers, body) assert expected == API.insert_instance name, opts, settings, tier - end + end - test "delete_instance" do + test "delete_instance" do instance = "instance" headers = [] body = "" - expected = build_expected(:delete, @instance_ep, headers, body, instance) + expected = build_expected(:delete, instance_ep(), headers, body, instance) assert expected == API.delete_instance instance end - test "clone_instance" do + test "clone_instance" do instance = "instance" dest_name = "dest_name" bin_log_file = "bin_log_file" @@ -123,44 +119,44 @@ defmodule CloudSQLTest do "kind" => "sql#cloneContext" } } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/clone") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/clone") assert expected == API.clone_instance instance, dest_name, bin_log_file, bin_log_pos end - test "restart_instance" do + test "restart_instance" do instance = "instance" headers = [{"Content-Type", "application/json"}] - expected = build_expected(:post, @instance_ep, headers, "", "#{instance}/restart") + expected = build_expected(:post, instance_ep(), headers, "", "#{instance}/restart") assert expected == API.restart_instance instance end - test "start_replica" do + test "start_replica" do instance = "instance" headers = [{"Content-Type", "application/json"}] - expected = build_expected(:post, @instance_ep, headers, "", "#{instance}/startReplica") + expected = build_expected(:post, instance_ep(), headers, "", "#{instance}/startReplica") - assert expected == API.start_replica instance + assert expected == API.start_replica instance end - test "stop_replica" do + test "stop_replica" do instance = "instance" headers = [{"Content-Type", "application/json"}] - expected = build_expected(:post, @instance_ep, headers, "", "#{instance}/stopReplica") + expected = build_expected(:post, instance_ep(), headers, "", "#{instance}/stopReplica") assert expected == API.stop_replica instance end - test "promote_replica" do + test "promote_replica" do instance = "instance" headers = [{"Content-Type", "application/json"}] - expected = build_expected(:post, @instance_ep, headers, "", "#{instance}/promoteReplica") + expected = build_expected(:post, instance_ep(), headers, "", "#{instance}/promoteReplica") assert expected == API.promote_replica instance end - test "failover_instance" do + test "failover_instance" do instance = "instance" settings_version = 123 headers = [{"Content-Type", "application/json"}] @@ -170,15 +166,15 @@ defmodule CloudSQLTest do "settingsVersion" => settings_version } } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/failover") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/failover") assert expected == API.failover_instance instance, settings_version end - test "reset_ssl_config" do + test "reset_ssl_config" do instance = "instance" headers = [{"Content-Type", "application/json"}] - expected = build_expected(:post, @instance_ep, headers, "", "#{instance}/resetSslConfig") + expected = build_expected(:post, instance_ep(), headers, "", "#{instance}/resetSslConfig") assert expected == API.reset_ssl_config instance end @@ -187,66 +183,66 @@ defmodule CloudSQLTest do ### Databases Tests ### ####################### - test "list_databases" do + test "list_databases" do instance = "instance" headers = [] - expected = build_expected(:get, @instance_ep, headers, "", "#{instance}/databases") + expected = build_expected(:get, instance_ep(), headers, "", "#{instance}/databases") assert expected == API.list_databases instance end - test "insert_database" do + test "insert_database" do instance = "instance" name = "name" headers = [{"Content-Type", "application/json"}] body = %{ "instance" => instance, "name" => name, - "project" => @project_id + "project" => GCloudex.get_project_id() } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/databases") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/databases") assert expected == API.insert_database instance, name end - test "get_database" do + test "get_database" do instance = "instance" database = "database" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/databases/#{database}") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/databases/#{database}") assert expected == API.get_database instance, database end - test "delete_database" do + test "delete_database" do instance = "instance" database = "database" headers = [] body = "" - expected = build_expected(:delete, @instance_ep, headers, body, "#{instance}/databases/#{database}") + expected = build_expected(:delete, instance_ep(), headers, body, "#{instance}/databases/#{database}") assert expected == API.delete_database instance, database end - test "patch_database" do + test "patch_database" do instance = "instance" database = "database" patch_map = %{"charset" => "abc", "collation" => "def"} headers = [{"Content-Type", "application/json"}] body = patch_map |> Poison.encode! - expected = build_expected(:patch, @instance_ep, headers, body, "#{instance}/databases/#{database}") + expected = build_expected(:patch, instance_ep(), headers, body, "#{instance}/databases/#{database}") assert expected == API.patch_database instance, database, patch_map end - test "update_database" do + test "update_database" do instance = "instance" database = "database" update_map = %{"field_1" => "abc", "field_2" => "def"} headers = [{"Content-Type", "application/json"}] body = update_map |> Poison.encode! - expected = build_expected(:put, @instance_ep, headers, body, "#{instance}/databases/#{database}") + expected = build_expected(:put, instance_ep(), headers, body, "#{instance}/databases/#{database}") assert expected == API.update_database instance, database, update_map end @@ -255,10 +251,10 @@ defmodule CloudSQLTest do ### Flags Tests ### ################### - test "list_flags" do + test "list_flags" do headers = [] body = "" - expected = build_expected(:get, @flag_ep, headers, body) + expected = build_expected(:get, flag_ep(), headers, body) assert expected == API.list_flags end @@ -267,21 +263,21 @@ defmodule CloudSQLTest do ### Operations Tests ### ######################## - test "list_operations" do + test "list_operations" do instance = "instance" headers = [] body = "" query = "?instance=#{instance}" - expected = build_expected(:get, @operation_ep <> query, headers, body) + expected = build_expected(:get, operation_ep() <> query, headers, body) assert expected == API.list_operations instance end test "get_operation" do - operation_id = "operation_id" + operation_id = "operation_id" headers = [] body = "" - expected = build_expected(:get, @operation_ep, headers, body, operation_id) + expected = build_expected(:get, operation_ep(), headers, body, operation_id) assert expected == API.get_operation operation_id end @@ -290,10 +286,10 @@ defmodule CloudSQLTest do ### Tiers Tests ### ################### - test "list_tiers" do + test "list_tiers" do headers = [] body = "" - expected = build_expected(:get, @tiers_ep, headers, body) + expected = build_expected(:get, tiers_ep(), headers, body) assert expected == API.list_tiers end @@ -302,16 +298,16 @@ defmodule CloudSQLTest do ### Users Tests ### ################### - test "list_users" do + test "list_users" do instance = "instance" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/users") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/users") assert expected == API.list_users instance end - test "insert_user without host" do + test "insert_user without host" do instance = "instance" name = "name" password = "password" @@ -321,14 +317,14 @@ defmodule CloudSQLTest do "password" => password, "instance" => instance, "host" => "%", - "project" => @project_id + "project" => GCloudex.get_project_id() } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/users") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/users") assert expected == API.insert_user instance, name, password end - test "insert_user with host" do + test "insert_user with host" do instance = "instance" name = "name" password = "password" @@ -339,12 +335,12 @@ defmodule CloudSQLTest do "password" => password, "instance" => instance, "host" => host, - "project" => @project_id + "project" => GCloudex.get_project_id() } |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/users") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/users") assert expected == API.insert_user instance, name, password, host - end + end test "update_user" do instance = "instance" @@ -354,19 +350,19 @@ defmodule CloudSQLTest do query = "?host=#{host}&name=#{name}" headers = [{"Content-Type", "application/json"}] body = %{"password" => password} |> Poison.encode! - expected = build_expected(:put, @instance_ep, headers, body, "#{instance}/users#{query}") + expected = build_expected(:put, instance_ep(), headers, body, "#{instance}/users#{query}") assert expected == API.update_user instance, host, name, password end - test "delete_user" do + test "delete_user" do instance = "instance" host = "host" name = "name" headers = [] query = "?host=#{host}&name=#{name}" body = "" - expected = build_expected(:delete, @instance_ep, headers, body, "#{instance}/users#{query}") + expected = build_expected(:delete, instance_ep(), headers, body, "#{instance}/users#{query}") assert expected == API.delete_user instance, host, name end @@ -375,64 +371,64 @@ defmodule CloudSQLTest do ### Backup Runs Tests ### ######################### - test "list_backup_runs" do + test "list_backup_runs" do instance = "instance" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/backupRuns") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/backupRuns") assert expected == API.list_backup_runs instance end - test "get_backup_run" do + test "get_backup_run" do instance = "instance" run_id = "run_id" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/backupRuns/#{run_id}") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/backupRuns/#{run_id}") assert expected == API.get_backup_run instance, run_id end - test "delete_backup_run" do + test "delete_backup_run" do instance = "instance" run_id = "run_id" headers = [] body = "" - expected = build_expected(:delete, @instance_ep, headers, body, "#{instance}/backupRuns/#{run_id}") + expected = build_expected(:delete, instance_ep(), headers, body, "#{instance}/backupRuns/#{run_id}") - assert expected == API.delete_backup_run instance, run_id + assert expected == API.delete_backup_run instance, run_id end ######################## ### SSL Certificates ### ######################## - test "list_ssl_certs" do + test "list_ssl_certs" do instance = "instance" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/sslCerts") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/sslCerts") assert expected == API.list_ssl_certs instance end - test "get_ssl_cert" do + test "get_ssl_cert" do instance = "instance" fingerprint = "fingerprint" headers = [] body = "" - expected = build_expected(:get, @instance_ep, headers, body, "#{instance}/sslCerts/#{fingerprint}") + expected = build_expected(:get, instance_ep(), headers, body, "#{instance}/sslCerts/#{fingerprint}") assert expected == API.get_ssl_cert instance, fingerprint end - test "insert_ssl_cert" do + test "insert_ssl_cert" do instance = "instance" common_name = "common_name" headers = [{"Content-Type", "application/json"}] body = %{"commonName" => common_name} |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/sslCerts") + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/sslCerts") assert expected == API.insert_ssl_cert instance, common_name end @@ -442,17 +438,17 @@ defmodule CloudSQLTest do fingerprint = "fingerprint" headers = [] body = "" - expected = build_expected(:delete, @instance_ep, headers, body, "#{instance}/sslCerts/#{fingerprint}") + expected = build_expected(:delete, instance_ep(), headers, body, "#{instance}/sslCerts/#{fingerprint}") assert expected == API.delete_ssl_cert instance, fingerprint end - test "create_ephemeral_ssl_cert" do + test "create_ephemeral_ssl_cert" do instance = "instance" public_key = "public_key" headers = [{"Content-Type", "application/json"}] - body = %{"public_key" => public_key} |> Poison.encode! - expected = build_expected(:post, @instance_ep, headers, body, "#{instance}/createEphemeral") + body = %{"public_key" => public_key} |> Poison.encode! + expected = build_expected(:post, instance_ep(), headers, body, "#{instance}/createEphemeral") assert expected == API.create_ephemeral_ssl_cert instance, public_key end @@ -464,20 +460,20 @@ defmodule CloudSQLTest do map = %{ verb: verb, host: endpoint, - headers: + headers: headers ++ [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} - ], + ], body: body, opts: [] } - if parameters != :empty do + if parameters != :empty do Map.put(map, :host, endpoint <> "/" <> parameters) else map end end -end \ No newline at end of file +end diff --git a/test/cloud_storage/cloud_storage_test.exs b/test/cloud_storage/cloud_storage_test.exs index 137b0f8..5999636 100644 --- a/test/cloud_storage/cloud_storage_test.exs +++ b/test/cloud_storage/cloud_storage_test.exs @@ -1,18 +1,17 @@ -defmodule Test.Dummy.CloudStorage do +defmodule Test.Dummy.CloudStorage do use ExUnit.Case use GCloudex.CloudStorage.Impl, :cloud_storage @endpoint "storage.googleapis.com" - @project_id GCloudex.get_project_id def request_service do %{ verb: :get, host: @endpoint, body: "", - headers: + headers: [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} ], opts: [] @@ -24,12 +23,12 @@ defmodule Test.Dummy.CloudStorage do verb: verb, host: bucket <> "." <> @endpoint, body: body, - headers: - headers ++ + headers: + headers ++ [ {"Authorization", "Bearer Dummy Token"} ], - opts: [] + opts: [] } end @@ -38,13 +37,13 @@ defmodule Test.Dummy.CloudStorage do verb: verb, host: bucket <> "." <> @endpoint <> "/" <> parameters, body: body, - headers: - headers ++ + headers: + headers ++ [ {"Authorization", "Bearer Dummy Token"} ], - opts: [] - } + opts: [] + } end end @@ -53,14 +52,13 @@ defmodule CloudStorageTest do alias Test.Dummy.CloudStorage, as: API @endpoint "storage.googleapis.com" - @project_id GCloudex.get_project_id ######################### ### GET Service Tests ### ######################### - test "list_buckets" do - expected = build_expected(:get, @endpoint, [{"x-goog-project-id", @project_id}], "") + test "list_buckets" do + expected = build_expected(:get, @endpoint, [{"x-goog-project-id", GCloudex.get_project_id()}], "") assert expected == API.list_buckets end @@ -69,7 +67,7 @@ defmodule CloudStorageTest do ### DELETE Bucket Tests ### ########################### - test "delete_bucket" do + test "delete_bucket" do expected = build_expected(:delete, "bucket.#{@endpoint}", [], "") assert expected == API.delete_bucket "bucket" @@ -85,7 +83,7 @@ defmodule CloudStorageTest do assert expected == API.list_objects "bucket" end - test "list_objects with query from non-empty list" do + test "list_objects with query from non-empty list" do expected = build_expected( :get, "bucket.#{@endpoint}/?key1=abc&key2=def", @@ -96,7 +94,7 @@ defmodule CloudStorageTest do assert expected == API.list_objects "bucket", [{"key1", "abc"}, {"key2", "def"}] end - test "list list_objects with query from empty list" do + test "list list_objects with query from empty list" do expected = build_expected( :get, "bucket.#{@endpoint}/?", @@ -107,75 +105,75 @@ defmodule CloudStorageTest do assert expected == API.list_objects "bucket", [] end - test "get_bucket_acl" do + test "get_bucket_acl" do expected = build_expected(:get, "bucket.#{@endpoint}/?acl", [], "") assert expected == API.get_bucket_acl "bucket" end - test "get_bucket_cors" do + test "get_bucket_cors" do expected = build_expected(:get, "bucket.#{@endpoint}/?cors", [], "") - assert expected == API.get_bucket_cors "bucket" + assert expected == API.get_bucket_cors "bucket" end - test "get_bucket_lifecycle" do + test "get_bucket_lifecycle" do expected = build_expected(:get, "bucket.#{@endpoint}/?lifecycle", [], "") - assert expected == API.get_bucket_lifecycle "bucket" + assert expected == API.get_bucket_lifecycle "bucket" end - test "get_bucket_region" do + test "get_bucket_region" do expected = build_expected(:get, "bucket.#{@endpoint}/?location", [], "") - assert expected == API.get_bucket_region "bucket" - end + assert expected == API.get_bucket_region "bucket" + end - test "get_bucket_logging" do + test "get_bucket_logging" do expected = build_expected(:get, "bucket.#{@endpoint}/?logging", [], "") - assert expected == API.get_bucket_logging "bucket" - end + assert expected == API.get_bucket_logging "bucket" + end - test "get_bucket_class" do + test "get_bucket_class" do expected = build_expected(:get, "bucket.#{@endpoint}/?storageClass", [], "") - assert expected == API.get_bucket_class "bucket" - end + assert expected == API.get_bucket_class "bucket" + end - test "get_bucket_versioning" do + test "get_bucket_versioning" do expected = build_expected(:get, "bucket.#{@endpoint}/?versioning", [], "") - assert expected == API.get_bucket_versioning "bucket" - end + assert expected == API.get_bucket_versioning "bucket" + end - test "get_bucket_website" do + test "get_bucket_website" do expected = build_expected(:get, "bucket.#{@endpoint}/?website", [], "") - assert expected == API.get_bucket_website "bucket" - end + assert expected == API.get_bucket_website "bucket" + end ######################### ### HEAD Bucket Tests ### ######################### - test "exists_bucket" do + test "exists_bucket" do expected = build_expected(:head, "bucket.#{@endpoint}", [], "") assert expected == API.exists_bucket "bucket" - end + end ######################## ### PUT Bucket Tests ### ######################## - test "create_bucket/1 (default class and region)" do - expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", @project_id}], "") + test "create_bucket/1 (default class and region)" do + expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", GCloudex.get_project_id()}], "") assert expected == API.create_bucket "bucket" end - test "create_bucket/2 (default class and custom region)" do + test "create_bucket/2 (default class and custom region)" do region = "region" body = """ @@ -183,12 +181,12 @@ defmodule CloudStorageTest do #{region} """ - expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", @project_id}], body) + expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", GCloudex.get_project_id()}], body) - assert expected == API.create_bucket "bucket", region + assert expected == API.create_bucket "bucket", region end - test "create_bucket/3 (custom class and custom region)" do + test "create_bucket/3 (custom class and custom region)" do region = "region" class = "class" body = @@ -198,71 +196,71 @@ defmodule CloudStorageTest do #{class} """ - expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", @project_id}], body) + expected = build_expected(:put, "bucket.#{@endpoint}", [{"x-goog-project-id", GCloudex.get_project_id()}], body) - assert expected == API.create_bucket "bucket", region, class - end + assert expected == API.create_bucket "bucket", region, class + end - test "set_bucket_acl" do + test "set_bucket_acl" do expected = build_expected(:put, "bucket.#{@endpoint}/?acl", [], "acl_config") assert expected == API.set_bucket_acl "bucket", "acl_config" end - test "set_bucket_cors" do + test "set_bucket_cors" do expected = build_expected(:put, "bucket.#{@endpoint}/?cors", [], "cors_config") - assert expected == API.set_bucket_cors "bucket", "cors_config" + assert expected == API.set_bucket_cors "bucket", "cors_config" end - test "set_bucket_lifecycle" do + test "set_bucket_lifecycle" do expected = build_expected(:put, "bucket.#{@endpoint}/?lifecycle", [], "lifecycle_config") - assert expected == API.set_bucket_lifecycle "bucket", "lifecycle_config" + assert expected == API.set_bucket_lifecycle "bucket", "lifecycle_config" end - test "set_bucket_logging" do + test "set_bucket_logging" do expected = build_expected(:put, "bucket.#{@endpoint}/?logging", [], "logging_config") - assert expected == API.set_bucket_logging "bucket", "logging_config" - end + assert expected == API.set_bucket_logging "bucket", "logging_config" + end - test "set_bucket_versioning" do + test "set_bucket_versioning" do expected = build_expected(:put, "bucket.#{@endpoint}/?versioning", [], "versioning_config") - assert expected == API.set_bucket_versioning "bucket", "versioning_config" - end + assert expected == API.set_bucket_versioning "bucket", "versioning_config" + end - test "set_bucket_website" do + test "set_bucket_website" do expected = build_expected(:put, "bucket.#{@endpoint}/?websiteConfig", [], "website_config") - assert expected == API.set_bucket_website "bucket", "website_config" - end + assert expected == API.set_bucket_website "bucket", "website_config" + end ##################### ### DELETE Object ### ##################### - test "delete_object/2 (no query)" do + test "delete_object/2 (no query)" do object = "object" expected = build_expected(:delete, "bucket.#{@endpoint}/#{object}", [], "") assert expected == API.delete_object "bucket", object end - test "delete_object/3 (with query)" do + test "delete_object/3 (with query)" do query = "key1=abc&key2=def" object = "object" expected = build_expected(:delete, "bucket.#{@endpoint}/#{object}?#{query}", [], "") assert expected == API.delete_object "bucket", object, [{"key1", "abc"}, {"key2", "def"}] - end + end ################## ### GET Object ### ################## - test "get_object/2 (no query)" do + test "get_object/2 (no query)" do object = "object" bucket = "bucket" expected = build_expected(:get, "#{bucket}.#{@endpoint}/#{object}", [], "") @@ -270,16 +268,16 @@ defmodule CloudStorageTest do assert expected == API.get_object bucket, object end - test "get_object/3 (with query)" do + test "get_object/3 (with query)" do object = "object" bucket = "bucket" query = "key1=abc&key2=def" expected = build_expected(:get, "#{bucket}.#{@endpoint}/#{object}?#{query}", [], "") assert expected == API.get_object bucket, object, [{"key1", "abc"}, {"key2", "def"}] - end + end - test "get_object_acl" do + test "get_object_acl" do bucket = "bucket" object = "object" expected = build_expected(:get, "#{bucket}.#{@endpoint}/#{object}?acl", [], "") @@ -291,7 +289,7 @@ defmodule CloudStorageTest do ### HEAD Object ### ################### - test "get_object_metadata/2 (no query)" do + test "get_object_metadata/2 (no query)" do bucket = "bucket" object = "object" expected = build_expected(:head, "#{bucket}.#{@endpoint}/#{object}", [], "") @@ -299,21 +297,21 @@ defmodule CloudStorageTest do assert expected == API.get_object_metadata bucket, object end - test "get_object_metadata/3 (with query)" do + test "get_object_metadata/3 (with query)" do bucket = "bucket" object = "object" query = "key1=abc&key2=def" expected = build_expected(:head, "#{bucket}.#{@endpoint}/#{object}?#{query}", [], "") assert expected == API.get_object_metadata bucket, object, [{"key1", "abc"}, {"key2", "def"}] - end + end ################## ### PUT Object ### ################## test "put_object/2 (no path for the file)" do - filepath = __DIR__ <> "README.md" + filepath = __DIR__ <> "README.md" body = {:file, filepath} bucket = "bucket" expected = build_expected(:put, "#{bucket}.#{@endpoint}/#{filepath}", [], body) @@ -322,21 +320,21 @@ defmodule CloudStorageTest do end test "put_object/3 (with path for the file)" do - filepath = __DIR__ <> "README.md" + filepath = __DIR__ <> "README.md" bucketpath = "folder_1/folder_2" body = {:file, filepath} bucket = "bucket" expected = build_expected(:put, "#{bucket}.#{@endpoint}/#{bucketpath}", [], body) assert expected == API.put_object bucket, filepath, bucketpath - end + end - test "copy_object" do + test "copy_object" do new_bucket = "new_bucket" new_object = "new_object" source_object = "source_object" expected = build_expected( - :put, + :put, "#{new_bucket}.#{@endpoint}/#{new_object}", [{"x-goog-copy-source", source_object}], "" @@ -345,7 +343,7 @@ defmodule CloudStorageTest do assert expected == API.copy_object new_bucket, new_object, source_object end - test "set_object_acl/3 (no query)" do + test "set_object_acl/3 (no query)" do bucket = "bucket" object = "object" acl_config = "acl_config" @@ -359,7 +357,7 @@ defmodule CloudStorageTest do assert expected == API.set_object_acl bucket, object, acl_config end - test "set_object_acl/4 (with query)" do + test "set_object_acl/4 (with query)" do bucket = "bucket" object = "object" acl_config = "acl_config" @@ -372,7 +370,7 @@ defmodule CloudStorageTest do ) assert expected == API.set_object_acl bucket, object, acl_config, [{"key1", "abc"}, {"key2", "def"}] - end + end ############### ### Helpers ### @@ -380,20 +378,20 @@ defmodule CloudStorageTest do defp build_expected(verb, host, headers, body, parameters \\ :empty) do map = %{ - verb: verb, - host: host, - headers: - headers ++ + verb: verb, + host: host, + headers: + headers ++ [{"Authorization", "Bearer Dummy Token"}], body: body, opts: [] } - - if parameters != :empty do + + if parameters != :empty do Map.put(map, host, host <> "/" <> parameters) else map end end -end \ No newline at end of file +end diff --git a/test/compute_engine/compute_engine_test.exs b/test/compute_engine/compute_engine_test.exs index 0e855a8..d69746c 100644 --- a/test/compute_engine/compute_engine_test.exs +++ b/test/compute_engine/compute_engine_test.exs @@ -1,17 +1,15 @@ defmodule Test.Dummy.ComputeEngine do use GCloudex.ComputeEngine.Impl, :compute_engine - - @project_id GCloudex.get_project_id - def request(verb, endpoint, headers, body, query \\ "") do + def request(verb, endpoint, headers, body, query \\ "") do %{ verb: verb, endpoint: endpoint, body: body, - headers: - headers ++ + headers: + headers ++ [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} ], query: query @@ -19,102 +17,101 @@ defmodule Test.Dummy.ComputeEngine do end end -defmodule ComputeEngineTest do +defmodule ComputeEngineTest do use ExUnit.Case, async: true alias Test.Dummy.ComputeEngine, as: API - @project_id GCloudex.get_project_id - @instance_ep "https://www.googleapis.com/compute/v1/projects/#{@project_id}/zones" - @no_zone_ep "https://www.googleapis.com/compute/v1/projects/#{@project_id}" + def instance_ep(), do: "https://www.googleapis.com/compute/v1/projects/#{GCloudex.get_project_id()}/zones" + def no_zone_ep(), do: "https://www.googleapis.com/compute/v1/projects/#{GCloudex.get_project_id()}" ######################### ### Autoscalers Tests ### ######################### - test "list_autoscalers (no fields)" do + test "list_autoscalers (no fields)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_autoscalers zone end - test "list_autoscalers (with fields)" do + test "list_autoscalers (with fields)" do zone = "zone" headers = [] body = "" query = %{"key1" => "abc", "key2" => "def"} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_autoscalers zone, query - end + end - test "get_autoscaler (no fields)" do + test "get_autoscaler (no fields)" do zone = "zone" autoscaler = "autoscaler" headers = [] body = "" _query = %{} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}" expected = build_expected(:get, endpoint, headers, body) - + assert expected == API.get_autoscaler zone, autoscaler - end + end - test "get_autoscaler (with fields)" do + test "get_autoscaler (with fields)" do zone = "zone" autoscaler = "autoscaler" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) - + assert expected == API.get_autoscaler zone, autoscaler, fields - end + end - test "insert_autoscaler (no fields)" do + test "insert_autoscaler (no fields)" do zone = "zone" resource = %{"name" => "abc", "target" => "def"} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:post, endpoint, headers, body) - + assert expected == API.insert_autoscaler zone, resource end - - test "insert_autoscaler (with fields)" do + + test "insert_autoscaler (with fields)" do zone = "zone" resource = %{"name" => "abc", "target" => "def"} headers = [{"Content-Type", "application/json"}] fields = "a,b,c" query = %{"fields" => fields} body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) - + assert expected == API.insert_autoscaler zone, resource, fields end - test "patch_autoscaler (no fields)" do + test "patch_autoscaler (no fields)" do zone = "zone" name = "name" resource = %{"field1" => "abc"} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! query = %{"autoscaler" => name} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:patch, endpoint, headers, body, query |> URI.encode_query) assert expected == API.patch_autoscaler zone, name, resource end - test "patch_autoscaler (with fields)" do + test "patch_autoscaler (with fields)" do zone = "zone" name = "name" resource = %{"field1" => "abc"} @@ -122,26 +119,26 @@ defmodule ComputeEngineTest do body = resource |> Poison.encode! fields = "a,b,c" query = %{"autoscaler" => name, "fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:patch, endpoint, headers, body, query |> URI.encode_query) assert expected == API.patch_autoscaler zone, name, resource, fields - end + end - test "update_autoscaler (no fields)" do + test "update_autoscaler (no fields)" do zone = "zone" name = "name" resource = %{"field1" => "abc"} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! query = %{"autoscaler" => name} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:put, endpoint, headers, body, query |> URI.encode_query) assert expected == API.update_autoscaler zone, name, resource end - test "update_autoscaler (with fields)" do + test "update_autoscaler (with fields)" do zone = "zone" name = "name" resource = %{"field1" => "abc"} @@ -149,13 +146,13 @@ defmodule ComputeEngineTest do body = resource |> Poison.encode! fields = "a,b,c" query = %{"autoscaler" => name, "fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:put, endpoint, headers, body, query |> URI.encode_query) assert expected == API.update_autoscaler zone, name, resource, fields - end + end - test "update_autoscaler (no name)" do + test "update_autoscaler (no name)" do zone = "zone" name = "" resource = %{"field1" => "abc"} @@ -163,199 +160,199 @@ defmodule ComputeEngineTest do body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:put, endpoint, headers, body, query |> URI.encode_query) assert expected == API.update_autoscaler zone, "", resource, fields - end + end - test "update_autoscaler (no fields no name)" do + test "update_autoscaler (no fields no name)" do zone = "zone" name = "" resource = %{"field1" => "abc"} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! query = %{} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers" expected = build_expected(:put, endpoint, headers, body, query |> URI.encode_query) assert expected == API.update_autoscaler zone, "", resource - end + end - test "delete_autoscaler (no fields)" do + test "delete_autoscaler (no fields)" do zone = "zone" autoscaler = "autoscaler" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_autoscaler zone, autoscaler end - test "delete_autoscaler (with fields)" do + test "delete_autoscaler (with fields)" do zone = "zone" autoscaler = "autoscaler" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/autoscalers/#{autoscaler}" + endpoint = no_zone_ep() <> "/zones/#{zone}/autoscalers/#{autoscaler}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_autoscaler zone, autoscaler, fields - end + end - test "aggregated_list_of_autoscalers (no query)" do + test "aggregated_list_of_autoscalers (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/autoscalers" + endpoint = no_zone_ep() <> "/aggregated/autoscalers" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_autoscalers end - test "aggregated_list_of_autoscalers (with query)" do + test "aggregated_list_of_autoscalers (with query)" do headers = [] body = "" query = %{"field1" => "abc", "field2" => "def"} - endpoint = @no_zone_ep <> "/aggregated/autoscalers" + endpoint = no_zone_ep() <> "/aggregated/autoscalers" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_autoscalers query - end + end ####################### ### DiskTypes Tests ### ####################### - test "list_disk_types (no query)" do + test "list_disk_types (no query)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/diskTypes" + endpoint = no_zone_ep() <> "/zones/#{zone}/diskTypes" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_disk_types zone end - test "list_disk_types (with query)" do + test "list_disk_types (with query)" do zone = "zone" headers = [] body = "" query = %{"field1" => "abc", "field2" => "def"} - endpoint = @no_zone_ep <> "/zones/#{zone}/diskTypes" + endpoint = no_zone_ep() <> "/zones/#{zone}/diskTypes" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_disk_types zone, query - end + end - test "get_disk_type (no fields)" do + test "get_disk_type (no fields)" do zone = "zone" disk_type = "disk_type" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/diskTypes/#{disk_type}" + endpoint = no_zone_ep() <> "/zones/#{zone}/diskTypes/#{disk_type}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_disk_type zone, disk_type - end + end - test "get_disk_type (with fields)" do + test "get_disk_type (with fields)" do zone = "zone" disk_type = "disk_type" headers = [] fields = "a,b,c" query = %{"fields" => fields} body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/diskTypes/#{disk_type}" + endpoint = no_zone_ep() <> "/zones/#{zone}/diskTypes/#{disk_type}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_disk_type zone, disk_type, fields - end + end - test "aggregated_list_of_disk_types (no query)" do + test "aggregated_list_of_disk_types (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/diskTypes" + endpoint = no_zone_ep() <> "/aggregated/diskTypes" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_disk_types - end + end - test "aggregated_list_of_disk_types (with query)" do + test "aggregated_list_of_disk_types (with query)" do headers = [] body = "" query = %{"field1" => 1, "field2" => 2} - endpoint = @no_zone_ep <> "/aggregated/diskTypes" + endpoint = no_zone_ep() <> "/aggregated/diskTypes" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_disk_types query - end + end ################### ### Disks Tests ### ################### - test "list_disks (no query)" do + test "list_disks (no query)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_disks zone end - test "list_disks (with query)" do + test "list_disks (with query)" do zone = "zone" headers = [] body = "" query = %{"a" => 1, "b" => 2} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_disks zone, query - end + end - test "get_disk (no fields)" do + test "get_disk (no fields)" do zone = "zone" disk = "disk" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_disk zone, disk - end + end - test "get_disk (with fields)" do + test "get_disk (with fields)" do zone = "zone" disk = "disk" headers = [] fields = "a,b,c" query = %{"fields" => fields} body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_disk zone, disk, fields - end + end - test "insert_disk (no fields with image)" do + test "insert_disk (no fields with image)" do zone = "zone" resource = %{"name" => "name"} source_img = "source_image" headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! query = %{"sourceImage" => source_img} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_disk zone, resource, source_img - end + end - test "insert_disk (with fields with image)" do + test "insert_disk (with fields with image)" do zone = "zone" resource = %{"name" => "name"} source_img = "source_image" @@ -363,13 +360,13 @@ defmodule ComputeEngineTest do fields = "a,b,c" body = resource |> Poison.encode! query = %{"sourceImage" => source_img, "fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_disk zone, resource, source_img, fields - end + end - test "insert_disk (with fields no image)" do + test "insert_disk (with fields no image)" do zone = "zone" resource = %{"name" => "name"} source_img = "" @@ -377,62 +374,62 @@ defmodule ComputeEngineTest do fields = "a,b,c" body = resource |> Poison.encode! query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_disk zone, resource, source_img, fields - end + end - test "insert_disk (no fields no image)" do + test "insert_disk (no fields no image)" do zone = "zone" resource = %{"name" => "name"} source_img = "" headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! query = %{} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_disk zone, resource, source_img - end + end - test "delete_disk (no fields)" do + test "delete_disk (no fields)" do zone = "zone" disk = "disk" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_disk zone, disk - end + end - test "delete_disk (with fields)" do + test "delete_disk (with fields)" do zone = "zone" disk = "disk" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_disk zone, disk, fields - end + end - test "resize_disk (no fields)" do + test "resize_disk (no fields)" do zone = "zone" disk = "disk" size = 10 headers = [{"Content-Type", "application/json"}] body = %{"sizeGb" => size} |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/resize" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/resize" expected = build_expected(:post, endpoint, headers, body) assert expected == API.resize_disk zone, disk, size - end + end - test "resize_disk (with fields)" do + test "resize_disk (with fields)" do zone = "zone" disk = "disk" size = 10 @@ -440,44 +437,44 @@ defmodule ComputeEngineTest do body = %{"sizeGb" => size} |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/resize" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/resize" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.resize_disk zone, disk, size, fields - end + end - test "aggregated_list_of_disks (no query)" do + test "aggregated_list_of_disks (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/disks" + endpoint = no_zone_ep() <> "/aggregated/disks" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_disks - end + end - test "aggregated_list_of_disks (with query)" do + test "aggregated_list_of_disks (with query)" do headers = [] body = "" query = %{"field1" => 1, "field2" => 2} - endpoint = @no_zone_ep <> "/aggregated/disks" + endpoint = no_zone_ep() <> "/aggregated/disks" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_disks query - end + end - test "create_snapshot (no fields)" do + test "create_snapshot (no fields)" do zone = "zone" disk = "disk" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/createSnapshot" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/createSnapshot" expected = build_expected(:post, endpoint, headers, body) assert expected == API.create_snapshot zone, disk, resource end - test "create_snapshot (with fields)" do + test "create_snapshot (with fields)" do zone = "zone" disk = "disk" resource = %{"abc" => 1, "def" => 2} @@ -485,426 +482,426 @@ defmodule ComputeEngineTest do fields = "a,b,c" query = %{"fields" => fields} body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/disks/#{disk}/createSnapshot" + endpoint = no_zone_ep() <> "/zones/#{zone}/disks/#{disk}/createSnapshot" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.create_snapshot zone, disk, resource, fields - end + end ####################### ### Firewalls Tests ### ####################### - test "list_firewalls (no query)" do + test "list_firewalls (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/global/firewalls" + endpoint = no_zone_ep() <> "/global/firewalls" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_firewalls end - test "list_firewalls (with query)" do + test "list_firewalls (with query)" do headers = [] body = "" query = %{"a" => 1, "b" => 2} - endpoint = @no_zone_ep <> "/global/firewalls" + endpoint = no_zone_ep() <> "/global/firewalls" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_firewalls query - end + end - test "get_firewall (no fields)" do + test "get_firewall (no fields)" do firewall = "firewall" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_firewall firewall end - test "get_firewall (with fields)" do + test "get_firewall (with fields)" do firewall = "firewall" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_firewall firewall, fields - end + end - test "insert_firewall (no fields)" do + test "insert_firewall (no fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/global/firewalls" + endpoint = no_zone_ep() <> "/global/firewalls" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_firewall resource end - test "insert_firewall (with fields)" do + test "insert_firewall (with fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/firewalls" + endpoint = no_zone_ep() <> "/global/firewalls" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_firewall resource, fields - end + end - test "patch_firewall (no fields)" do + test "patch_firewall (no fields)" do firewall = "firewall" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:patch, endpoint, headers, body) assert expected == API.patch_firewall firewall, resource - end + end - test "patch_firewall (with fields)" do + test "patch_firewall (with fields)" do firewall = "firewall" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:patch, endpoint, headers, body, query |> URI.encode_query) assert expected == API.patch_firewall firewall, resource, fields - end + end - test "update_firewall (no fields)" do + test "update_firewall (no fields)" do firewall = "firewall" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:put, endpoint, headers, body) assert expected == API.update_firewall firewall, resource - end + end - test "update_firewall (with fields)" do + test "update_firewall (with fields)" do firewall = "firewall" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:put, endpoint, headers, body, query |> URI.encode_query) assert expected == API.update_firewall firewall, resource, fields - end + end - test "delete_firewall (no fields)" do + test "delete_firewall (no fields)" do firewall = "firewall" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_firewall firewall - end + end - test "delete_firewall (with fields)" do + test "delete_firewall (with fields)" do firewall = "firewall" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/firewalls/#{firewall}" + endpoint = no_zone_ep() <> "/global/firewalls/#{firewall}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) - assert expected == API.delete_firewall firewall, fields - end + assert expected == API.delete_firewall firewall, fields + end #################### ### Images Tests ### #################### - test "list_images (no query)" do + test "list_images (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_images end - test "list_images (with query)" do + test "list_images (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_images query end - test "get_image (no fields)" do + test "get_image (no fields)" do image = "image" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/images/#{image}" + endpoint = no_zone_ep() <> "/global/images/#{image}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_image image - end + end - test "get_image (with fields)" do + test "get_image (with fields)" do image = "image" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/images/#{image}" + endpoint = no_zone_ep() <> "/global/images/#{image}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_image image - end + end - test "insert_image_with_resource (no fields)" do + test "insert_image_with_resource (no fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_image_with_resource resource - end + end - test "insert_image_with_resource (with fields)" do + test "insert_image_with_resource (with fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_image_with_resource resource, fields - end + end - test "insert_image (no fields)" do + test "insert_image (no fields)" do name = "name" url = "url" headers = [{"Content-Type", "application/json"}] body = %{"name" => name, "rawDisk" => %{"source" => url}} |> Poison.encode! - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_image name, url - end + end - test "insert_image (with fields)" do + test "insert_image (with fields)" do name = "name" url = "url" headers = [{"Content-Type", "application/json"}] fields = "a,b,c" query = %{"fields" => fields} body = %{"name" => name, "rawDisk" => %{"source" => url}} |> Poison.encode! - endpoint = @no_zone_ep <> "/global/images" + endpoint = no_zone_ep() <> "/global/images" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_image name, url, fields - end + end - test "delete_image (no fields)" do + test "delete_image (no fields)" do image = "image" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/images/#{image}" + endpoint = no_zone_ep() <> "/global/images/#{image}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_image image - end + end - test "delete_image (with fields)" do + test "delete_image (with fields)" do image = "image" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/images/#{image}" + endpoint = no_zone_ep() <> "/global/images/#{image}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_image image, fields - end + end - test "deprecate_image (no fields)" do + test "deprecate_image (no fields)" do image = "image" request = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = request |> Poison.encode! - endpoint = @no_zone_ep <> "/global/images/#{image}/deprecate" + endpoint = no_zone_ep() <> "/global/images/#{image}/deprecate" expected = build_expected(:post, endpoint, headers, body) assert expected == API.deprecate_image image, request - end + end - test "deprecate_image (with fields)" do + test "deprecate_image (with fields)" do image = "image" request = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = request |> Poison.encode! fields = "a,b,c" - query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/images/#{image}/deprecate" + query = %{"fields" => fields} + endpoint = no_zone_ep() <> "/global/images/#{image}/deprecate" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.deprecate_image image, request, fields - end + end ############################ ### InstanceGroups Tests ### ############################ - test "list_instance_groups (no query)" do + test "list_instance_groups (no query)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_instance_groups zone end - test "list_instance_groups (with query)" do + test "list_instance_groups (with query)" do zone = "zone" headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_instance_groups zone, query end - test "list_instances_in_group (no query)" do + test "list_instances_in_group (no query)" do zone = "zone" group = "group" state = "state" headers = [{"Content-Type", "application/json"}] body = %{"instanceState" => state} |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/listInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/listInstances" expected = build_expected(:post, endpoint, headers, body) assert expected == API.list_instances_in_group zone, group, state end - test "list_instances_in_group (with) query)" do + test "list_instances_in_group (with) query)" do zone = "zone" group = "group" state = "state" headers = [{"Content-Type", "application/json"}] body = %{"instanceState" => state} |> Poison.encode! query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/listInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/listInstances" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_instances_in_group zone, group, state, query end - test "get_instance_group (no fields)" do + test "get_instance_group (no fields)" do zone = "zone" group = "group" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_instance_group zone, group - end + end - test "get_instance_group (with fields)" do + test "get_instance_group (with fields)" do zone = "zone" group = "group" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_instance_group zone, group, fields - end + end - test "insert_instance_group (no fields)" do + test "insert_instance_group (no fields)" do zone = "zone" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_instance_group zone, resource - end + end - test "insert_instance_group (with fields)" do + test "insert_instance_group (with fields)" do zone = "zone" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_instance_group zone, resource, fields - end + end - test "delete_instance_group (no fields)" do + test "delete_instance_group (no fields)" do zone = "zone" group = "group" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_instance_group zone, group - end + end - test "delete_instance_group (with fields)" do + test "delete_instance_group (with fields)" do zone = "zone" group = "group" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_instance_group zone, group, fields - end + end - test "aggregated_list_of_instance_groups (no query)" do + test "aggregated_list_of_instance_groups (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/instanceGroups" + endpoint = no_zone_ep() <> "/aggregated/instanceGroups" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_instance_groups - end + end - test "aggregated_list_of_instance_groups (with query)" do + test "aggregated_list_of_instance_groups (with query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/instanceGroups" + endpoint = no_zone_ep() <> "/aggregated/instanceGroups" query = %{"abc" => 1, "def" => 2} expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_instance_groups query - end + end - test "add_instances_to_group (no fields)" do + test "add_instances_to_group (no fields)" do zone = "zone" group = "group" instances = ["a", "b", "c"] @@ -916,13 +913,13 @@ defmodule ComputeEngineTest do %{"instance" => "c"} ] } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/addInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/addInstances" expected = build_expected(:post, endpoint, headers, body) assert expected == API.add_instances_to_group zone, group, instances - end + end - test "add_instances_to_group (with fields)" do + test "add_instances_to_group (with fields)" do zone = "zone" group = "group" instances = ["a", "b", "c"] @@ -936,13 +933,13 @@ defmodule ComputeEngineTest do %{"instance" => "c"} ] } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/addInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/addInstances" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.add_instances_to_group zone, group, instances, fields - end + end - test "remove_instances_from_group (no fields)" do + test "remove_instances_from_group (no fields)" do zone = "zone" group = "group" instances = ["a", "b", "c"] @@ -954,13 +951,13 @@ defmodule ComputeEngineTest do %{"instance" => "c"} ] } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/removeInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/removeInstances" expected = build_expected(:post, endpoint, headers, body) assert expected == API.remove_instances_from_group zone, group, instances - end + end - test "remove_instances_from_group (with fields)" do + test "remove_instances_from_group (with fields)" do zone = "zone" group = "group" instances = ["a", "b", "c"] @@ -974,13 +971,13 @@ defmodule ComputeEngineTest do %{"instance" => "c"} ] } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/removeInstances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/removeInstances" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.remove_instances_from_group zone, group, instances, fields - end + end - test "set_named_ports_for_group (no fields)" do + test "set_named_ports_for_group (no fields)" do zone = "zone" group = "group" fp = "fingerprint" @@ -994,13 +991,13 @@ defmodule ComputeEngineTest do ], "fingerprint" => fp } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/setNamedPorts" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/setNamedPorts" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_named_ports_for_group zone, group, ports, fp - end + end - test "set_named_ports_for_group (with fields)" do + test "set_named_ports_for_group (with fields)" do zone = "zone" group = "group" fp = "fingerprint" @@ -1016,182 +1013,182 @@ defmodule ComputeEngineTest do ], "fingerprint" => fp } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instanceGroups/#{group}/setNamedPorts" + endpoint = no_zone_ep() <> "/zones/#{zone}/instanceGroups/#{group}/setNamedPorts" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_named_ports_for_group zone, group, ports, fp - end + end ####################### ### Instances Tests ### ####################### - test "list_instances (no query)" do + test "list_instances (no query)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_instances zone end - test "list_instances (with query)" do + test "list_instances (with query)" do zone = "zone" headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_instances zone, query end - test "get_instance (no fields)" do + test "get_instance (no fields)" do zone = "zone" instance = "instance" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_instance zone, instance - end + end - test "get_instance (with fields)" do + test "get_instance (with fields)" do zone = "zone" instance = "instance" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_instance zone, instance, fields - end + end - test "insert_instance (no fields)" do + test "insert_instance (no fields)" do zone = "zone" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_instance zone, resource - end + end - test "insert_instance (with fields)" do + test "insert_instance (with fields)" do zone = "zone" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_instance zone, resource, fields - end + end - test "delete_instance (no fields)" do + test "delete_instance (no fields)" do zone = "zone" instance = "instance" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_instance zone, instance - end + end - test "delete_instance (with fields)" do + test "delete_instance (with fields)" do zone = "zone" instance = "instance" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_instance zone, instance, fields - end + end - test "start_instance (no fields)" do + test "start_instance (no fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/start" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/start" expected = build_expected(:post, endpoint, headers, body) assert expected == API.start_instance zone, instance - end + end - test "start_instance (with fields)" do + test "start_instance (with fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/start" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/start" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.start_instance zone, instance, fields - end + end - test "stop_instance (no fields)" do + test "stop_instance (no fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/stop" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/stop" expected = build_expected(:post, endpoint, headers, body) assert expected == API.stop_instance zone, instance - end + end - test "stop_instance (with fields)" do + test "stop_instance (with fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/stop" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/stop" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.stop_instance zone, instance, fields - end + end - test "reset_instance (no fields)" do + test "reset_instance (no fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/reset" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/reset" expected = build_expected(:post, endpoint, headers, body) assert expected == API.reset_instance zone, instance - end + end - test "reset_instance (with fields)" do + test "reset_instance (with fields)" do zone = "zone" instance = "instance" headers = [{"Content-Type", "application/json"}] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/reset" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/reset" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.reset_instance zone, instance, fields - end + end - test "add_access_config (no fields with nat)" do + test "add_access_config (no fields with nat)" do zone = "zone" instance = "instance" interface = "interface" @@ -1205,13 +1202,13 @@ defmodule ComputeEngineTest do "name" => name, "natIP" => nat } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.add_access_config zone, instance, interface, name, nat - end + end - test "add_access_config (no fields no nat)" do + test "add_access_config (no fields no nat)" do zone = "zone" instance = "instance" interface = "interface" @@ -1224,13 +1221,13 @@ defmodule ComputeEngineTest do "type" => "ONE_TO_ONE_NAT", "name" => name, } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.add_access_config zone, instance, interface, name, nat - end + end - test "add_access_config (with fields with nat)" do + test "add_access_config (with fields with nat)" do zone = "zone" instance = "instance" interface = "interface" @@ -1245,13 +1242,13 @@ defmodule ComputeEngineTest do "name" => name, "natIP" => nat } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.add_access_config zone, instance, interface, name, nat, fields - end + end - test "add_access_config (no fields no interface)" do + test "add_access_config (no fields no interface)" do zone = "zone" instance = "instance" interface = "interface" @@ -1266,13 +1263,13 @@ defmodule ComputeEngineTest do "type" => "ONE_TO_ONE_NAT", "name" => name, } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/addAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.add_access_config zone, instance, interface, name, nat - end + end - test "delete_access_config (no fields)" do + test "delete_access_config (no fields)" do zone = "zone" instance = "instance" interface = "interface" @@ -1280,13 +1277,13 @@ defmodule ComputeEngineTest do headers = [{"Content-Type", "application/json"}] query = %{"networkInterface" => interface, "accessConfig" => config} body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/deleteAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/deleteAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_access_config zone, instance, config, interface - end + end - test "delete_access_config (with fields)" do + test "delete_access_config (with fields)" do zone = "zone" instance = "instance" interface = "interface" @@ -1294,49 +1291,49 @@ defmodule ComputeEngineTest do headers = [{"Content-Type", "application/json"}] fields = "a,b,c" query = %{ - "networkInterface" => interface, - "accessConfig" => config, + "networkInterface" => interface, + "accessConfig" => config, "fields" => fields } body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/deleteAccessConfig" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/deleteAccessConfig" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_access_config zone, instance, config, interface, fields - end + end - test "aggregated_list_of_instances (no query)" do + test "aggregated_list_of_instances (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/instances" + endpoint = no_zone_ep() <> "/aggregated/instances" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_instances end - test "aggregated_list_of_instances (with query)" do + test "aggregated_list_of_instances (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/aggregated/instances" + endpoint = no_zone_ep() <> "/aggregated/instances" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_instances query - end + end - test "attach_disk (no fields)" do + test "attach_disk (no fields)" do zone = "zone" instance = "instance" resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/attachDisk" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/attachDisk" expected = build_expected(:post, endpoint, headers, body) assert expected == API.attach_disk zone, instance, resource - end + end - test "attach_disk (with fields)" do + test "attach_disk (with fields)" do zone = "zone" instance = "instance" resource = %{"abc" => 1, "def" => 2} @@ -1344,26 +1341,26 @@ defmodule ComputeEngineTest do body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/attachDisk" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/attachDisk" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.attach_disk zone, instance, resource, fields - end + end - test "detach_disk (no fields)" do + test "detach_disk (no fields)" do zone = "zone" instance = "instance" device = "device" headers = [{"Content-Type", "application/json"}] body = "" query = %{"deviceName" => device} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/detachDisk" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/detachDisk" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.detach_disk zone, instance, device - end + end - test "detach_disk (with fields)" do + test "detach_disk (with fields)" do zone = "zone" instance = "instance" device = "device" @@ -1371,13 +1368,13 @@ defmodule ComputeEngineTest do body = "" fields = "a,b,c" query = %{"deviceName" => device, "fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/detachDisk" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/detachDisk" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.detach_disk zone, instance, device, fields - end + end - test "set_disk_auto_delete (no fields)" do + test "set_disk_auto_delete (no fields)" do zone = "zone" instance = "instance" delete = true @@ -1385,13 +1382,13 @@ defmodule ComputeEngineTest do headers = [{"Content-Type", "application/json"}] body = "" query = %{"deviceName" => device, "autoDelete" => delete} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setDiskAutoDelete" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setDiskAutoDelete" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.set_disk_auto_delete zone, instance, delete, device - end + end - test "set_disk_auto_delete (with fields)" do + test "set_disk_auto_delete (with fields)" do zone = "zone" instance = "instance" delete = true @@ -1400,30 +1397,30 @@ defmodule ComputeEngineTest do body = "" fields = "a,b,c" query = %{ - "deviceName" => device, + "deviceName" => device, "autoDelete" => delete, "fields" => fields } - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setDiskAutoDelete" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setDiskAutoDelete" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.set_disk_auto_delete zone, instance, delete, device, fields - end + end - test "get_serial_port_output (no fields)" do + test "get_serial_port_output (no fields)" do zone = "zone" instance = "instance" port = 1 headers = [] body = "" query = %{"port" => port} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/serialPort" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/serialPort" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_serial_port_output zone, instance, port - end + end - test "get_serial_port_output (with fields)" do + test "get_serial_port_output (with fields)" do zone = "zone" instance = "instance" port = 1 @@ -1431,25 +1428,25 @@ defmodule ComputeEngineTest do body = "" fields = "a,b,c" query = %{"port" => port, "fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/serialPort" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/serialPort" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_serial_port_output zone, instance, port, fields - end + end - test "set_machine_type (no fields)" do + test "set_machine_type (no fields)" do zone = "zone" instance = "instance" type = "type" headers = [{"Content-Type", "application/json"}] body = %{"machineType" => type} |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setMachineType" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setMachineType" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_machine_type zone, instance, type - end + end - test "set_machine_type (with fields)" do + test "set_machine_type (with fields)" do zone = "zone" instance = "instance" type = "type" @@ -1457,13 +1454,13 @@ defmodule ComputeEngineTest do body = %{"machineType" => type} |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setMachineType" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setMachineType" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.set_machine_type zone, instance, type, fields - end + end - test "set_metadata (no fields)" do + test "set_metadata (no fields)" do zone = "zone" instance = "instance" fp = "fingerprint" @@ -1477,13 +1474,13 @@ defmodule ComputeEngineTest do "fingerprint" => fp, "items" => items } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setMetadata" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setMetadata" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_metadata zone, instance, fp, items - end + end - test "set_metadata (with fields)" do + test "set_metadata (with fields)" do zone = "zone" instance = "instance" fp = "fingerprint" @@ -1499,13 +1496,13 @@ defmodule ComputeEngineTest do } |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setMetadata" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setMetadata" expected = build_expected(:post, endpoint, headers, body, query) assert expected == API.set_metadata zone, instance, fp, items, fields - end + end - test "set_scheduling (no fields)" do + test "set_scheduling (no fields)" do zone = "zone" instance = "instance" on_host = "abc" @@ -1517,13 +1514,13 @@ defmodule ComputeEngineTest do "automaticRestart" => restart, "preemptible" => pre } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setScheduling" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setScheduling" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_scheduling zone, instance, {on_host, restart, pre} - end + end - test "set_scheduling (with fields)" do + test "set_scheduling (with fields)" do zone = "zone" instance = "instance" on_host = "abc" @@ -1537,13 +1534,13 @@ defmodule ComputeEngineTest do } |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setScheduling" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setScheduling" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.set_scheduling zone, instance, {on_host, restart, pre}, fields - end + end - test "set_tags (no fields)" do + test "set_tags (no fields)" do zone = "zone" instance = "instance" fp = "fingerprint" @@ -1553,13 +1550,13 @@ defmodule ComputeEngineTest do "items" => items, "fingerprint" => fp } |> Poison.encode! - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setTags" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setTags" expected = build_expected(:post, endpoint, headers, body) assert expected == API.set_tags zone, instance, fp, items - end + end - test "set_tags (with fields)" do + test "set_tags (with fields)" do zone = "zone" instance = "instance" fp = "fingerprint" @@ -1571,284 +1568,284 @@ defmodule ComputeEngineTest do } |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/instances/#{instance}/setTags" + endpoint = no_zone_ep() <> "/zones/#{zone}/instances/#{instance}/setTags" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.set_tags zone, instance, fp, items, fields - end + end ###################### ### Licenses Tests ### ###################### - test "get_license (no fields)" do + test "get_license (no fields)" do license = "license" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/licenses/#{license}" + endpoint = no_zone_ep() <> "/global/licenses/#{license}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_license license - end + end - test "get_license (with fields)" do + test "get_license (with fields)" do license = "license" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/licenses/#{license}" + endpoint = no_zone_ep() <> "/global/licenses/#{license}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_license license, fields - end + end ##################### ### Regions Tests ### ##################### - test "list_regions (no query)" do + test "list_regions (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/regions" + endpoint = no_zone_ep() <> "/regions" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_regions end - test "list_regions (with query)" do + test "list_regions (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/regions" + endpoint = no_zone_ep() <> "/regions" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_regions query end - test "get_region (no fields)" do + test "get_region (no fields)" do region = "region" headers = [] body = "" - endpoint = @no_zone_ep <> "/regions/#{region}" + endpoint = no_zone_ep() <> "/regions/#{region}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_region region - end + end - test "get_region (with fields)" do + test "get_region (with fields)" do region = "region" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/regions/#{region}" + endpoint = no_zone_ep() <> "/regions/#{region}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_region region, fields - end + end ########################## ### MachineTypes Tests ### ########################## - test "list_machine_types (no query)" do + test "list_machine_types (no query)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/machineTypes" + endpoint = no_zone_ep() <> "/zones/#{zone}/machineTypes" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_machine_types zone end - test "list_machine_types (with query)" do + test "list_machine_types (with query)" do zone = "zone" headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/zones/#{zone}/machineTypes" + endpoint = no_zone_ep() <> "/zones/#{zone}/machineTypes" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_machine_types zone, query - end + end - test "get_machine_type (no fields)" do + test "get_machine_type (no fields)" do zone = "zone" type = "machine_type" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}/machineTypes/#{type}" + endpoint = no_zone_ep() <> "/zones/#{zone}/machineTypes/#{type}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_machine_type zone, type - end + end - test "get_machine_type (with fields)" do + test "get_machine_type (with fields)" do zone = "zone" type = "machine_type" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}/machineTypes/#{type}" + endpoint = no_zone_ep() <> "/zones/#{zone}/machineTypes/#{type}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_machine_type zone, type, fields - end + end - test "aggregated_list_of_machine_types (no query)" do + test "aggregated_list_of_machine_types (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/aggregated/machineTypes" + endpoint = no_zone_ep() <> "/aggregated/machineTypes" expected = build_expected(:get, endpoint, headers, body) assert expected == API.aggregated_list_of_machine_types - end + end - test "aggregated_list_of_machine_types (with query)" do + test "aggregated_list_of_machine_types (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/aggregated/machineTypes" + endpoint = no_zone_ep() <> "/aggregated/machineTypes" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.aggregated_list_of_machine_types query - end + end ###################### ### Networks Tests ### ###################### - test "list_networks (no query)" do + test "list_networks (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/global/networks" + endpoint = no_zone_ep() <> "/global/networks" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_networks end - test "list_networks (with query)" do + test "list_networks (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/global/networks" + endpoint = no_zone_ep() <> "/global/networks" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_networks query - end + end - test "get_network (no fields)" do + test "get_network (no fields)" do network = "network" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/networks/#{network}" + endpoint = no_zone_ep() <> "/global/networks/#{network}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_network network - end + end - test "get_network (with fields)" do + test "get_network (with fields)" do network = "network" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/networks/#{network}" + endpoint = no_zone_ep() <> "/global/networks/#{network}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_network network, fields - end + end - test "insert_network (no fields)" do + test "insert_network (no fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! - endpoint = @no_zone_ep <> "/global/networks" + endpoint = no_zone_ep() <> "/global/networks" expected = build_expected(:post, endpoint, headers, body) assert expected == API.insert_network resource - end + end - test "insert_network (with fields)" do + test "insert_network (with fields)" do resource = %{"abc" => 1, "def" => 2} headers = [{"Content-Type", "application/json"}] body = resource |> Poison.encode! fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/networks" + endpoint = no_zone_ep() <> "/global/networks" expected = build_expected(:post, endpoint, headers, body, query |> URI.encode_query) assert expected == API.insert_network resource, fields - end + end - test "delete_network (no fields)" do + test "delete_network (no fields)" do network = "network" headers = [] body = "" - endpoint = @no_zone_ep <> "/global/networks/#{network}" + endpoint = no_zone_ep() <> "/global/networks/#{network}" expected = build_expected(:delete, endpoint, headers, body) assert expected == API.delete_network network - end + end - test "delete_network (with fields)" do + test "delete_network (with fields)" do network = "network" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/global/networks/#{network}" + endpoint = no_zone_ep() <> "/global/networks/#{network}" expected = build_expected(:delete, endpoint, headers, body, query |> URI.encode_query) assert expected == API.delete_network network, fields - end + end ################### ### Zones Tests ### ################### - test "list_zones (no query)" do + test "list_zones (no query)" do headers = [] body = "" - endpoint = @no_zone_ep <> "/zones" + endpoint = no_zone_ep() <> "/zones" expected = build_expected(:get, endpoint, headers, body) assert expected == API.list_zones end - test "list_zones (with query)" do + test "list_zones (with query)" do headers = [] body = "" query = %{"abc" => 1, "def" => 2} - endpoint = @no_zone_ep <> "/zones" + endpoint = no_zone_ep() <> "/zones" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.list_zones query end - test "get_zone (no fields)" do + test "get_zone (no fields)" do zone = "zone" headers = [] body = "" - endpoint = @no_zone_ep <> "/zones/#{zone}" + endpoint = no_zone_ep() <> "/zones/#{zone}" expected = build_expected(:get, endpoint, headers, body) assert expected == API.get_zone zone - end + end - test "get_zone (with fields)" do + test "get_zone (with fields)" do zone = "zone" headers = [] body = "" fields = "a,b,c" query = %{"fields" => fields} - endpoint = @no_zone_ep <> "/zones/#{zone}" + endpoint = no_zone_ep() <> "/zones/#{zone}" expected = build_expected(:get, endpoint, headers, body, query |> URI.encode_query) assert expected == API.get_zone zone, fields - end + end ############### ### Helpers ### @@ -1856,12 +1853,12 @@ defmodule ComputeEngineTest do defp build_expected(verb, host, headers, body, query \\ "") do %{ - verb: verb, - endpoint: host, - headers: - headers ++ + verb: verb, + endpoint: host, + headers: + headers ++ [ - {"x-goog-project-id", @project_id}, + {"x-goog-project-id", GCloudex.get_project_id()}, {"Authorization", "Bearer Dummy Token"} ], body: body,