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/05/25 09:34:06 UTC

[1/4] git commit: EC2 Frontend: Replaced Nokogiri with HAML views, added more actions

Updated Branches:
  refs/heads/master e8c5efe1a -> c175084c9


EC2 Frontend: Replaced Nokogiri with HAML views, added more actions


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

Branch: refs/heads/master
Commit: 888b759119843200e91888296a259128ee63b390
Parents: 3baf29c
Author: Michal Fojtik <mf...@redhat.com>
Authored: Tue May 15 14:49:30 2012 +0200
Committer: Michal fojtik <mf...@redhat.com>
Committed: Fri May 25 09:33:50 2012 +0200

----------------------------------------------------------------------
 server/lib/ec2/helpers/converter.rb                |  140 ---------------
 server/lib/ec2/helpers/result.rb                   |   31 ++++
 server/lib/ec2/query_parser.rb                     |   51 ++++--
 server/lib/ec2/server.rb                           |    4 +-
 server/lib/ec2/views/create_key_pair.haml          |    3 +
 server/lib/ec2/views/delete_key_pair.haml          |    1 +
 .../lib/ec2/views/describe_availability_zones.haml |    6 +
 server/lib/ec2/views/describe_images.haml          |   10 +
 server/lib/ec2/views/describe_instance_set.haml    |   20 ++
 server/lib/ec2/views/describe_instances.haml       |    9 +
 server/lib/ec2/views/describe_key_pairs.haml       |    5 +
 server/lib/ec2/views/instance_action.haml          |    9 +
 server/lib/ec2/views/reboot_instances.haml         |    1 +
 server/lib/ec2/views/run_instances.haml            |    7 +
 server/lib/ec2/views/start_instances.haml          |    1 +
 server/lib/ec2/views/stop_instances.haml           |    1 +
 server/lib/ec2/views/terminate_instances.haml      |    9 +
 17 files changed, 151 insertions(+), 157 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/helpers/converter.rb
