Skip to content

Commit

Permalink
chg: [connection] prevent modifying type
Browse files Browse the repository at this point in the history
  • Loading branch information
gallypette committed Oct 11, 2024
1 parent 0de657c commit 7545012
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
15 changes: 13 additions & 2 deletions lib/cocktailparty/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ defmodule Cocktailparty.Input do
{:ok, connection} = Repo.update(changeset)
ConnectionManager.start_connection(connection)
{:ok, connection}
# same for its sources

else
Repo.update(changeset)
end
Expand Down Expand Up @@ -234,6 +232,19 @@ defmodule Cocktailparty.Input do
Connection.changeset(connection, attrs)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking connection changes -- without the type
## Examples
iex> change_edit_connection(connection)
%Ecto.Changeset{data: %Connection{}}
"""
def change_edit_connection(%Connection{} = connection, attrs \\ %{}) do
Connection.edit_changeset(connection, attrs)
end

@doc """
Get the status of a redis connection
Expand Down
13 changes: 11 additions & 2 deletions lib/cocktailparty/input/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ defmodule Cocktailparty.Input.Connection do
|> validate_config()
end

@doc false
# we don't allow for changing a connection type once created
def edit_changeset(connection, attrs) do
connection
|> cast(attrs, [:name, :config, :enabled, :sink])
|> validate_required([:name, :config, :enabled, :sink])
|> unique_constraint(:name)
end

defp validate_config(changeset) do
case get_field(changeset, :type) do
nil ->
Expand All @@ -41,7 +50,7 @@ defmodule Cocktailparty.Input.Connection do
end

@doc """
Kill processes related to a redis instance
Kill processes related to a connection
TODO
"""
Expand Down Expand Up @@ -71,7 +80,7 @@ defmodule Cocktailparty.Input.Connection do
nil ->
# TODO
Logger.error(
"It looks like the redis instances dynamic supervisor is dead, it's not looking good."
"It looks like the connection dynamic supervisor is dead, it's not looking good."
)

nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule CocktailpartyWeb.Admin.ConnectionController do

connection_types = ConnectionTypes.all()

render(conn, :new, changeset: new_changeset, connection_types: connection_types)
render(conn, :new, changeset: new_changeset, connection_types: connection_types, show_connection_types: true)
end
end

Expand All @@ -51,14 +51,15 @@ defmodule CocktailpartyWeb.Admin.ConnectionController do
connection_map = Input.get_connection_map!(id)

changeset =
Input.change_connection(connection_map)
Input.change_edit_connection(connection_map)
|> Map.put(:data, Input.switch_config_repr!(connection_map))

connection_types = ConnectionTypes.all()

render(conn, :edit,
connection: connection_map,
changeset: changeset,
show_connection_types: false,
connection_types: connection_types
)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/cocktailparty_web/controllers/admin/connection_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule CocktailpartyWeb.Admin.ConnectionHTML do
Renders a connection form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :connection_types, :list, required: true
attr :connection_types, :list, required: false
attr :show_connection_types, :boolean, required: true
attr :action, :string, required: true

def connection_form(assigns)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:name]} type="text" label="Name" />
<.input
<.input :if={@show_connection_types}
field={f[:type]}
type="select"
label="Type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<:subtitle>Use this form to manage connection records in your database.</:subtitle>
</.header>

<.connection_form changeset={@changeset} action={~p"/admin/connections/#{@connection}"} connection_types={@connection_types} />
<.connection_form
changeset={@changeset}
action={~p"/admin/connections/#{@connection}"}
connection_types={@connection_types}
show_connection_types={@show_connection_types}
/>

<.back navigate={~p"/admin/connections"}>Back to connections</.back>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<:subtitle>Use this form to manage connection records in your database.</:subtitle>
</.header>

<.connection_form changeset={@changeset} action={~p"/admin/connections"} connection_types={@connection_types} />
<.connection_form changeset={@changeset} action={~p"/admin/connections"} connection_types={@connection_types} show_connection_types={@show_connection_types}/>

<.back navigate={~p"/admin/connections"}>Back to connections</.back>

0 comments on commit 7545012

Please sign in to comment.