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

Update a service's lifecycle_state. #18803

Merged
merged 1 commit into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

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

@lfu I know we discussed using 'unprovisioned', but I'm wondering if there is another value we could use here?
@mkanoor Thoughts?


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