Skip to content

Commit

Permalink
Add built in policy to prevent transformed VM from starting.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed May 7, 2018
1 parent bf52707 commit 27bb242
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Vm < VmOrTemplate
has_one :container_deployment_node

virtual_has_one :supported_consoles, :class_name => "Hash"
virtual_column :transformed, :type => :boolean

extend InterRegionApiMethodRelay
include CustomActionsMixin
Expand Down Expand Up @@ -133,6 +134,12 @@ def validate_v2v_migration
vm_as_resources.all? { |rsc| rsc.status == ServiceResource::STATUS_FAILED } ? TransformationMapping::VM_VALID : TransformationMapping::VM_IN_OTHER_PLAN
end

def transformed
!ServiceResource.joins(:service_template)
.where(:service_templates => {:type => 'ServiceTemplateTransformationPlan'})
.where(:resource => self, :status => ServiceResource::STATUS_COMPLETED).empty?
end

private

def vnc_support
Expand Down
13 changes: 13 additions & 0 deletions product/policy/built_in_policies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
:modifier: deny
:mode: control
:action: vm_suspend
- :name: Prevent Transformed VM from Starting
:description: Prevent Transformed VM from starting
:towhat: Vm
:event: request_vm_start
:applies_to?: true
:active: true
:condition:
"=":
field: Vm-transformed
value: true
:modifier: deny
:mode: control
:action: prevent
- :name: Stop Retired VM
:description: Stop Retired VM
:towhat: Vm
Expand Down
17 changes: 17 additions & 0 deletions spec/models/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,21 @@
expect(vm.supported_consoles.keys).to match_array([:spice, :vnc, :vmrc, :webmks, :cockpit])
end
end

context "#transformed" do
let(:plan) { FactoryGirl.create(:service_template_transformation_plan) }
let(:vm) { FactoryGirl.create(:vm) }
let(:service_resource) { FactoryGirl.create(:service_resource, :resource => vm, :service_template => plan) }
subject { vm.transformed }

it 'returns true' do
service_resource.update_attributes(:status => ServiceResource::STATUS_COMPLETED)

expect(subject).to be true
end

it 'returnes false' do
expect(subject).to be false
end
end
end

0 comments on commit 27bb242

Please sign in to comment.