Class: VagrantPlugins::Skytap::API::Vm

Inherits:
Resource show all
Includes:
RunstateOperations
Defined in:
lib/vagrant-skytap/api/vm.rb

Constant Summary

Constants included from Busyable

Busyable::WAIT_ITERATION_PERIOD, Busyable::WAIT_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Resource

#attrs, #env

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RunstateOperations

#busy?, #poweroff!, #run!, #running?, #runstate, #set_runstate, #stop!, #stopped?, #suspend!, #wait_for_runstate, #wait_until_ready

Methods included from Busyable

#retry_while_resource_busy, #update_with_retry

Methods inherited from Resource

#reload, resource_name, #url

Methods included from SpecifiedAttributes

#get_api_attribute, included

Constructor Details

#initialize(attrs, environment, env) ⇒ Vm

Returns a new instance of Vm.



31
32
33
34
35
# File 'lib/vagrant-skytap/api/vm.rb', line 31

def initialize(attrs, environment, env)
  super
  @environment = environment
  @provider_config = env[:machine].provider_config
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



13
14
15
# File 'lib/vagrant-skytap/api/vm.rb', line 13

def environment
  @environment
end

#provider_configObject (readonly)

Returns the value of attribute provider_config.



12
13
14
# File 'lib/vagrant-skytap/api/vm.rb', line 12

def provider_config
  @provider_config
end

Class Method Details

.current_vm(env) ⇒ Object



24
25
26
27
28
# File 'lib/vagrant-skytap/api/vm.rb', line 24

def current_vm(env)
  if (environment = env[:environment]) && (machine = env[:machine]) && machine.id
    environment.get_vm_by_id(machine.id)
  end
end

.fetch(env, url) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/vagrant-skytap/api/vm.rb', line 18

def fetch(env, url)
  raise Errors::BadVmUrl, url: url unless url =~ /\/vms\/\d+/
  resp = env[:api_client].get(url)
  new(JSON.load(resp.body), env[:environment], env)
end

Instance Method Details

#communicate_ready?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/vagrant-skytap/api/vm.rb', line 94

def communicate_ready?
  current_machine? && machine.communicate.ready?
end

#credentialsObject



49
50
51
52
53
# File 'lib/vagrant-skytap/api/vm.rb', line 49

def credentials
  @credentials ||= (get_api_attribute('credentials') || []).collect do |cred_attrs|
    Credentials.new(cred_attrs, self, env)
  end
end

#current_machine?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/vagrant-skytap/api/vm.rb', line 90

def current_machine?
  id && machine && id == machine.id
end

#deleteObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-skytap/api/vm.rb', line 75

def delete
  begin
    retry_while_resource_busy do
      api_client.delete(url)
      break
    end
  rescue Errors::OperationFailed => ex
    raise Errors::OperationFailed, err: 'Failed to delete VM'
  end
end

#from_template?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/vagrant-skytap/api/vm.rb', line 59

def from_template?
  !!get_api_attribute('template_url')
end

#hardwareObject



55
56
57
# File 'lib/vagrant-skytap/api/vm.rb', line 55

def hardware
  get_api_attribute('hardware')
end

#interfacesObject



43
44
45
46
47
# File 'lib/vagrant-skytap/api/vm.rb', line 43

def interfaces
  @interfaces ||= (get_api_attribute('interfaces') || []).collect do |iface_attrs|
    Interface.new(iface_attrs, self, env)
  end
end

#machineObject



86
87
88
# File 'lib/vagrant-skytap/api/vm.rb', line 86

def machine
  env[:machine]
end

#refresh(attrs) ⇒ Object



37
38
39
40
41
# File 'lib/vagrant-skytap/api/vm.rb', line 37

def refresh(attrs)
  @interfaces = nil
  @credentials = nil
  super
end

#regionObject



68
69
70
71
72
73
# File 'lib/vagrant-skytap/api/vm.rb', line 68

def region
  return @region if @region
  resp = env[:api_client].get(get_api_attribute('template_url'))
  template_attrs = JSON.load(resp.body)
  @region = template_attrs['region']
end

#template_idObject



63
64
65
66
# File 'lib/vagrant-skytap/api/vm.rb', line 63

def template_id
  get_api_attribute('template_url') =~ /templates\/(\d+)/
  $1
end