From: Michal Fojtik <mfoj...@redhat.com> * HardwareProfile model used :name attribute as an ID, which is an exception to other Deltacloud models. This patch will unify this naming, so all models in Deltacloud should have the :id and :name parameter.
Signed-off-by: Michal fojtik <mfoj...@redhat.com> --- server/lib/deltacloud/drivers/base_driver.rb | 31 +++--- server/lib/deltacloud/models/hardware_profile.rb | 120 +++++++++++----------- server/views/hardware_profiles/index.html.haml | 2 +- server/views/hardware_profiles/show.html.haml | 4 +- server/views/hardware_profiles/show.xml.haml | 3 +- 5 files changed, 78 insertions(+), 82 deletions(-) diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb index f5124f4..bead6cf 100644 --- a/server/lib/deltacloud/drivers/base_driver.rb +++ b/server/lib/deltacloud/drivers/base_driver.rb @@ -69,19 +69,13 @@ module Deltacloud ExceptionHandler::exceptions(&block) end - def self.define_hardware_profile(name,&block) + def self.define_hardware_profile(profile_id, &block) @hardware_profiles ||= [] - hw_profile = @hardware_profiles.find{|e| e.name == name} + hw_profile = @hardware_profiles.find{|e| e.id == profile_id } return if hw_profile - hw_profile = ::Deltacloud::HardwareProfile.new( name, &block ) + hw_profile = ::Deltacloud::HardwareProfile.new(profile_id, &block ) @hardware_profiles << hw_profile hw_profile.params - # FIXME: Features - #unless hw_params.empty? - # feature :instances, :hardware_profiles do - # decl.operation(:create) { add_params(hw_params) } - # end - #end end def self.hardware_profiles @@ -89,14 +83,14 @@ module Deltacloud @hardware_profiles end - def hardware_profiles(credentials, opts = nil) + def hardware_profiles(credentials, opts = {}) results = self.class.hardware_profiles filter_hardware_profiles(results, opts) end - def hardware_profile(credentials, name) - name = name[:id] if name.kind_of? Hash - hardware_profiles(credentials, :id => name).first + def hardware_profile(credentials, profile_id) + profile_id = profile_id[:id] if profile_id.kind_of? Hash + hardware_profiles(credentials, :id => profile_id).first end def filter_hardware_profiles(profiles, opts) @@ -104,25 +98,24 @@ module Deltacloud if v = opts[:architecture] profiles = profiles.select { |hwp| hwp.include?(:architecture, v) } end - # As a request param, we call 'name' 'id' if v = opts[:id] - profiles = profiles.select { |hwp| hwp.name == v } + profiles = profiles.select { |hwp| hwp.id == v } end end profiles end - def find_hardware_profile(credentials, name, image_id) + def find_hardware_profile(credentials, profile_id, image_id) hwp = nil if name - unless hwp = hardware_profiles(credentials, :id => name).first + unless hwp = hardware_profile(credentials, profile_id) raise BackendError.new(400, "bad-hardware-profile-name", - "Hardware profile '#{name}' does not exist", nil) + "Hardware profile '#{name}' does not exist", nil) end else unless image = image(credentials, :id=>image_id) raise BackendError.new(400, "bad-image-id", - "Image with ID '#{image_id}' does not exist", nil) + "Image with ID '#{image_id}' does not exist", nil) end hwp = hardware_profiles(credentials, :architecture=>image.architecture).first diff --git a/server/lib/deltacloud/models/hardware_profile.rb b/server/lib/deltacloud/models/hardware_profile.rb index f9678b0..6b6370b 100644 --- a/server/lib/deltacloud/models/hardware_profile.rb +++ b/server/lib/deltacloud/models/hardware_profile.rb @@ -15,7 +15,7 @@ # under the License. module Deltacloud - class HardwareProfile + class HardwareProfile < BaseModel UNITS = { :memory => "MB", @@ -28,6 +28,62 @@ module Deltacloud UNITS[name] end + class << self + def property(prop) + define_method(prop) do |*args| + values, opts, *ignored = *args + unless values.nil? + @properties[prop] = Property.new(prop, values, opts || {}) + end + @properties[prop] + end + end + end + + attr_accessor :name + + property :cpu + property :architecture + property :memory + property :storage + + def initialize(profile_id, &block) + @properties = {} + super(:id => profile_id) + result = instance_eval(&block) if block_given? + @name ||= profile_id + result + end + + def each_property(&block) + @properties.each_value { |prop| yield prop } + end + + def properties + @properties.values + end + + def property(name) + @properties[name.to_sym] + end + + def default?(prop, v) + property(prop) && property(prop).default.to_s == v + end + + def include?(prop, v) + return false unless p = property(prop) + return true if p.kind == :range and (p.first..p.last).include?(v) + return true if p.kind == :enum and p.values.include?(v) + false + end + + def params + @properties.values.inject([]) { |m, prop| + m << prop.to_param + }.compact + end + class Property attr_reader :name, :kind, :default # kind == :range @@ -82,10 +138,10 @@ module Deltacloud # overide fixed values. # # when :fixed then (v == @default.to_s) - when :fixed then true - when :range then match_type?(first, v) and (first..last).include?(v) - when :enum then match_type?(values.first, v) and values.include?(v) - else false + when :fixed then true + when :range then match_type?(first, v) and (first..last).include?(v) + when :enum then match_type?(values.first, v) and values.include?(v) + else false end end @@ -115,59 +171,5 @@ module Deltacloud v.to_s end end - - class << self - def property(prop) - define_method(prop) do |*args| - values, opts, *ignored = *args - instvar = :"@#{prop}" - unless values.nil? - @properties[prop] = Property.new(prop, values, opts || {}) - end - @properties[prop] - end - end - end - - attr_reader :name - property :cpu - property :architecture - property :memory - property :storage - - def initialize(name,&block) - @properties = {} - @name = name - instance_eval &block if block_given? - end - - def each_property(&block) - @properties.each_value { |prop| yield prop } - end - - def properties - @properties.values - end - - def property(name) - @properties[name.to_sym] - end - - def default?(prop, v) - property(prop) && property(prop).default.to_s == v - end - - def include?(prop, v) - return false unless p = property(prop) - return true if p.kind == :range and (p.first..p.last).include?(v) - return true if p.kind == :enum and p.values.include?(v) - false - end - - def params - @properties.values.inject([]) { |m, prop| - m << prop.to_param - }.compact - end end end diff --git a/server/views/hardware_profiles/index.html.haml b/server/views/hardware_profiles/index.html.haml index 011e80f..f02cb53 100644 --- a/server/views/hardware_profiles/index.html.haml +++ b/server/views/hardware_profiles/index.html.haml @@ -5,7 +5,7 @@ %ul{ :'data-role' => :listview, :'data-inset' => :true } - for profile in order_hardware_profiles(@hardware_profiles) %li{ :'data-theme' => 'c'} - %a{ :href => hardware_profile_url(profile.name)} + %a{ :href => hardware_profile_url(profile.id)} %img{ :class => 'ui-link-thumb', :src => '/images/profile.png'} %h3= profile.name %p diff --git a/server/views/hardware_profiles/show.html.haml b/server/views/hardware_profiles/show.html.haml index 033e94d..3fbb16f 100644 --- a/server/views/hardware_profiles/show.html.haml +++ b/server/views/hardware_profiles/show.html.haml @@ -1,11 +1,11 @@ =header "Hardware profiles" -=subheader @hardware_profile.name +=subheader @hardware_profile.name || @hardware_profile.id %div{ :'data-role' => :content, :'data-theme' => 'c'} %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'd'} %li{ :'data-role' => 'list-divider'} Name %li - %p{ :'data-role' => 'fieldcontain'}=@hardware_profile.name + %p{ :'data-role' => 'fieldcontain'}=@hardware_profile.name || @hardware_profile.id - @hardware_profile.each_property do |p| %li{ :'data-role' => 'list-divider'} #{p.name.to_s.titlecase} %li diff --git a/server/views/hardware_profiles/show.xml.haml b/server/views/hardware_profiles/show.xml.haml index 21e488c..677674f 100644 --- a/server/views/hardware_profiles/show.xml.haml +++ b/server/views/hardware_profiles/show.xml.haml @@ -1,6 +1,7 @@ - unless defined?(partial) !!! XML -%hardware_profile{ :href => hardware_profile_url(@hardware_profile.name), :id => @hardware_profile.name } +%hardware_profile{ :href => hardware_profile_url(@hardware_profile.id), :id => @hardware_profile.id } + %id= @hardware_profile.id %name< = @hardware_profile.name - @hardware_profile.each_property do |prop| -- 1.7.10.2