From cf27213285b00a900246d6d859eab9fe31bc3088 Mon Sep 17 00:00:00 2001 From: Varik Matevosyan Date: Sun, 5 May 2024 12:16:31 -0700 Subject: [PATCH] Add labels to lantern_resources (#21) * add labels to lantern resources * add labels to lantern resource in api as well --- migrate/20240505_lantern_resource_label.rb | 9 +++++++++ prog/lantern/lantern_resource_nexus.rb | 6 ++++-- routes/api/project/lantern.rb | 1 + routes/web/project/lantern.rb | 1 + serializers/api/lantern.rb | 1 + serializers/web/lantern.rb | 1 + spec/routes/api/project/lantern_spec.rb | 3 ++- spec/routes/web/project/lantern_spec.rb | 23 +++++++++++++++++++++- views/lantern/create.erb | 13 ++++++++++++ views/lantern/index.erb | 2 +- views/lantern/show.erb | 1 + 11 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 migrate/20240505_lantern_resource_label.rb diff --git a/migrate/20240505_lantern_resource_label.rb b/migrate/20240505_lantern_resource_label.rb new file mode 100644 index 000000000..bc63362bc --- /dev/null +++ b/migrate/20240505_lantern_resource_label.rb @@ -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 diff --git a/prog/lantern/lantern_resource_nexus.rb b/prog/lantern/lantern_resource_nexus.rb index 733384138..328356d4e 100644 --- a/prog/lantern/lantern_resource_nexus.rb +++ b/prog/lantern/lantern_resource_nexus.rb @@ -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 @@ -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) diff --git a/routes/api/project/lantern.rb b/routes/api/project/lantern.rb index 451e81f60..cdeeed08c 100644 --- a/routes/api/project/lantern.rb +++ b/routes/api/project/lantern.rb @@ -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, diff --git a/routes/web/project/lantern.rb b/routes/web/project/lantern.rb index 83a0a6b08..f5622ec8f 100644 --- a/routes/web/project/lantern.rb +++ b/routes/web/project/lantern.rb @@ -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, diff --git a/serializers/api/lantern.rb b/serializers/api/lantern.rb index c3afa9501..60cd8d24b 100644 --- a/serializers/api/lantern.rb +++ b/serializers/api/lantern.rb @@ -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, diff --git a/serializers/web/lantern.rb b/serializers/web/lantern.rb index 533552e01..23655aad0 100644 --- a/serializers/web/lantern.rb +++ b/serializers/web/lantern.rb @@ -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, diff --git a/spec/routes/api/project/lantern_spec.rb b/spec/routes/api/project/lantern_spec.rb index b06b1b604..a40b9c71f 100644 --- a/spec/routes/api/project/lantern_spec.rb +++ b/spec/routes/api/project/lantern_spec.rb @@ -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") diff --git a/spec/routes/web/project/lantern_spec.rb b/spec/routes/web/project/lantern_spec.rb index 3130e058e..fd6411e65 100644 --- a/spec/routes/web/project/lantern_spec.rb +++ b/spec/routes/web/project/lantern_spec.rb @@ -69,6 +69,7 @@ location: "us-central1", name: "instance-2", target_vm_size: "n1-standard-2", + label: "test", target_storage_size_gib: 100, org_id: 0 ) @@ -76,7 +77,25 @@ 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 @@ -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 @@ -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 diff --git a/views/lantern/create.erb b/views/lantern/create.erb index 42517c80d..36c44aae2 100644 --- a/views/lantern/create.erb +++ b/views/lantern/create.erb @@ -42,6 +42,19 @@ } ) %> +
+ <%== render( + "components/form/text", + locals: { + name: "label", + label: "Label", + attributes: { + required: false, + placeholder: "Enter label" + } + } + ) %> +
<%== render( "components/form/text", diff --git a/views/lantern/index.erb b/views/lantern/index.erb index 1b5bcaa0e..bc4297037 100644 --- a/views/lantern/index.erb +++ b/views/lantern/index.erb @@ -37,7 +37,7 @@ <% @lantern_databases.each do |pg| %> - <%= pg[:name] %> + <%= pg[:name] %> (<%= (!pg[:label] || pg[:label].empty?) ? "no-label" : pg[:label] %>) <%= pg[:location] %> <%== render("components/pg_state_label", locals: { state: pg[:state] }) %> diff --git a/views/lantern/show.erb b/views/lantern/show.erb index 533df3a57..ebe1a1168 100644 --- a/views/lantern/show.erb +++ b/views/lantern/show.erb @@ -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]],