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) ⇒ Vapp

Returns a new instance of Vapp.



8
9
10
11
12
13
# File 'lib/vcloud/core/vapp.rb', line 8

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) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vcloud/core/vapp.rb', line 29

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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vcloud/core/vapp.rb', line 15

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) ⇒ Object



71
72
73
74
75
# File 'lib/vcloud/core/vapp.rb', line 71

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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vcloud/core/vapp.rb', line 77

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

#hrefObject



54
55
56
# File 'lib/vcloud/core/vapp.rb', line 54

def href
  vcloud_attributes[:href]
end

#nameObject



50
51
52
# File 'lib/vcloud/core/vapp.rb', line 50

def name
  vcloud_attributes[:name]
end

#networksObject



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

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

#power_onObject



111
112
113
114
115
116
# File 'lib/vcloud/core/vapp.rb', line 111

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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vcloud/core/vapp.rb', line 91

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_attributesObject



41
42
43
# File 'lib/vcloud/core/vapp.rb', line 41

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

#vdc_idObject



58
59
60
61
# File 'lib/vcloud/core/vapp.rb', line 58

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

#vmsObject



63
64
65
# File 'lib/vcloud/core/vapp.rb', line 63

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