Files
ubicloud/routes/project/location/private_subnet.rb
Jeremy Evans 64f9d09f65 Use normal HTML form submission for delete button with POST method
It looks like the private subnet disconnect button is the only button
we have that does this. There doesn't appear to be a need to use
an ajax submission in this case.  However, javascript is still needed
for the confirmation.

Change the javascript to use a normal HTTP form submission if the
method is POST, instead of using an ajax submission. This requires
wrapping the button in an HTML form, so do that in the delete button
template.

Change the private subnet disconnect route to redirect after
successful disconnect, instead of returning 204. Also, simplify the
error handling in the disconnect route, and use
handle_validation_failure.

This allows the specs to use normal Capybara methods for clicking the
button, instead of dropping down to rack-test methods.

Use Disconnect as the label for the disconnect button. Previously, this
just showed a trash icon, which was less accessible to non-visual users,
and even for visual users, odd, because the trash icon is for
deleting objects, and this does a disconnect and not a delete.
2025-08-08 01:52:14 +09:00

103 lines
3.0 KiB
Ruby

# frozen_string_literal: true
class Clover
hash_branch(:project_location_prefix, "private-subnet") do |r|
r.get api? do
private_subnet_list
end
r.on PRIVATE_SUBNET_NAME_OR_UBID do |ps_name, ps_id|
if ps_name
r.post api? do
check_visible_location
private_subnet_post(ps_name)
end
filter = {Sequel[:private_subnet][:name] => ps_name}
else
filter = {Sequel[:private_subnet][:id] => UBID.to_uuid(ps_id)}
end
filter[:location_id] = @location.id
ps = @ps = @project.private_subnets_dataset.first(filter)
check_found_object(ps)
r.post "connect" do
authorize("PrivateSubnet:connect", ps.id)
handle_validation_failure("networking/private_subnet/show")
unless (subnet = authorized_private_subnet(key: "connected-subnet-id", perm: "PrivateSubnet:connect"))
raise CloverError.new(400, "InvalidRequest", "Subnet to be connected not found")
end
DB.transaction do
ps.connect_subnet(subnet)
audit_log(ps, "connect", subnet)
end
if api?
Serializers::PrivateSubnet.serialize(ps)
else
flash["notice"] = "#{subnet.name} will be connected in a few seconds"
r.redirect "#{@project.path}#{ps.path}"
end
end
r.post "disconnect", :ubid_uuid do |id|
authorize("PrivateSubnet:disconnect", ps.id)
handle_validation_failure("networking/private_subnet/show")
unless (subnet = authorized_private_subnet(id:, perm: "PrivateSubnet:disconnect"))
raise CloverError.new(400, "InvalidRequest", "Subnet to be disconnected not found")
end
DB.transaction do
ps.disconnect_subnet(subnet)
audit_log(ps, "disconnect", subnet)
end
if api?
Serializers::PrivateSubnet.serialize(ps)
else
flash["notice"] = "#{subnet.name} will be disconnected in a few seconds"
r.redirect "#{@project.path}#{ps.path}"
end
end
r.is do
r.get do
authorize("PrivateSubnet:view", ps.id)
if api?
Serializers::PrivateSubnet.serialize(ps)
else
view "networking/private_subnet/show"
end
end
r.delete do
authorize("PrivateSubnet:delete", ps.id)
vms_dataset = ps.vms_dataset
.association_join(:strand)
.exclude(label: "destroy")
.exclude(Sequel[:vm][:id] => Semaphore
.where(
strand_id: ps.nics_dataset.select(:vm_id),
name: "destroy"
)
.select(:strand_id))
unless vms_dataset.empty?
fail DependencyError.new("Private subnet '#{ps.name}' has VMs attached, first, delete them.")
end
DB.transaction do
ps.incr_destroy
audit_log(ps, "destroy")
end
204
end
end
end
end
end