-
Notifications
You must be signed in to change notification settings - Fork 900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export by safer element's filename or id #19702
Changes from 14 commits
5bc8971
4a74832
9ce816e
cfb27b1
c63b4d9
5657291
b0cf105
4b0afa0
5c9d252
51e6c33
28bd713
f4a5c1c
86c4b0e
a3c32b1
cb01e28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,51 @@ | ||
module TaskHelpers | ||
class Exports | ||
def self.safe_filename(filename, keep_spaces = false) | ||
new_filename = keep_spaces ? filename : filename.gsub(%r{[ ]}, '_') | ||
new_filename.gsub(%r{[|/]}, '/' => 'slash', '|' => 'pipe') | ||
def self.get_tmp_filename(my_object) | ||
# Description: This method is used to generate a legacy, error-prone filename based on selected string fields. | ||
# There is no handling of CustomButtons because safe_filename is not called. | ||
# Build Arrays for plain classes where filenames are either set by name or description | ||
description_types = [MiqAlert, MiqAlertSet, MiqPolicy, MiqPolicySet, Classification, MiqWidget] | ||
names_types = [GenericObjectDefinition, MiqReport, ScanItemSet, MiqSchedule] | ||
tmp_filename = '' | ||
if names_types.include?(my_object.class) | ||
tmp_filename = my_object.name | ||
elsif description_types.include?(my_object.class) | ||
tmp_filename = my_object.description | ||
# Handle specifically crafted Hashes | ||
elsif my_object.class == Hash | ||
# CustomizationTemplate Hash | ||
if my_object[:class].include?("CustomizationTemplate") | ||
image_type_name = my_object.fetch_path(:pxe_image_type, :name) || "Examples" | ||
tmp_filename = "#{image_type_name}-#{object[:name]}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
# Provisioning Dialog Hash | ||
elsif my_object[:class].include?("MiqDialog") | ||
tmp_filename = "#{my_object[:dialog_type]}-#{my_object[:name]}" | ||
# Role Hash | ||
elsif my_object[:class].include?("MiqUserRole") | ||
tmp_filename = role_hash[:name] | ||
# Service Dialog Hash | ||
elsif my_object[:class].include?("Dialog") | ||
tmp_filename = my_object[:label] | ||
end | ||
end | ||
tmp_filename | ||
end | ||
|
||
def self.safe_filename(my_object, keep_spaces = false, super_safe_filename = false) | ||
# Description: generate a safe filename either by some string fields or by object id's. | ||
# We expect parameter object to be a crafted hash or a miq class suitable for export. | ||
|
||
new_filename = '' | ||
# Generate filename by id. Hashes and Miq classes must be handled. | ||
if super_safe_filename | ||
new_filename = my_object.class == Hash ? my_object[:id] : my_object.id | ||
# Generate filename by not so safe strings. | ||
else | ||
tmp_filename = Exports.get_tmp_filename(my_object) | ||
new_filename = keep_spaces ? tmp_filename : tmp_filename.gsub(%r{[ ]}, '_') | ||
new_filename.gsub(%r{[|/]}, '/' => 'slash', '|' => 'pipe') | ||
end | ||
new_filename | ||
end | ||
|
||
def self.parse_options | ||
|
@@ -11,6 +54,7 @@ def self.parse_options | |
opt :keep_spaces, 'Keep spaces in filenames', :type => :boolean, :short => 's', :default => false | ||
opt :directory, 'Directory to place exported files in', :type => :string, :required => true | ||
opt :all, 'Export read-only objects', :type => :boolean, :default => false | ||
opt :super_safe_filename, 'Filenames are generated by object id', :type => :boolean, :default => true | ||
end | ||
|
||
error = validate_directory(options[:directory]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module TaskHelpers | ||
class Exports | ||
class CustomizationTemplates | ||
EXCLUDE_ATTRS = %i(created_at updated_at id pxe_image_type_id class).freeze | ||
EXCLUDE_ATTRS = %i(created_at updated_at pxe_image_type_id).freeze | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes me think that this maybe isn't the right way to do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you elaborate your thoughts? Whats the point in removing these attributes anyway? You can still throw them away while importing. |
||
def export(options = {}) | ||
export_dir = options[:directory] | ||
|
||
|
@@ -13,8 +13,7 @@ def export(options = {}) | |
ct_hash = Exports.exclude_attributes(customization_template.to_model_hash, EXCLUDE_ATTRS) | ||
ct_hash.merge!(pxe_image_type_hash(customization_template.pxe_image_type)) | ||
|
||
image_type_name = ct_hash.fetch_path(:pxe_image_type, :name) || "Examples" | ||
filename = Exports.safe_filename("#{image_type_name}-#{ct_hash[:name]}", options[:keep_spaces]) | ||
filename = Exports.safe_filename(ct_hash, options[:keep_spaces], options[:super_safe_filename]) | ||
File.write("#{export_dir}/#{filename}.yaml", ct_hash.to_yaml) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we tend to prefer
is_a?
orkind_of?