----------------------------------------------------------------------
diff --git a/server/lib/ec2/helpers/converter.rb b/server/lib/ec2/helpers/converter.rb
deleted file mode 100644
index 8a70fe1..0000000
--- a/server/lib/ec2/helpers/converter.rb
+++ /dev/null
@@ -1,140 +0,0 @@
-# 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 Deltacloud::EC2
-
-  class Converter
-
-    def self.convert(builder, action, result)
-      klass_name = ActionHandler::MAPPINGS[action][:method].to_s.camelize
-      klass = Converter.const_get(klass_name)
-      klass.new(builder, result).to_xml
-    end
-
-    class Base
-
-      attr_reader :xml
-      attr_reader :obj
-
-      def initialize(builder, object)
-        @xml = builder
-        @obj = object
-      end
-
-    end
-
-    class Realms < Base
-
-      def to_xml
-        xml.availabilityZoneInfo {
-          obj.each do |item|
-            xml.item {
-              xml.zoneName item.id
-              xml.zoneState item.state
-              xml.regionName item.name
-            }
-          end
-        }
-      end
-
-    end
-
-    class Images < Base
-
-      def to_xml
-        xml.imagesSet {
-          obj.each do |item|
-            xml.item {
-              xml.imageId item.id
-              xml.imageState item.state.downcase
-              xml.imageOwnerId item.owner_id
-              xml.architecture item.architecture
-              xml.imageType 'machine'
-              xml.name item.name
-              xml.description item.description
-            }
-          end
-        }
-      end
-
-    end
-
-    class CreateInstance < Base
-
-      def to_xml
-        xml.reservationId 'r-11111111'
-        xml.ownerId @obj.owner_id
-        xml.groupSet {
-          xml.item {
-            xml.groupId 'sg-11111111'
-            xml.groupName 'default'
-          }
-        }
-        Instances.new(@xml, [@obj]).instance_set
-      end
-
-    end
-
-    class Instances < Base
-
-      def instance_set
-        xml.instancesSet {
-          obj.each do |item|
-            xml.item {
-              xml.instanceId item.id
-              xml.imageId item.image_id
-              xml.instanceType item.instance_profile.name
-              xml.launchTime item.launch_time
-              xml.ipAddress item.public_addresses.first.address
-              xml.privateIpAddress item.public_addresses.first.address
-              xml.dnsName item.public_addresses.first.address
-              xml.privateDnsName item.private_addresses.first.address
-              xml.architecture item.instance_profile.architecture
-              xml.keyName item.keyname
-              xml.instanceState {
-                xml.code '16'
-                xml.name item.state.downcase
-              }
-              xml.placement {
-                xml.availabilityZone item.realm_id
-                xml.groupName
-                xml.tenancy 'default'
-              }
-            }
-          end
-        }
-      end
-
-      def to_xml
-        xml.reservationSet {
-          xml.item {
-            xml.reservationId 'r-11111111'
-            xml.ownerId 'deltacloud'
-            xml.groupSet {
-              xml.item {
-                xml.groupId 'sg-11111111'
-                xml.groupName 'default'
-              }
-            }
-            self.instance_set
-          }
-        }
-      end
-
-    end
-
-  end
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/helpers/result.rb
----------------------------------------------------------------------
diff --git a/server/lib/ec2/helpers/result.rb b/server/lib/ec2/helpers/result.rb
new file mode 100644
index 0000000..0fea9c2
--- /dev/null
+++ b/server/lib/ec2/helpers/result.rb
@@ -0,0 +1,31 @@
+# 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 Deltacloud::EC2
+  module ResultHelper
+
+    def instance_state_code(state)
+      case state
+        when 'running' then '16'
+        when 'pending' then '0'
+        when 'stopping' then '64'
+        when 'stopped' then '80'
+        when 'shutting-down' then '32'
+        else '-1'
+      end
+    end
+
+  end
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/query_parser.rb
----------------------------------------------------------------------
diff --git a/server/lib/ec2/query_parser.rb b/server/lib/ec2/query_parser.rb
index 7eebc9a..8d2abc9 100644
--- a/server/lib/ec2/query_parser.rb
+++ b/server/lib/ec2/query_parser.rb
@@ -21,7 +21,14 @@ module Deltacloud::EC2
       :describe_availability_zones => { :method => :realms, :params => { 'ZoneName.1' => :id } },
       :describe_images => { :method => :images, :params => { 'ImageId.1' => :id }},
       :describe_instances => { :method => :instances, :params => {} },
-      :run_instances => { :method => :create_instance, :params => { 'ImageId' => :image_id, 'InstanceType' => :hwp_id, 'Placement.AvailabilityZone' => :realm_id }}
+      :describe_key_pairs => { :method => :keys, :params => {} },
+      :create_key_pair => { :method => :create_key, :params => { 'KeyName' => :key_name }},
+      :delete_key_pair => { :method => :destroy_key, :params => { 'KeyName' => :id }},
+      :run_instances => { :method => :create_instance, :params => { 'ImageId' => :image_id, 'InstanceType' => :hwp_id, 'Placement.AvailabilityZone' => :realm_id }},
+      :stop_instances => { :method => :stop_instance, :params => { 'InstanceId.1' => :id }},
+      :start_instances => { :method => :start_instance, :params => { 'InstanceId.1' => :id }},
+      :reboot_instances => { :method => :reboot_instance, :params => { 'InstanceId.1' => :id }},
+      :terminate_instances => { :method => :destroy_instance, :params => { 'InstanceId.1' => :id }},
     }
 
     attr_reader :action
@@ -44,35 +51,49 @@ module Deltacloud::EC2
     def perform!(credentials, driver)
       @result = case deltacloud_method
         when :create_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:image_id), deltacloud_method_params)
+        when :stop_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
+        when :start_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
+        when :destroy_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
+        when :reboot_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
         else driver.send(deltacloud_method, credentials, deltacloud_method_params)
       end
     end
 
-    def to_xml
-      ResultParser.parse(action, @result).to_xml
+    def to_xml(context)
+      ResultParser.parse(action, @result, context)
     end
 
   end
 
   class ResultParser
 
