Skip to content

Commit

Permalink
Add labels to lantern_resources (#21)
Browse files Browse the repository at this point in the history
* add labels to lantern resources

* add labels to lantern resource in api as well
  • Loading branch information
var77 authored May 5, 2024
1 parent fa00159 commit cf27213
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 5 deletions.
9 changes: 9 additions & 0 deletions migrate/20240505_lantern_resource_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Sequel.migration do
change do
alter_table(:lantern_resource) do
add_column :label, :text, null: true
end
end
end
6 changes: 4 additions & 2 deletions prog/lantern/lantern_resource_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Prog::Lantern::LanternResourceNexus < Prog::Base

def self.assemble(project_id:, location:, name:, target_vm_size:, target_storage_size_gib:, ha_type: LanternResource::HaType::NONE, parent_id: nil, restore_target: nil,
org_id: nil, db_name: "postgres", db_user: "postgres", db_user_password: nil, superuser_password: nil, repl_password: nil, app_env: Config.rack_env,
lantern_version: Config.lantern_default_version, extras_version: Config.lantern_extras_default_version, minor_version: Config.lantern_minor_default_version, domain: nil, enable_debug: false)
lantern_version: Config.lantern_default_version, extras_version: Config.lantern_extras_default_version, minor_version: Config.lantern_minor_default_version, domain: nil, enable_debug: false,
label: "")
unless (project = Project[project_id])
fail "No existing project"
end
Expand Down Expand Up @@ -77,7 +78,8 @@ def self.assemble(project_id:, location:, name:, target_vm_size:, target_storage
project_id: project_id, location: location, name: name, org_id: org_id, app_env: app_env,
superuser_password: superuser_password, ha_type: ha_type, parent_id: parent_id,
restore_target: restore_target, db_name: db_name, db_user: db_user,
db_user_password: db_user_password, repl_user: repl_user, repl_password: repl_password
db_user_password: db_user_password, repl_user: repl_user, repl_password: repl_password,
label: label
) { _1.id = ubid.to_uuid }
lantern_resource.associate_with_project(project)

Expand Down
1 change: 1 addition & 0 deletions routes/api/project/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CloverApi
project_id: @project.id,
location: r.params["location"],
name: r.params["name"],
label: r.params["label"],
org_id: r.params["org_id"].to_i,
target_vm_size: parsed_size.vm_size,
target_storage_size_gib: r.params["storage_size_gib"] || parsed_size.storage_size_gib,
Expand Down
1 change: 1 addition & 0 deletions routes/web/project/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CloverWeb
project_id: @project.id,
location: r.params["location"],
name: r.params["name"],
label: r.params["label"],
org_id: r.params["org_id"],
target_vm_size: parsed_size.vm_size,
target_storage_size_gib: parsed_size.storage_size_gib,
Expand Down
1 change: 1 addition & 0 deletions serializers/api/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.base(pg)
parent_id: pg.parent_id,
path: pg.path,
name: pg.name,
label: pg.label,
state: pg.display_state,
instance_type: pg.representative_server&.instance_type,
location: pg.location,
Expand Down
1 change: 1 addition & 0 deletions serializers/web/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.base(pg)
ubid: pg.ubid,
path: pg.path,
name: pg.name,
label: pg.label,
state: pg.display_state,
primary?: pg.representative_server&.primary?,
instance_type: pg.representative_server&.instance_type,
Expand Down
3 changes: 2 additions & 1 deletion spec/routes/api/project/lantern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@

describe "create" do
it "creates new lantern database" do
post "/api/project/#{project.ubid}/lantern", {size: "n1-standard-2", name: "instance-2", org_id: 0, location: "us-central1", storage_size_gib: 100, lantern_version: "0.2.2", extras_version: "0.1.4", minor_version: "1", domain: "test.db.lantern.dev", app_env: "test", repl_password: "test-repl-pass", enable_telemetry: true, postgres_password: "test-pg-pass"}
post "/api/project/#{project.ubid}/lantern", {size: "n1-standard-2", name: "instance-2", label: "test-label", org_id: 0, location: "us-central1", storage_size_gib: 100, lantern_version: "0.2.2", extras_version: "0.1.4", minor_version: "1", domain: "test.db.lantern.dev", app_env: "test", repl_password: "test-repl-pass", enable_telemetry: true, postgres_password: "test-pg-pass"}

body = JSON.parse(last_response.body)
expect(last_response.status).to eq(200)

expect(body["name"]).to eq("instance-2")
expect(body["label"]).to eq("test-label")
expect(body["state"]).to eq("creating")
expect(body["instance_type"]).to eq("writer")
expect(body["location"]).to eq("us-central1")
Expand Down
23 changes: 22 additions & 1 deletion spec/routes/web/project/lantern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,33 @@
location: "us-central1",
name: "instance-2",
target_vm_size: "n1-standard-2",
label: "test",
target_storage_size_gib: 100,
org_id: 0
)

visit "#{project.path}/lantern"

expect(page.title).to eq("Ubicloud - Lantern Databases")
expect(page).to have_content "instance-2"
expect(page).to have_content "instance-2 (test)"
end

it "show no-label if no label was specified" do
Prog::Lantern::LanternResourceNexus.assemble(
project_id: project.id,
location: "us-central1",
name: "instance-4",
target_vm_size: "n1-standard-2",
target_storage_size_gib: 100,
org_id: 0
)

LanternResource[name: "instance-4"].update(label: nil)

visit "#{project.path}/lantern"

expect(page.title).to eq("Ubicloud - Lantern Databases")
expect(page).to have_content "instance-4 (no-label)"
end
end

Expand All @@ -87,6 +106,7 @@
expect(page.title).to eq("Ubicloud - Create Lantern Database")
name = "new-pg-db"
fill_in "Name", with: name
fill_in "Label", with: "test-label"
choose option: "us-central1"
choose option: "n1-standard-2"
find_by_id("parent_id").find(:xpath, "option[1]").select_option
Expand All @@ -97,6 +117,7 @@
expect(page).to have_content "'#{name}' will be ready in a few minutes"
expect(LanternResource.count).to eq(1)
expect(LanternResource.first.projects.first.id).to eq(project.id)
expect(LanternResource.first.label).to eq("test-label")
end

it "can create new Lantern database with domain" do
Expand Down
13 changes: 13 additions & 0 deletions views/lantern/create.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
}
) %>
</div>
<div class="sm:col-span-3">
<%== render(
"components/form/text",
locals: {
name: "label",
label: "Label",
attributes: {
required: false,
placeholder: "Enter label"
}
}
) %>
</div>
<div class="sm:col-span-3">
<%== render(
"components/form/text",
Expand Down
2 changes: 1 addition & 1 deletion views/lantern/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<tbody class="divide-y divide-gray-200 bg-white">
<% @lantern_databases.each do |pg| %>
<tr>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6" scope="row"><%= pg[:name] %></td>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6" scope="row"><%= pg[:name] %> (<%= (!pg[:label] || pg[:label].empty?) ? "no-label" : pg[:label] %>)</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= pg[:location] %></td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<%== render("components/pg_state_label", locals: { state: pg[:state] }) %>
Expand Down
1 change: 1 addition & 0 deletions views/lantern/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<% data = [
["ID", @pg[:ubid]],
["Name", @pg[:name]],
["Label", @pg[:label]],
["Org Id", @pg[:org_id]],
["Instance Type", @pg[:instance_type]],
["Lantern Version", @pg[:lantern_version]],
Expand Down

0 comments on commit cf27213

Please sign in to comment.