-
Notifications
You must be signed in to change notification settings - Fork 992
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
Fixes #22893 - Specify taxonomies on template import #5323
Conversation
Issues: #22893 |
test/models/template_test.rb
Outdated
template.verify | ||
end | ||
|
||
test 'locking can be overriden by force option' do | ||
template = Minitest::Mock.new | ||
template.expect(:ignore_locking, true) | ||
Template.expects(:import_without_save => template) | ||
Template.import!('test', '', :force => true) | ||
Template.import!(name: 'test', text: '', options: :force => true) |
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.
unexpected token tASSOC
ACK from QE. Tests Performed:
Also, Please check the |
bd2680a
to
43b8e7b
Compare
I found out passing |
8e1c2e0
to
c8d4255
Compare
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.
thanks @xprazak2, please see comments inline, I'll continue with testing afterwards
app/models/template.rb
Outdated
@@ -82,8 +82,12 @@ def import_without_save(text, options = {}) | |||
self.snippet = !!@importing_metadata[:snippet] | |||
self.locked = options[:lock] unless options[:lock].nil? | |||
|
|||
import_locations(options) | |||
import_organizations(options) | |||
if options[:taxonomy_params].empty? |
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.
what if I want to only override organizations? could we split this based on incoming params?
app/models/template.rb
Outdated
@@ -114,7 +118,7 @@ def self.parse_metadata(text) | |||
# :force - set to true if you want to bypass locked templates | |||
# :associate - either 'new', 'always' or 'never', determines when the template should associate objects based on metadata | |||
# :lock - lock imported templates (false by default) | |||
def self.import!(name, text, options = {}) | |||
def self.import!(name:, text:, options: {}) |
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.
out of curiosity, why this is needed? the method import_attrs_for
could have returned array and params could be passed with splat operator *
while I'm not against the change, I'm just curious why we change the method prototype
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 reverted this back, changed import_params_for
to return an array.
The change was not strictly needed, the suggested solution with array and splat works. I could also do:
def import_params_for(resource_name)
# ...
{ :name => name, :template => template, :options => options }
end
# then in controller
def import
import_params = import_attrs_for(:provisioning_template)
@provisioning_template = ProvisioningTemplate.import!(import_params[:name], import_params[:template], import_params[:options])
process_response @provisioning_template
end
My reasoning was that when I need multiple different values returned from a method, hash is better option than array, because I do not have to keep track of order and I do not use array as if it were a tuple. And when I have proper keys in hash, why not use named params for destructuring instead of doing it manually.
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 see, thanks, I think it's good refactoring in general, but I'd prefer to keep the change minimal for now as we're getting closer to 1.18.
app/models/template.rb
Outdated
@@ -146,6 +150,10 @@ def import_oses(options) | |||
end | |||
end | |||
|
|||
def import_taxonomy_override(taxonomy_params) | |||
taxonomy_params.map { |key, value| self.public_send("#{key}=", value) } |
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.
this does not seem very readable, I'd prefer to be more explicit and go over all enabled taxonomies https://github.com/theforeman/foreman/blob/develop/app/models/taxonomy.rb#L91
app/models/template.rb
Outdated
@@ -114,7 +113,7 @@ def self.parse_metadata(text) | |||
# :force - set to true if you want to bypass locked templates | |||
# :associate - either 'new', 'always' or 'never', determines when the template should associate objects based on metadata | |||
# :lock - lock imported templates (false by default) | |||
def self.import!(name, text, options = {}) | |||
def self.import!(name, text, options ={}) |
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.
Surrounding space missing in default value assignment.
Updated, it is now possible to override just locations or organizations. |
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.
few more commemts, but getting there :-)
@@ -21,4 +21,12 @@ def ptable_params_filter | |||
def ptable_params | |||
self.class.ptable_params_filter.filter_params(params, parameter_filter_context) | |||
end | |||
|
|||
def organization_params | |||
self.class.organization_params_filter(::ProvisioningTemplate).filter_params(params, parameter_filter_context) |
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.
Should this use Ptable?
end | ||
|
||
def location_params | ||
self.class.location_params_filter(::ProvisioningTemplate).filter_params(params, parameter_filter_context) |
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.
Also Ptable I think
app/models/template.rb
Outdated
def process_taxonomies(options, taxonomy) | ||
tax_options = options["#{taxonomy}_params".to_sym] | ||
if tax_options.empty? | ||
send("import_#{taxonomy}s", options) |
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.
How about #{taxonomy.pluralize}
just in case we ever another taxonomy with irregular pluralization rules (and to be consistent with the rest of the code)
app/models/template.rb
Outdated
@@ -146,6 +145,32 @@ def import_oses(options) | |||
end | |||
end | |||
|
|||
def import_taxonomies(options) | |||
process_organizations options | |||
process_locations options |
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 this adds unnecessary layer, could we jusř use process_organizations a locations directly?
I made changes as suggested. |
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.
found just one thing, it would be good to document new api parameters using param_group :taxonomies, ::Api::V2::BaseController
but then, it's good to go
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.
Some files could not be reviewed due to errors:
Error: The `Style/TrailingCommaInLiteral` cop no longer exists. Please use `S...
Error: The `Style/TrailingCommaInLiteral` cop no longer exists. Please use `Style/TrailingCommaInArrayLiteral` and/or `Style/TrailingCommaInHashLiteral` instead. (obsolete configuration found in .rubocop.yml, please update it) The `Performance/HashEachMethods` cop has been removed since it no longer provides performance benefits in modern rubies.
Updated with api doc for taxonomies. |
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.
ok, let's just wait for Mr. Jenkins to confirm
Thanks @xprazak2, merging! |
If any taxonomy params are passed for template import, they are used instead of what is in metadata.