Module: CIMI::Helper

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

Instance Method Summary collapse

Instance Method Details

#deltacloud_create_method_for(cimi_entity) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cimi/helpers/cimi_helper.rb', line 95

def deltacloud_create_method_for(cimi_entity)
  case cimi_entity
    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)


19
20
21
22
# File 'lib/cimi/helpers/cimi_helper.rb', line 19

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



54
55
56
57
58
59
60
# File 'lib/cimi/helpers/cimi_helper.rb', line 54

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

#grab_content_type(request_content_type, request_body) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/cimi/helpers/cimi_helper.rb', line 62

def grab_content_type(request_content_type, request_body)
  case request_content_type
    when /xml$/ then :xml
    when /json$/ then :json
    else raise CIMI::Model::UnsupportedMediaType.new("Unsupported content type - only xml and json supported by CIMI")
    #guess_content_type(request_body)
  end
end

#guess_content_type(request_body) ⇒ Object

not being used - was called from above grab_content_type decided to reject anything not xml || json



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cimi/helpers/cimi_helper.rb', line 73

def guess_content_type(request_body)
  xml = json = false
  body = request_body.read
  request_body.rewind
  begin
    XmlSimple.xml_in(body)
    xml = true
  rescue Exception
    xml = false
  end
  begin
    JSON.parse(body)
    json = true
  rescue Exception
    json = false
  end
  if (json == xml) #both true or both false
    raise CIMI::Model::BadRequest.new("Couldn't guess content type of: #{body}")
  end
  type = (xml)? :xml : :json
end

#headers_for_create(resource) ⇒ Object

Set status to 201 and a Location header



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

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

#href_id(href, entity) ⇒ Object



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

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



24
25
26
27
# File 'lib/cimi/helpers/cimi_helper.rb', line 24

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

#to_kibibyte(value, unit) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cimi/helpers/cimi_helper.rb', line 40

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