diff --git a/app/models/mixins/lifecycle_mixin.rb b/app/models/mixins/lifecycle_mixin.rb new file mode 100644 index 00000000000..9dff6953f9d --- /dev/null +++ b/app/models/mixins/lifecycle_mixin.rb @@ -0,0 +1,25 @@ +module LifecycleMixin + extend ActiveSupport::Concern + + STATE_ERROR_PROVISIONING = 'error_in_provisioning'.freeze + STATE_PROVISIONED = 'provisioned'.freeze + STATE_PROVISIONING = 'provisioning'.freeze + + def update_lifecycle_state + case miq_request.request_state + when "finished" + lifecycle_state = miq_request.status == 'Ok' ? STATE_PROVISIONED : STATE_ERROR_PROVISIONING + update(:lifecycle_state => lifecycle_state) + else + update(:lifecycle_state => STATE_PROVISIONING) + end + end + + def provisioned? + lifecycle_state == STATE_PROVISIONED + end + + def provision_failed? + lifecycle_state == STATE_ERROR_PROVISIONING + end +end diff --git a/app/models/service.rb b/app/models/service.rb index 77281faede3..c5f15209d4a 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 12811fc4686..79ddf7cf2e5 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_request_spec.rb b/spec/models/service_template_provision_request_spec.rb index 924d5e1d9a0..000e6efced2 100644 --- a/spec/models/service_template_provision_request_spec.rb +++ b/spec/models/service_template_provision_request_spec.rb @@ -103,6 +103,7 @@ end it "generic service do_request" do + @task_1_1.destination = FactoryBot.create(:service) expect { @task_1_1.do_request }.not_to raise_error expect(@task_1_1.state).to eq('provisioned') end diff --git a/spec/models/service_template_provision_task_spec.rb b/spec/models/service_template_provision_task_spec.rb index 2bddc54eb2b..f4e0f7e7a9b 100644 --- a/spec/models/service_template_provision_task_spec.rb +++ b/spec/models/service_template_provision_task_spec.rb @@ -258,6 +258,29 @@ 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 + @task_3.source = FactoryBot.create(:service_template) + @task_3.destination = @service + @task_3.do_request + expect(@service.lifecycle_state).to eq(Service::STATE_PROVISIONING) + end + + it 'set service 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.provisioned?).to be true + end + + it 'set service 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.provision_failed?).to be true + end end describe "#mark_execution_servers" do