Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Vcloud] Instantiate Vapps & get firewall acls

-Also a small refactor of some of the vdc model's collections
  • Loading branch information...
commit 07a23dbbe59c208f5fe0490c7ad3f0fdef2cac27 1 parent 11fb548
authored July 01, 2010 geemus committed July 02, 2010
12  lib/fog/vcloud.rb
@@ -73,6 +73,12 @@ def default_organization_uri
73 73
         end
74 74
       end
75 75
 
  76
+      def xmlns
  77
+        { "xmlns" => "http://www.vmware.com/vcloud/v0.8",
  78
+          "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
  79
+          "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" }
  80
+      end
  81
+
76 82
       def reload
77 83
         @connections.each_value { |k,v| v.reset if v }
78 84
       end
@@ -343,12 +349,6 @@ def initialize(credentials = {})
343 349
         @login_uri = get_login_uri
344 350
       end
345 351
 
346  
-      def xmlns
347  
-        { "xmlns" => "http://www.vmware.com/vcloud/v0.8",
348  
-          "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
349  
-          "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" }
350  
-      end
351  
-
352 352
       def mock_it(status, mock_data, mock_headers = {})
353 353
         response = Excon::Response.new
354 354
 
7  lib/fog/vcloud/terremark/ecloud.rb
@@ -7,6 +7,8 @@ module Ecloud
7 7
         model_path 'fog/vcloud/terremark/ecloud/models'
8 8
         model :catalog_item
9 9
         model :catalog
  10
+        model :firewall_acl
  11
+        model :firewall_acls
10 12
         model :internet_service
11 13
         model :internet_services
12 14
         model :ip
@@ -27,6 +29,7 @@ module Ecloud
27 29
         request_path 'fog/vcloud/terremark/ecloud/requests'
28 30
         request :add_internet_service
29 31
         request :add_node
  32
+        request :clone_vapp
30 33
         request :configure_internet_service
31 34
         request :configure_network
32 35
         request :configure_network_ip
@@ -38,6 +41,8 @@ module Ecloud
38 41
         request :get_catalog
39 42
         request :get_catalog_item
40 43
         request :get_customization_options
  44
+        request :get_firewall_acls
  45
+        request :get_firewall_acl
41 46
         request :get_internet_services
42 47
         request :get_network
43 48
         request :get_network_ip
@@ -50,7 +55,9 @@ module Ecloud
50 55
         request :get_task
51 56
         request :get_task_list
52 57
         request :get_vapp
  58
+        request :get_vapp_template
53 59
         request :get_vdc
  60
+        request :instantiate_vapp_template
54 61
         request :power_off
55 62
         request :power_on
56 63
         request :power_reset
28  lib/fog/vcloud/terremark/ecloud/models/firewall_acl.rb
... ...
@@ -0,0 +1,28 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+        class FirewallAcl < Fog::Vcloud::Model
  6
+
  7
+          identity :href, :Href
  8
+
  9
+          ignore_attributes :xmlns, :xmlns_i
  10
+
  11
+          attribute :name, :aliases => :Name
  12
+          attribute :id, :aliases => :Id
  13
+          attribute :protocol, :aliases => :Protocol
  14
+          attribute :source, :aliases => :Source
  15
+          attribute :destination, :aliases => :Destination
  16
+          attribute :permission, :aliases => :Permission
  17
+          attribute :port_start, :aliases => :PortStart
  18
+          attribute :port_end, :aliases => :PortEnd
  19
+          attribute :port_type, :aliases => :PortType
  20
+          attribute :type, :aliases => :Type
  21
+
  22
+        end
  23
+      end
  24
+    end
  25
+  end
  26
+end
  27
+
  28
+
31  lib/fog/vcloud/terremark/ecloud/models/firewall_acls.rb
... ...
@@ -0,0 +1,31 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        class FirewallAcls < Fog::Vcloud::Collection
  7
+
  8
+          model Fog::Vcloud::Terremark::Ecloud::FirewallAcl
  9
+
  10
+          attribute :href, :aliases => :Href
  11
+
  12
+          def all
  13
+            if data = connection.get_firewall_acls(href).body[:FirewallAcl]
  14
+              data = [ data ] if data.is_a?(Hash)
  15
+              load(data)
  16
+            end
  17
+          end
  18
+
  19
+          def get(uri)
  20
+            if data = connection.get_firewall_acl(uri).body
  21
+              new(data)
  22
+            end
  23
+          rescue Fog::Errors::NotFound
  24
+            nil
  25
+          end
  26
+
  27
+        end
  28
+      end
  29
+    end
  30
+  end
  31
+end
2  lib/fog/vcloud/terremark/ecloud/models/internet_services.rb
@@ -21,7 +21,7 @@ def all
21 21
             end
22 22
           end
