ManageIQ/manageiq-content

View on GitHub
content/automate/ManageIQ/Cloud/VM/Retirement/StateMachines/Methods.class/__methods__/check_pre_retirement.rb

Summary

Maintainability
A
2 hrs
Test Coverage
A
100%
# / Cloud / VM / Retirement / StateMachines / CheckPreRetirement

#
# Description: This method checks to see if the VM has been powered off or suspended
#

module ManageIQ
  module Automate
    module Cloud
      module VM
        module Retirement
          module StateMachines
            class CheckPreRetirement
              def initialize(handle = $evm)
                @handle = handle
              end

              def main
                # Get vm from root object
                vm = @handle.root['vm']

                check_power_state(vm)
              end

              def check_power_state(vm)
                ems = vm.ext_management_system if vm
                if vm.nil? || ems.nil?
                  @handle.log('info', "Skipping check pre retirement for VM:<#{vm.try(:name)}> "\
                                      "on EMS:<#{ems.try(:name)}>")
                  return
                end

                power_state = vm.power_state
                @handle.log('info', "VM:<#{vm.name}> on Provider:<#{ems.name}> has Power State:<#{power_state}>")

                # If VM is powered off or suspended exit

                if %w[off suspended unknown].include?(power_state)
                  # Bump State
                  @handle.root['ae_result'] = 'ok'
                elsif power_state == "never"
                  # If never then this VM is a template so exit the retirement state machine
                  @handle.root['ae_result'] = 'error'
                else
                  @handle.root['ae_result'] = 'retry'
                  @handle.root['ae_retry_interval'] = '60.seconds'
                end
              end
            end
          end
        end
      end
    end
  end
end

ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::CheckPreRetirement.new.main