You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/11/29 14:05:43 UTC
[PATCH core 5/6] CIMI: Added MachineTemplate collection and CRUD operations to model
From: Michal Fojtik <mf...@redhat.com>
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
server/lib/cimi/collections/machine_templates.rb | 73 ++++++++++++++++++++++++
server/lib/cimi/models/machine_template.rb | 63 ++++++++++++++++++++
2 files changed, 136 insertions(+)
create mode 100644 server/lib/cimi/collections/machine_templates.rb
diff --git a/server/lib/cimi/collections/machine_templates.rb b/server/lib/cimi/collections/machine_templates.rb
new file mode 100644
index 0000000..17dfbd1
--- /dev/null
+++ b/server/lib/cimi/collections/machine_templates.rb
@@ -0,0 +1,73 @@
+# 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 MachineTemplates < Base
+
+ set :capability, lambda { |t| true }
+
+ collection :machine_templates do
+
+ operation :index do
+ description "List all machine templates"
+ control do
+ machine_templates = MachineTemplate.list(self).filter_by(params['$select'])
+ respond_to do |format|
+ format.xml { machine_templates.to_xml }
+ format.json { machine_templates.to_json }
+ end
+ end
+ end
+
+ operation :show do
+ description "Show specific machine template"
+ control do
+ machine_template = MachineTemplate.find(params[:id], self)
+ respond_to do |format|
+ format.xml { machine_template.to_xml }
+ format.json { machine_template.to_json }
+ end
+ end
+ end
+
+ operation :create do
+ description "Create new machine template"
+ control do
+ if request.content_type.end_with?("json")
+ new_machine_template = MachineTemplate.create_from_json(request.body.read, self)
+ else
+ new_machine_template = MachineTemplate.create_from_xml(request.body.read, self)
+ end
+ status 201 # Created
+ headers 'Location' => new_machine_template.id
+ respond_to do |format|
+ format.json { new_machine_template.to_json }
+ format.xml { new_machine_template.to_xml }
+ end
+ end
+ end
+
+ operation :destroy do
+ description "Delete a specified machine template"
+ control do
+ MachineTemplate.delete!(params[:id], self)
+ no_content_with_status(200)
+ end
+ end
+
+ end
+
+ end
+end
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 7b64b98..fbc6326 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -40,4 +40,67 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
array :operations do
scalar :rel, :href
end
+
+ class << self
+ def find(id, context)
+ if id == :all
+ context.current_db.machine_template_entities.all.map { |t| from_db(t, context) }
+ else
+ template = context.current_db.machine_template_entities.first(:id => id)
+ raise CIMI::Model::NotFound unless template
+ from_db(template, context)
+ end
+ end
+
+ def create_from_json(body, context)
+ json = JSON.parse(body)
+ new_template = context.current_db.machine_template_entities.new(
+ :name => json['name'],
+ :description => json['description'],
+ :machine_config => json['machineConfig']['href'],
+ :machine_image => json['machineImage']['href'],
+ :ent_properties => json['properties'].to_json,
+ :be_kind => 'machine_template',
+ :be_id => ''
+ )
+ new_template.save!
+ from_db(new_template, context)
+ end
+
+ def create_from_xml(body, context)
+ xml = XmlSimple.xml_in(body)
+ new_template = context.current_db.machine_template_entities.new(
+ :name => xml['name'].first,
+ :description => xml['description'].first,
+ :machine_config => xml['machineConfig'].first['href'],
+ :machine_image => xml['machineImage'].first['href'],
+ :ent_properties => xml['properties'].first.to_json,
+ :be_kind => 'machine_template',
+ :be_id => ''
+ )
+ new_template.save!
+ from_db(new_template, context)
+ end
+
+ def delete!(id, context)
+ context.current_db.machine_template_entities.first(:id => id).destroy
+ end
+
+ private
+
+ def from_db(model, context)
+ self.new(
+ :id => context.machine_template_url(model.id),
+ :name => model.name,
+ :description => model.description,
+ :machine_config => { :href => model.machine_config },
+ :machine_image => { :href => model.machine_image },
+ :property => model.ent_properties,
+ :operations => [
+ { :href => context.destroy_machine_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete' }
+ ]
+ )
+ end
+ end
+
end
--
1.8.0
Re: [PATCH core 5/6] CIMI: Added MachineTemplate collection and
CRUD operations to model
Posted by David Lutterkort <lu...@redhat.com>.
On Thu, 2012-11-29 at 14:05 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
>
>
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
> server/lib/cimi/collections/machine_templates.rb | 73 ++++++++++++++++++++++++
> server/lib/cimi/models/machine_template.rb | 63 ++++++++++++++++++++
> 2 files changed, 136 insertions(+)
> create mode 100644 server/lib/cimi/collections/machine_templates.rb
ACK
David