Files
ubicloud/routes/helpers/vm.rb
Jeremy Evans 51b1a4184a Change Routes::Common::VmHelper#post to Clover#vm_post
This removes Routes::Common::VmHelper as it is no longer used.

This completes the merging of the project/location/vm and
project/vm routes.  I think the merged routes are significantly
simpler to understand.  In most cases, indirection is avoided.
For the list and post routes, there is still indirection to
a helper method, but the helper method has been simplied, and
no longer requires a context switch, since it runs in the
same context as the routing tree.
2024-11-05 10:47:00 -08:00

81 lines
3.3 KiB
Ruby

# frozen_string_literal: true
class Clover
def vm_list_dataset
@project.vms_dataset.authorized(current_account.id, "Vm:view")
end
def vm_list_api_response(dataset)
dataset = dataset.where(location: @location) if @location
result = dataset.paginated_result(
start_after: request.params["start_after"],
page_size: request.params["page_size"],
order_column: request.params["order_column"]
)
{
items: Serializers::Vm.serialize(result[:records]),
count: result[:count]
}
end
def vm_post(name)
project = @project
Authorization.authorize(current_account.id, "Vm:create", project.id)
fail Validation::ValidationFailed.new({billing_info: "Project doesn't have valid billing information"}) unless project.has_valid_payment_method?
required_parameters = ["public_key"]
required_parameters << "name" << "location" if web?
allowed_optional_parameters = ["size", "storage_size", "unix_user", "boot_image", "enable_ip4", "private_subnet_id"]
request_body_params = Validation.validate_request_body(json_params, required_parameters, allowed_optional_parameters)
assemble_params = request_body_params.slice(*allowed_optional_parameters).compact
# Generally parameter validation is handled in progs while creating resources.
# Since Vm::Nexus both handles VM creation requests from user and also Postgres
# service, moved the boot_image validation here to not allow users to pass
# postgres image as boot image while creating a VM.
if assemble_params["boot_image"]
Validation.validate_boot_image(assemble_params["boot_image"])
end
# Same as above, moved the size validation here to not allow users to
# pass gpu instance while creating a VM.
if assemble_params["size"]
parsed_size = Validation.validate_vm_size(assemble_params["size"], only_visible: true)
end
if assemble_params["storage_size"]
storage_size = Validation.validate_vm_storage_size(assemble_params["size"], assemble_params["storage_size"])
assemble_params["storage_volumes"] = [{size_gib: storage_size, encrypted: true}]
assemble_params.delete("storage_size")
end
if assemble_params["private_subnet_id"] && assemble_params["private_subnet_id"] != ""
ps = PrivateSubnet.from_ubid(assemble_params["private_subnet_id"])
if !ps || ps.location != @location
fail Validation::ValidationFailed.new({private_subnet_id: "Private subnet with the given id \"#{assemble_params["private_subnet_id"]}\" is not found in the location \"#{LocationNameConverter.to_display_name(@location)}\""})
end
Authorization.authorize(current_account.id, "PrivateSubnet:view", ps.id)
end
assemble_params["private_subnet_id"] = ps&.id
requested_vm_core_count = parsed_size.nil? ? 1 : parsed_size.vcpu / 2
Validation.validate_core_quota(project, "VmCores", requested_vm_core_count)
st = Prog::Vm::Nexus.assemble(
request_body_params["public_key"],
project.id,
name: name,
location: @location,
**assemble_params.transform_keys(&:to_sym)
)
if api?
Serializers::Vm.serialize(st.subject, {detailed: true})
else
flash["notice"] = "'#{name}' will be ready in a few minutes"
request.redirect "#{project.path}#{st.subject.path}"
end
end
end