Skip to content

Commit

Permalink
Refs #24489 - Report templates - inputs, clone, interactive updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mbacovsky authored and Tomas Strachota committed Nov 6, 2018
1 parent b8f5f16 commit ebc7bd6
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 24 deletions.
40 changes: 23 additions & 17 deletions lib/hammer_cli_foreman/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,31 +531,37 @@ def request_options
{ :response => :raw }
end

def validate_options
super
validator.option(:option_path).required
option "--path", "PATH", _("Path to directory where downloaded content will be saved"),
:attribute_name => :option_path

def execute
response = send_request
if option_path
filepath = store_response(response)
print_message(_('The response has been saved to %{path}s.'), {:path => filepath})
else
puts response.body
end
return HammerCLI::EX_OK
end

def success_message
super || _('The report has been saved to %s.') % @filepath
def default_filename
"Downloaded-#{Time.new.strftime("%Y-%m-%d")}.txt"
end

option "--path", "PATH", _("Path to directory where downloaded file will be saved"),
:attribute_name => :option_path
private

def print_data(response)
if response.body.empty?
print_message "Response success but empty body was returned - file was not saved"
return
def store_response(response)
if response.headers.key?(:content_disposition)
suggested_filename = response.headers[:content_disposition].match(/filename="(.*)"/)
end
# get file name from header, remove leading and trailing quotes
filename = response.headers[:content_disposition].match(/filename=(.*)/)[1].chop.reverse.chop.reverse
filename = suggested_filename ? suggested_filename[1] : default_filename
path = option_path.dup
path << '/' unless path.end_with? '/'
raise "Cannot save file: #{path} does not exist" unless File.directory?(path)
@filepath = path + filename
File.write(@filepath, response.body)
print_success_message(response)
raise _("Cannot save file: %s does not exist") % path unless File.directory?(path)
filepath = path + filename
File.write(filepath, response.body)
filepath
end
end
end
93 changes: 86 additions & 7 deletions lib/hammer_cli_foreman/report_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ReportTemplate < HammerCLIForeman::Command
resource :report_templates

class ListCommand < HammerCLIForeman::ListCommand

output do
field :id, _("Id")
field :name, _("Name")
Expand All @@ -13,17 +12,38 @@ class ListCommand < HammerCLIForeman::ListCommand
build_options
end


class InfoCommand < HammerCLIForeman::InfoCommand
output ListCommand.output_definition do
field :locked, _("Locked"), Fields::Boolean
field :default, _("Default"), Fields::Boolean
HammerCLIForeman::References.timestamps(self)
HammerCLIForeman::References.taxonomies(self)
collection :inputs, _("Template inputs") do
field :id, _("Id"), Fields::Id
field :name, _('Name')
field :description, _('Description')
field :required, _("Required"), Fields::Boolean
field :options, _('Options'), Fields::List, :hide_blank => true
end
end

def extend_data(template)
template[:inputs] = load_inputs
template
end

build_options
end

private

def load_inputs
template_inputs_api = HammerCLIForeman.foreman_api.resource(:template_inputs)
params = {:template_id => options['option_id']}
params[:organization_id] = options['option_organization_id'] if options['option_organization_id']
params[:location_id] = options['option_location_id'] if options['option_locations_id']
HammerCLIForeman.collection_to_common_format(template_inputs_api.call(:index, params))
end
end

class DumpCommand < HammerCLIForeman::InfoCommand
command_name "dump"
Expand All @@ -36,20 +56,45 @@ def print_data(report_template)
build_options
end


class GenerateCommand < HammerCLIForeman::DownloadCommand
command_name "generate"
action :generate
desc _("Generate report")

option '--inputs', 'INPUTS', N_('Specify inputs'),
:format => HammerCLI::Options::Normalizers::KeyValueList.new

def default_filename
"Report-#{Time.new.strftime("%Y-%m-%d")}.txt"
end

def request_params
params = super
params['input_values'] = option_inputs || {}
params
end

build_options
end


class CreateCommand < HammerCLIForeman::CreateCommand
option ['--interactive', '-i'], :flag, _('Open empty template in an $EDITOR. Upload the result')
option "--file", "LAYOUT", _("Path to a file that contains the report template content"),
:attribute_name => :option_template, :format => HammerCLI::Options::Normalizers::File.new

option "--file", "LAYOUT", _("Path to a file that contains the report template content"), :attribute_name => :option_template,
:required => true, :format => HammerCLI::Options::Normalizers::File.new
validate_options do
any(:option_interactive, :option_template).required
end

def request_params
params = super
if option_interactive?
params['report_template']['template'] = HammerCLI.open_in_editor(
'', content_type: 'report_template', suffix: '.erb')
end
params
end

success_message _("Report template created.")
failure_message _("Could not create the report template")
Expand All @@ -59,15 +104,49 @@ class CreateCommand < HammerCLIForeman::CreateCommand


class UpdateCommand < HammerCLIForeman::UpdateCommand
option "--file", "LAYOUT", _("Path to a file that contains the report template content"), :attribute_name => :option_template,
option ['--interactive', '-i'], :flag, _('Dump existing template and open it in an $EDITOR. Update with the result')
option '--file', 'REPORT', _("Path to a file that contains the report template content"), :attribute_name => :option_template,
:format => HammerCLI::Options::Normalizers::File.new

def request_params
params = super
if option_interactive?
template = load_template
params['report_template']['template'] = HammerCLI.open_in_editor(
template['template'], content_type: 'report_template', suffix: '.erb')
end
params
end

success_message _("Report template updated.")
failure_message _("Could not update the report template")

build_options :without => [:template]

private

def load_template
template_api = HammerCLIForeman.foreman_api.resource(:report_templates)
params = {:id => options['option_id']}
params[:organization_id] = options['option_organization_id'] if options['option_organization_id']
params[:location_id] = options['option_location_id'] if options['option_locations_id']
HammerCLIForeman.record_to_common_format(template_api.call(:show, params))
end
end

class CloneCommand < HammerCLIForeman::UpdateCommand
action :clone
command_name 'clone'

success_message _('Report template cloned.')
failure_message _('Could not clone the report template')

validate_options do
option(:option_new_name).required
end

build_options
end

class DeleteCommand < HammerCLIForeman::DeleteCommand
success_message _("Report template deleted.")
Expand Down
Loading

0 comments on commit ebc7bd6

Please sign in to comment.