23 23
 
24  
-          # Optimize later, no need to get_internet_services again
  24
+          # Optimize later, no need to get_internet_services again?
25 25
           def get(uri)
26 26
             internet_services = connection.get_internet_services(href).body[:InternetService]
27 27
             internet_services = [ internet_services ] if internet_services.is_a?(Hash)
12  lib/fog/vcloud/terremark/ecloud/models/server.rb
@@ -91,7 +91,7 @@ def cpus=(qty)
91 91
 
92 92
           def memory
93 93
             { :amount => memory_mess[:VirtualQuantity].to_i,
94  
-              :units => memory_mess[:VirtualQuantityUnits] }
  94
+              :units => memory_mess[:AllocationUnits] }
95 95
           end
96 96
 
97 97
           def memory=(amount)
@@ -132,17 +132,14 @@ def delete_disk(number)
132 132
           end
133 133
 
134 134
           def reload
135  
-            super
136 135
             reset_tracking
  136
+            super
137 137
           end
138 138
 
139 139
           def save
140 140
             if new_record?
141  
-              requires :name
142  
-              puts "NOOP"
143  
-              return false
144  
-              #result = connection.instantiate_vapp_template( stuff )
145  
-              #merge_attributes(result.body)
  141
+              #Lame ...
  142
+              raise RuntimeError, "Should not be here"
146 143
             else
147 144
               if on?
148 145
                 if @changed
@@ -159,7 +156,6 @@ def save
159 156
           def reset_tracking
160 157
             @disk_change = false
161 158
             @changed = false
162  
-            true
163 159
           end
164 160
 
165 161
           def _compose_vapp_data
11  lib/fog/vcloud/terremark/ecloud/models/servers.rb
@@ -5,6 +5,8 @@ module Ecloud
5 5
 
6 6
         class Servers < Fog::Vcloud::Collection
7 7
 
  8
+          undef_method :create
  9
+
8 10
           model Fog::Vcloud::Terremark::Ecloud::Server
9 11
 
10 12
           attribute :href, :aliases => :Href
@@ -21,6 +23,15 @@ def get(uri)
21 23
             nil
22 24
           end
23 25
 
  26
+          def create( catalog_item_uri, options )
  27
+            options[:vdc_uri] = href
  28
+            options[:cpus] ||= 1
  29
+            options[:memory] ||= 512
  30
+            data = connection.instantiate_vapp_template( catalog_item_uri, options ).body
  31
+            object = new(data)
  32
+            object
  33
+          end
  34
+
24 35
           private
25 36
 
26 37
           def _resource_entities
35  lib/fog/vcloud/terremark/ecloud/models/vdc.rb
@@ -20,15 +20,11 @@ class Vdc < Fog::Vcloud::Model
20 20
           attribute :instantiated_vm_quota
21 21
 
22 22
           def public_ips
23  
-            load_unless_loaded!
24  
-            @public_ips ||= Fog::Vcloud::Terremark::Ecloud::PublicIps.new( :connection => connection,
25  
-                                                                           :href => other_links.detect { |link| link[:type] == "application/vnd.tmrk.ecloud.publicIpsList+xml" }[:href] )
  23
+            @public_ips ||= collection_based_on_type("application/vnd.tmrk.ecloud.publicIpsList+xml")
26 24
           end
27 25
 
28 26
           def internet_services
29  
-            @internet_services ||= Fog::Vcloud::Terremark::Ecloud::InternetServices.
30  
-              new( :connection => connection,
31  
-                   :href => href.to_s.gsub('vdc','extensions/vdc') + "/internetServices" )
  27
+            @internet_services ||= collection_based_on_type("application/vnd.tmrk.ecloud.internetServicesList+xml")
32 28
           end
33 29
 
34 30
           def networks
@@ -50,11 +46,32 @@ def tasks
50 46
           end
51 47
 
52 48
           def catalog
53  
-            @catalog ||= Fog::Vcloud::Terremark::Ecloud::Catalog.
54  
-              new( :connection => connection,
55  
-                   :href => href + "/catalog" )
  49
+            @catalog ||= collection_based_on_type("application/vnd.vmware.vcloud.catalog+xml")
  50
+          end
  51
+
  52
+          def firewall_acls
  53
+            @firewall_acls ||= collection_based_on_type("application/vnd.tmrk.ecloud.firewallAclsList+xml")
56 54
           end
57 55
 
  56
+          private
  57
+
  58
+          def collection_based_on_type(type)
  59
+            load_unless_loaded!
  60
+            if link = other_links.detect { |link| link[:type] == type }
  61
+              case type
  62
+              when "application/vnd.tmrk.ecloud.publicIpsList+xml"
  63
+                Fog::Vcloud::Terremark::Ecloud::PublicIps
  64
