Files
ubicloud/cli-commands/lb/post/create.rb
Jeremy Evans 78f45d392f Improve error output for invalid CLI option arguments
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
2025-07-02 05:02:05 +09:00

34 lines
1.5 KiB
Ruby

# frozen_string_literal: true
UbiCli.on("lb").run_on("create") do
desc "Create a load balancer"
algorithms = %w[round_robin hash_based].freeze.each(&:freeze)
health_check_protocols = %w[http https tcp].freeze.each(&:freeze)
stacks = %w[dual ipv4 ipv6].freeze.each(&:freeze)
options("ubi lb location/lb-name create [options] ps-id src-port dst-port", key: :lb_create) do
on("-a", "--algorithm=alg", algorithms, "set the algorithm to use")
on("-e", "--check-endpoint=path", "set the health check endpoint (default: #{Prog::Vnet::LoadBalancerNexus::DEFAULT_HEALTH_CHECK_ENDPOINT})")
on("-p", "--check-protocol=proto", health_check_protocols, "set the health check protocol")
on("-s", "--stack=stack", stacks, "set the stack")
end
help_option_values("Algorithm:", algorithms)
help_option_values("Health Check Protocol:", health_check_protocols)
help_option_values("Stack:", stacks)
args 3
run do |private_subnet_id, src_port, dst_port, opts, cmd|
params = underscore_keys(opts[:lb_create])
if (endpoint = params.delete(:check_endpoint))
params[:health_check_endpoint] = endpoint
end
params[:health_check_protocol] = params.delete(:check_protocol)
params[:private_subnet_id] = private_subnet_id
params[:src_port] = need_integer_arg(src_port, "src-port", cmd)
params[:dst_port] = need_integer_arg(dst_port, "dst-port", cmd)
id = sdk.load_balancer.create(location: @location, name: @name, **params).id
response("Load balancer created with id: #{id}")
end
end