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!, #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.


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

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

Instance Attribute Details

#networksObject (readonly)

Returns the value of attribute networks


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

def networks
  @networks
end

#provider_configObject (readonly)

Returns the value of attribute provider_config


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

def provider_config
  @provider_config
end

#vmsObject (readonly)

Returns the value of attribute vms


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

def vms
  @vms
end

Class Method Details

.check_vm_before_adding(env, vm) ⇒ Object


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

def check_vm_before_adding(env, vm)
  raise Errors::NotTemplateVm, url: vm.url unless vm.from_template?
  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


92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-skytap/api/environment.rb', line 92

def add_vm(vm_url)
  vm = Vm.fetch(env, vm_url)
  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

#check_vm_before_adding(env, vm) ⇒ Object


102
103
104
105
# File 'lib/vagrant-skytap/api/environment.rb', line 102

def check_vm_before_adding(env, vm)
  raise Errors::RegionMismatch, environment_region: region, vm_region: vm.region unless vm.region == region
  self.class.check_vm_before_adding(env, vm)
end

#current_vmObject


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

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

#deleteObject


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

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

#get_vm_by_id(id) ⇒ Object


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

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

#propertiesObject


107
108
109
# File 'lib/vagrant-skytap/api/environment.rb', line 107

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

#refresh(attrs) ⇒ Object


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

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

#regionObject


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

def region
  get_api_attribute('region')
end

#run!(vm_ids = nil) ⇒ Object


81
82
83
# File 'lib/vagrant-skytap/api/environment.rb', line 81

def run!(vm_ids = nil)
  set_runstate :running, vm_ids: vm_ids
end