-    def self.parse(parser, result)
-      Nokogiri::XML::Builder.new do |xml|
-        xml.send(:"#{parser.action.to_s.camelize}Response", :xmlns => 'http://ec2.amazonaws.com/doc/2012-04-01/') {
-          xml.requestId parser.request_id
-          new(xml, parser, result).build_xml
-        }
-      end
+    include ResultHelper
+
+    attr_reader :query
+    attr_reader :object
+    attr_reader :context
+
+    def self.parse(query, result, context)
+      parser = new(query, result, context)
+      layout = "%#{query.action.to_s.camelize}Response{:xmlns => 'http://ec2.amazonaws.com/doc/2012-04-01/'}\n"+
+        "\t%requestId #{query.request_id}\n" +
+        "\t=render(:#{query.action}, object)\n"
+      Haml::Engine.new(layout, :filename => 'layout').render(parser)
     end
 
-    def initialize(xml, parser, result)
-      @builder = xml
-      @parser = parser
-      @result = result
+    def initialize(query, object, context)
+      @context = context
+      @query = query
+      @object = object
     end
 
     def build_xml
-      Converter.convert(@builder, @parser.action, @result)
+      Converter.convert(query.action, object)
+    end
+
+    def render(template, obj)
+      template_filename = File.join(File.dirname(__FILE__), 'views', '%s.haml' % template.to_s)
+      Haml::Engine.new(File.read(template_filename), :filename => template_filename).render(self, :object => obj)
     end
 
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/ec2/server.rb b/server/lib/ec2/server.rb
index 29fc545..d229c94 100644
--- a/server/lib/ec2/server.rb
+++ b/server/lib/ec2/server.rb
@@ -14,8 +14,8 @@
 # under the License.
 
 require 'rubygems'
-require 'nokogiri'
 require 'sinatra/base'
+require 'haml'
 
 require_relative '../sinatra'
 require_relative './helpers'
@@ -53,7 +53,7 @@ module Deltacloud::EC2
       content_type :xml
       ec2_action = QueryParser.parse(params, request_id)
       ec2_action.perform!(credentials, driver)
