-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sends first and second warning emails, then deletes
- Loading branch information
1 parent
80f3b64
commit 3af5226
Showing
15 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Cron.Prune.Cloud do | ||
@moduledoc """ | ||
Reaps unpaid cloud consoles | ||
""" | ||
use Cron | ||
alias Core.Schema.{ConsoleInstance} | ||
alias Core.Services.Cloud | ||
|
||
def run() do | ||
ConsoleInstance.unpaid() | ||
|> ConsoleInstance.reapable() | ||
|> ConsoleInstance.ordered(asc: :id) | ||
|> Core.Repo.stream(method: :keyset) | ||
|> Core.throttle() | ||
|> Enum.each(&Cloud.reap/1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Cron.Prune.CloudTest do | ||
use Core.SchemaCase | ||
alias Cron.Prune.Cloud | ||
|
||
describe "#run/0" do | ||
test "it will prune old, unused invites" do | ||
old = Timex.now() |> Timex.shift(weeks: -1) |> Timex.shift(minutes: -30) | ||
account = insert(:account) | ||
user = insert(:user, account: account) | ||
insert(:platform_subscription, account: account) | ||
|
||
first = insert(:console_instance) | ||
ignore = insert(:console_instance, owner: user) | ||
ignore2 = insert(:console_instance, first_notif_at: Timex.now()) | ||
second = insert(:console_instance, first_notif_at: old) | ||
third = insert(:console_instance, first_notif_at: old, second_notif_at: old) | ||
|
||
Cloud.run() | ||
|
||
assert refetch(first).first_notif_at | ||
refute refetch(ignore).first_notif_at | ||
refute refetch(ignore2).second_notif_at | ||
assert refetch(second).second_notif_at | ||
assert refetch(third).deleted_at | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Email.Builder.ConsoleReaped do | ||
use Email.Builder.Base | ||
alias Core.Schema.ConsoleInstance | ||
|
||
def email(inst) do | ||
%{owner: user} = inst = Core.Repo.preload(inst, [:owner]) | ||
|
||
base_email() | ||
|> to(expand_service_account(user)) | ||
|> subject("Your Plural Cloud Instance #{inst.name} is eligible to be decommissioned") | ||
|> assign(:inst, inst) | ||
|> assign(:warning, warning(inst)) | ||
|> render(:console_reaped) | ||
end | ||
|
||
defp warning(%ConsoleInstance{second_notif_at: nil}), do: 1 | ||
defp warning(_), do: 2 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defimpl Email.Deliverable, for: Core.PubSub.ConsoleInstanceReaped do | ||
def email(%{item: inst}), do: Email.Builder.ConsoleReaped.email(inst) | ||
end |
22 changes: 22 additions & 0 deletions
22
apps/email/lib/email_web/templates/email/console_reaped.html.eex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="fit-content margin-horizontal-auto text-lead"> | ||
Your Plural Cloud instance <%= @inst.name %> is eligible to be deprovisioned. | ||
</div> | ||
<div class="fit-content margin-horizontal-auto text-content text-align-center margin-top-large"> | ||
You must have an active, paid plan to continue using Plural Cloud, please update your billing information and/or initiate a new subscription | ||
to continue using your instance | ||
</div> | ||
|
||
<%= if @warning == 2 do %> | ||
<div class="fit-content margin-horizontal-auto text-content text-align-center margin-top-large"> | ||
Since your instance has been unpaid for 1 week, you have <b>only one more week</b> before we reap your instance | ||
</div> | ||
<% else %> | ||
<div class="fit-content margin-horizontal-auto text-content text-align-center margin-top-large"> | ||
We provide a <b>two week grace period</b> for delinquent accounts to fix any billing issues. After that period, your instance will | ||
be scheduled to be reaped. | ||
</div> | ||
<% end %> | ||
|
||
<a class="button margin-top-large" href="<%= url("/account/billing") %>"> | ||
Go to Billing | ||
</a> |
11 changes: 11 additions & 0 deletions
11
apps/email/lib/email_web/templates/email/console_reaped.text.eex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Your Plural Cloud instance is eligible to be deprovisioned. | ||
|
||
You must have an active, paid plan to continue using Plural Cloud, please update your billing information and/or initiate a new subscription to continue using your instance. | ||
|
||
<%= if @warning == 2 do %> | ||
Since your instance has been unpaid for 1 week, you have *only one more week* before we reap your instance | ||
<% else %> | ||
We provide a *two week grace period* for delinquent accounts to fix any billing issues. After that period, your instance will be scheduled to be reaped. | ||
<% end %> | ||
|
||
Go to Billing here: <%= url("/account/billing") %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Email.Deliverable.CloudTest do | ||
use Core.SchemaCase, async: true | ||
use Bamboo.Test | ||
|
||
alias Core.PubSub | ||
alias Email.PubSub.Consumer | ||
|
||
describe "ConsoleInstanceReaped" do | ||
test "it can send a first warning" do | ||
owner = insert(:user, service_account: true) | ||
insert(:impersonation_policy_binding, | ||
policy: build(:impersonation_policy, user: owner), | ||
user: build(:user) | ||
) | ||
inst = insert(:console_instance, owner: owner, first_notif_at: Timex.now()) | ||
|
||
event = %PubSub.ConsoleInstanceReaped{item: inst} | ||
Consumer.handle_event(event) | ||
|
||
assert_delivered_email Email.Builder.ConsoleReaped.email(inst) | ||
end | ||
|
||
test "it can send a second warning email" do | ||
owner = insert(:user, service_account: true) | ||
insert(:impersonation_policy_binding, | ||
policy: build(:impersonation_policy, user: owner), | ||
user: build(:user) | ||
) | ||
inst = insert(:console_instance, owner: owner, second_notif_at: Timex.now()) | ||
|
||
event = %PubSub.ConsoleInstanceReaped{item: inst} | ||
Consumer.handle_event(event) | ||
|
||
assert_delivered_email Email.Builder.ConsoleReaped.email(inst) | ||
end | ||
end | ||
end |