Packages

Create cloud portable Elixir and Phoenix apps. Write once, use everywhere!

Current section

Files

Jump to
nomad lib mix tasks sql database_instance_delete.ex
Raw

lib/mix/tasks/sql/database_instance_delete.ex

defmodule Mix.Tasks.Nomad.DatabaseInstance.Delete do
use Mix.Task
@moduledoc """
Task for automatically deleting a remote SQL database on a pre-determined
cloud provider. The instance deletion is done through the cloud provider's
API.
Usage:
mix nomad.database_instance.delete
# Will be prompted for name
mix nomad.database_instance.delete <name>
# Won't be prompted for name
"""
@shortdoc"""
Delete a SQL database instance on the chosen cloud provider's SQL service.
"""
@doc """
Runs the task for the chosen cloud provider. The shell prompts for the
instance's name and informs of the result.
"""
@spec run(list) :: binary
def run(args) do
case Application.get_env(:nomad, :cloud_provider) do
:aws ->
Application.ensure_all_started(:ex_aws)
Application.ensure_all_started(:httpoison)
:gcl ->
Application.ensure_all_started(:httpoison)
Application.ensure_all_started(:goth)
Application.ensure_all_started(:gcloudex)
end
delete_instance_api_call args
end
# When no args are provided the prompt asks for the instance's name
defp delete_instance_api_call([]) do
name = Mix.Shell.IO.prompt("Insert the name of the instance you want to delete: ")
|> String.rstrip
del name
end
# When a name is provided the API call is made directly
defp delete_instance_api_call([name]) do
del name
end
defp del(name) do
case Nomad.SQL.delete_instance(name) do
:ok ->
Mix.Shell.IO.info("The instance has been deleted successfully.")
msg ->
Mix.Shell.IO.info("There was a problem deleting the instance:\n#{msg}")
end
end
end