-      ec2_action.to_xml
+      ec2_action.to_xml(self)
     end
 
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/create_key_pair.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/create_key_pair.haml b/server/lib/ec2/views/create_key_pair.haml
new file mode 100644
index 0000000..ed00492
--- /dev/null
+++ b/server/lib/ec2/views/create_key_pair.haml
@@ -0,0 +1,3 @@
+%keyName=object.name
+%keyFingerprint=object.fingerprint
+%keyMaterial=object.pem_rsa_key

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/delete_key_pair.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/delete_key_pair.haml b/server/lib/ec2/views/delete_key_pair.haml
new file mode 100644
index 0000000..d809ea8
--- /dev/null
+++ b/server/lib/ec2/views/delete_key_pair.haml
@@ -0,0 +1 @@
+%return true

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/describe_availability_zones.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/describe_availability_zones.haml b/server/lib/ec2/views/describe_availability_zones.haml
new file mode 100644
index 0000000..1d96617
--- /dev/null
+++ b/server/lib/ec2/views/describe_availability_zones.haml
@@ -0,0 +1,6 @@
+%availabilityZoneInfo
+  - object.each do |item|
+    %item
+      %zoneName=item.id
+      %zoneState=item.state
+      %regionName=item.name

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/describe_images.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/describe_images.haml b/server/lib/ec2/views/describe_images.haml
new file mode 100644
index 0000000..a49a5ee
--- /dev/null
+++ b/server/lib/ec2/views/describe_images.haml
@@ -0,0 +1,10 @@
+%imagesSet
+  - object.each do |item|
+    %item
+      %imageId=item.id
+      %imageState=item.state.downcase
+      %imageOwnerId=item.owner_id
+      %architecture item.architecture
+      %imageType machine
+      %name=item.name
+      %description=item.description

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/describe_instance_set.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/describe_instance_set.haml b/server/lib/ec2/views/describe_instance_set.haml
new file mode 100644
index 0000000..b8c7c1c
--- /dev/null
+++ b/server/lib/ec2/views/describe_instance_set.haml
@@ -0,0 +1,20 @@
+%instanceSet
+  - object.each do |item|
+    %item
+      %instanceId=item.id
+      %imageId=item.image_id
+      %instanceType=item.instance_profile.name
+      %launchTime=item.launch_time
+      %ipAddress=item.public_addresses.first.address
+      %privateIpAddress=item.public_addresses.first.address
+      %dnsName=item.public_addresses.first.address
+      %privateDnsName=item.private_addresses.first.address
+      %architecture=item.instance_profile.architecture
+      %keyName=item.keyname
+      %instanceState
+        %code=instance_state_code(item.state.downcase)
+        %name=item.state.downcase
+      %placement
+        %availabilityZone=item.realm_id
+        %groupName
+        %tenancy default

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/describe_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/describe_instances.haml b/server/lib/ec2/views/describe_instances.haml
new file mode 100644
index 0000000..75bcf7c
--- /dev/null
+++ b/server/lib/ec2/views/describe_instances.haml
@@ -0,0 +1,9 @@
+%reservationSet
+  %item
+    %reservationId r-11111111
+    %ownerId deltacloud
+    %groupSet
+      %item
+        %groupId sg-11111111
+        %groupName default
+    =render(:describe_instance_set, object)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/describe_key_pairs.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/describe_key_pairs.haml b/server/lib/ec2/views/describe_key_pairs.haml
new file mode 100644
index 0000000..9b97bca
--- /dev/null
+++ b/server/lib/ec2/views/describe_key_pairs.haml
@@ -0,0 +1,5 @@
+%keySet
+  - object.each do |item|
+    %item
+      %keyName=item.name
+      %keyFingerprint=item.fingerprint

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/instance_action.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/instance_action.haml b/server/lib/ec2/views/instance_action.haml
new file mode 100644
index 0000000..7cafefa
--- /dev/null
+++ b/server/lib/ec2/views/instance_action.haml
@@ -0,0 +1,9 @@
+%instancesSet
+  %item
+    %instanceId=object.id
+    %currentState
+      %code=instance_state_code(object.state.downcase)
+      %name=object.state.downcase
+    %previousState
+      %code -1
+      %name unknown

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/reboot_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/reboot_instances.haml b/server/lib/ec2/views/reboot_instances.haml
new file mode 100644
index 0000000..cc966ed
--- /dev/null
+++ b/server/lib/ec2/views/reboot_instances.haml
@@ -0,0 +1 @@
+=render :instance_action, object

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/run_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/run_instances.haml b/server/lib/ec2/views/run_instances.haml
new file mode 100644
index 0000000..ef306a7
--- /dev/null
+++ b/server/lib/ec2/views/run_instances.haml
@@ -0,0 +1,7 @@
+%reservationId r-11111111
+%ownerId deltacloud
+%groupSet
+  %item
+    %groupId sg-11111111
+    %groupName default
+=render :describe_instance_set, [object]

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/start_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/start_instances.haml b/server/lib/ec2/views/start_instances.haml
new file mode 100644
index 0000000..cc966ed
--- /dev/null
+++ b/server/lib/ec2/views/start_instances.haml
@@ -0,0 +1 @@
+=render :instance_action, object

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/stop_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/stop_instances.haml b/server/lib/ec2/views/stop_instances.haml
new file mode 100644
index 0000000..cc966ed
--- /dev/null
+++ b/server/lib/ec2/views/stop_instances.haml
@@ -0,0 +1 @@
+=render :instance_action, object

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/888b7591/server/lib/ec2/views/terminate_instances.haml
----------------------------------------------------------------------
diff --git a/server/lib/ec2/views/terminate_instances.haml b/server/lib/ec2/views/terminate_instances.haml
new file mode 100644
index 0000000..ee7cba1
--- /dev/null
+++ b/server/lib/ec2/views/terminate_instances.haml
@@ -0,0 +1,9 @@
+%instancesSet
+  %item
+    %instanceId=context.params['InstanceId.1']
+    %currentState
+      %code=32
+      %name shutting-down
+    %previousState
+      %code -1
+      %name unknown