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 May 23, 2019
1 parent 21ec615 commit 7ea19a1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/models/mixins/lifecycle_mixin.rb
Original file line number Diff line number Diff line change
@@ -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
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
24 changes: 24 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,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
Expand Down

0 comments on commit 7ea19a1

Please sign in to comment.