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 error importing Widget on Custom Report page #16034

Merged
merged 1 commit into from
Oct 9, 2017

Conversation

bmclaughlin
Copy link
Contributor

@bmclaughlin bmclaughlin commented Sep 25, 2017

Fixes an exception to a much more user friendly flash message.

@miq-bot add_labels bug, core, ui

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

@@ -37,6 +38,10 @@ def import(fd, options = {})
raise "Invalid YAML file"
end

if options.key?(:expected_class) && (options[:expected_class] != reps[0].keys.first)
raise _("Incorrect format: Expected #{options[:expected_class]} and received #{reps[0].keys.first}")
end
Copy link
Member

Choose a reason for hiding this comment

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

@bmclaughlin I'm ok with this check, but it looks like doing this for MiqReport (ManageIQ/manageiq-ui-classic#2227) would negate the need for the rescue below on line 59: # for the legacy MiqReport as we would never get to that code.

@gtanzillo Can you comment on this or recommend someone?

@gmcculloug
Copy link
Member

Thinking more about this maybe a better approach is to create a validate_import_data method that gets called as part of the import method instead of the caller needing to pass the class.

From what I can tell three classes use this mixin: MiqReport, MiqWidget and MiqPolicy. Both MiqReport and MiqWidget expect the root of the import to be the same class name. However, MiqPolicy supports MiqPolicy, MiqPolicySet and MiqAlert.

The mixin could contain the validation method that checks the the name matches the class name and the classes would expose a constant that defines the IMPORT_CLASS_NAMES.

Something like this:

    def import(fd, options = {})
      fd.rewind # ensure to be at the beginning as the file is read multiple times
      begin
        reps = YAML.load(fd.read)
        validate_import_data(reps)
      rescue Psych::SyntaxError => err
        _log.error("Failed to load from #{fd}: #{err}")
        raise "Invalid YAML file"
      end

      return reps, import_from_array(reps, options)
    end

    def validate_import_data(data)
      data_class_name = data.first.keys.first

      if !self::IMPORT_CLASS_NAMES.include?(data_class_name)
        raise _("Incorrect format: Expected #{self::IMPORT_CLASS_NAMES.join(", ")} and received #{data_class_name}")
      end
    end

Then in app/models/miq_policy/import_export.rb (also MiqReport and MiqWidget)

module MiqPolicy::ImportExport
  extend ActiveSupport::Concern

  IMPORT_CLASS_NAMES = %w(MiqPolicy MiqPolicySet MiqAlert)
...

@gtanzillo @eclarizio Thoughts?

@eclarizio
Copy link
Member

@gmcculloug Yeah, I like this idea, and I would even further look into a way to incorporate the already existing WidgetImportValidator class (or create a new one if WidgetImport is too specific?).

@bmclaughlin
Copy link
Contributor Author

@gmcculloug, implemented your idea, much better solution than what I had originally. Thanks!

@eclarizio, or perhaps validate_import_data should be renamed to validate_import_class to better identify what the method is accomplishing? I didn't see a good way to incorporate your feedback, I think you're looking at this from a different angle than I am.

Copy link
Member

@eclarizio eclarizio left a comment

Choose a reason for hiding this comment

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

@bmclaughlin My original point was mostly directed at attempting to encapsulate the validation in a different class, but I think since we're not really validating the data, just ensuring that the class is correct, I agree with your other point of changing the name of the method to validate_import_class instead of validate_import_data. Or maybe validate_import_data_class, but either way, I think validate_import_data implies it does more than it really does.

@gmcculloug
Copy link
Member

@bmclaughlin Specs are failing because they still references the old method name.

@@ -56,4 +56,18 @@
expect { subject.import(@fd) }.to raise_error("Invalid YAML file")
end
end

context ".validate_import_data" do
Copy link
Member

Choose a reason for hiding this comment

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

Missed one. 😱

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So close...

@gmcculloug
Copy link
Member

For our final act, can you drop the first commit since it's not being used and squash the others?

@bmclaughlin bmclaughlin force-pushed the import-custom-report-error branch 2 times, most recently from ccf92cd to d5ae286 Compare October 6, 2017 19:22
@gmcculloug
Copy link
Member

Somehow you pulled in one of Jillian's PRs!?!

@miq-bot
Copy link
Member

miq-bot commented Oct 6, 2017

Checked commit bmclaughlin@b26b013 with ruby 2.3.3, rubocop 0.47.1, and haml-lint 0.20.0
5 files checked, 0 offenses detected
Everything looks fine. 🏆

Copy link
Member

@eclarizio eclarizio left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@gtanzillo gtanzillo left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@gmcculloug gmcculloug merged commit 16602fa into ManageIQ:master Oct 9, 2017
@gmcculloug gmcculloug added this to the Sprint 71 Ending Oct 16, 2017 milestone Oct 9, 2017
@bmclaughlin bmclaughlin deleted the import-custom-report-error branch October 9, 2017 13:24
simaishi pushed a commit that referenced this pull request Nov 13, 2017
@simaishi
Copy link
Contributor

Fine backport details:

$ git log -1
commit afd835e9fc432d98c789740216455f808d052b18
Author: Greg McCullough <gmccullo@redhat.com>
Date:   Mon Oct 9 09:24:02 2017 -0400

    Merge pull request #16034 from bmclaughlin/import-custom-report-error
    
    Fix error importing Widget on Custom Report page
    (cherry picked from commit 16602fac1c78ba1ff09da8035e489a4c4453895c)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1500029

d-m-u pushed a commit to d-m-u/manageiq that referenced this pull request Jun 6, 2018
…rt-error

Fix error importing Widget on Custom Report page
(cherry picked from commit 16602fa)

https://bugzilla.redhat.com/show_bug.cgi?id=1500029
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.

6 participants