From: David Lutterkort lutter@redhat.com
* new class Feature * base_driver: add features and support for defining them in drivers * views (xml): list features for each collection in api.xml * views (html): produce table with all features for a driver --- server/libexec/app/views/api/show.html.haml | 21 ++++++++++++++++- server/libexec/app/views/api/show.xml.haml | 4 +++ server/libexec/lib/deltacloud/base_driver.rb | 14 +++++++++++ server/libexec/lib/deltacloud/feature.rb | 31 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletions(-) create mode 100644 server/libexec/lib/deltacloud/feature.rb
diff --git a/server/libexec/app/views/api/show.html.haml b/server/libexec/app/views/api/show.html.haml index 95bb744..f6712c5 100644 --- a/server/libexec/app/views/api/show.html.haml +++ b/server/libexec/app/views/api/show.html.haml @@ -3,5 +3,24 @@
%ul - for entry_point in @entry_points - %li + %li = link_to entry_point[0].to_s.titlecase, entry_point[1] + +%h2 Optional Features + +%table + %tr + %th Collection + %th Name + %th Constraints + - for coll in driver.class.features.keys.sort + - for feature in driver.features(coll) + %tr + %td= coll + %td= feature.name + %td + - first = true + - for constr in feature.constraints + = " and " unless first + - first = false + #{constr.name} <= #{constr.human_value} diff --git a/server/libexec/app/views/api/show.xml.haml b/server/libexec/app/views/api/show.xml.haml index a41acb8..5520fe9 100644 --- a/server/libexec/app/views/api/show.xml.haml +++ b/server/libexec/app/views/api/show.xml.haml @@ -1,3 +1,7 @@ %api{ :version=>@version, :driver=>DRIVER } - for entry_point in @entry_points %link{ :rel=>entry_point[0], :href=>entry_point[1] } + - for feature in driver.features(entry_point[0]) + %feature{ :name=>feature.name } + - for constr in feature.constraints + %constraint{ :name => constr.name, :unit => constr.unit } #{constr.value} diff --git a/server/libexec/lib/deltacloud/base_driver.rb b/server/libexec/lib/deltacloud/base_driver.rb index 5add111..03d3d5b 100644 --- a/server/libexec/lib/deltacloud/base_driver.rb +++ b/server/libexec/lib/deltacloud/base_driver.rb @@ -45,6 +45,20 @@ module Deltacloud self.class.hardware_profiles.find{|e| e.name == name } end
+ def self.features + @features ||= {} + end + + def self.feature(collection, name, &block) + features[collection] ||= [] + return if features[collection].find { |f| f.name == name } + features[collection] << Feature.new(name, &block) + end + + def features(collection) + self.class.features[collection] || [] + end + def self.define_instance_states(&block) machine = ::Deltacloud::StateMachine.new(&block) @instance_state_machine = machine diff --git a/server/libexec/lib/deltacloud/feature.rb b/server/libexec/lib/deltacloud/feature.rb new file mode 100644 index 0000000..5e20ba3 --- /dev/null +++ b/server/libexec/lib/deltacloud/feature.rb @@ -0,0 +1,31 @@ +class Deltacloud::Feature + attr_reader :name, :constraints + + class Constraint + attr_reader :name, :value, :unit + + def initialize(name, value, opts) + @name = name + @value = value + @unit = opts[:unit] || "count" + end + + def human_value + if unit == "count" + value.to_s + else + "#{value} #{unit}" + end + end + end + + def initialize(name, &block) + @name = name + instance_eval &block + end + + def constraint(name, value, opts = {}) + @constraints ||= [] + @constraints << Constraint.new(name, value, opts) + end +end