Skip to content

Commit

Permalink
Add dissociate fork functionality for lantern resource
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed May 2, 2024
1 parent 9f2edc5 commit 4c212d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
8 changes: 8 additions & 0 deletions model/lantern/lantern_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class LanternResource < Sequel::Model
one_to_one :strand, key: :id
many_to_one :project
many_to_one :parent, key: :parent_id, class: self
one_to_many :forks, key: :parent_id, class: self
one_to_many :servers, class: LanternServer, key: :resource_id
one_to_one :representative_server, class: LanternServer, key: :resource_id, conditions: Sequel.~(representative_at: nil)
one_through_one :timeline, class: LanternTimeline, join_table: :lantern_server, left_key: :resource_id, right_key: :timeline_id
Expand Down Expand Up @@ -51,6 +52,13 @@ def required_standby_count
required_standby_count_map[ha_type]
end

def dissociate_forks
forks.each {
_1.update(parent_id: nil)
_1.timeline.update(parent_id: nil)
}
end

module HaType
NONE = "none"
ASYNC = "async"
Expand Down
17 changes: 13 additions & 4 deletions routes/api/project/location/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class CloverApi

r.delete true do
Authorization.authorize(@current_user.id, "Postgres:delete", pg.id)
if pg.forks.size > 0
response.status = 409
return {"error" => "Can not delete resource which has active forks"}
end
pg.incr_destroy
response.status = 200
r.halt
Expand Down Expand Up @@ -136,12 +140,17 @@ class CloverApi
r.halt
rescue => e
Clog.emit("Error while pushing backup") { {error: e} }
if e.message.include? "Another backup"
response.status = 409
response.write e.message
response.status = if e.message.include? "Another backup"
409
else
response.status = 400
400
end
return {"error" => e.message}
end

r.post "dissociate-forks" do
pg.dissociate_forks
response.status = 200
r.halt
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/model/lantern/lantern_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@
expect(lantern_resource.required_standby_count).to eq(2)
end
end

describe "#dissociate_forks" do
it "removes parents from forks" do
child = instance_double(described_class, parent_id: lantern_resource.id, timeline: instance_double(LanternTimeline, parent_id: "test"))
forks = [child]
expect(lantern_resource).to receive(:forks).and_return(forks)
expect(child).to receive(:update).with(parent_id: nil)
expect(child.timeline).to receive(:update).with(parent_id: nil)

expect { lantern_resource.dissociate_forks }.not_to raise_error
end
end
end
26 changes: 25 additions & 1 deletion spec/routes/api/project/location/lantern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
end

describe "delete" do
it "does not delete instance if has forks" do
expect(Project).to receive(:from_ubid).and_return(project).at_least(:once)
query_res = class_double(LanternResource, first: pg)
allow(query_res).to receive(:where).and_return(query_res)
expect(project).to receive(:lantern_resources_dataset).and_return(query_res)
expect(pg).to receive(:forks).and_return([instance_double(LanternResource)])

delete "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1"
expect(last_response.status).to eq(409)
expect(JSON.parse(last_response.body)).to eq({"error" => "Can not delete resource which has active forks"})
end

it "deletes instance" do
delete "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1"
expect(last_response.status).to eq(200)
Expand Down Expand Up @@ -282,7 +294,7 @@

post "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1/push-backup"
expect(last_response.status).to eq(409)
expect(last_response.body).to eq("Another backup is in progress please try again later")
expect(JSON.parse(last_response.body)).to eq({"error" => "Another backup is in progress please try again later"})
end

it "fails to create new backup with unknown" do
Expand All @@ -296,5 +308,17 @@
expect(last_response.status).to eq(400)
end
end

describe "dissociate-forks" do
it "dissociate-forkses" do
expect(Project).to receive(:from_ubid).and_return(project).at_least(:once)
query_res = class_double(LanternResource, first: pg)
allow(query_res).to receive(:where).and_return(query_res)
expect(project).to receive(:lantern_resources_dataset).and_return(query_res)
expect(pg).to receive(:dissociate_forks)
post "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1/dissociate-forks"
expect(last_response.status).to eq(200)
end
end
end
end

0 comments on commit 4c212d9

Please sign in to comment.