Class: MiqDevUtil::Automate

Inherits:
Object
  • Object
show all
Defined in:
lib/miq_dev_util/automate.rb

Overview

The Automate class is intended to hold methods that are useful when interacting with the ManageIQ automate system directly.

Instance Method Summary collapse

Constructor Details

#initialize(evm) ⇒ Automate

Returns a new instance of Automate.



5
6
7
# File 'lib/miq_dev_util/automate.rb', line 5

def initialize(evm)
  @evm = evm
end

Instance Method Details

#create_automation_request(options, userid = 'admin', auto_approve = false) ⇒ Object

A wrapper to handle MIQ 5.4 which does not have this built in and 5.5+ which does.



57
58
59
60
61
62
63
# File 'lib/miq_dev_util/automate.rb', line 57

def create_automation_request(options, userid = 'admin', auto_approve = false)
  unless service_method_defined?(:create_automation_request)
    backport_create_automation_request
  end

  @evm.execute('create_automation_request', options, userid, auto_approve)
end

#get_instance_with_attributes(path) ⇒ Object

This is a hacky workaround used to get an instance without executing the methods on it. It fails if a message is passed in the path or if the message field on the any of the methods are *.



23
24
25
26
27
28
29
30
31
# File 'lib/miq_dev_util/automate.rb', line 23

def get_instance_with_attributes(path)
  if path =~ /#/
    raise "Does not work with messages in the path."
  end
  fake_message = "callingWithAFakeMessage"
  empty_instance = @evm.instantiate("#{path}##{fake_message}")
  instance_name = empty_instance.name
  @evm.instance_get(instance_name)
end

#instantiate_or_raise(path, message) ⇒ Object

Instantiate an automate instance at path or raise an exception with the message provided if the instantiation returns nil (not found).



11
12
13
14
15
16
17
18
# File 'lib/miq_dev_util/automate.rb', line 11

def instantiate_or_raise(path, message)
  object = @evm.instantiate(path)
  if object.nil?
    raise message
  end

  object
end

#resolve_vm(lookup_order: [:rootvm, :dialog_id, :provision], dialog_name: 'dialog_target_server') ⇒ Object

Condense multiple types of VM lookups into one call. This is useful when making an Automate method generic enough to be used during provisioning, with a custom button, or as a catalog item.

Lookup methods used and their order can be overridden by specifying :lookup_order

The dialog name that may hold the miq ID is specified via :dialog_name



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/miq_dev_util/automate.rb', line 41

def resolve_vm(lookup_order: [:rootvm, :dialog_id, :provision],
               dialog_name: 'dialog_target_server')

  vm = nil
  lookup_order.each do |lu_method|
    vm = vm_lookup_by(lu_method, dialog_name)

    # If we found a VM we can stop looking
    break unless vm.nil?
  end

  vm
end

#wait_for_automation_request(request, options = {}) ⇒ Object

Wait until the automation request shows as finished. Wait at most max_wait seconds. Check the status ever poll_interval seconds.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/miq_dev_util/automate.rb', line 68

def wait_for_automation_request(request, options = {})
  max_wait = options['max_wait'] || 600
  poll_interval = options['poll_interval'] || 15

  start_time = Time.now

  while (Time.now - start_time) < max_wait
    request = $evm.vmdb(:automation_request).find(request.id)
    return request if request['request_state'] == 'finished'
    sleep(poll_interval)
  end

  raise 'Automation request wait time exceeded.'
end

#zone_aware_vm_automation_request(vm, options, userid = 'admin', auto_approve = false) ⇒ Object

Create an automation request that will run in the same zone as the VM’s EMS. Also set some values to make this have similar data to that which a method responding to a button press would have.



86
87
88
89
90
91
92
93
94
# File 'lib/miq_dev_util/automate.rb', line 86

def zone_aware_vm_automation_request(vm, options, userid = 'admin',
                                     auto_approve = false)
  options[:miq_zone] = vm.ext_management_system.zone_name
  options[:attrs] = {} if options[:attrs].nil?
  options[:attrs][:vm] = vm
  options[:attrs][:vm_id] = vm.id

  create_automation_request(options, userid, auto_approve)
end