From: Michal Fojtik <[email protected]>
Signed-off-by: Michal fojtik <[email protected]> --- server/lib/cimi/drivers/drivers.rb | 55 +++++++++++++++++++++++++++ server/lib/cimi/drivers/mock/mock_driver.rb | 53 ++++++++++++++++++++++++++ server/lib/cimi/server.rb | 18 ++------ 3 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 server/lib/cimi/drivers/drivers.rb create mode 100644 server/lib/cimi/drivers/mock/mock_driver.rb diff --git a/server/lib/cimi/drivers/drivers.rb b/server/lib/cimi/drivers/drivers.rb new file mode 100644 index 0000000..4d1dd2e --- /dev/null +++ b/server/lib/cimi/drivers/drivers.rb @@ -0,0 +1,55 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +module CIMI + + module Drivers + + DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock + + def driver_config + if Thread::current[:drivers].nil? + Thread::current[:drivers] = {} + top_srcdir = File.join(File.dirname(__FILE__), '..', '..', '..') + Dir[File.join(top_srcdir, 'config', 'drivers', '*.yaml')].each do |driver_file| + Thread::current[:drivers].merge!(YAML::load(File::read(driver_file))) + end + end + Thread::current[:drivers] + end + + def driver_name + driver_config[:"#{driver_symbol}"][:name] + end + + def driver_symbol + (Thread.current[:driver] || DRIVER).to_sym + end + + def driver_class + basename = driver_config[:"#{driver_symbol}"][:class] || "#{driver_name}" + CIMI::Drivers.const_get(driver_name).const_get(basename) + end + + def driver_source_name + File.join("cimi", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb") + end + + def driver + require driver_source_name + @driver ||= driver_class.new + end + end +end diff --git a/server/lib/cimi/drivers/mock/mock_driver.rb b/server/lib/cimi/drivers/mock/mock_driver.rb new file mode 100644 index 0000000..bcb1e2c --- /dev/null +++ b/server/lib/cimi/drivers/mock/mock_driver.rb @@ -0,0 +1,53 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +require 'deltacloud/drivers/mock/mock_driver' + +class CIMI::Drivers::Mock + include Deltacloud::Drivers::Mock + include CIMI::Model + + def initialize + @deltacloud_driver = MockDriver.new + end + + def machine_configuration(credentials, opts={}) + hwp = @deltacloud_driver.hardware_profile(credentials, opts[:id]) + convert_machine_description(hwp) + end + + def machine_configurations(credentials, opts={}) + hwp_list = @deltacloud_driver.hardware_profiles(credentials) + hwp_list.map { |hwp| convert_machine_description(hwp) } + end + + private + + def convert_machine_description(hwp) + MachineConfiguration.new( + :name => hwp.name, + :description => machine_conf_desc(hwp), + :cpu => hwp.cpu.value, + :created => Time.now.to_s, + :memory => { :quantity => hwp.memory.value, :units => hwp.memory.unit }, + :disks => [ { :capacity => { :quantity => hwp.storage.value, :units => hwp.storage.unit } } ] + ) + end + + def machine_conf_desc(hwp) + "Machine Configuration with #{hwp.memory.value} #{hwp.memory.unit} of memory and #{hwp.cpu.value} CPU" + end + +end diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb index ba925fa..39f72f5 100644 --- a/server/lib/cimi/server.rb +++ b/server/lib/cimi/server.rb @@ -112,19 +112,11 @@ global_collection :machine_configurations do with_capability :hardware_profile param :id, :string, :required control do - @profile = driver.hardware_profile(credentials, params[:id]) - if @profile - #setup the default values for a machine configuration - resource_default = get_resource_default "machine_configuration" - #get the actual values from profile - resource_value = { "name" => @profile.name,"uri" => @profile.name, - "href" => machine_configuration_url(@profile.name) } - #mixin actual values get from profile - @dmtfitem = resource_default["dmtfitem"].merge resource_value - show_resource "machine_configurations/show", "MachineConfiguration", - {"property" => "properties", "disk" => "disks", "operation" => "operations"} - else - report_error(404) + machine_conf = driver.machine_configuration(credentials, :id => params[:id]) + machine_conf.uri = machine_configuration_url(params[:id]) + respond_to do |format| + format.xml { machine_conf.to_xml } + format.json { machine_conf.to_json } end end end -- 1.7.4.4
