Class: VCloudSdk::VApp

Inherits:
Object
  • Object
show all
Includes:
Infrastructure, Powerable
Defined in:
lib/ruby_vcloud_sdk/vapp.rb

Constant Summary

Constants included from Infrastructure

Infrastructure::ERROR_STATUSES, Infrastructure::SUCCESS_STATUS

Instance Method Summary collapse

Methods included from Powerable

#power_off, #power_on, #status

Constructor Details

#initialize(session, link) ⇒ VApp

Returns a new instance of VApp.



11
12
13
14
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 11

def initialize(session, link)
  @session = session
  @link = link
end

Instance Method Details

#add_network_by_name(network_name, vapp_net_name = nil, fence_mode = Xml::FENCE_MODES[:BRIDGED]) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 103

def add_network_by_name(
    network_name,
    vapp_net_name = nil,
    fence_mode = Xml::FENCE_MODES[:BRIDGED])
  fail CloudError,
       "Invalid fence mode '#{fence_mode}'" unless Xml::FENCE_MODES
                                                     .each_value
                                                     .any? { |m| m == fence_mode }
  network = find_network_by_name(network_name)
  new_vapp_net_name = vapp_net_name.nil? ? network.name : vapp_net_name
  network_config_param = network_config_param(
                           network,
                           new_vapp_net_name,
                           fence_mode)
  payload = entity_xml.network_config_section
  payload.add_network_config(network_config_param)
  task = connection.put(payload.href,
                        payload,
                        Xml::MEDIA_TYPE[:NETWORK_CONFIG_SECTION])
  monitor_task(task)
  self
end

#deleteObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 20

def delete
  vapp = entity_xml
  vapp_name = name

  if is_status?(vapp, :POWERED_ON)
    fail CloudError,
         "vApp #{vapp_name} is powered on, power-off before deleting."
  end

  wait_for_running_tasks(vapp, "VApp #{vapp_name}")

  Config.logger.info "Deleting vApp #{vapp_name}."
  monitor_task(connection.delete(vapp.remove_link.href),
               @session.time_limit[:delete_vapp]) do |task|
    Config.logger.info "vApp #{vapp_name} deleted."
    return
  end

  fail ApiRequestError,
       "Fail to delete vApp #{vapp_name}"
end

#delete_network_by_name(name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 126

def delete_network_by_name(name)
  unless list_networks.any? { |network_name| network_name == name }
    fail ObjectNotFoundError,
         "Network '#{name}' is not found"
  end

  fail CloudError,
       %Q{
           Network '#{name}' is being used by one or more VMs.
           Please remove the NIC(s) in VM(s) that are in use of the network.
           Check logs for details.
         } if network_in_use?(name)

  payload = entity_xml.network_config_section
  payload.delete_network_config(name)
  task = connection.put(payload.href,
                        payload,
                        Xml::MEDIA_TYPE[:NETWORK_CONFIG_SECTION])
  monitor_task(task)
  self
end

#find_vm_by_name(name) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 76

def find_vm_by_name(name)
  entity_xml.vms.each do |vm|
    return VCloudSdk::VM.new(@session, vm.href) if vm.name == name
  end

  fail ObjectNotFoundError, "VM '#{name}' is not found"
end

#list_networksObject



96
97
98
99
100
101
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 96

def list_networks
  entity_xml
    .network_config_section
    .network_configs
    .map { |network_config| network_config.network_name }
end

#list_vmsObject



64
65
66
67
68
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 64

def list_vms
  entity_xml.vms.map do |vm|
    vm.name
  end
end

#nameObject



16
17
18
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 16

def name
  entity_xml.name
end

#recompose_from_vapp_template(catalog_name, template_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 42

def recompose_from_vapp_template(catalog_name, template_name)
  recompose_vapp_link = get_recompose_vapp_link

  Config.logger.info "Recomposing from template '#{template_name}' in catalog '#{catalog_name}'."
  catalog = find_catalog_by_name catalog_name

  template = catalog.find_vapp_template_by_name template_name

  task = connection.post recompose_vapp_link.href,
                         recompose_from_vapp_template_param(template)

  monitor_task task, @session.time_limit[:recompose_vapp]
  Config.logger.info "vApp #{name} is recomposed."
  self
end

#remove_vm_by_name(vm_name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 84

def remove_vm_by_name(vm_name)
  target_vm = find_vm_by_name vm_name
  recompose_vapp_link = get_recompose_vapp_link

  task = connection.post recompose_vapp_link.href,
                         remove_vm_param(target_vm)

  monitor_task task, @session.time_limit[:recompose_vapp]
  Config.logger.info "VM #{vm_name} is removed."
  self
end

#vm_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 70

def vm_exists?(name)
  entity_xml.vms.any? do |vm|
    vm.name == name
  end
end

#vmsObject



58
59
60
61
62
# File 'lib/ruby_vcloud_sdk/vapp.rb', line 58

def vms
  entity_xml.vms.map do |vm|
    VCloudSdk::VM.new(@session, vm.href)
  end
end