Class: Vcloud::Core::Vapp

Inherits:
Object
  • Object
show all
Extended by:
ComputeMetadata
Defined in:
lib/vcloud/core/vapp.rb

Defined Under Namespace

Modules: STATUS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ComputeMetadata

get_metadata

Constructor Details

#initialize(id) ⇒ Vcloud::Core::Vapp

Initialize a Vcloud::Core::Vapp

Parameters:

  • id (String)

    the vApp ID



12
13
14
15
16
17
# File 'lib/vcloud/core/vapp.rb', line 12

def initialize(id)
  unless id =~ /^#{self.class.id_prefix}-[-0-9a-f]+$/
    raise "#{self.class.id_prefix} id : #{id} is not in correct format"
  end
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/vcloud/core/vapp.rb', line 6

def id
  @id
end

Class Method Details

.get_by_child_vm_id(vm_id) ⇒ String

Return the ID of the vApp which contains a particular VM

Parameters:

  • vm_id (String)

    the ID of the VM to find the parent for

Returns:

  • (String)

    the vApp ID

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vcloud/core/vapp.rb', line 41

def self.get_by_child_vm_id(vm_id)
  raise ArgumentError, "Must supply a valid Vm id" unless vm_id =~ /^vm-[-0-9a-f]+$/
  vm_body = Vcloud::Core::Fog::ServiceInterface.new.get_vapp(vm_id)
  parent_vapp_link = vm_body.fetch(:Link).detect do |link|
    link[:rel] == Fog::RELATION::PARENT && link[:type] == Fog::ContentTypes::VAPP
  end
  unless parent_vapp_link
    raise RuntimeError, "Could not find parent vApp for VM '#{vm_id}'"
  end
  return self.new(parent_vapp_link.fetch(:href).split('/').last)
end

.get_by_name(name) ⇒ String

Return the ID of a named vApp

Parameters:

  • name (String)

    the name of the vApp to find

Returns:

  • (String)

    the vApp ID



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vcloud/core/vapp.rb', line 23

def self.get_by_name(name)
  q = Vcloud::Core::QueryRunner.new
  query_results = q.run('vApp', :filter => "name==#{name}")
  raise "Error finding vApp by name #{name}" unless query_results
  case query_results.size
  when 0
    raise "vApp #{name} not found"
  when 1
    return self.new(query_results.first[:href].split('/').last)
  else
    raise "found multiple vApp entities with name #{name}!"
  end
end

.get_by_name_and_vdc_name(name, vdc_name) ⇒ String

Find a vApp by name and vDC

Parameters:

  • name (String)

    name of the vApp to find

  • vdc_name (String)

    name of the vDC

Returns:

  • (String)

    the ID of the instance



106
107
108
109
110
# File 'lib/vcloud/core/vapp.rb', line 106

def self.get_by_name_and_vdc_name(name, vdc_name)
  fog_interface = Vcloud::Core::Fog::ServiceInterface.new
  attrs = fog_interface.get_vapp_by_name_and_vdc_name(name, vdc_name)
  self.new(attrs[:href].split('/').last) if attrs && attrs.key?(:href)
end

.id_prefixObject



167
168
169
# File 'lib/vcloud/core/vapp.rb', line 167

def self.id_prefix
  'vapp'
end

.instantiate(name, network_names, template_id, vdc_name) ⇒ String

Instantiate a vApp

Parameters:

  • name (String)

    Name to use when creating the vApp

  • network_names (Array)

    Array of Strings with names of Networks to connect to the vApp

  • template_id (String)

    The ID of the template to use when creating the vApp

  • vdc_name (String)

    The name of the vDC to create vApp in

Returns:

  • (String)

    the id of the created vApp



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vcloud/core/vapp.rb', line 119

def self.instantiate(name, network_names, template_id, vdc_name)
  Vcloud::Core.logger.info("Instantiating new vApp #{name} in vDC '#{vdc_name}'")
  fog_interface = Vcloud::Core::Fog::ServiceInterface.new
  networks = get_networks(network_names, vdc_name)

  attrs = fog_interface.post_instantiate_vapp_template(
      fog_interface.vdc(vdc_name),
      template_id,
      name,
      InstantiationParams: build_network_config(networks)
  )
  self.new(attrs[:href].split('/').last) if attrs and attrs.key?(:href)
end

Instance Method Details

#hrefString

Return the href of vApp

Returns:

  • (String)

    the href of instance



75
76
77
# File 'lib/vcloud/core/vapp.rb', line 75

def href
  vcloud_attributes[:href]
end

#nameString

Return the name of vApp

Returns:

  • (String)

    the name of instance



68
69
70
# File 'lib/vcloud/core/vapp.rb', line 68

def name
  vcloud_attributes[:name]
end

#networksHash

Return the networks connected to vApp

Returns:

  • (Hash)

    a hash describing the networks



97
98
99
# File 'lib/vcloud/core/vapp.rb', line 97

def networks
  vcloud_attributes[:'ovf:NetworkSection'][:'ovf:Network']
end

#power_onBoolean

Power on vApp

Returns:

  • (Boolean)

    Returns true if the VM is running, false otherwise



160
161
162
163
164
165
# File 'lib/vcloud/core/vapp.rb', line 160

def power_on
  raise "Cannot power on a missing vApp." unless id
  return true if running?
  Vcloud::Core::Fog::ServiceInterface.new.power_on_vapp(id)
  running?
end

#update_custom_fields(custom_fields) ⇒ Boolean

Update custom_fields for vApp

Parameters:

  • custom_fields (Array)

    Array of Hashes describing the custom fields

Returns:

  • (Boolean)

    return true or throws error



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vcloud/core/vapp.rb', line 137

def update_custom_fields(custom_fields)
  return if custom_fields.nil?
  fields = custom_fields.collect do |field|
    user_configurable = field[:user_configurable] || true
    type              = field[:type] || 'string'
    password          = field[:password] || false

    {
      :id                => field[:name],
      :value             => field[:value],
      :user_configurable => user_configurable,
      :type              => type,
      :password          => password
    }
  end

  Vcloud::Core.logger.debug("adding custom fields #{fields.inspect} to vapp #{@id}")
  Vcloud::Core::Fog::ServiceInterface.new.put_product_sections(@id, fields)
end

#vcloud_attributesHash

Return the vCloud data associated with vApp

Returns:

  • (Hash)

    the complete vCloud data for vApp



56
57
58
# File 'lib/vcloud/core/vapp.rb', line 56

def vcloud_attributes
  Vcloud::Core::Fog::ServiceInterface.new.get_vapp(id)
end

#vdc_idString

Return the ID of the vDC containing vApp

Returns:

  • (String)

    the ID of the vDC containing vApp



82
83
84
85
# File 'lib/vcloud/core/vapp.rb', line 82

def vdc_id
  link = vcloud_attributes[:Link].detect { |l| l[:rel] == Fog::RELATION::PARENT && l[:type] == Fog::ContentTypes::VDC }
  link ? link[:href].split('/').last : raise('a vapp without parent vdc found')
end

#vmsHash

Return the VMs within vApp

Returns:

  • (Hash)

    the VMs contained in the vApp



90
91
92
# File 'lib/vcloud/core/vapp.rb', line 90

def vms
  vcloud_attributes[:Children][:Vm]
end