module AvailabilityZoneHelper::TextualSummary
  #
  # Groups
  #

  def textual_group_relationships
    %i(ems_cloud instances)
  end

  def textual_group_tags
    %i(tags)
  end

  def textual_group_availability_zone_totals
    %i(block_storage_disk_capacity block_storage_disk_usage)
  end

  #
  # Items
  #

  def textual_ems_cloud
    textual_link(@record.ext_management_system)
  end

  def textual_instances
    label = ui_lookup(:tables => "vm_cloud")
    num   = @record.number_of(:vms)
    h     = {:label => label, :image => "vm", :value => num}
    if num > 0 && role_allows(:feature => "vm_show_list")
      h[:link]  = url_for(:action => 'show', :id => @availability_zone, :display => 'instances')
      h[:title] = "Show all #{label}"
    end
    h
  end

  def textual_block_storage_disk_capacity
    return nil unless @record.respond_to?(:block_storage_disk_capacity)
    {:value => number_to_human_size(@record.block_storage_disk_capacity.gigabytes, :precision => 2)}
  end

  def textual_block_storage_disk_usage
    return nil unless @record.respond_to?(:block_storage_disk_usage)
    {:value => number_to_human_size(@record.block_storage_disk_usage.bytes, :precision => 2)}
  end
end

module AvailabilityZoneHelper::TextualSummary
  #
  # Groups
  #

  def textual_group_relationships
    %i(ems_cloud instances)
  end

  def textual_group_tags
    %i(tags)
  end

  def textual_group_availability_zone_totals
    %i(block_storage_disk_capacity block_storage_disk_usage)
  end

  #
  # Items
  #

  def textual_ems_cloud
    textual_link(@record.ext_management_system)
  end

  def textual_instances
    label = ui_lookup(:tables => "vm_cloud")
    num   = @record.number_of(:vms)
    h     = {:label => label, :image => "vm", :value => num}
    if num > 0 && role_allows(:feature => "vm_show_list")
      h[:link]  = url_for(:action => 'show', :id => @availability_zone, :display => 'instances')
      h[:title] = "Show all #{label}"
    end
    h
  end

  def textual_block_storage_disk_capacity
    return nil unless @record.respond_to?(:block_storage_disk_capacity) && !@record.ext_management_system.provider.nil?
    {:value => number_to_human_size(@record.block_storage_disk_capacity.gigabytes, :precision => 2)}
  end

  def textual_block_storage_disk_usage
    return nil unless @record.respond_to?(:block_storage_disk_usage)
    {:value => number_to_human_size(@record.block_storage_disk_usage.bytes, :precision => 2)}
  end
end

Fix availability zone views when infra provider is not attached
Fix availability zone views when infra provider is not attached

When an OpenStack infrastructure provider is not attached to
the cloud provider, an "undefined method 'infra_ems'" error is
thrown. This fix adds a nil check on provider. If it is nil,
the disk capacity is not shown for the availability zone.

Fixes BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1291774
(transferred from ManageIQ/manageiq@7f0a1e0f8a084ae01b981b64e26b1379c710139d)
