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

Fix virtual attribute selection #15387

Merged
merged 1 commit into from
Jun 27, 2017
Merged

Fix virtual attribute selection #15387

merged 1 commit into from
Jun 27, 2017

Conversation

jntullo
Copy link

@jntullo jntullo commented Jun 15, 2017

BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1461939

When expanding a virtual attribute that is an array of values (ie, power_states => ["ok","ok"] on a service), it is currently failing with the error:

{"error":{"kind":"internal_server_error","message":"undefined method `id' for \"on\":String","klass":"NoMethodError"}}

This is due to these values being passed to Rbac.filterer, which is looking for id of object. This fix avoids sending the value of the virtual attribute to filterer.

@miq-bot add_label api, bug
@miq-bot assign @abellotti
cc: @AllenBW

@@ -166,7 +166,9 @@ def virtual_attribute_search(resource, attribute)
if resource.class < ApplicationRecord
# is relation in 'attribute' variable plural in the model class (from 'resource.class') ?
if [:has_many, :has_and_belongs_to_many].include?(resource.class.reflection_with_virtual(attribute).try(:macro))
Rbac.filtered(resource.public_send(attribute))
resource_attr = resource.public_send(attribute)
return resource_attr if resource_attr.kind_of?(Array) && resource_attr.first.kind_of?(String)
Copy link
Contributor

@lpichler lpichler Jun 16, 2017

Choose a reason for hiding this comment

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

it is because Service#power_states is :virtual_has_many relation what is a custom method and I think that it can return any data type In general, so I think the condition have to be built on fact what we can pass to RBAC,
we can pass to Rbac.filtered(Vm):

  1. model (Rbac.filtered(Vm))
  2. relation, scope (Vm.with_ownership.class
    Vm::ActiveRecord_Relation < ActiveRecord::Relation)
  3. the array of ids
  4. the array of ActiveRecord objects

and I think that only point 4 is is relevant for this case.

@jntullo what do you think about it?

Copy link
Contributor

@lpichler lpichler left a comment

Choose a reason for hiding this comment

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

thanks to @jntullo for fixing the bug and to @AllenBW for finding the bug 👍 👍

@AllenBW
Copy link
Member

AllenBW commented Jun 16, 2017

🌮 💃 ❤️


expected = {
"resources" => [
a_hash_including("power_states" => %w(on on))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these values come from the service's vms (defined somewhere above) - based on the default values from the factory. I think it could be a good idea to specify the raw power state of the vms when they are built, so they are independent of those default values

@@ -166,7 +166,9 @@ def virtual_attribute_search(resource, attribute)
if resource.class < ApplicationRecord
# is relation in 'attribute' variable plural in the model class (from 'resource.class') ?
if [:has_many, :has_and_belongs_to_many].include?(resource.class.reflection_with_virtual(attribute).try(:macro))
Rbac.filtered(resource.public_send(attribute))
resource_attr = resource.public_send(attribute)
return resource_attr unless resource_attr.first.kind_of?(ApplicationRecord)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm having a hard time trying to parse this mentally - perhaps a comment would help here? If I understand this correctly (I may not) - I'm concerned whether Model#some_attribute is guaranteed to respond to first?

Copy link
Contributor

Choose a reason for hiding this comment

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

it is under if with condition is it a plural relation? so I think it has to be guaranteed. Or rather add try(:first)?

Copy link
Contributor

Choose a reason for hiding this comment

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

@lpichler could this be a problem with RBAC? Or a problem with this being virtual? If it's virtual but can't be treated like a "real" relation, it seems like something is wrong there :P

@miq-bot
Copy link
Member

miq-bot commented Jun 16, 2017

Checked commit jntullo@4713a61 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
2 files checked, 0 offenses detected
Everything looks fine. 🏆

@AllenBW
Copy link
Member

AllenBW commented Jun 27, 2017

I love this pr. SUPER love this pr. 🌮 🎉

@abellotti
Copy link
Member

This LGTM!! Thanks @jntullo for fixing this. 👍

@abellotti abellotti merged commit e92afb1 into ManageIQ:master Jun 27, 2017
@abellotti abellotti added this to the Sprint 64 Ending Jul 3, 2017 milestone Jun 27, 2017
@jntullo
Copy link
Author

jntullo commented Jun 29, 2017

@miq-bot add_label fine/yes

@jntullo
Copy link
Author

jntullo commented Jun 29, 2017

simaishi pushed a commit that referenced this pull request Jul 6, 2017
@simaishi
Copy link
Contributor

simaishi commented Jul 6, 2017

Fine backport details:

$ git log -1
commit d4fdfebcd331fe352eda12fef64f73917bc40ca3
Author: Alberto Bellotti <abellotti@users.noreply.github.com>
Date:   Tue Jun 27 11:01:17 2017 -0400

    Merge pull request #15387 from jntullo/bz/fix_virtual_attr_expansion
    
    Fix virtual attribute selection
    (cherry picked from commit e92afb191454382639fb6f69423ef3419dec31c6)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1468294

@jntullo jntullo deleted the bz/fix_virtual_attr_expansion branch November 28, 2017 19:42
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.

7 participants