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

Use constant to store ServiceResource status. #17256

Merged
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
6 changes: 6 additions & 0 deletions app/models/service_resource.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class ServiceResource < ApplicationRecord
STATUS_ACTIVE = 'Active'.freeze
STATUS_APPROVED = 'Approved'.freeze
STATUS_COMPLETED = 'Completed'.freeze
STATUS_FAILED = 'Failed'.freeze
STATUS_QUEUED = 'Queued'.freeze

belongs_to :service_template
belongs_to :service
belongs_to :resource, :polymorphic => true
Expand Down
2 changes: 1 addition & 1 deletion app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.create_catalog_item(options, _auth_user = nil)
transaction do
create_from_options(options.merge(default_options)).tap do |service_template|
service_template.add_resource(enhanced_config_info[:transformation_mapping])
enhanced_config_info[:vms].each { |vm| service_template.add_resource(vm, :status => 'Queued') }
enhanced_config_info[:vms].each { |vm| service_template.add_resource(vm, :status => ServiceResource::STATUS_QUEUED) }
service_template.create_resource_actions(enhanced_config_info)
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/service_template_transformation_plan_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class ServiceTemplateTransformationPlanRequest < ServiceTemplateProvisionRequest
delegate :transformation_mapping, :vm_resources, :to => :source

def requested_task_idx
vm_resources.where(:status => 'Approved')
vm_resources.where(:status => ServiceResource::STATUS_APPROVED)
end

def customize_request_task_attributes(req_task_attrs, vm_resource)
req_task_attrs[:source] = vm_resource.resource
end

def source_vms
vm_resources.where(:status => %w(Queued Failed)).pluck(:resource_id)
vm_resources.where(:status => [ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_FAILED]).pluck(:resource_id)
end

def validate_vm(_vm_id)
Expand All @@ -21,6 +21,6 @@ def validate_vm(_vm_id)
end

def approve_vm(vm_id)
vm_resources.find_by(:resource_id => vm_id).update_attributes!(:status => 'Approved')
vm_resources.find_by(:resource_id => vm_id).update_attributes!(:status => ServiceResource::STATUS_APPROVED)
end
end
4 changes: 2 additions & 2 deletions app/models/service_template_transformation_plan_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def update_transformation_progress(progress)

def task_finished
# update the status of vm transformation status in the plan
vm_resource.update_attributes(:status => status == 'Ok' ? 'Completed' : 'Failed')
vm_resource.update_attributes(:status => status == 'Ok' ? ServiceResource::STATUS_COMPLETED : ServiceResource::STATUS_FAILED)
end

def task_active
vm_resource.update_attributes(:status => 'Active')
vm_resource.update_attributes(:status => ServiceResource::STATUS_ACTIVE)
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe ServiceTemplateTransformationPlanRequest do
let(:vms) { Array.new(3) { FactoryGirl.create(:vm_or_template) } }
let(:vm_requests) do
%w(Queued Failed Approved).zip(vms).collect do |status, vm|
[ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_FAILED, ServiceResource::STATUS_APPROVED].zip(vms).collect do |status, vm|
ServiceResource.new(:resource => vm, :status => status)
end
end
Expand All @@ -10,7 +10,7 @@

describe '#requested_task_idx' do
it 'selects approved vm requests' do
expect(request.requested_task_idx.first).to have_attributes(:resource => vms[2], :status => 'Approved')
expect(request.requested_task_idx.first).to have_attributes(:resource => vms[2], :status => ServiceResource::STATUS_APPROVED)
end
end

Expand All @@ -35,7 +35,7 @@
describe '#approve_vm' do
it 'turns the status to Approved' do
request.approve_vm(vms[0].id)
expect(ServiceResource.find_by(:resource => vms[0]).status).to eq('Approved')
expect(ServiceResource.find_by(:resource => vms[0]).status).to eq(ServiceResource::STATUS_APPROVED)
end
end
end
2 changes: 1 addition & 1 deletion spec/models/service_template_transformation_plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
expect(service_template.name).to eq('Transformation Plan')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.collect(&:status)).to eq(%w(Queued Queued))
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
describe 'task_active' do
it 'sets vm_request status to Started' do
task.task_active
expect(plan.vm_resources.first.status).to eq('Active')
expect(plan.vm_resources.first.status).to eq(ServiceResource::STATUS_ACTIVE)
end
end

describe 'task_finished' do
it 'sets vm_request status to Completed' do
task.task_finished
expect(plan.vm_resources.first.status).to eq('Completed')
expect(plan.vm_resources.first.status).to eq(ServiceResource::STATUS_COMPLETED)
end
end

Expand Down