You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2012/11/30 09:48:25 UTC

[5/8] git commit: CIMI: Added MachineTemplate collection and CRUD operations to model

CIMI: Added MachineTemplate collection and CRUD operations to model


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/ff59a160
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/ff59a160
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/ff59a160

Branch: refs/heads/master
Commit: ff59a160a549bb2637c0e4bd18eb990bf5f42228
Parents: d81bd20
Author: Michal Fojtik <mf...@redhat.com>
Authored: Thu Nov 29 13:55:41 2012 +0100
Committer: Michal fojtik <mf...@redhat.com>
Committed: Fri Nov 30 09:42:20 2012 +0100

----------------------------------------------------------------------
 server/lib/cimi/collections/machine_templates.rb |   73 +++++++++++++++++
 server/lib/cimi/models/machine_template.rb       |   63 ++++++++++++++
 2 files changed, 136 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ff59a160/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

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ff59a160/server/lib/cimi/models/machine_template.rb
----------------------------------------------------------------------
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