Skip to content
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

Merged
merged 1 commit into from
Apr 4, 2018

Conversation

xprazak2
Copy link
Contributor

If any taxonomy params are passed for template import, they are used instead of what is in metadata.

@theforeman-bot
Copy link
Member

Issues: #22893

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected token tASSOC

@jyejare
Copy link
Contributor

jyejare commented Mar 19, 2018

ACK from QE.

Tests Performed:

1. POST without overridden taxonomies with some Provisioning Templates kinds- provision, script, snippet, finish, PXELinux.
Assert: All the POST requests for all the kinds work as expected.

2. POST with overridden taxonomies with some Provisioning Templates kinds- provision, script, snippet, finish, PXELinux.
Assert: All the POST requests for all the kinds work as expected.

3. POST without overridden taxonomies with Partition Table kind.
Assert: The POST request works as expected.

4. POST with overridden taxonomies Partition Table kind.
Assert: The POST request works as expected.

Extras(Regression Testing):

5. PUT to update the taxonomies of ptables and provisioning templates is not compromised.
Assert: PUT request works as expected.

Also, Please check the checks are failing.

@xprazak2 xprazak2 force-pushed the tax-override branch 3 times, most recently from bd2680a to 43b8e7b Compare March 26, 2018 11:43
@xprazak2
Copy link
Contributor Author

I found out passing organizations or locations did not work as expected so I removed them, now only organization_ids and organization_names are acceptable. Same for locations.

@xprazak2 xprazak2 force-pushed the tax-override branch 3 times, most recently from 8e1c2e0 to c8d4255 Compare March 27, 2018 07:29
Copy link
Member

@ares ares left a 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

@@ -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?
Copy link
Member

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?

@@ -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: {})
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

@@ -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) }
Copy link
Member

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

@@ -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 ={})

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.

@xprazak2
Copy link
Contributor Author

Updated, it is now possible to override just locations or organizations.

Copy link
Member

@ares ares left a 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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Ptable I think

def process_taxonomies(options, taxonomy)
tax_options = options["#{taxonomy}_params".to_sym]
if tax_options.empty?
send("import_#{taxonomy}s", options)
Copy link
Member

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)

@@ -146,6 +145,32 @@ def import_oses(options)
end
end

def import_taxonomies(options)
process_organizations options
process_locations options
Copy link
Member

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?

@xprazak2
Copy link
Contributor Author

I made changes as suggested.

Copy link
Member

@ares ares left a 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

Copy link

@houndci-bot houndci-bot left a 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.

@xprazak2
Copy link
Contributor Author

xprazak2 commented Apr 4, 2018

Updated with api doc for taxonomies.

Copy link
Member

@ares ares left a 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

@ares
Copy link
Member

ares commented Apr 4, 2018

Thanks @xprazak2, merging!

@ares ares merged commit cddb566 into theforeman:develop Apr 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants