Module: CIMI::Helper

Defined in:
lib/cimi/helpers/cimi_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_content_typeObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cimi/helpers/cimi_helper.rb', line 19

def current_content_type
  case request.content_type
    when 'application/json' then :json
    when 'text/xml', 'application/xml' then :xml
    else
      raise Deltacloud::Exceptions.exception_from_status(
        406,
        translate_error_code(406)[:message]
      )
  end
end

#deltacloud_create_method_for(cimi_entity) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cimi/helpers/cimi_helper.rb', line 74

def deltacloud_create_method_for(cimi_entity)
  case cimi_entity.to_s
    when "machine"                then "create_instance"
    when "machine_configuration"  then "create_hardware_profile"
    when "machine_image"          then "create_image"
    when "volume"                 then "create_storage_volume"
    when "volume_image"           then "create_storage_snapshot"
    else "create_#{cimi_entity}"
  end

end

#expand?(collection) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/cimi/helpers/cimi_helper.rb', line 31

def expand?(collection)
  params['$expand'] == '*' ||
    (params['$expand'] || '').split(',').include?(collection.to_s)
end

#from_kibibyte(value, unit = "GB") ⇒ Object

e.g. convert volume to GB for deltacloud driver



66
67
68
69
70
71
72
# File 'lib/cimi/helpers/cimi_helper.rb', line 66

def from_kibibyte(value, unit="GB")
  case unit
    when "GB" then ((value.to_f)/1024/1024)
    when "MB" then ((value.to_f)/1024)
    else nil
  end
end

#headers_for_create(resource) ⇒ Object

Set status to 201 and a Location header



42
43
44
45
# File 'lib/cimi/helpers/cimi_helper.rb', line 42

def headers_for_create(resource)
  status 201
  headers 'Location' => resource.id
end

#href_id(href, entity) ⇒ Object



47
48
49
50
# File 'lib/cimi/helpers/cimi_helper.rb', line 47

def href_id(href, entity)
  split_on = self.send(:"#{entity.to_s}_url")
  href.split("#{split_on}/").last
end

#no_content_with_status(code = 200) ⇒ Object



36
37
38
39
# File 'lib/cimi/helpers/cimi_helper.rb', line 36

def no_content_with_status(code=200)
  body ''
  status code
end

#to_kibibyte(value, unit) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cimi/helpers/cimi_helper.rb', line 52

def to_kibibyte(value, unit)
  #value may be a string. convert to_f
  value = value.to_f # not to_i because e.g. 0.5 GB
  case unit
  when "GB"
    (value*1024*1024).to_i
  when "MB"
    (value*1024).to_i
  else
    nil # should probably be exploding something here...
  end
end