Skip to content

Commit

Permalink
Update a service's lifecycle_state.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Jun 20, 2019
1 parent dbd6f04 commit da1c436
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/models/mixins/lifecycle_mixin.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Service < ApplicationRecord
include ServiceMixin
include OwnershipMixin
include CustomAttributeMixin
include LifecycleMixin
include NewWithTypeStiMixin
include ProcessTasksMixin
include TenancyMixin
Expand All @@ -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] }
Expand Down
6 changes: 5 additions & 1 deletion app/models/service_template_provision_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/models/service_template_provision_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions spec/models/service_template_provision_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da1c436

Please sign in to comment.