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

Added vm_retire_extend method for new email. #366

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# Description: This method is used to add 14 days to retirement date when target
# VM has a retires_on value and is not already retired
#

module ManageIQ
module Automate
module System
module Request
class VmRetireExtend
DEFAULT_EXTENSION_DAYS = 14
def initialize(handle = $evm)
@handle = handle
end

def main
validate
extend_retirement
end

private

def vm
@vm ||= @handle.root["vm"].tap do |vm|
if vm.nil?
@handle.log(:error, 'vm is nil')
raise 'ERROR - vm object not passed in'
end
end
end

def validate
if vm.retires_on.blank?
raise "ERROR - VM #{vm.name} has no retirement date - extension bypassed. No Action taken"
end

if vm.retired
raise "ERROR - VM #{vm.name} is already retired - extension bypassed. No Action taken"
end
end

def retirement_extend_days
@vm_retire_extend_days ||= @handle.object['vm_retire_extend_days'] || DEFAULT_EXTENSION_DAYS
end

def extend_retirement
@handle.log("info", "Number of days to extend: <#{retirement_extend_days}>")
@handle.log("info", "VM: <#{vm.name}> current retirement date is #{vm.retires_on}")
@handle.log("info", "Extending retirement <#{retirement_extend_days}> days for VM: <#{vm.name}>")

vm.extend_retires_on(retirement_extend_days, vm.retires_on)

@handle.log("info", "VM: <#{vm.name}> new retirement date is #{vm.retires_on}")
@handle.log("info", "Inspecting retirement vm: <#{vm.retirement_state}>")
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::System::Request::VmRetireExtend.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
object_type: method
version: 1.0
object:
attributes:
name: vm_retire_extend
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ object:
inherits:
description:
fields:
- meth4:
value: vm_retire_extend
- rel5:
value: "/System/Notification/Email/${/#ae_provider_category}VmRetireExtend?event=vm_retire_extend"
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_domain_file

describe ManageIQ::Automate::System::Request::VmRetireExtend do
let(:user) { FactoryGirl.create(:user_with_email_and_group) }
let(:zone) { FactoryGirl.create(:zone) }
let(:ems) { FactoryGirl.create(:ems_microsoft, :zone => zone, :tenant => Tenant.root_tenant) }
let(:miq_server) { EvmSpecHelper.local_miq_server }
let(:vm) do
FactoryGirl.create(:vm_microsoft,
:raw_power_state => "PowerOff",
:retires_on => Time.zone.now,
:evm_owner => user,
:ems_id => ems.id)
end

let(:root_hash) do
{ 'vm' => vm }
end

let(:root_object) do
Spec::Support::MiqAeMockObject.new(root_hash)
end

let(:ae_service) do
Spec::Support::MiqAeMockService.new(root_object).tap do |service|
current_object = Spec::Support::MiqAeMockObject.new
current_object.parent = root_object
service.object = current_object
end
end

before do
allow(ae_service).to receive(:execute)
end

context "when vm_retire_extend_days is 14" do
it "returns a new retirement date " do
future_retires_on = Time.zone.now + 14.days
described_class.new(ae_service).main
expect(vm.retires_on.day).to eq(future_retires_on.day)
end
end

context "when vm retires_on is nil" do
it "does not update retires_on date" do
vm.update_attributes(:retires_on => nil)
errormsg = "ERROR - VM #{vm} has no retirement date - extension bypassed. No Action taken"
expect { described_class.new(ae_service).main }.to raise_error(errormsg)
end
end

context "when vm retired is true " do
it "does not update retires_on date" do
vm.update_attributes(:retired => true)
errormsg = "ERROR - VM #{vm} is already retired - extension bypassed. No Action taken"
expect { described_class.new(ae_service).main }.to raise_error(errormsg)
end
end

context "when there is no vm" do
let(:root_hash) { {} }
let(:svc_model_service) { nil }
let(:vm) { nil }
it "raises error message" do
errormsg = 'ERROR - vm object not passed in'
expect { described_class.new(ae_service).main }.to raise_error(errormsg)
end
end
end