From: marios <[email protected]>
Signed-off-by: marios <[email protected]> --- server/lib/cimi/collections/volume_templates.rb | 69 ++++++++++++++++++++++++ server/lib/cimi/models/volume_template.rb | 71 +++++++++++++++++++++++++ server/lib/db.rb | 1 + server/lib/db/provider.rb | 5 ++ server/lib/db/volume_template.rb | 12 +++++ 5 files changed, 158 insertions(+) create mode 100644 server/lib/cimi/collections/volume_templates.rb create mode 100644 server/lib/db/volume_template.rb diff --git a/server/lib/cimi/collections/volume_templates.rb b/server/lib/cimi/collections/volume_templates.rb new file mode 100644 index 0000000..6ba7a62 --- /dev/null +++ b/server/lib/cimi/collections/volume_templates.rb @@ -0,0 +1,69 @@ +# 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::Collections + class VolumeTemplates < Base + + set :capability, lambda { |m| driver.respond_to? m } + + collection :volume_templates do + + operation :index, :with_capability => :storage_volumes do + description "Retrieve the Volume Template Collection" + control do + volume_template = VolumeTemplate.list(self).filter_by(params['$select']) + respond_to do |format| + format.xml { volume_template.to_xml } + format.json { volume_template.to_json } + end + end + end + + operation :show, :with_capability => :storage_volume do + description "Get a specific VolumeTemplate" + control do + volume_template = VolumeTemplate.find(params[:id], self) + respond_to do |format| + format.xml { volume_template.to_xml } + format.json { volume_template.json } + end + end + end + + operation :create, :with_capability => :create_storage_volume do + description "Create new VolumeTemplate" + control do + content_type = grab_content_type(request.content_type, request.body) + new_template = CIMI::Model::VolumeTemplate.create(request.body.read, self, content_type) + headers_for_create new_template + respond_to do |format| + format.json { new_template.to_json } + format.xml { new_template.to_xml } + end + end + end + + operation :destroy, :with_capability => :destroy_storage_volume do + description "Delete a specified VolumeTemplate" + control do + CIMI::Model::VolumeTemplate.delete!(params[:id], self) + no_content_with_status(200) + end + end + + end + + end +end diff --git a/server/lib/cimi/models/volume_template.rb b/server/lib/cimi/models/volume_template.rb index b2ac323..f40a300 100644 --- a/server/lib/cimi/models/volume_template.rb +++ b/server/lib/cimi/models/volume_template.rb @@ -19,7 +19,78 @@ class CIMI::Model::VolumeTemplate < CIMI::Model::Base href :volume_config href :volume_image + + array :meter_templates do + end + + href :event_log_template + array :operations do scalar :rel, :href end + + def self.find(id, context) + if id==:all + if context.driver.respond_to? :volume_templates + context.driver.volume_templates(context.credentials, {:env=>context}) + else + Deltacloud::Database::VolumeTemplate.all( + 'provider.driver' => driver_symbol.to_s, + 'provider.url' => current_provider + ).map { |t| from_db(t, context) } + end + else + if context.driver.respond_to? :volume_template + context.driver.volume_template(context.credentials, id, :env=>context) + else + template = Deltacloud::Database::VolumeTemplate.first( + 'provider.driver' => driver_symbol.to_s, + 'provider.url' => current_provider, + :id => id + ) + raise CIMI::Model::NotFound unless template + from_db(template, context) + end + end + end + + def self.create(body, context, type) + input = (type == :xml)? XmlSimple.xml_in(body, {"ForceArray"=>false,"NormaliseSpace"=>2}) : JSON.parse(body) + input['property'] ||= [] + vol_image = input['volumeImage']['href'] if input['volumeImage'] + new_template = current_db.volume_templates.new( + :name => input['name'], + :description => input['description'], + :volume_config => input['volumeConfig']['href'], + :volume_image => vol_image, + :ent_properties => input['property'].inject({}) { |r, p| r[p['key']]=p['content']; r }, + :be_kind => 'volume_template', + :be_id => '' + ) + new_template.save! + from_db(new_template, context) + end + + def self.delete!(id, context) + current_db.volume_templates.first(:id => id).destroy + end + +private + + def self.from_db(model, context) + self.new( + :id => context.volume_template_url(model.id), + :name => model.name, + :description => model.description, + :volume_config => {:href => model.volume_config}, + :volume_image => {:href => model.volume_image}, + :property => model.ent_properties, + :operations => [ + { :href => context.destroy_volume_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete' } + ] + ) + end + + + end diff --git a/server/lib/db.rb b/server/lib/db.rb index 9564b79..87a951d 100644 --- a/server/lib/db.rb +++ b/server/lib/db.rb @@ -10,6 +10,7 @@ module Deltacloud require_relative './db/entity' require_relative './db/machine_template' require_relative './db/address_template' + require_relative './db/volume_template' end DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}", 'db.sqlite') diff --git a/server/lib/db/provider.rb b/server/lib/db/provider.rb index 789bf3e..f9b3c47 100644 --- a/server/lib/db/provider.rb +++ b/server/lib/db/provider.rb @@ -11,6 +11,7 @@ module Deltacloud has n, :entities has n, :machine_templates has n, :address_templates + has n, :volume_templates # This is a workaround for strange bug in Fedora MRI: # @@ -22,6 +23,10 @@ module Deltacloud AddressTemplate.all(:provider_id => self.id) end + def volume_templates + VolumeTemplate.all(:provider_id => self.id) + end + def entities Entity.all(:provider_id => self.id) end diff --git a/server/lib/db/volume_template.rb b/server/lib/db/volume_template.rb new file mode 100644 index 0000000..8658b8b --- /dev/null +++ b/server/lib/db/volume_template.rb @@ -0,0 +1,12 @@ +module Deltacloud + module Database + + class VolumeTemplate < Entity + belongs_to :provider + + property :volume_config, String + property :volume_image, String + end + + end +end -- 1.7.11.7
