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

Expose error messages from ServiceTemplate.orderable? #656

Conversation

ghost
Copy link

@ghost ghost commented Aug 21, 2019

This pull request is a follow-up of ManageIQ/manageiq#19186 to accept an array of error messages from ServiceTemplate.orderable?, and to display these messages in the response.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1744197
Depends on: ManageIQ/manageiq#19186

@ghost
Copy link
Author

ghost commented Aug 21, 2019

@miq-bot add-label enhancement, ivanchuk/yes, wip

@miq-bot miq-bot changed the title Expose error messages from ServiceTemplate.orderable? [WIP] Expose error messages from ServiceTemplate.orderable? Aug 21, 2019
@ghost
Copy link
Author

ghost commented Aug 24, 2019

@miq-bot remove-label wip
@miq-bot add-reviewer @d-m-u
@miq-bot add-reviewer @agrare

@miq-bot miq-bot changed the title [WIP] Expose error messages from ServiceTemplate.orderable? Expose error messages from ServiceTemplate.orderable? Aug 24, 2019
@miq-bot miq-bot removed the wip label Aug 24, 2019
@miq-bot miq-bot requested review from d-m-u and agrare August 24, 2019 07:10
@agrare
Copy link
Member

agrare commented Aug 27, 2019

@miq-bot assign @abellotti

@ghost ghost closed this Aug 27, 2019
@ghost ghost reopened this Aug 27, 2019
@agrare
Copy link
Member

agrare commented Aug 27, 2019

@fdupont-redhat can you look at the failing spec?

@d-m-u
Copy link
Contributor

d-m-u commented Aug 27, 2019

@agrare I think those two test failures are preexisting at the moment and have nothing to do with this code

@agrare
Copy link
Member

agrare commented Aug 27, 2019

Oh it looks like master has been failing for a while...

@agrare
Copy link
Member

agrare commented Aug 27, 2019

Yeah just noticed that @d-m-u thanks

@d-m-u
Copy link
Contributor

d-m-u commented Aug 27, 2019

they're fixed by #662

@ghost
Copy link
Author

ghost commented Aug 27, 2019

@agrare @d-m-u I'll wait for #662 to be merged then.

@@ -14,9 +15,10 @@ def order_service_template(id, data, scheduled_time = nil)

private

def orderable?(service_template)
api_request_allowed? && service_template.orderable?
def validate_order(service_template)
Copy link
Member

Choose a reason for hiding this comment

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

maybe I'm missing something, but I don't see validate_order, api_request_allowed? or orderable? called from the API.

If they're no longer needed, they should go. though I do have a concern that we're letting go of the logic in api_request_allowed? checking UI calls & Settings.product. wasn't this needed for a fix at some point ?

Copy link
Author

Choose a reason for hiding this comment

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

@abellotti that's an error. I've re-added the check with an error message.

Copy link
Member

Choose a reason for hiding this comment

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

Merci Fabien for the update.

@ghost ghost force-pushed the v2v_allow_error_messages_in_service_template_orderable branch from 07d0313 to 17e5f6f Compare August 27, 2019 15:48
Copy link
Contributor

@d-m-u d-m-u left a comment

Choose a reason for hiding this comment

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

Looks okay to me, @abellotti please take a look

@abellotti
Copy link
Member

Ok, the problem with introducing the validate_<action> method is that it has a special purpose for the API to determine if an action should even be listed in the action list of a resource. So we really cannot have that here, also, validate_<action> should not raise exceptions. Best to put that exception as part of the ordering method. Here's the diff:

diff --git a/app/controllers/api/mixins/service_templates.rb b/app/controllers/api/mixins/service_templates.rb
index 3c24b0fb..bde03fca 100644
--- a/app/controllers/api/mixins/service_templates.rb
+++ b/app/controllers/api/mixins/service_templates.rb
@@ -3,7 +3,7 @@ module Api
     module ServiceTemplates
       def order_service_template(id, data, scheduled_time = nil)
         service_template = resource_search(id, :service_templates, ServiceTemplate)
-        validate_order(service_template)
+        raise BadRequestError, "Service ordering via API is not allowed" unless api_request_allowed?
         raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered - #{service_template.unsupported_reason(:order)}" unless service_template.supports_order?
 
         request_result = service_template.order(User.current_user, (data || {}), order_request_options, scheduled_time)
@@ -16,13 +16,6 @@ module Api
 
       private
 
-      def validate_order(_service_template)
-        raise BadRequestError, "Service ordering via API is not allowed" unless api_request_allowed?
-
-        true
-      end
-      alias orderable? validate_order
-
       def api_request_allowed?
         return true if request_from_ui?
         Settings.product.allow_api_service_ordering
diff --git a/spec/requests/service_templates_spec.rb b/spec/requests/service_templates_spec.rb
index 23093ed9..54e780ff 100644
--- a/spec/requests/service_templates_spec.rb
+++ b/spec/requests/service_templates_spec.rb
@@ -628,6 +628,7 @@ describe "Service Templates API" do
 
     context "with the product setting not allowing automate to run on submit" do
       let(:allow_api_service_ordering) { false }
+      let(:template_no_display) { FactoryBot.create(:service_template, :display => false) }
 
       context "if the token info is blank" do
         before do
@@ -636,7 +637,7 @@ describe "Service Templates API" do
 
         it "rejects the request" do
           api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)
-          post(api_service_template_url(nil, service_template), :params => { :action => "order" })
+          post(api_service_template_url(nil, template_no_display), :params => { :action => "order" })
           expected = {
             "error" => a_hash_including(
               "kind"    => "bad_request",

@miq-bot
Copy link
Member

miq-bot commented Sep 11, 2019

Checked commits fabiendupont/manageiq-api@d2c94a9~...67c5ce9 with ruby 2.4.6, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 🍰

@abellotti
Copy link
Member

Thanks @fdupont-redhat for the fix and update. LGTM!! 👍

@abellotti abellotti merged commit 2aa1e4c into ManageIQ:master Sep 11, 2019
@ghost ghost deleted the v2v_allow_error_messages_in_service_template_orderable branch September 11, 2019 12:52
simaishi pushed a commit that referenced this pull request Nov 4, 2019
…in_service_template_orderable

Expose error messages from ServiceTemplate.orderable?

(cherry picked from commit 2aa1e4c)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1768520
@simaishi
Copy link
Contributor

simaishi commented Nov 4, 2019

Ivanchuk backport details:

$ git log -1
commit f852e171afddb2f52d772126e958fa24c27c519b
Author: Alberto Bellotti <[email protected]>
Date:   Wed Sep 11 08:48:40 2019 -0400

    Merge pull request #656 from fdupont-redhat/v2v_allow_error_messages_in_service_template_orderable
    
    Expose error messages from ServiceTemplate.orderable?
    
    (cherry picked from commit 2aa1e4cfd2a4c12fc81a76533aba95b5b1050149)
    
    Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1768520

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