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 RunstateOperations

RunstateOperations::RUNSTATE_RETRY

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 inherited from Resource

#delete, #reload, rest_name, short_name, #update, #url

Methods included from SpecifiedAttributes

#get_api_attribute, included

Constructor Details

#initialize(attrs, env) ⇒ Environment

Returns a new instance of Environment.



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

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

Instance Attribute Details

#networksObject (readonly)

Returns the value of attribute networks.



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

def networks
  @networks
end

#provider_configObject (readonly)

Returns the value of attribute provider_config.



100
101
102
# File 'lib/vagrant-skytap/api/environment.rb', line 100

def provider_config
  @provider_config
end

#vmsObject (readonly)

Returns the value of attribute vms.



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

def vms
  @vms
end

Class Method Details

.check_vms_before_adding(vms, environment = nil) ⇒ Boolean

Validates that a set of VMs can be used together in a REST call to create a new environment, or to add to an existing environment.

Parameters:

  • vms (Array)

    The [API::Vm] objects to validate

  • environment (API::Environment) (defaults to: nil)

    to validate against (optional)

Returns:

  • (Boolean)

    true, if no exceptions were raised

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vagrant-skytap/api/environment.rb', line 83

def check_vms_before_adding(vms, environment = nil)
  vms.each do |vm|
    raise Errors::SourceVmNotStopped, url: vm.url unless vm.stopped?
  end

  raise Errors::VmParentMismatch, vm_ids: vms.collect(&:id).join(', ') unless vms.collect(&:parent_url).uniq.count == 1

  if environment
    parent = vms.first.parent
    unless parent.region == environment.region
      raise Errors::RegionMismatch, environment_region: environment.region, vm_region: parent.region
    end
  end
  true
end

.create!(env, vms) ⇒ API::Environment

Makes the REST call to create a new environment, using the provided VMs as sources.

Parameters:

  • env (Hash)

    The environment hash passed in from the action

  • vms (Array)

    The source [API::Vm] objects

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-skytap/api/environment.rb', line 47

def create!(env, vms)
  check_vms_before_adding(vms)
  vm = vms.first

  args = {vm_ids: vms.collect(&:id)}.tap do |ret|
    if vm.from_template?
      ret[:template_id] = vm.template_id
    else
      ret[:configuration_id] = vm.configuration_id
    end
  end

  resp = env[:api_client].post(RESOURCE_ROOT, JSON.dump(args))
  new(JSON.load(resp.body), env)
end

.fetch(env, url) ⇒ API::Environment

Makes the REST call to retrieve an existing environment.

Parameters:

  • env (Hash)

    The environment hash passed in from the action

  • url (String)

    The url of the remote resource.

Returns:



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

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

.properties(env) ⇒ Object



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

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

Instance Method Details

#add_vms(vms) ⇒ Array

Makes the REST call to add VMs to this environment, using the provided VMs as sources.

Parameters:

  • vms (Array)

    The source [API::Vm] objects

Returns:

  • (Array)

    The new [API::Vm] objects



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vagrant-skytap/api/environment.rb', line 152

def add_vms(vms)
  return unless vms.present?
  self.class.check_vms_before_adding(vms, self)

  args = {vm_ids: vms.collect(&:id)}.tap do |ret|
    if vms.first.from_template?
      ret[:template_id] = vms.first.template_id
    else
      ret[:merge_configuration] = vms.first.configuration_id
    end
  end

  existing_vm_ids = self.vms.collect(&:id)
  update(args)
  get_vms_by_id(self.vms.collect(&:id) - existing_vm_ids)
end

#create_publish_set(attrs = {}) ⇒ Object



169
170
171
172
# File 'lib/vagrant-skytap/api/environment.rb', line 169

def create_publish_set(attrs={})
  resp = api_client.post("#{url}/publish_sets", JSON.dump(attrs))
  PublishSet.new(JSON.load(resp.body), self, env)
end

#get_vm_by_id(id) ⇒ Object



120
121
122
# File 'lib/vagrant-skytap/api/environment.rb', line 120

def get_vm_by_id(id)
  get_vms_by_id([id]).first
end

#get_vms_by_id(ids) ⇒ Object



116
117
118
# File 'lib/vagrant-skytap/api/environment.rb', line 116

def get_vms_by_id(ids)
  vms.select{|vm| ids.include?(vm.id)}
end

#propertiesObject



174
175
176
# File 'lib/vagrant-skytap/api/environment.rb', line 174

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

#publish_setsObject



130
131
132
133
134
# File 'lib/vagrant-skytap/api/environment.rb', line 130

def publish_sets
  @publish_sets ||= (get_api_attribute('publish_sets') || []).collect do |ps_attrs|
    PublishSet.new(ps_attrs, self, env)
  end
end

#refresh(attrs) ⇒ Object



136
137
138
139
140
141
# File 'lib/vagrant-skytap/api/environment.rb', line 136

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

#routable?Boolean

Indicates whether traffic will be routed between networks within this environment. (This is different from routing traffic to/from a network within another environment, which requires an ICNR tunnel.)

Returns:

  • (Boolean)


183
184
185
# File 'lib/vagrant-skytap/api/environment.rb', line 183

def routable?
  !!routable
end

#run!(vm_ids = nil) ⇒ Object



143
144
145
# File 'lib/vagrant-skytap/api/environment.rb', line 143

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