+              when "application/vnd.tmrk.ecloud.internetServicesList+xml"
  65
+                Fog::Vcloud::Terremark::Ecloud::InternetServices
  66
+              when "application/vnd.vmware.vcloud.catalog+xml"
  67
+                Fog::Vcloud::Terremark::Ecloud::Catalog
  68
+              when "application/vnd.tmrk.ecloud.firewallAclsList+xml"
  69
+                Fog::Vcloud::Terremark::Ecloud::FirewallAcls
  70
+              end.new( :connection => connection, :href => link[:href] )
  71
+            else
  72
+              [ ]
  73
+            end
  74
+          end
58 75
         end
59 76
       end
60 77
     end
50  lib/fog/vcloud/terremark/ecloud/requests/clone_vapp.rb
... ...
@@ -0,0 +1,50 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        module Real
  7
+
  8
+          def validate_clone_vapp_options(options)
  9
+            valid_opts = [:name, :poweron]
  10
+            unless valid_opts.all? { |opt| options.keys.include?(opt) }
  11
+              raise ArgumentError.new("Required data missing: #{(valid_opts - options.keys).map(&:inspect).join(", ")}")
  12
+            end
  13
+          end
  14
+
  15
+          def generate_clone_vapp_request(uri, options)
  16
+            xml = Builder::XmlMarkup.new
  17
+            xml.CloneVAppParams(xmlns.merge!(:name => options[:name], :deploy => "true", :powerOn => options[:poweron])) {
  18
+              xml.VApp( :href => uri, :type => "application/vnd.vmware.vcloud.vApp+xml",
  19
+                        :xmlns => "http://www.vmware.com/vcloud/v0.8")
  20
+            }
  21
+          end
  22
+
  23
+          def clone_vapp(vdc_uri, vapp_uri, options = {})
  24
+            unless options.has_key?(:poweron)
  25
+              options[:poweron] = "false"
  26
+            end
  27
+
  28
+            validate_clone_vapp_options(options)
  29
+
  30
+            request(
  31
+              :body     => generate_clone_vapp_request(vapp_uri, options),
  32
+              :expects  => 202,
  33
+              :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.cloneVAppParams+xml'},
  34
+              :method   => 'POST',
  35
+              :uri      => vdc_uri + '/action/clonevapp',
  36
+              :parse    => true
  37
+            )
  38
+          end
  39
+        end
  40
+
  41
+        module Mock
  42
+          def clone_vapp(vdc_uri, vapp_uri, customization_data)
  43
+            validate_customization_data(customization_data)
  44
+            Fog::Mock.not_implemented
  45
+          end
  46
+        end
  47
+      end
  48
+    end
  49
+  end
  50
+end
18  lib/fog/vcloud/terremark/ecloud/requests/get_firewall_acl.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        module Real
  7
+          basic_request :get_firewall_acl
  8
+        end
  9
+
  10
+        module Mock
  11
+          def get_firewall_acl(firewall_acl_uri)
  12
+            Fog::Mock.not_implemented
  13
+          end
  14
+        end
  15
+      end
  16
+    end
  17
+  end
  18
+end
18  lib/fog/vcloud/terremark/ecloud/requests/get_firewall_acls.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        module Real
  7
+          basic_request :get_firewall_acls
  8
+        end
  9
+
  10
+        module Mock
  11
+          def get_firewall_acls(firewall_acls_uri)
  12
+            Fog::Mock.not_implemented
  13
+          end
  14
+        end
  15
+      end
  16
+    end
  17
+  end
  18
+end
18  lib/fog/vcloud/terremark/ecloud/requests/get_vapp_template.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        module Real
  7
+          basic_request :get_vapp_template
  8
+        end
  9
+
  10
+        module Mock
  11
+          def get_vapp_template(templace_uri)
  12
+            Fog::Mock.not_implemented
  13
+          end
  14
+        end
  15
+      end
  16
+    end
  17
+  end
  18
+end
100  lib/fog/vcloud/terremark/ecloud/requests/instantiate_vapp_template.rb
... ...
@@ -0,0 +1,100 @@
  1
+module Fog
  2
+  module Vcloud
  3
+    module Terremark
  4
+      module Ecloud
  5
+
  6
+        module Real
  7
+
  8
+          def validate_instantiate_vapp_template_options(catalog_item_uri, options)
  9
+            valid_opts = [:name, :vdc_uri, :network_uri, :cpus, :memory, :row, :group]
  10
+            unless valid_opts.all? { |opt| options.keys.include?(opt) }
  11
+              raise ArgumentError.new("Required data missing: #{(valid_opts - options.keys).map(&:inspect).join(", ")}")
  12
+            end
  13
+
  14
+            # Figure out the template_uri
  15
+            catalog_item = get_catalog_item( catalog_item_uri ).body
  16
