Class: VagrantPlugins::Skytap::API::Environment

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

Constant Summary collapse

RESOURCE_ROOT =
'/configurations'

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, env) ⇒ Environment

Returns a new instance of Environment.



45
46
47
48
# File 'lib/vagrant-skytap/api/environment.rb', line 45

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

Instance Attribute Details

#networksObject (readonly)

Returns the value of attribute networks.



41
42
43
# File 'lib/vagrant-skytap/api/environment.rb', line 41

def networks
  @networks
end

#provider_configObject (readonly)

Returns the value of attribute provider_config.



40
41
42
# File 'lib/vagrant-skytap/api/environment.rb', line 40

def provider_config
  @provider_config
end

#vmsObject (readonly)

Returns the value of attribute vms.



41
42
43
# File 'lib/vagrant-skytap/api/environment.rb', line 41

def vms
  @vms
end

Class Method Details

.check_vm_before_adding(env, vm) ⇒ Object



34
35
36
# File 'lib/vagrant-skytap/api/environment.rb', line 34

def check_vm_before_adding(env, vm)
  raise Errors::SourceVmNotStopped, name: env[:machine].name, url: vm.url unless vm.stopped?
end

.create!(env, vm_url) ⇒ Object



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

def create!(env, vm_url)
  vm = Vm.fetch(env, vm_url)
  check_vm_before_adding(env, vm)

  resp = env[:api_client].post(RESOURCE_ROOT, JSON.dump(template_id: vm.template_id, vm_ids: [vm.id]))
  fetch(env, resp['Location'])
end

.fetch(env, url) ⇒ Object



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

def fetch(env, url)
  resp = env[:api_client].get(url)
  new(JSON.load(resp.body), env)
end

.properties(env) ⇒ Object



30
31
32
# File 'lib/vagrant-skytap/api/environment.rb', line 30

def properties(env)
  EnvironmentProperties.read(env[:machine].env.local_data_path)
end

Instance Method Details

#add_vm(vm_url) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-skytap/api/environment.rb', line 83

def add_vm(vm_url)
  vm = Vm.fetch(env, vm_url)
  self.class.check_vm_before_adding(env, vm)
  vm_ids = vms.collect(&:id)

  update_with_retry(template_id: vm.template_id, vm_ids: [vm.id])
  new_vm_ids = vms.collect(&:id) - vm_ids
  get_vm_by_id(new_vm_ids.last)
end

#current_vmObject



60
61
62
# File 'lib/vagrant-skytap/api/environment.rb', line 60

def current_vm
  vms.find(&:current_machine?)
end

#deleteObject



76
77
78
79
80
81
# File 'lib/vagrant-skytap/api/environment.rb', line 76

def delete
  retry_while_resource_busy do
    api_client.delete(url)
    break
  end
end

#get_vm_by_id(id) ⇒ Object



56
57
58
# File 'lib/vagrant-skytap/api/environment.rb', line 56

def get_vm_by_id(id)
  vms.find{|vm|vm.id == id}
end

#propertiesObject



93
94
95
# File 'lib/vagrant-skytap/api/environment.rb', line 93

def properties
  @properties ||= EnvironmentProperties.new(env[:machine].env.local_data_path)
end

#refresh(attrs) ⇒ Object



70
71
72
73
74
# File 'lib/vagrant-skytap/api/environment.rb', line 70

def refresh(attrs)
  @vms = nil
  @networks = nil
  super
end