Module: Chef::Knife::XapiVmSelect

Included in:
XapiGuestStart, XapiGuestStop
Defined in:
lib/chef/knife/xapi_vmselect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/chef/knife/xapi_vmselect.rb', line 5

def self.included(includer)
  includer.class_eval do
  option :uuid,
    :short => "-U",
    :long => "--uuid",
    :description => "Treat the label as a UUID not a name label"
  end
end

Instance Method Details

#select_vm(name) ⇒ Object



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
# File 'lib/chef/knife/xapi_vmselect.rb', line 14

def select_vm(name)
  if name.nil?
    ui.msg "Must Provide VM Name"
    ui.msg "Usage: " + banner
    exit 1
  end

  vms = []
  if config[:uuid]
    vms << xapi.VM.get_by_uuid(name).flatten
  else
    vms << xapi.VM.get_by_name_label(name).flatten
  end
  vms.flatten!

  if vms.empty?
    ui.msg ui.color "could not find vm named #{name}", :red
    exit 1
  end

  if vms.length > 1
    vm = user_select(vms)
    if vm == :all 
      return vms
    end
  else
    vm = vms.first
  end
  vm
end