module Deltacloud::Helpers::Application

Constants

NEW_BLOB_FORM_ID

Public Instance Methods

action_method(action, collection) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 46
def action_method(action, collection)
  http_method = collection.operation(action).http_method
  http_method || Sinatra::Rabbit::BaseCollection.http_method_for(action)
end
additional_features_for?(collection_name, apart_from = []) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 284
def additional_features_for?(collection_name, apart_from = [])
  features_arr = (driver.class.features[collection_name.to_sym] || [] )  - apart_from
  not features_arr.empty?
end
auth_feature_name() click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 37
def auth_feature_name
  return 'key' if driver.class.has_feature?(:instances, :authentication_key)
  return 'password' if driver.class.has_feature?(:instances, :authentication_password)
end
cdata(text = nil, &block) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 148
def cdata(text = nil, &block)
  text ||= capture_haml(&block)
  "<![CDATA[#{text.strip}]]>"
end
driver_provider(d) click to toggle source

Reverse the entrypoints hash for a driver from drivers.yaml; note that d is a hash, not an actual driver object

# File lib/deltacloud/helpers/deltacloud_helper.rb, line 188
def driver_provider(d)
  result = {}
  if d[:entrypoints]
    d[:entrypoints].each do |kind, details|
      details.each do |prov, url|
        result[prov] ||= {}
        result[prov][kind] = url
      end
    end
  end
  result
end
filter_all(model) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 51
def filter_all(model)
  filter = {}
  filter.merge!(:id => params[:id]) if params[:id]
  filter.merge!(:architecture => params[:architecture]) if params[:architecture]
  filter.merge!(:owner_id => params[:owner_id]) if params[:owner_id]
  filter.merge!(:state => params[:state]) if params[:state]
  filter = {} if filter.keys.size.eql?(0)
  begin
    @benchmark = Benchmark.measure do
      @elements = driver.send(model.to_sym, credentials, filter)
    end
  rescue
    @exception = $!
  end
  if @elements
    headers['X-Backend-Runtime'] = @benchmark.real.to_s
    instance_variable_set(:"@#{model}", @elements)
    respond_to do |format|
      format.html { haml :"#{model}/index" }
      format.xml { haml :"#{model}/index" }
      format.json { @media_type=:xml; to_json(haml(:"#{model}/index")) }
    end
  else
    report_error(@exception.respond_to?(:code) ? @exception.code : 500)
  end
end
format_hardware_property(prop) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 249
def format_hardware_property(prop)
  return "&empty;" unless prop
  u = hardware_property_unit(prop)
  case prop.kind
  when :range
    "#{prop.first} #{u} - #{prop.last} #{u} (default: #{prop.default} #{u})"
  when :enum
    prop.values.collect{ |v| "#{v} #{u}"}.join(', ') + " (default: #{prop.default} #{u})"
  else
    "#{prop.value} #{u}"
  end
end
format_instance_profile(ip) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 262
def format_instance_profile(ip)
  o = ip.overrides.collect do |p, v|
    u = hardware_property_unit(p)
    "#{p} = #{v} #{u}"
  end
  if o.empty?
    nil
  else
    "with #{o.join(", ")}"
  end
end
header(title, opts={}, &block) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 201
def header(title, opts={}, &block)
  opts[:theme] ||= 'b'
  opts[:back] ||= 'true'
  capture_haml do
    haml_tag :div, :'data-role' => :header, :'data-theme' => opts[:theme], :'data-add-back-btn' => opts[:back] do
      haml_tag :a, :'data-rel' => :back do
        haml_concat "Back"
      end if opts[:back] == 'true'
      haml_tag :h1 do
        haml_concat title
      end
      block.call if block_given?
    end
  end
end
image_for_state(state) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 180
def image_for_state(state)
  capture_haml do
    haml_tag :img, :src => "/images/#{state}" % state.downcase, :title => state
  end