+            catalog_item[:Entity] = [ catalog_item[:Entity] ] if catalog_item[:Entity].is_a?(Hash)
  17
+            catalog_item[:Link] = [ catalog_item[:Link] ] if catalog_item[:Link].is_a?(Hash)
  18
+
  19
+            options[:template_uri] = begin
  20
+               catalog_item[:Entity].detect { |entity| entity[:type] == "application/vnd.vmware.vcloud.vAppTemplate+xml" }[:href]
  21
+            rescue
  22
+              raise RuntimeError.new("Unable to locate template uri for #{catalog_item_uri}")
  23
+            end
  24
+
  25
+            customization_options = begin
  26
+                customization_href = catalog_item[:Link].detect { |link| link[:type] == "application/vnd.tmrk.ecloud.catalogItemCustomizationParameters+xml" }[:href]
  27
+                get_customization_options( customization_href ).body
  28
+            rescue
  29
+              raise RuntimeError.new("Unable to get customization options for #{catalog_item_uri}")
  30
+            end
  31
+
  32
+            # Check to see if we can set the password
  33
+            if options[:password] and customization_options[:CustomizePassword] == "false"
  34
+              raise ArgumentError.new("This catalog item (#{catalog_item_uri}) does not allow setting a password.")
  35
+            end
  36
+
  37
+            # According to the docs if CustomizePassword is "true" then we NEED to set a password
  38
+            if customization_options[:CustomizePassword] == "true" and ( options[:password].nil? or options[:password].empty? )
  39
+              raise ArgumentError.new("This catalog item (#{catalog_item_uri}) requires a :password to instantiate.")
  40
+            end
  41
+          end
  42
+
  43
+          def generate_instantiate_vapp_template_request(options)
  44
+            xml = Builder::XmlMarkup.new
  45
+            xml.InstantiateVAppTemplateParams(xmlns.merge!(:name => options[:name], :"xml:lang" => "en")) {
  46
+              xml.VAppTemplate(:href => options[:template_uri])
  47
+              xml.InstantiationParams {
  48
+                xml.ProductSection( :"xmlns:q1" => "http://www.vmware.com/vcloud/v0.8", :"xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") {
  49
+                  if options[:password]
  50
+                    xml.Property( :xmlns => "http://schemas.dmtf.org/ovf/envelope/1", :"ovf:key" => "password", :"ovf:value" => options[:password] )
  51
+                  end
  52
+                  xml.Property( :xmlns => "http://schemas.dmtf.org/ovf/envelope/1", :"ovf:key" => "row", :"ovf:value" => options[:row] )
  53
+                  xml.Property( :xmlns => "http://schemas.dmtf.org/ovf/envelope/1", :"ovf:key" => "group", :"ovf:value" => options[:group] )
  54
+                }
  55
+                xml.VirtualHardwareSection( :"xmlns:q1" => "http://www.vmware.com/vcloud/v0.8" ) {
  56
+                  # # of CPUS
  57
+                  xml.Item( :xmlns => "http://schemas.dmtf.org/ovf/envelope/1" ) {
  58
+                    xml.InstanceID(1, :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  59
+                    xml.ResourceType(3, :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  60
+                    xml.VirtualQuantity(options[:cpus], :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  61
+                  }
  62
+                  # Memory
  63
+                  xml.Item( :xmlns => "http://schemas.dmtf.org/ovf/envelope/1" ) {
  64
+                    xml.InstanceID(2, :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  65
+                    xml.ResourceType(4, :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  66
+                    xml.VirtualQuantity(options[:memory], :xmlns => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
  67
+                  }
  68
+                }
  69
+                xml.NetworkConfigSection {
  70
+                  xml.NetworkConfig {
  71
+                    xml.NetworkAssociation( :href => options[:network_uri] )
  72
+                  }
  73
+                }
  74
+              }
  75
+            }
  76
+          end
  77
+
  78
+          def instantiate_vapp_template(catalog_item_uri, options = {})
  79
+            validate_instantiate_vapp_template_options(catalog_item_uri, options)
  80
+
  81
+            request(
  82
+              :body     => generate_instantiate_vapp_template_request(options),
  83
+              :expects  => 200,
  84
+              :headers  => {'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'},
  85
+              :method   => 'POST',
  86
+              :uri      => options[:vdc_uri] + '/action/instantiatevAppTemplate',
  87
+              :parse    => true
  88
+            )
  89
+          end
  90
+        end
  91
+
  92
+        module Mock
  93
+          def instantiate_vapp_template(vdc_uri)
  94
+            Fog::Mock.not_implemented
  95
+          end
  96
+        end
  97
+      end
  98
+    end
  99
+  end
  100
+end

0 notes on commit 07a23db

Please sign in to comment.
Something went wrong with that request. Please try again.