ubicloud/helpers/ssh_public_key.rb
Jeremy Evans 07985a75a8 Add support for managing SSH public keys via the api
No direct specs for this, it will be tested via cli specs in a
coming commit.
2025-10-04 01:36:33 +09:00

31 lines
969 B
Ruby

# frozen_string_literal: true
class Clover
def ssh_public_key_save
create = @ssh_public_key.new?
handle_validation_failure("ssh-public-key/register") do
flash.now["error"] = "Error #{create ? "registering" : "updating"} SSH public key"
end
name, public_key = typecast_params.nonempty_str(%w[name public_key])
if create || web?
@ssh_public_key.name = name
@ssh_public_key.public_key = public_key
else
@ssh_public_key.name = name if name
@ssh_public_key.public_key = public_key if public_key
end
DB.transaction do
@ssh_public_key.save_changes
audit_log(@ssh_public_key, create ? "create" : "update")
end
if api?
Serializers::SshPublicKey.serialize(@ssh_public_key, detailed: true)
else
flash["notice"] = "SSH public key with name #{@ssh_public_key.name} #{create ? "registered" : "updated"}"
request.redirect "#{@project.path}/ssh-public-key"
end
end
end