Module: Deltacloud::Helpers::Application
- Defined in:
- lib/deltacloud/helpers/deltacloud_helper.rb
Defined Under Namespace
Modules: SinatraHelper
Constant Summary collapse
- NEW_BLOB_FORM_ID =
'new_blob_form_d15cfd90'
- HTML_ESCAPE =
{ '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
Class Method Summary collapse
Instance Method Summary collapse
- #action_method(action, collection) ⇒ Object
- #additional_features_for?(collection_name, apart_from = []) ⇒ Boolean
- #auth_feature_name ⇒ Object
- #bt(trace) ⇒ Object
- #cdata(text = nil, &block) ⇒ Object
- #collections_to_json(collections) ⇒ Object
- #constraints_hash_for(collection_name, feature_name) ⇒ Object
- #current_provider ⇒ Object
-
#driver_provider(d) ⇒ Object
Reverse the entrypoints hash for a driver from drivers.yaml; note that
d
is a hash, not an actual driver object. - #filter_all(model) ⇒ Object
- #format_hardware_property(prop) ⇒ Object
- #format_instance_profile(ip) ⇒ Object
- #h(s) ⇒ Object
- #header(title, opts = {}, &block) ⇒ Object
- #image_for_state(state) ⇒ Object
- #instance_action(name) ⇒ Object
- #instance_action_method(action) ⇒ Object
- #link_to_action(action, url, method) ⇒ Object
- #link_to_format(format) ⇒ Object
-
#log ⇒ Object
Log errors to the same logger as we use for logging requests.
- #new_blob_form_url(bucket) ⇒ Object
- #order_hardware_profiles(profiles) ⇒ Object
- #render_cdata(text) ⇒ Object
- #report_error(code = nil, message = nil) ⇒ Object
- #request_headers ⇒ Object
- #show(model) ⇒ Object
- #subheader(title, opts = {}) ⇒ Object
- #translate_error_code(code) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
343 344 345 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 343 def Application.included(klass) klass.extend SinatraHelper end |
Instance Method Details
#action_method(action, collection) ⇒ Object
63 64 65 66 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 63 def action_method(action, collection) http_method = collection.operation(action).http_method http_method || Deltacloud::Rabbit::BaseCollection.http_method_for(action) end |
#additional_features_for?(collection_name, apart_from = []) ⇒ Boolean
319 320 321 322 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 319 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 ⇒ Object
54 55 56 57 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 54 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 |
#bt(trace) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 353 def bt(trace) return [] unless trace return trace.join("\n") if params['fulltrace'] app_path = File::("../../..", __FILE__) dots = false trace = trace.map { |t| t.match(%r{^#{app_path}(.*)$}) ? "$app#{$1}" : "..." }.select do |t| if t == "..." keep = ! dots dots = true else keep = true dots = false end keep end "[\nAbbreviated trace\n pass fulltrace=1 as query param to see everything\n $app = #{app_path}\n]\n" + trace.join("\n") end |
#cdata(text = nil, &block) ⇒ Object
182 183 184 185 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 182 def cdata(text = nil, &block) text ||= capture_haml(&block) "<![CDATA[#{text.strip}]]>" end |
#collections_to_json(collections) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 25 def collections_to_json(collections) r = { :version => settings.version, :driver => driver_symbol, :provider => Thread.current[:provider] || ENV['API_PROVIDER'], :links => collections.map { |c| { :rel => c.collection_name, :href => self.send(:"#{c.collection_name}_url"), :features => c.features.select { |f| driver.class.has_feature?(c.collection_name, f.name) }.map { |f| f.operations.map { |o| { :name => f.name, :rel => o.name, :params => o.params_array, :constraints => constraints_hash_for(c.collection_name, f.name) } } } } } } r[:provider] ||= 'default' JSON::dump(:api => r) end |
#constraints_hash_for(collection_name, feature_name) ⇒ Object
46 47 48 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 46 def constraints_hash_for(collection_name, feature_name) driver.class.constraints(:collection => collection_name, :feature => feature_name).inject({}) { |r, v| r[v[0]]=v[1];r } end |
#current_provider ⇒ Object
21 22 23 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 21 def current_provider Thread.current[:provider] || ENV['API_PROVIDER'] || 'default' end |
#driver_provider(d) ⇒ Object
Reverse the entrypoints hash for a driver from drivers.yaml; note that d
is a hash, not an actual driver object
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 222 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) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 68 def filter_all(model) begin @benchmark = Benchmark.measure { @elements = driver.send(model.to_sym, credentials, params) } rescue => e @exception = e 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 { JSON::dump({ model => @elements.map { |el| el.to_hash(self) }}) } end else report_error(@exception.respond_to?(:code) ? @exception.code : nil) end end |
#format_hardware_property(prop) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 284 def format_hardware_property(prop) return "∅" 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) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 297 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 |
#h(s) ⇒ Object
349 350 351 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 349 def h(s) s.to_s.gsub(/[&"><]/n) { |special| HTML_ESCAPE[special] } end |
#header(title, opts = {}, &block) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 235 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) ⇒ Object
214 215 216 217 218 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 214 def image_for_state(state) capture_haml do haml_tag :img, :src => "/images/#{state}" % state.downcase, :title => state end end |
#instance_action(name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 146 def instance_action(name) unless original_instance = driver.instance(credentials, :id => params[:id]) return report_error(403) end # 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 { JSON::dump(@instance.to_hash(self)) } end halt 202, response end end |
#instance_action_method(action) ⇒ Object
59 60 61 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 59 def instance_action_method(action) action_method(action, Deltacloud::Rabbit::InstancesCollection) end |
#link_to_action(action, url, method) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 191 def link_to_action(action, url, method) capture_haml do haml_tag :form, :method => :post, :action => url, :class => [:link, method], :'data-ajax' => 'false' do haml_tag :input, :type => :hidden, :name => '_method', :value => method haml_tag :button, :type => :submit, :'data-ajax' => 'false', :'data-inline' => "true" do haml_concat action end end end end |
#link_to_format(format) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 202 def link_to_format(format) return unless request.env['REQUEST_URI'] uri = request.env['REQUEST_URI'] return if uri.include?('format=') uri += uri.include?('?') ? "&format=#{format}" : "?format=#{format}" capture_haml do haml_tag :a, :href => uri, :'data-ajax' => 'false', :'data-icon' => 'grid' do haml_concat format.to_s.upcase end end end |
#log ⇒ Object
Log errors to the same logger as we use for logging requests
105 106 107 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 105 def log Deltacloud::Exceptions.logger(Deltacloud.default_frontend.logger) end |
#new_blob_form_url(bucket) ⇒ Object
280 281 282 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 280 def new_blob_form_url(bucket) bucket_url(@bucket.name) + "/" + NEW_BLOB_FORM_ID end |
#order_hardware_profiles(profiles) ⇒ Object
309 310 311 312 313 314 315 316 317 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 309 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) ⇒ Object
187 188 189 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 187 def render_cdata(text) "<![CDATA[#{text.strip}]]>" unless text.nil? end |
#report_error(code = nil, message = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 109 def report_error(code=nil, =nil) if !code.nil? @error = Deltacloud::Exceptions.exception_from_status(code, || translate_error_code(code)[:message]) @code = code = @error. else @error = request.env['sinatra.error'] || @exception @code = @error.respond_to?(:code) ? @error.code : 500 = @error.respond_to?(:message) ? @error. : translate_error_code(code)[:message] end response.status = @code backtrace = (@error.respond_to?(:backtrace) and !@error.backtrace.nil?) ? "\n\n#{@error.backtrace[0..20].join("\n")}\n\n" : '' if @code.to_s =~ /5(\d+)/ log.error(@code.to_s) { "[#{@error.class.to_s}] #{}#{backtrace}" } end respond_to do |format| format.xml { haml :"errors/common", :layout => false } format.json { JSON::dump({ :code => @code || @error.code, :message => , :error => @error.class.name }) } format.html { begin haml :"errors/common", :layout => :error rescue RuntimeError # If the HTML representation of error is missing, then try to report # it through XML @media_type=:xml haml :"errors/common", :layout => false end } end end |
#request_headers ⇒ Object
50 51 52 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 50 def request_headers env.inject({}){|acc, (k,v)| acc[$1.downcase] = v if k =~ /^http_(.*)/i; acc} end |
#show(model) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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 { JSON::dump(model => @element.to_hash(self)) } end else report_error(404) end end |
#subheader(title, opts = {}) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 251 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 |
#translate_error_code(code) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/deltacloud/helpers/deltacloud_helper.rb', line 262 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 409; { :message => "Resource Conflict" } when 500; { :message => "Internal Server Error" } when 502; { :message => "Backend Server Error" } when 504; { :message => "Gateway Timeout" } when 501; { :message => "Not Supported" } end end |