Files
ubicloud/routes/project/object_info.rb
Jeremy Evans e78a7bb897 Allow using ubid without location in the cli
For example:

```
ubi vm vmdzyppz6j166jh5e9t2dwrfas show
```

This adds an object-info api route under project that returns the type,
location, and name of a supported object (currently the objects supported
by the api).  If there is no slash in the object reference in the cli,
and the object reference is a supported ubid format, then assume the
object reference is a ubid, and have the cli use the object-info endpoint
to get the location, and if a location is returned, it then uses that
location for the request.

Fix the error message for invalid references to not include an
underscore for the ubid, and to separate the location/name and ubid
formats to make it apparent to the user that location is not needed for
ubid (it is still accepted, but we don't need to document that).

Add LoadBalancer#display_location to avoid having different code for
LoadBalancer.
2025-02-26 09:23:01 -08:00

27 lines
843 B
Ruby

# frozen_string_literal: true
class Clover
type_ds_perm_map = {
"fw" => [:firewalls_dataset, "Firewall:view"],
"1b" => [:load_balancers_dataset, "LoadBalancer:view"],
"pg" => [:postgres_resources_dataset, "Postgres:view"],
"ps" => [:private_subnets_dataset, "PrivateSubnet:view"],
"vm" => [:vms_dataset, "Vm:view"]
}.freeze
type_ds_perm_map.each_value(&:freeze)
hash_branch(:project_prefix, "object-info") do |r|
r.get(api?, UbiCli::OBJECT_INFO_REGEXP) do |ubid, type|
ds_method, perm = type_ds_perm_map[type]
if (object = dataset_authorize(@project.send(ds_method), perm).first(id: UBID.to_uuid(ubid)))
{
"type" => object.class.table_name.to_s.tr("_", " "),
"location" => object.display_location,
"name" => object.name
}
end
end
end
end