For option arguments that must be members of an array, do the check in the option parsing by providing an array of allowed option arguments (e.g. vm create -s bad). Emit better errors for options arguments that should be integers (e.g. fw add-rule -s/-e), and for regular arguments that should be integers (e.g. lb create src-port/dst-port). Include help output in additional cases: * invalid id formats with slashes * invalid/duplicate/missing fields used when multiple fields separated by a comma is allowed * invalid object references when using post subcommands * invalid location for list -l option
46 lines
1.7 KiB
Ruby
46 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
UbiCli.on("vm").run_on("create") do
|
|
desc "Create a virtual machine"
|
|
|
|
vm_sizes = Option::VmSizes.select(&:visible)
|
|
server_sizes = vm_sizes.map(&:name).uniq.freeze
|
|
storage_sizes = vm_sizes.map(&:storage_size_options).flatten.uniq.sort.map(&:to_s).freeze.each(&:freeze)
|
|
|
|
options("ubi vm location/vm-name create [options] public_key", key: :vm_create) do
|
|
on("-6", "--ipv6-only", "do not enable IPv4")
|
|
on("-b", "--boot-image=image_name", Option::BootImages.map(&:name), "boot image")
|
|
on("-p", "--private-subnet-id=id", "place VM into specific private subnet")
|
|
on("-s", "--size=size", server_sizes, "server size")
|
|
on("-S", "--storage-size=size", storage_sizes, "storage size")
|
|
on("-u", "--unix-user=username", "username (default: ubi)")
|
|
end
|
|
help_option_values("Boot Image:", Option::BootImages.map(&:name))
|
|
help_option_values("Size:", server_sizes)
|
|
help_option_values("Storage Size:", storage_sizes)
|
|
|
|
help_example 'ubi vm eu-central-h1/my-vm-name create "$(cat ~/.ssh/id_ed25519.pub)"'
|
|
help_example 'ubi vm eu-central-h1/my-vm-name create "$(cat ~/.ssh/authorized_keys)"'
|
|
|
|
args 1
|
|
|
|
run do |public_key, opts, command|
|
|
params = underscore_keys(opts[:vm_create])
|
|
unless params.delete(:ipv6_only)
|
|
params[:enable_ip4] = "1"
|
|
end
|
|
|
|
unless Vm::VALID_SSH_AUTHORIZED_KEYS.match?(public_key)
|
|
command.raise_failure("public key provided is not in authorized_keys format")
|
|
end
|
|
|
|
unless Vm::VALID_SSH_PUBLIC_KEY_LINE.match?(public_key)
|
|
command.raise_failure("public key provided does not contain a valid public key")
|
|
end
|
|
|
|
params[:public_key] = public_key
|
|
id = sdk.vm.create(location: @location, name: @name, **params).id
|
|
response("VM created with id: #{id}")
|
|
end
|
|
end
|