Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions app/controllers/api/v2/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions config/initializers/apipie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading