Skip to content

Commit

Permalink
Fix cloud instance validations
Browse files Browse the repository at this point in the history
* off by one on the regex
* create instance first so its uniq constraints fire first
  • Loading branch information
michaeljguarino committed Aug 30, 2024
1 parent b4a1f29 commit 6a220e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
15 changes: 0 additions & 15 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
<!--- Hello Plural contributor! It's great to have you on board! -->

## Summary
<!-- Describe your changes here, include the motivation/context, test coverage, -->
<!-- the type of change i.e. breaking change, new feature, or bug fix -->
<!-- and related GitHub issue or screenshots (if applicable). -->

<!-- Adding a meaningful title and description allows us to better communicate -->
<!-- your work with our users. -->

## Labels
<!-- For breaking changes, add the `breaking-change` label.️ -->
<!-- For bug fixes, add the `bug-fix` label. -->
<!-- For new features and notable changes, add the `enhancement` label. -->


## Test Plan
<!--- Please describe the tests you have added and your testing environment (if applicable). -->
Expand Down
12 changes: 11 additions & 1 deletion apps/core/lib/core/schema/console_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,19 @@ defmodule Core.Schema.ConsoleInstance do
|> cast_embed(:configuration, with: &configuration_changeset/2)
|> cast_embed(:instance_status, with: &status_changeset/2)
|> validate_required(@valid -- [:external_id])
|> foreign_key_constraint(:cluster_id)
|> foreign_key_constraint(:postgres_id)
|> foreign_key_constraint(:owner_id)
|> unique_constraint(:subdomain)
|> unique_constraint(:name)
|> validate_format(:name, ~r/[a-z][a-z0-9]{5,10}/, message: "must be an alphanumeric string between 5 and 10 characters")
|> validate_format(:name, ~r/[a-z][a-z0-9]{5,10}/, message: "must be an alphanumeric string between 5 and 11 characters")
|> validate_region()
end

def create_changeset(model, attrs \\ %{}) do
model
|> cast(attrs, @valid)
|> validate_required(~w(name region status cloud size)a)
|> validate_region()
end

Expand Down
19 changes: 14 additions & 5 deletions apps/core/lib/core/services/cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Core.Services.Cloud do

def get_instance!(id), do: Repo.get!(ConsoleInstance, id)

def get_instance_by_name(name), do: Repo.get_by(ConsoleInstance, name: name)

@spec upsert_cluster(map, binary) :: cluster_resp
def upsert_cluster(attrs, name) do
case Repo.get_by(CloudCluster, name: name) do
Expand All @@ -39,7 +41,12 @@ defmodule Core.Services.Cloud do
@spec create_instance(map, User.t) :: console_resp
def create_instance(%{name: name} = attrs, %User{} = user) do
start_transaction()
|> add_operation(:auth, fn _ -> allow(%ConsoleInstance{}, user, :create) end)
|> add_operation(:inst, fn _ ->
%ConsoleInstance{status: :pending}
|> ConsoleInstance.create_changeset(attrs)
|> allow(user, :create)
|> when_ok(:insert)
end)
|> add_operation(:cluster, fn _ -> select_cluster(attrs[:cloud], attrs[:region]) end)
|> add_operation(:postgres, fn _ -> select_roach(attrs[:cloud]) end)
|> add_operation(:sa, fn _ ->
Expand Down Expand Up @@ -67,10 +74,12 @@ defmodule Core.Services.Cloud do
redirect_uris: Shell.merge_uris(["https://console.#{name}.#{domain()}/oauth/callback"], inst.oidc_provider)
}, inst.id, sa)
end)
|> add_operation(:instance, fn %{oidc: oidc, token: token, cluster: cluster, postgres: roach, sa: sa} ->
%ConsoleInstance{status: :pending, cluster_id: cluster.id, postgres_id: roach.id, owner_id: sa.id}
|> ConsoleInstance.changeset(add_configuration(attrs, name, token.token, oidc, user))
|> Repo.insert()
|> add_operation(:instance, fn %{inst: inst, oidc: oidc, token: token, cluster: cluster, postgres: roach, sa: sa} ->
ConsoleInstance.changeset(inst, Map.merge(
%{cluster_id: cluster.id, postgres_id: roach.id, owner_id: sa.id},
add_configuration(attrs, name, token.token, oidc, user)
))
|> Repo.update()
end)
|> execute(extract: :instance)
|> notify(:create, user)
Expand Down

0 comments on commit 6a220e9

Please sign in to comment.