incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Lalancette <clala...@redhat.com>
Subject [PATCH] Rationalize names.
Date Fri, 03 Jun 2011 13:37:45 GMT
The different deltacloud drivers were all handling names a
bit differently.  This series rationalizes the drivers so
that:

1)  If a name is provided by the user (opts[:name]), we
use that to start with
2)  If a name is not provided, then we generate one
3)  We then check that the name conforms to the rules for
the cloud backend.  If the name does not conform, we throw
an error

Signed-off-by: Chris Lalancette <clalance@redhat.com>
---
 .../lib/deltacloud/drivers/gogrid/gogrid_driver.rb |   16 +++++++++++-----
 .../drivers/opennebula/opennebula_driver.rb        |    6 ++++++
 .../lib/deltacloud/drivers/rhevm/rhevm_driver.rb   |   10 +++++++---
 .../drivers/rimuhosting/rimuhosting_driver.rb      |   10 +++++-----
 server/lib/deltacloud/drivers/sbc/sbc_driver.rb    |    4 ++++
 .../drivers/terremark/terremark_driver.rb          |   10 +++++++---
 6 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
index e4c1f94..7bae587 100644
--- a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
+++ b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
@@ -94,9 +94,19 @@ class GogridDriver < Deltacloud::BaseDriver
     else
       server_ram = "512MB"
     end
+
+    name = opts[:name]
+    if not name
+      name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
+    end
+
+    if name.length > 20
+      raise "Parameter name must be 20 characters or less"
+    end
+
     client = new_client(credentials)
     params = {
-      'name' => opts[:name] || get_random_instance_name,
+      'name' => name,
       'image' => image_id,
       'server.ram' => server_ram,
       'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
@@ -458,10 +468,6 @@ class GogridDriver < Deltacloud::BaseDriver
     )
   end
 
-  def get_random_instance_name
-    "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
-  end
-
   def convert_server_state(state, id)
     return 'PENDING' unless id
     state.eql?('Off') ? 'STOPPED' : 'RUNNING'
diff --git a/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb b/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
index deda431..84cf99d 100644
--- a/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
+++ b/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
@@ -31,6 +31,8 @@ module Deltacloud
 
 class OpennebulaDriver < Deltacloud::BaseDriver
 
+  feature :instances, :user_name
+
   ######################################################################
   # Hardware profiles
   ######################################################################
@@ -126,6 +128,10 @@ class OpennebulaDriver < Deltacloud::BaseDriver
 
 	hwp_id = opts[:hwp_id] || 'small'
 
+	if not opts[:name]
+          opts[:name] = "#{Time.now.to_i.to_s}#{rand(9)}"
+        end
+
 	instancexml = ERB.new(OCCI_VM).result(binding)
 	instancefile = "|echo '#{instancexml}'"
 
diff --git a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
index 43c1af6..40d814a 100644
--- a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
+++ b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
@@ -168,11 +168,15 @@ class RHEVMDriver < Deltacloud::BaseDriver
   def create_instance(credentials, image_id, opts={})
     client = new_client(credentials)
     params = {}
-    if opts[:name] && opts[:name].length > 50
-      raise "Parameter name must be shorter than 50 characters"
+    name = opts[:name]
+    if not name
+      name = Time.now.to_i.to_s
+    end
+    if name.length > 50
+      raise "Parameter name must be 50 characters or less"
     end
     safely do
-      params[:name] = opts[:name] if opts[:name]
+      params[:name] = name
       params[:realm_id] = opts[:realm_id] if opts[:realm_id]
       params[:hwp_id] = opts[:hwp_id] if opts[:hwp_id]
       params[:hwp_memory] = opts[:hwp_memory] if opts[:hwp_memory]
diff --git a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
index 6d37644..e39c877 100755
--- a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
+++ b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
@@ -108,13 +108,13 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   end
 
   def create_instance(credentials, image_id, opts)
-     rh = RimuHostingClient.new(credentials)
+    rh = RimuHostingClient.new(credentials)
     # really need to raise an exception here.
     hwp_id = opts[:hwp_id] || 1
-    # really bad, but at least its a fqdn
-    name = Time.now.to_s + '.com'
-    if (opts[:name]) then
-      name = opts[:name]
+    name = opts[:name]
+    if not name
+      # really bad, but at least its a fqdn
+      name = Time.now.to_i.to_s + '.com'
     end
     convert_srv_to_instance(rh.create_server(image_id, hwp_id, name))
 
diff --git a/server/lib/deltacloud/drivers/sbc/sbc_driver.rb b/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
index 63a57cd..2e5b39c 100644
--- a/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
+++ b/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
@@ -88,6 +88,10 @@ class SBCDriver < Deltacloud::BaseDriver
     body['location'] = opts[:realm_id] || @last_image['location']
     body['instanceType'] = opts[:hwp_id].gsub('-', '/') || @last_image['supportedInstanceTypes'][0]['id']
 
+    if not body['name']
+      body['name'] = Time.now.to_i.to_s
+    end
+
     # Submit instance, parse response
     convert_instance(sbc_client.create_instance(body).map[0])
   end
diff --git a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
index d56df6b..88d3f47 100644
--- a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
+++ b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
@@ -128,9 +128,13 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" =>
 "STOPPED", "4"
     new_vapp = nil
     vapp_opts = {} #assemble options to pass to Fog::Terremark::Real.instantiate_vapp_template
     terremark_hwp = hardware_profiles(credentials, {:name => 'default'}).first #sanity
check values against default
-    name = opts['name'] #name could be nil or length 0 or too long
-    name = "inst#{Time.now.to_i}" if (name.nil? || (name.length == 0))
-    name = name.slice(0..13) #name < 15 chars (says terremark)
+    name = opts[:name]
+    if not name
+      name = "inst#{Time.now.to_i}"
+    end
+    if name.length > 15
+      raise "Parameter name must be 15 characters or less"
+    end
     unless ( (terremark_hwp.include?(:cpu, opts[:hwp_cpu].to_i)) &&
               (terremark_hwp.include?(:memory, opts[:hwp_memory].to_i)) ) then
        raise Deltacloud::Validation::Failure.new(Deltacloud::Validation::Param.new(["cpu"]),
"Error with cpu and/or memory values. you said cpu->#{opts[:hwp_cpu]} and mem->#{opts[:hwp_memory]}")
-- 
1.7.4.4


Mime
View raw message