module VCloudSdk::Infrastructure

Shared functions by classes such as Client, Catalog and VDC Make sure instance variable @session is available

Constants

ERROR_STATUSES
SUCCESS_STATUS

Public Instance Methods

find_network_by_name(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 23
def find_network_by_name(name)
  @session.org.networks.each do |network_link|
    if network_link.name == name
      return VCloudSdk::Network.new(@session, network_link)
    end
  end

  fail ObjectNotFoundError, "Network '#{name}' is not found"
end
network_exists?(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 33
def network_exists?(name)
  @session.org.networks.any? do |network|
    network.name == name
  end
end

Private Instance Methods

catalog_exists?(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 51
def catalog_exists?(name)
  @session.org.catalogs.any? do |catalog|
    catalog.name == name
  end
end
catalogs() click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 39
def catalogs
  @session.org.catalogs.map do |catalog_link|
    VCloudSdk::Catalog.new(@session, catalog_link)
  end
end
connection() click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 67
def connection
  @session.connection
end
entity_xml() click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 144
def entity_xml
  connection.get(@link)
end
find_catalog_by_name(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 57
def find_catalog_by_name(name)
  @session.org.catalogs.each do |catalog_link|
    if catalog_link.name == name
      return VCloudSdk::Catalog.new(@session, catalog_link)
    end
  end

  fail ObjectNotFoundError, "Catalog '#{name}' is not found"
end
find_vdc_by_name(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 11
def find_vdc_by_name(name)
  vdc_link = @session.org.vdc_link(name)
  fail ObjectNotFoundError, "VDC #{name} not found" unless vdc_link
  VCloudSdk::VDC.new(@session, vdc_link)
end
list_catalogs() click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 45
def list_catalogs
  @session.org.catalogs.map do |catalog_link|
    catalog_link.name
  end
end
monitor_task( task, time_limit = @session.time_limit[:default], error_statuses = ERROR_STATUSES, success = SUCCESS_STATUS, delay = @session.delay, &b) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 71
def monitor_task(
  task,
  time_limit = @session.time_limit[:default],
  error_statuses = ERROR_STATUSES,
  success = SUCCESS_STATUS,
  delay = @session.delay,
  &b)

  iterations = time_limit / delay
  i = 0
  prev_progress = task.progress
  prev_status = task.status
  current_task = task
  while i < iterations
    Config.logger.debug %Q{
      #{current_task.urn} #{current_task.operation} is #{current_task.status}
    }

    if task_is_success(current_task, success)
      if b
        return b.call(current_task)
      else
        return current_task
      end
    elsif task_has_error(current_task, error_statuses)
      fail ApiRequestError,
           "Task #{task.urn} #{task.operation} did not complete successfully."
    elsif task_progressed?(current_task, prev_progress, prev_status)
      Config.logger.debug %Q{
        task status #{prev_status} =>
        #{current_task.status}, progress #{prev_progress}% =>
        #{current_task.progress}%, timer #{i} reset.
      }
      prev_progress = current_task.progress
      prev_status = current_task.status
      i = 0  # Reset clock if status changes or running task makes progress
      sleep(delay)
    else
      Config.logger.debug %Q{
        Approximately #{i * delay}s elapsed waiting for #{current_task.operation} to
        reach #{success.join("/")}/#{error_statuses.join("/")}.
        Checking again in #{delay} seconds.
      }
      if current_task.progress
        Config.logger.debug(
          "Task #{task.urn} progress: #{current_task.progress} %.")
      end
      sleep(delay)
    end
    current_task = connection.get(task)
    i += 1
  end
  fail ApiTimeoutError,
       "Task #{task.operation} did not complete within limit of #{time_limit} seconds."
end
task_has_error(current_task, error_statuses = ERROR_STATUSES) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 138
def task_has_error(current_task, error_statuses = ERROR_STATUSES)
  error_statuses.find do |s|
    s.downcase == current_task.status.downcase
  end
end
task_is_success(current_task, success = SUCCESS_STATUS) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 132
def task_is_success(current_task, success = SUCCESS_STATUS)
  success.find do |s|
    s.downcase == current_task.status.downcase
  end
end
task_progressed?(current_task, prev_progress, prev_status) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 127
def task_progressed?(current_task, prev_progress, prev_status)
  (current_task.progress && (current_task.progress != prev_progress)) ||
    (current_task.status && (current_task.status != prev_status))
end
vdc_exists?(name) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 17
def vdc_exists?(name)
  @session.org.vdcs.any? do |vdc|
    vdc.name == name
  end
end
wait_for_running_tasks(subject, subject_display) click to toggle source
# File lib/ruby_vcloud_sdk/infrastructure.rb, line 148
def wait_for_running_tasks(subject, subject_display)
  unless subject.running_tasks.empty?
    Config.logger.info "#{subject_display} has tasks in progress, wait until done."
    subject.running_tasks.each do |task|
      monitor_task(task)
    end
  end
end