diff --git a/app/controllers/api/v2/hosts_controller.rb b/app/controllers/api/v2/hosts_controller.rb index fea24492326..f4e3279e86c 100644 --- a/app/controllers/api/v2/hosts_controller.rb +++ b/app/controllers/api/v2/hosts_controller.rb @@ -181,14 +181,7 @@ def enc api :GET, "/hosts/:id/status/:type", N_("Get status of host") param :id, :identifier_dottable, :required => true - param :type, [HostStatus::Global] + HostStatus.status_registry.to_a.map { |s| s.humanized_name }, :required => true, :desc => N_( - <<~EOS - status type, can be one of - * global - * configuration - * build - EOS - ) + param :type, :callable_enum, of: -> { [HostStatus::Global.status_name.underscore] + HostStatus.status_registry.to_a.map { |s| s.humanized_name } }, required: true description N_('Returns string representing a host status of a given type') def get_status @@ -203,11 +196,7 @@ def get_status api :DELETE, "/hosts/:id/status/:type", N_("Clear sub-status of host") param :id, :identifier_dottable, :required => true - param :type, HostStatus.status_registry.to_a.map { |s| s.humanized_name }, :required => true, :desc => N_( - <<~EOS - status type - EOS - ) + param :type, :callable_enum, of: -> { HostStatus.status_registry.to_a.map { |s| s.humanized_name } }, required: true description N_('Clears a host sub-status of a given type') def forget_status diff --git a/config/initializers/apipie.rb b/config/initializers/apipie.rb index 7d0f852fef7..2a6d8b30ba4 100644 --- a/config/initializers/apipie.rb +++ b/config/initializers/apipie.rb @@ -173,3 +173,30 @@ def expected_type :any_type end end + +class CallableEnumValidator < Apipie::Validator::BaseValidator + def initialize(param_description, block) + super(param_description) + @block = block + end + + def self.build(param_description, argument, options, block) + if argument == :callable_enum + raise ArgumentError, "options[:of] must be provided and must be a proc/lambda" unless options[:of].is_a?(Proc) + new(param_description, options[:of]) + end + end + + def values + @block&.call || [] + end + + def validate(value) + values.include?(value) + end + + def description + enum = values.map { |value| format_description_value(value) }.join(', ') + "Must be one of: #{enum}." + end +end