From 7ea19a10d086597c7eef67d595b9c3a90c8ba068 Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Thu, 23 May 2019 13:57:02 -0400 Subject: [PATCH] Update a service's lifecycle_state. https://bugzilla.redhat.com/show_bug.cgi?id=1677571 --- app/models/mixins/lifecycle_mixin.rb | 21 ++++++++++++++++ app/models/service.rb | 2 ++ app/models/service_template_provision_task.rb | 6 ++++- .../service_template_provision_task_spec.rb | 24 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 app/models/mixins/lifecycle_mixin.rb diff --git a/app/models/mixins/lifecycle_mixin.rb b/app/models/mixins/lifecycle_mixin.rb new file mode 100644 index 000000000000..2cc19f80133b --- /dev/null +++ b/app/models/mixins/lifecycle_mixin.rb @@ -0,0 +1,21 @@ +module LifecycleMixin + extend ActiveSupport::Concern + + def update_lifecycle_state + case miq_request.request_state + when "finished" + lifecycle_state = miq_request.status == 'Ok' ? "provisioned" : "error_provisioned" + update(:lifecycle_state => lifecycle_state) + else + update(:lifecycle_state => 'provisioning') + end + end + + def provisioned? + lifecycle_state == 'provisioned' + end + + def provision_failed? + lifecycle_state == 'error_provisioned' + end +end diff --git a/app/models/service.rb b/app/models/service.rb index 77281faede3b..c5f15209d4ab 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -62,6 +62,7 @@ class Service < ApplicationRecord include ServiceMixin include OwnershipMixin include CustomAttributeMixin + include LifecycleMixin include NewWithTypeStiMixin include ProcessTasksMixin include TenancyMixin @@ -88,6 +89,7 @@ class Service < ApplicationRecord default_value_for :display, false default_value_for :retired, false default_value_for :initiator, 'user' + default_value_for :lifecycle_state, 'unprovisioned' validates :display, :inclusion => { :in => [true, false] } validates :retired, :inclusion => { :in => [true, false] } diff --git a/app/models/service_template_provision_task.rb b/app/models/service_template_provision_task.rb index 12811fc46863..79ddf7cf2e5b 100644 --- a/app/models/service_template_provision_task.rb +++ b/app/models/service_template_provision_task.rb @@ -94,6 +94,7 @@ def do_request update_and_notify_parent(:message => message) queue_post_provision end + destination.update_lifecycle_state end def queue_post_provision @@ -210,7 +211,10 @@ def update_and_notify_parent(*args) def task_finished service = destination - service.raise_provisioned_event unless service.nil? + return if service.nil? + + service.raise_provisioned_event + service.update_lifecycle_state if miq_request_task.nil? end private diff --git a/spec/models/service_template_provision_task_spec.rb b/spec/models/service_template_provision_task_spec.rb index 2bddc54eb2bd..8668297bf796 100644 --- a/spec/models/service_template_provision_task_spec.rb +++ b/spec/models/service_template_provision_task_spec.rb @@ -258,6 +258,30 @@ def service_resource_id(index, scaling_max) @task_1_2.destination = @service @task_1_2.update_and_notify_parent(:state => "finished", :status => "Ok", :message => "Test Message") end + + it 'set service lifecycle_state to provisioning' do + zone = FactoryBot.create(:zone, :name => "service_template_zone") + @task_3.source = FactoryBot.create(:service_template, :zone => zone) + @task_3.destination = @service + @task_3.do_request + expect(@service.lifecycle_state).to eq('provisioning') + end + + it 'set servcie lifecycle_state to provisioned' do + expect(MiqEvent).to receive(:raise_evm_event).with(@service, :service_provisioned) + @task_0.destination = @service + @request.miq_request_tasks.except(@task_0).each { |t| t.update(:state => "finished") } + @task_0.update_and_notify_parent(:state => "finished", :status => "Ok", :message => "Test Message") + expect(@service.lifecycle_state).to eq('provisioned') + end + + it 'set servcie lifecycle_state to error_provisioned' do + expect(MiqEvent).to receive(:raise_evm_event).with(@service, :service_provisioned) + @task_0.destination = @service + @request.miq_request_tasks.except(@task_0).each { |t| t.update(:state => "finished") } + @task_0.update_and_notify_parent(:state => "finished", :status => "Error", :message => "Test Message") + expect(@service.lifecycle_state).to eq('error_provisioned') + end end describe "#mark_execution_servers" do