Class: VagrantPlugins::VCenter::Action::IsCreated

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vcenter/action/is_created.rb

Overview

This class verifies if the VM has been created.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ IsCreated

Returns a new instance of IsCreated.



6
7
8
9
# File 'lib/vagrant-vcenter/action/is_created.rb', line 6

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_vcenter::action::is_created')
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-vcenter/action/is_created.rb', line 11

def call(env)
  vm_id = env[:machine].id
  if vm_id

    # VM is in the vagrant registry, now we need to check if it's
    # actually in vcenter

    # FIXME: this part needs some cleanup
    config = env[:machine].provider_config

    # FIXME: Raise a correct exception
    dc = config.vcenter_cnx.serviceInstance.find_datacenter(
         config.datacenter_name) or abort 'datacenter not found'

    root_vm_folder = dc.vmFolder

    vm = root_vm_folder.findByUuid(env[:machine].id)

    unless vm
      @logger.info('VM is in the vagrant registry but not in vcenter')
      # Clear the ID
      env[:machine].id = nil
      env[:result] = false
    end

    # VM is in the registry AND in vcenter
    @logger.info("VM has been created and ID is: [#{vm_id}]")
    env[:result] = true

  else
    # VM is not in the registry
    @logger.warn('VM has not been created')
    env[:result] = false
  end

  @app.call env
end