end
instance_action(name) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 114
def instance_action(name)
  original_instance = driver.instance(credentials, :id => params[:id])

  # If original instance doesn't include called action
  # return with 405 error (Method is not Allowed)
  unless driver.instance_actions_for(original_instance.state).include?(name.to_sym)
    return report_error(405)
  end

  @benchmark = Benchmark.measure do
    @instance = driver.send(:"#{name}_instance", credentials, params[:id])
  end

  headers['X-Backend-Runtime'] = @benchmark.real.to_s

  if name == :destroy
    response = respond_to do |format|
      format.html { redirect(instances_url) }
    end
    halt 204, response
  end

  unless @instance.class == Instance
    redirect instance_url(params[:id])
  else
    response = respond_to do |format|
      format.xml { haml :"instances/show" }
      format.html { haml :"instances/show" }
      format.json { xml_to_json("instances/show") }
    end
    halt 202, response
  end
end
instance_action_method(action) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 42
def instance_action_method(action)
  action_method(action, Sinatra::Rabbit::InstancesCollection)
end
new_blob_form_url(bucket) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 245
def new_blob_form_url(bucket)
  bucket_url(@bucket.name) + "/" + NEW_BLOB_FORM_ID
end
order_hardware_profiles(profiles) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 274
def order_hardware_profiles(profiles)
  #have to deal with opaque hardware profiles
  uncomparables = profiles.select{|x| x.cpu.nil? or x.memory.nil? }
  if uncomparables.empty?
    profiles.sort_by{|a| [a.cpu.default, a.memory.default] }
  else
    (profiles - uncomparables).sort_by{|a| [a.cpu.default, a.memory.default] } + uncomparables
  end
end
render_cdata(text) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 153
def render_cdata(text)
  "<![CDATA[#{text.strip}]]>" unless text.nil?
end
report_error(code=nil) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 104
def report_error(code=nil)
  @error, @code = (request.env['sinatra.error'] || @exception), code
  @code = 500 if not @code and not @error.class.method_defined? :code
  response.status = @code || @error.code
  respond_to do |format|
    format.xml {  haml :"errors/#{@code || @error.code}", :layout => false }
    format.html { haml :"errors/#{@code || @error.code}", :layout => :error }
  end
end
show(model) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 87
def show(model)
  @benchmark = Benchmark.measure do
    @element = driver.send(model, credentials, { :id => params[:id]} )
  end
  headers['X-Backend-Runtime'] = @benchmark.real.to_s
  instance_variable_set("@#{model}", @element)
  if @element
    respond_to do |format|
      format.html { haml :"#{model.to_s.pluralize}/show" }
      format.xml { haml :"#{model.to_s.pluralize}/show" }
      format.json { @media_type=:xml; to_json(haml(:"#{model.to_s.pluralize}/show")) }
    end
  else
    report_error(404)
  end
end
subheader(title, opts={}) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 217
def subheader(title, opts={})
  opts[:theme] ||= 'a'
  capture_haml do
    haml_tag :div, :'data-role' => :header, :'data-theme' => opts[:theme] do
      haml_tag :p, :class => 'inner-right' do
        haml_concat title
      end
    end
  end
end
supported_collections() { |c| ... } click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 21
def supported_collections
  collection_arr = []
  Deltacloud::Collections.deltacloud_modules.each do |m|
    m.collections.each do |c|
      # Get the required capability for the :index operation (like 'realms' or 'instance_state_machine')
      index_operation_capability = c.operation(:index).required_capability
      # Then use this capability to check if the 'capability' lambda defined
      # for the Sinatra::Base class evaluate to 'true'
      next if m.settings.respond_to?(:capability) and !m.settings.capability(index_operation_capability)
      yield c if block_given?
      collection_arr << c
    end
  end
  collection_arr
end
to_json(xml) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 83
def to_json(xml)
  Crack::XML.parse(xml).to_json
end
translate_error_code(code) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 228
def translate_error_code(code)
  case code
  when 400; { :message => "Bad Request" }
  when 401; { :message => "Unauthorized" }
  when 403; { :message => "Forbidden" }
  when 404; { :message => "Not Found" }
  when 405; { :message => "Method Not Allowed" }
  when 406; { :message => "Not Acceptable" }
  when 500; { :message => "Internal Server Error" }
  when 502; { :message => "Backend Server Error" }
  when 504; { :message => "Gateway Timeout" }
  when 501; { :message => "Not Supported" }
  end
end
xml_to_json(model) click to toggle source
# File lib/deltacloud/helpers/deltacloud_helper.rb, line 78
def xml_to_json(model)
  @media_type = :xml
  to_json(haml(:"#{model}"))
end